Skip to main content

ChatOCIGenAI

本笔记本提供了关于如何开始使用 OCIGenAI 聊天模型 的快速概述。有关所有 ChatOCIGenAI 功能和配置的详细文档,请访问 API 参考

Oracle Cloud Infrastructure (OCI) 生成式 AI 是一项完全托管的服务,提供一组最先进的、可定制的大型语言模型 (LLMs),涵盖广泛的用例,并通过单一 API 提供。 使用 OCI 生成式 AI 服务,您可以访问现成的预训练模型,或根据自己的数据在专用 AI 集群上创建和托管自己的微调自定义模型。有关该服务和 API 的详细文档可在 这里这里 找到。

概述

集成细节

类别包名本地可序列化JS 支持包下载包最新
ChatOCIGenAIlangchain-communityPyPI - 下载量PyPI - 版本

模型特性

工具调用结构化输出JSON模式图像输入音频输入视频输入令牌级流式处理原生异步令牌使用Logprobs

设置

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

凭证

此集成支持的凭证和身份验证方法与其他 OCI 服务使用的方法相同,并遵循 标准 SDK 身份验证 方法,具体包括 API 密钥、会话令牌、实例主体和资源主体。

API 密钥是上述示例中使用的默认身份验证方法。以下示例演示如何使用不同的身份验证方法(会话令牌)

安装

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

%pip install -qU langchain-community oci

实例化

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

<!--IMPORTS:[{"imported": "ChatOCIGenAI", "source": "langchain_community.chat_models.oci_generative_ai", "docs": "https://python.langchain.com/api_reference/community/chat_models/langchain_community.chat_models.oci_generative_ai.ChatOCIGenAI.html", "title": "ChatOCIGenAI"}, {"imported": "AIMessage", "source": "langchain_core.messages", "docs": "https://python.langchain.com/api_reference/core/messages/langchain_core.messages.ai.AIMessage.html", "title": "ChatOCIGenAI"}, {"imported": "HumanMessage", "source": "langchain_core.messages", "docs": "https://python.langchain.com/api_reference/core/messages/langchain_core.messages.human.HumanMessage.html", "title": "ChatOCIGenAI"}, {"imported": "SystemMessage", "source": "langchain_core.messages", "docs": "https://python.langchain.com/api_reference/core/messages/langchain_core.messages.system.SystemMessage.html", "title": "ChatOCIGenAI"}]-->
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)

链接

我们可以像这样将我们的模型与提示词模板链接

<!--IMPORTS:[{"imported": "ChatPromptTemplate", "source": "langchain_core.prompts", "docs": "https://python.langchain.com/api_reference/core/prompts/langchain_core.prompts.chat.ChatPromptTemplate.html", "title": "ChatOCIGenAI"}]-->
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 功能和配置的详细文档,请访问 API 参考:https://python.langchain.com/api_reference/community/chat_models/langchain_community.chat_models.oci_generative_ai.ChatOCIGenAI.html

相关


Was this page helpful?


You can also leave detailed feedback on GitHub.

扫我,入群扫我,找书