Skip to main content

OpenAI

caution

您当前所在的页面记录了 OpenAI 文本补全模型 的使用。OpenAI 最新且最受欢迎的模型是 聊天补全模型

除非您正在专门使用 gpt-3.5-turbo-instruct,否则您可能需要查看 这个页面

OpenAI 是一家人工智能 (AI) 研究实验室。

这将帮助您使用 LangChain 开始使用 OpenAI 补全模型 (LLMs)。有关 OpenAI 的功能和配置选项的详细文档,请参阅 API 参考文档

概览

集成详情

类别本地支持可序列化Python 支持包下载量最新版本
OpenAI@langchain/openaiNPM - 下载量NPM - 版本

准备工作

要访问 OpenAI 模型,您需要创建一个 OpenAI 账户,获取 API 密钥,并安装 @langchain/openai 集成包。

凭证信息

前往 platform.openai.com 注册 OpenAI 账号并生成 API 密钥。完成此操作后,请设置 OPENAI_API_KEY 环境变量:

export OPENAI_API_KEY="your-api-key"

如果您希望自动追踪模型调用,还可以取消以下 LangSmith API 密钥的注释:

# export LANGSMITH_TRACING="true"
# export LANGSMITH_API_KEY="your-api-key"

安装

LangChain OpenAI 集成位于 @langchain/openai 包中:

:::提示 请参阅安装集成包的一般说明部分。 :::

yarn add @langchain/openai @langchain/core

实例化

现在我们可以实例化我们的模型对象并生成聊天补全结果:

import { OpenAI } from "@langchain/openai";

const llm = new OpenAI({
model: "gpt-3.5-turbo-instruct",
temperature: 0,
maxTokens: undefined,
timeout: undefined,
maxRetries: 2,
apiKey: process.env.OPENAI_API_KEY,
// other params...
});

调用

const inputText = "OpenAI is an AI company that ";

const completion = await llm.invoke(inputText);
completion;
develops and promotes friendly AI for the benefit of humanity. It was founded in 2015 by Elon Musk, Sam Altman, Greg Brockman, Ilya Sutskever, Wojciech Zaremba, John Schulman, and Chris Olah. The company's mission is to create and promote artificial general intelligence (AGI) that is safe and beneficial to humanity.

OpenAI conducts research in various areas of AI, including deep learning, reinforcement learning, robotics, and natural language processing. The company also develops and releases open-source tools and platforms for AI research, such as the GPT-3 language model and the Gym toolkit for reinforcement learning.

One of the main goals of OpenAI is to ensure that the development of AI is aligned with human values and does not pose a threat to humanity. To this end, the company has established a set of principles for safe and ethical AI development, and it actively collaborates with other organizations and researchers in the field.

OpenAI has received funding from various sources, including tech giants like Microsoft and Amazon, as well as individual investors. It has also partnered with companies and organizations such as Google, IBM, and the United Nations to advance its research and promote responsible AI development.

In addition to its research and development

链式调用

我们可以像这样将补全模型与提示模板进行链式调用

import { PromptTemplate } from "@langchain/core/prompts";

const prompt = new PromptTemplate({
template: "How to say {input} in {output_language}:\n",
inputVariables: ["input", "output_language"],
});

const chain = prompt.pipe(llm);
await chain.invoke({
output_language: "German",
input: "I love programming.",
});

Ich liebe Programmieren.

如果您属于某个组织,可以将 process.env.OPENAI_ORGANIZATION 设置为您的 OpenAI 组织 ID,或者在初始化模型时将其作为 organization 参数传入。

自定义 URL

您可以通过传递如下所示的 configuration 参数,自定义 SDK 发送请求的基础 URL:

const llmCustomURL = new OpenAI({
temperature: 0.9,
configuration: {
baseURL: "https://your_custom_url.com",
},
});

你还可以传递官方 SDK 接受的其他ClientOptions参数。

如果你使用的是 Azure OpenAI 托管服务,请查看专用页面

API 参考文档

有关所有 OpenAI 功能和配置的详细文档,请访问 API 参考页面: https://api.js.langchain.com/classes/langchain_openai.OpenAI.html


Was this page helpful?


You can also leave detailed feedback on GitHub.