Skip to main content

ChatOCIGenAI

This notebook provides a quick overview for getting started with OCIGenAI chat models. For detailed documentation of all ChatOCIGenAI features and configurations head to the API reference.

Oracle Cloud Infrastructure (OCI) Generative AI is a fully managed service that provides a set of state-of-the-art, customizable large language models (LLMs) that cover a wide range of use cases, and which is available through a single API. Using the OCI Generative AI service you can access ready-to-use pretrained models, or create and host your own fine-tuned custom models based on your own data on dedicated AI clusters. Detailed documentation of the service and API is available here and here.

Overviewโ€‹

Integration detailsโ€‹

ClassPackageLocalSerializableJS support
ChatOCIGenAIlangchain-communityโŒโŒโŒ

Model featuresโ€‹

Tool callingStructured outputJSON modeImage inputAudio inputVideo inputToken-level streamingNative asyncToken usageLogprobs
โœ…โœ…โœ…โœ…โŒโŒโœ…โŒโŒโŒ

Setupโ€‹

To access OCIGenAI models you'll need to install the oci and langchain-community packages.

Credentialsโ€‹

The credentials and authentication methods supported for this integration are equivalent to those used with other OCI services and follow the standard SDK authentication methods, specifically API Key, session token, instance principal, and resource principal.

API key is the default authentication method used in the examples above. The following example demonstrates how to use a different authentication method (session token)

Installationโ€‹

The LangChain OCIGenAI integration lives in the langchain-community package and you will also need to install the oci package:

%pip install -qU langchain-community oci

Instantiationโ€‹

Now we can instantiate our model object and generate chat completions:

from langchain_community.chat_models.oci_generative_ai import ChatOCIGenAI
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage

chat = ChatOCIGenAI(
model_id="cohere.command-r-16k",
service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com",
compartment_id="MY_OCID",
model_kwargs={"temperature": 0.7, "max_tokens": 500},
)

Invocationโ€‹

messages = [
SystemMessage(content="your are an AI assistant."),
AIMessage(content="Hi there human!"),
HumanMessage(content="tell me a joke."),
]
response = chat.invoke(messages)
print(response.content)

Chainingโ€‹

We can chain our model with a prompt template like so:

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_template("Tell me a joke about {topic}")
chain = prompt | chat

response = chain.invoke({"topic": "dogs"})
print(response.content)
API Reference:ChatPromptTemplate

API referenceโ€‹

For detailed documentation of all ChatOCIGenAI features and configurations head to the API reference: https://python.langchain.com/api_reference/community/chat_models/langchain_community.chat_models.oci_generative_ai.ChatOCIGenAI.html


Was this page helpful?