Skip to main content
Open In ColabOpen on GitHub

ChatOCIGenAI

本笔记本提供了关于 OCIGenAI chat models 的快速入门指南。如需了解 ChatOCIGenAI 的所有功能和配置的详细文档,请前往 API reference

Oracle Cloud Infrastructure (OCI) Generative AI 是一项完全托管的服务,提供一系列先进的、可定制的大型语言模型 (LLMs),可满足广泛的应用场景,并通过单一 API 进行访问。 使用 OCI Generative AI 服务,您可以访问现成的预训练模型,或者根据您自己的数据,在专用的 AI 集群上创建和托管您自己的微调自定义模型。有关服务和 API 的详细文档,请__在此__ 和____ 链接中查看。

概览

集成详情

ClassPackageLocalSerializableJS support
ChatOCIGenAIlangchain-community

模型功能

Tool callingStructured outputJSON modeImage inputAudio inputVideo inputToken-level streamingNative asyncToken usageLogprobs

设置

要访问 OCIGenAI 模型,您需要安装 ocilangchain-community 包。

凭证

此集成支持的凭证和身份验证方法等同于用于其他 OCI 服务的凭证和身份验证方法,并遵循__标准 SDK 身份验证(Standard SDK Authentication)__方法,特别是 API Key、session token、instance principal 和 resource principal。

API Key 是上述示例中默认使用的身份验证方法。以下示例展示了如何使用其他身份验证方法(session token)

安装

LangChain OCIGenAI 集成位于 langchain-community 包中,您还需要安装 oci 包:

%pip install -qU langchain-community oci

实例化

现在我们可以实例化我们的模型对象并生成聊天补全:

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},
)

调用

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)

链式调用

我们可以像下面这样,将我们的模型与提示模板链式调用:

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 参考

有关所有 ChatOCIGenAI 功能和配置的详细文档,请访问 API 参考:https://python.langchain.com/api_reference/community/chat_models/langchain_community.chat_models.oci_generative_ai.ChatOCIGenAI.html