Christoph's 2 Cents

A Backup for My Brain!

Artificial IntelligenceCloudLinuxOracle Cloud InfrastructrureOracle Linuxshell scripting

OCI CLI – Using Generative AI

The OCI CLI (command line interface) to Oracle Cloud provides you with an interface to the OCI Generative AI service.. The official CLI reference provides most of the details on how to interact with the Generative AI service.

Make sure that the OCI account you use has access to the Generative AI service. See Getting Access to Generative AI.

Basic Chat Request

A basic chat request that is similar to what you may be used to from Chat GPT, Gemini, or Claude, can be made via the chat-result command. It requires a compartment OCID, and two complex parameters --chat-request and --serving-mode.

These complex types are JSON objects, best passed into the command as files.

To generate examples of these complex types, use the following commands:

oci generative-ai-inference chat-result chat --generate-param-json-input chat-request > chat_request_example.json

The resulting file, will contain a JSON array of multiple examples of the complex type. You only need one of them. In this example, I’ll use the example for COHERE, which is one of the available large language models (LLM) in the Generative AI service.

Simply copy and paste the JSON object into a new file called chat-request.json, and edit the values as shown:

  {
    "apiFormat": "COHERE",
    "chatHistory": [],
    "frequencyPenalty": 0,
    "isSearchQueriesOnly": false,
    "isStream": false,
    "maxTokens": 600,
    "message": "why is the sky blue",
    "preambleOverride": "string",
    "presencePenalty": 0,
    "temperature": 0.75,
    "topK": 0,
    "topP": 0.7
  }

Note that the message is what we typically refer to as the prompt of a AI chat interaction.

Next, generate the example for the serving-mode complex type:

oci generative-ai-inference chat-result chat --generate-param-json-input serving-mode > serving_mode_example.json

Like the chat-request example, it too consists of a JSON array with multiple examples. Again, we only need one of the JSON objects, so copy & paste the ON_DEMAND example into a new file named serving-mode.json:

  {
    "modelId": "cohere.command-r-16k",
    "servingType": "ON_DEMAND"
  }

The modelId determines the large language model.

With these two files in place, we can now execute the command to interact with the Generative AI service. Make sure you specify the region of your choice.

oci generative-ai-inference chat-result chat \
    --region uk-london-1 \
    --compartment-id ocid1.compartment.oc1.your.compartment.ocid \ 
    --chat-request file://chat-request.json \
    --serving-mode file://serving-mode.json \
    --query "data.\"chat-response\".text"

"The sky appears blue because molecules in the air scatter blue light from the sun more than other colors. As sunlight passes through the atmosphere, the relatively shorter wavelength of blue light means it gets scattered more frequently by the air molecules and small particles in the sky. This phenomenon is called Rayleigh scattering, named after the physicist Lord Rayleigh, who explained the process in 1899.\n\nWhen the sun's rays reach the Earth's atmosphere, they interact with molecules of nitrogen and oxygen, which dominate the air. The scattering of light happens when sunlight encounters these molecules and is redirected in all directions. Blue light, with its shorter wavelength, is scattered more strongly than other colors because it interacts more with the molecules. This scattered blue light reaches our eyes, making the sky appear blue to us.\n\nThe color of the sky can also depend on other factors like the position of the sun, which affects the amount of light scattered, and the amount of dust or water droplets in the atmosphere, which can also influence the color of the sky. But during the day, when the sun is high in the sky, the dominant reason the sky appears blue is due to this selective scattering of blue light, giving the atmosphere its characteristic blue hue.

This will return a JSON object with the chat response. The --query parameter to extracts just the message. You can leave it out to see the entire JSON.