跳到主要内容
Oracle Cloud Infrastructure (OCI) 生成式 AI 是一项完全托管的服务,提供了一套最先进、可定制的大型语言模型 (LLM),涵盖了广泛的使用场景,并且可以通过单一 API 访问。使用 OCI 生成式 AI 服务,您可以访问开箱即用的预训练模型,或根据您自己的数据在专用 AI 集群上创建和托管您自己微调的自定义模型。服务和 API 的详细文档可在此处和此处找到。 本 notebook 解释了如何将 OCI 的生成式 AI 模型与 LangChain 结合使用。

先决条件

我们需要安装 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)

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.