文档API 参考📓 教程🧑‍🍳 食谱🤝 集成💜 Discord🎨 Studio
文档

OpenAIGenerator

OpenAIGenerator 使用 OpenAI 的大型语言模型 (LLM) 来生成文本。

pipeline 中的最常见位置PromptBuilder 之后
必需的初始化变量"api_key": OpenAI API 密钥。可以使用OPENAI_API_KEY 环境变量设置。
强制运行变量“prompt”:一个包含 LLM 提示的字符串
输出变量“replies”:一个包含 LLM 生成的所有回复的字符串列表

”meta”:一个包含与每个回复相关的元数据的字典列表,例如 token 计数、结束原因等
API 参考Generators (生成器)
GitHub 链接https://github.com/deepset-ai/haystack/blob/main/haystack/components/generators/openai.py

概述

OpenAIGenerator 支持从 gpt-3.5-turbo 及更新的模型(如 gpt-4、gpt-4-turbo 等)开始的 OpenAI 模型。

OpenAIGenerator 需要 OpenAI 密钥才能工作。它直接使用OPENAI_API_KEY 环境变量,默认为此。否则,您可以在初始化时使用api_key:

generator = OpenAIGenerator(api_key=Secret.from_token("<your-api-key>"), model="gpt-4o-mini")

然后,该组件需要一个 prompt 来运行,但您可以通过openai.ChatCompletion.create 方法通过generation_kwargs 参数,无论是初始化时还是run() 方法直接调用此组件。有关 OpenAI API 支持的参数的更多详细信息,请参阅 OpenAI 文档

OpenAIGenerator 支持通过api_base_url 初始化参数来自定义部署 OpenAI 模型。

流式传输

OpenAIGenerator 支持直接在输出中流式传输 LLM 的 token。为此,请将一个函数传递给streaming_callback 初始化参数。请注意,流式传输 token 仅与生成单个响应兼容,因此n 必须设置为 1 才能使流式传输生效。

📘

此组件专为文本生成而设计,而非聊天。如果您想将 OpenAI LLM 用于聊天,请改用 OpenAIChatGenerator

用法

单独使用

基本用法

from haystack.components.generators import OpenAIGenerator
from haystack.utils import Secret

client = OpenAIGenerator(model="gpt-4", api_key=Secret.from_token("<your-api-key>"))
response = client.run("What's Natural Language Processing? Be brief.")
print(response)

>>> {'replies': ['Natural Language Processing, often abbreviated as NLP, is a field 
    of artificial intelligence that focuses on the interaction between computers 
    and humans through natural language. The primary aim of NLP is to enable 
    computers to understand, interpret, and generate human language in a valuable way.'], 
    'meta': [{'model': 'gpt-4-0613', 'index': 0, 'finish_reason': 
    'stop', 'usage': {'prompt_tokens': 16, 'completion_tokens': 53, 
    'total_tokens': 69}}]}

使用流式传输

from haystack.components.generators import OpenAIGenerator
from haystack.utils import Secret

client = OpenAIGenerator(streaming_callback=lambda chunk: print(chunk.content, end="", flush=True))
response = client.run("What's Natural Language Processing? Be brief.")
print(response)

>>> Natural Language Processing (NLP) is a branch of artificial 
	intelligence that focuses on the interaction between computers and human 
  language. It involves enabling computers to understand, interpret,and respond 
  to natural human language in a way that is both meaningful and useful.
>>> {'replies': ['Natural Language Processing (NLP) is a branch of artificial 
	intelligence that focuses on the interaction between computers and human 
  language. It involves enabling computers to understand, interpret,and respond 
  to natural human language in a way that is both meaningful and useful.'], 
  'meta': [{'model': 'gpt-4o-mini', 'index': 0, 'finish_reason': 
  'stop', 'usage': {'prompt_tokens': 16, 'completion_tokens': 49, 
  'total_tokens': 65}}]}

在 Pipeline 中

以下是 RAG Pipeline 的示例

from haystack import Pipeline
from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
from haystack.components.builders.prompt_builder import PromptBuilder
from haystack.components.generators import OpenAIGenerator
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack import Document
from haystack.utils import Secret

docstore = InMemoryDocumentStore()
docstore.write_documents([Document(content="Rome is the capital of Italy"), Document(content="Paris is the capital of France")])

query = "What is the capital of France?"

template = """
Given the following information, answer the question.

Context: 
{% for document in documents %}
    {{ document.content }}
{% endfor %}

Question: {{ query }}?
"""
pipe = Pipeline()

pipe.add_component("retriever", InMemoryBM25Retriever(document_store=docstore))
pipe.add_component("prompt_builder", PromptBuilder(template=template))
pipe.add_component("llm", OpenAIGenerator(api_key=Secret.from_token("<your-api-key>"))
pipe.connect("retriever", "prompt_builder.documents")
pipe.connect("prompt_builder", "llm")

res=pipe.run({
    "prompt_builder": {
        "query": query
    },
    "retriever": {
        "query": query
    }
})

print(res)

相关链接

有关参数的详细信息,请参阅我们的 API 参考。