ChatOCIGenAI
此笔记本提供了快速入门OCIGenAI聊天模型的概览。要查看所有ChatOCIGenAI功能和配置的详细文档,请访问API参考。
Oracle Cloud Infrastructure (OCI) 生成式 AI 是一项完全托管的服务,提供了多种高度先进的可自定义的大语言模型(LLMs),这些模型覆盖了广泛的用例,并可通过单一 API 获取。 使用 OCI 生成式 AI 服务,您可以访问预训练的现成模型,或基于您自己的数据在专用 AI 集群上创建和托管自定义微调模型。有关该服务和 API 的详细文档请参阅 这里 和 这里。
概览
集成细节
| Class | 包 | 本地 | 序列化 | JS支持 |
|---|---|---|---|---|
| ChatOCIGenAI | langchain-community | ❌ | ❌ | ❌ |
模型特性
| 工具调用 | 结构化输出 | JSON 模式 | 图像输入 | 音频输入 | 视频输入 | Token级流式传输 | 原生异步 | Token 使用 | 对数概率 |
|---|---|---|---|---|---|---|---|---|---|
| ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ |
设置
要访问OCIGenAI模型,您需要安装oci和langchain-community包。
Credentials
The credentials and authentication methods supported for this integration are equivalent to those used with other OCI services and follow the 标准SDK认证方法, specifically API Key, session token, instance principal, and resource principal.
API密钥是上面示例中默认的认证方法。以下示例演示了如何使用不同的认证方法(会话令牌)。
安装
The LangChain OCIGenAI 整合位于 langchain-community 包中,你还需要安装 oci 包:
%pip install -qU langchain-community oci
Instantiation
现在我们就可以实例化我们的模型对象并生成聊天完成内容:
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)
链式调用
我们可以通过以下方式将模型与提示模板进行链接:
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 参考
详细文档请参阅所有ChatOCIGenAI功能和配置:https://python.langchain.com/api_reference/community/chat_models/langchain_community.chat_models.oci_generative_ai.ChatOCIGenAI.html