跳至主要内容

OpenAI

注意 (caution)

您当前位于一个文档页面,该页面介绍了 OpenAI 文本完成模型 的使用。最新且最流行的 OpenAI 模型是 聊天完成模型。 (You are currently on a page documenting the use of OpenAI text completion models. The latest and most popular OpenAI models are chat completion models.)

除非您专门使用 gpt-3.5-turbo-instruct,否则您可能需要查看 此页面。 (Unless you are specifically using gpt-3.5-turbo-instruct, you are probably looking for this page instead.)

OpenAI 提供一系列不同能力级别的模型,适用于不同的任务。 ( OpenAI offers a spectrum of models with different levels of power suitable for different tasks.)

此示例介绍如何使用 LangChain 与 OpenAI 模型 进行交互。 (This example goes over how to use LangChain to interact with OpenAI models)

概述

集成详细信息

类 (Class)包 (Package)本地 (Local)可序列化 (Serializable)JS 支持 (JS support)包下载 (Package downloads)包最新版本 (Package latest)
ChatOpenAIlangchain-openaibetaPyPI - DownloadsPyPI - Version

设置

要访问 OpenAI 模型,您需要创建一个 OpenAI 帐户,获取 API 密钥并安装 langchain-openai 集成包。 (To access OpenAI models you'll need to create an OpenAI account, get an API key, and install the langchain-openai integration package.)

凭据

前往 https://platform.openai.com 注册 OpenAI 并生成 API 密钥。完成此操作后,设置 OPENAI_API_KEY 环境变量。 (Head to https://platform.openai.com to sign up to OpenAI and generate an API key. Once you've done this set the OPENAI_API_KEY environment variable)

import getpass
import os

if "OPENAI_API_KEY" not in os.environ:
os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter your OpenAI API key: ")

如果您希望自动获得最佳的模型调用跟踪,您还可以通过取消以下注释来设置您的 LangSmith API 密钥。 (If you want to get automated best in-class tracing of your model calls you can also set your LangSmith API key by uncommenting below)

# os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
# os.environ["LANGSMITH_TRACING"] = "true"

安装

LangChain OpenAI 集成位于 langchain-openai 包中。 (The LangChain OpenAI integration lives in the langchain-openai package)

%pip install -qU langchain-openai

如果您需要指定您的组织 ID,您可以使用以下单元格。但是,如果您只属于一个组织或打算使用您的默认组织,则不需要此步骤。您可以 此处 查看您的默认组织。 (Should you need to specify your organization ID, you can use the following cell. However, it is not required if you are only part of a single organization or intend to use your default organization. You can check your default organization here.)

要指定您的组织,您可以使用以下代码: (To specify your organization, you can use this)

OPENAI_ORGANIZATION = getpass()

os.environ["OPENAI_ORGANIZATION"] = OPENAI_ORGANIZATION

实例化

现在我们可以实例化我们的模型对象并生成聊天完成内容。 (Now we can instantiate our model object and generate chat completions)

from langchain_openai import OpenAI

llm = OpenAI()
API 参考:OpenAI

调用

llm.invoke("Hello how are you?")
'\n\nI am an AI and do not have emotions like humans do, so I am always functioning at my optimal level. Thank you for asking! How can I assist you today?'

链接

from langchain_core.prompts import PromptTemplate

prompt = PromptTemplate.from_template("How to say {input} in {output_language}:\n")

chain = prompt | llm
chain.invoke(
{
"output_language": "German",
"input": "I love programming.",
}
)
API 参考:PromptTemplate
'\nIch liebe Programmieren.'

使用代理

如果您在显式代理后面,则可以指定 http_client 传递。 (If you are behind an explicit proxy, you can specify the http_client to pass through)

%pip install httpx

import httpx

openai = OpenAI(
model_name="gpt-3.5-turbo-instruct",
http_client=httpx.Client(proxies="http://proxy.yourcompany.com:8080"),
)

API 参考

有关所有 OpenAI llm 功能和配置的详细文档,请查看 API 参考: https://python.langchain.ac.cn/v0.2/api_reference/openai/llms/langchain_openai.llms.base.OpenAI.html (For detailed documentation of all OpenAI llm features and configurations head to the API reference: https://python.langchain.ac.cn/v0.2/api_reference/openai/llms/langchain_openai.llms.base.OpenAI.html)


此页面是否有帮助? (Was this page helpful?)


您也可以在 GitHub 上留下详细的反馈 (You can also leave detailed feedback) 在 GitHub 上 (on GitHub).