ChatOCIGenAI
本Notebook提供了一个快速入门指南,以帮助您开始使用OCIGenAI聊天模型。有关所有ChatOCIGenAI功能和配置的详细文档,请参阅API参考。
Oracle Cloud Infrastructure (OCI) Generative AI是一项完全托管的服务,提供了一套最先进、可定制的大型语言模型 (LLM),涵盖了广泛的使用场景,并通过单一API提供访问。使用OCI Generative AI服务,您可以访问即用型预训练模型,也可以根据自己的数据在专用的AI集群上创建和托管您自己的微调自定义模型。有关服务和API的详细文档可在此处此处和此处获取。
概述
集成详情
类别 | 包 | 本地 | 可序列化 | JS 支持 |
---|---|---|---|---|
ChatOCIGenAI | langchain-community | ❌ | ❌ | ❌ |
模型特性
工具调用 | 结构化输出 | JSON 模式 | 图片输入 | 音频输入 | 视频输入 | 逐令牌流式传输 | 原生异步 | 令牌使用量 | 对数概率 |
---|---|---|---|---|---|---|---|---|---|
✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ |
设置
要访问OCIGenAI模型,您需要安装oci
和langchain-community
包。
凭证
此集成支持的凭证和身份验证方法与OCI其他服务使用的凭证和身份验证方法相同,并遵循标准SDK身份验证方法,特别是API密钥、会话令牌、实例主体和资源主体。
API密钥是上述示例中使用的默认身份验证方法。以下示例演示了如何使用不同的身份验证方法(会话令牌)
安装
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 参考:ChatPromptTemplate
API 参考
有关所有 ChatOCIGenAI 功能和配置的详细文档,请查阅 API 参考:https://python.langchain.ac.cn/api_reference/community/chat_models/langchain_community.chat_models.oci_generative_ai.ChatOCIGenAI.html