Skip to main content

Documentation Index

Fetch the complete documentation index at: https://nvd-54.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

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.Unless you are specifically using gpt-3.5-turbo-instruct, you are probably looking for this page instead.
OpenAI 是一个人工智能 (AI) 研究实验室。 This will help you get started with OpenAI completion models (LLMs) using LangChain. For detailed documentation on OpenAI features and configuration options, please refer to the API reference.

概述

集成详情

ClassPackageLocalSerializablePY supportDownloadsVersion
OpenAI@langchain/openaiNPM - DownloadsNPM - Version

设置

要访问 OpenAI models,你需要create an OpenAI account, get an API key, and install the @langchain/openai integration package.

凭证

前往 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 包中:
npm install @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,
  // 其他参数...
})

调用

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

自定义 URL

You can customize the base URL the SDK sends requests to by passing a configuration parameter like this:
const llmCustomURL = new OpenAI({
  temperature: 0.9,
  configuration: {
    baseURL: "https://your_custom_url.com",
  },
});
You can also pass other ClientOptions parameters accepted by the official SDK. If you are hosting on Azure OpenAI, see the dedicated page instead.

API 参考

有关所有 OpenAI 功能和配置的详细文档,请前往 API 参考