Skip to main content

oci_generative_ai

Oracle Cloud Infrastructure 生成式人工智能

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

本笔记本解释了如何使用 OCI 的生成式人工智能完整模型与 LangChain。

设置

确保已安装 oci sdk 和 langchain-community 包

!pip install -U oci langchain-community

用法

from langchain_community.llms.oci_generative_ai import OCIGenAI

llm = OCIGenAI(
model_id="cohere.command",
service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com",
compartment_id="MY_OCID",
model_kwargs={"temperature": 0, "max_tokens": 500},
)

response = llm.invoke("Tell me one fact about earth", temperature=0.7)
print(response)

使用提示模板进行链式调用

from langchain_core.prompts import PromptTemplate

llm = OCIGenAI(
model_id="cohere.command",
service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com",
compartment_id="MY_OCID",
model_kwargs={"temperature": 0, "max_tokens": 500},
)

prompt = PromptTemplate(input_variables=["query"], template="{query}")
llm_chain = prompt | llm

response = llm_chain.invoke("what is the capital of france?")
print(response)

流式处理

llm = OCIGenAI(
model_id="cohere.command",
service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com",
compartment_id="MY_OCID",
model_kwargs={"temperature": 0, "max_tokens": 500},
)

for chunk in llm.stream("Write me a song about sparkling water."):
print(chunk, end="", flush=True)

身份验证

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

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

llm = OCIGenAI(
model_id="cohere.command",
service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com",
compartment_id="MY_OCID",
auth_type="SECURITY_TOKEN",
auth_profile="MY_PROFILE", # replace with your profile name
)

专用AI集群

要访问托管在专用AI集群中的模型 创建一个端点,其分配的OCID(当前以‘ocid1.generativeaiendpoint.oc1.us-chicago-1’为前缀)用作您的模型ID。

在访问托管在专用AI集群中的模型时,您需要使用两个额外的必需参数(“provider”和“context_size”)初始化OCIGenAI接口。

llm = OCIGenAI(
model_id="ocid1.generativeaiendpoint.oc1.us-chicago-1....",
service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com",
compartment_id="DEDICATED_COMPARTMENT_OCID",
auth_profile="MY_PROFILE", # replace with your profile name,
provider="MODEL_PROVIDER", # e.g., "cohere" or "meta"
context_size="MODEL_CONTEXT_SIZE", # e.g., 128000
)

相关


此页面是否有帮助?


您还可以留下详细的反馈 在 GitHub 上