Skip to main content
Oracle Cloud Infrastructure(OCI)生成式 AI 是一项完全托管的服务,提供一套最先进的可定制大型语言模型(LLM),覆盖广泛的使用场景,并通过单一 API 提供访问。使用 OCI 生成式 AI 服务,您可以访问即用的预训练模型,或在专用 AI 集群上基于您自己的数据创建并托管经过微调的自定义模型。服务和 API 的详细文档可在__此处此处__查阅。 本教程介绍如何在 LangChain 中使用 OCI 的生成式 AI 模型。

前置条件

我们需要安装 OCI SDK:
!pip install -U oci

OCI 生成式 AI API 端点

inference.generativeai.us-chicago-1.oci.oraclecloud.com

身份验证

此 LangChain 集成支持以下身份验证方式:
  1. API 密钥
  2. 会话令牌
  3. 实例主体
  4. 资源主体
这些方式遵循__此处__详述的标准 SDK 身份验证方法。

使用方式

from langchain_community.embeddings import OCIGenAIEmbeddings

# use default authN method API-key
embeddings = OCIGenAIEmbeddings(
    model_id="MY_EMBEDDING_MODEL",
    service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com",
    compartment_id="MY_OCID",
)


query = "This is a query in English."
response = embeddings.embed_query(query)
print(response)

documents = ["This is a sample document", "and here is another one"]
response = embeddings.embed_documents(documents)
print(response)
# Use Session Token to authN
embeddings = OCIGenAIEmbeddings(
    model_id="MY_EMBEDDING_MODEL",
    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
    auth_file_location="MY_CONFIG_FILE_LOCATION",  # replace with file location where profile name configs present
)


query = "This is a sample query"
response = embeddings.embed_query(query)
print(response)

documents = ["This is a sample document", "and here is another one"]
response = embeddings.embed_documents(documents)
print(response)