OpenAPIServiceToFunctions
OpenAPIServiceToFunctions 是一个组件,用于将 OpenAPI 服务规范转换为与 OpenAI 的函数调用机制兼容的格式。
| pipeline 中的最常见位置 | 灵活 |
| 强制运行变量 | “sources”: OpenAPI 规范源的列表,可以是文件路径或 ByteStream 对象 |
| 输出变量 | “functions”: JSON OpenAI 函数调用定义对象的列表。对于 OpenAPI 规范中的每个路径定义,都会生成一个相应的 OpenAI 函数调用定义。 “openapi_specs”: JSON/YAML 对象列表,其中包含已解析的引用。此类 OpenAPI 规范(在解析引用后)反过来可用作 OpenAPIServiceConnector 的输入。 |
| API 参考 | Converters (转换器) |
| GitHub 链接 | https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/openapi_functions.py |
概述
OpenAPIServiceToFunctions 将 OpenAPI 服务规范转换为 OpenAI 函数调用格式。它接收 OpenAPI 规范,对其进行处理以提取函数定义,并将这些定义格式化为与 OpenAI 的函数调用 JSON 格式兼容。
OpenAPIServiceToFunctions 与 OpenAPIServiceConnector 组件一起使用时非常有价值。它将 OpenAPI 规范转换为适合 OpenAI 函数调用的定义,从而允许OpenAPIServiceConnector 处理 OpenAPI 规范的输入参数,并通过OpenAPIServiceConnector.
要使用OpenAPIServiceToFunctions 促进它们在 REST API 调用中的使用,您需要安装一个可选的jsonref 依赖项,使用
pip install jsonref
OpenAPIServiceToFunctions 组件没有任何初始化参数。
用法
单独使用
该组件主要用于管道中。单独使用该组件很有用,当您想将 OpenAPI 规范转换为 OpenAI 的函数调用规范,然后可能将其保存到文件中,并随后在函数调用中使用时。
在 pipeline 中
在管道上下文中,OpenAPIServiceToFunctions 与OpenAPIServiceConnector 一起使用时最有价值。例如,让我们考虑将 serper.dev 搜索引擎桥集成到管道中。OpenAPIServiceToFunctions 从 https://bit.ly/serper_dev_spec 检索 Serper 的 OpenAPI 规范,将此规范转换为 OpenAI 的函数调用机制可以理解的格式,然后无缝地将此翻译后的规范作为generation_kwargs 提供给 LLM 函数调用。
要运行以下代码片段,请注意您必须拥有自己的 Serper 和 OpenAI API 密钥。
import json
import requests
from typing import Dict, Any, List
from haystack import Pipeline
from haystack.components.generators.utils import print_streaming_chunk
from haystack.components.converters import OpenAPIServiceToFunctions, OutputAdapter
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.components.connectors import OpenAPIServiceConnector
from haystack.components.fetchers import LinkContentFetcher
from haystack.dataclasses import ChatMessage, ByteStream
from haystack.utils import Secret
def prepare_fc_params(openai_functions_schema: Dict[str, Any]) -> Dict[str, Any]:
return {
"tools": [{
"type": "function",
"function": openai_functions_schema
}],
"tool_choice": {
"type": "function",
"function": {"name": openai_functions_schema["name"]}
}
}
system_prompt = requests.get("https://bit.ly/serper_dev_system_prompt").text
serper_spec = requests.get("https://bit.ly/serper_dev_spec").text
pipe = Pipeline()
pipe.add_component("spec_to_functions", OpenAPIServiceToFunctions())
pipe.add_component("functions_llm", OpenAIChatGenerator(api_key=Secret.from_token(llm_api_key), model="gpt-3.5-turbo-0613"))
pipe.add_component("openapi_container", OpenAPIServiceConnector())
pipe.add_component("a1", OutputAdapter("{{functions[0] | prepare_fc}}", Dict[str, Any], {"prepare_fc": prepare_fc_params}))
pipe.add_component("a2", OutputAdapter("{{specs[0]}}", Dict[str, Any]))
pipe.add_component("a3", OutputAdapter("{{system_message + service_response}}", List[ChatMessage]))
pipe.add_component("llm", OpenAIChatGenerator(api_key=Secret.from_token(llm_api_key), model="gpt-4-1106-preview", streaming_callback=print_streaming_chunk))
pipe.connect("spec_to_functions.functions", "a1.functions")
pipe.connect("spec_to_functions.openapi_specs", "a2.specs")
pipe.connect("a1", "functions_llm.generation_kwargs")
pipe.connect("functions_llm.replies", "openapi_container.messages")
pipe.connect("a2", "openapi_container.service_openapi_spec")
pipe.connect("openapi_container.service_response", "a3.service_response")
pipe.connect("a3", "llm.messages")
user_prompt = "Why was Sam Altman ousted from OpenAI?"
result = pipe.run(data={"functions_llm": {"messages":[ChatMessage.from_system("Only do function calling"), ChatMessage.from_user(user_prompt)]},
"openapi_container": {"service_credentials": serper_dev_key},
"spec_to_functions": {"sources": [ByteStream.from_string(serper_spec)]},
"a3": {"system_message": [ChatMessage.from_system(system_prompt)]}})
>Sam Altman was ousted from OpenAI on November 17, 2023, following
>a "deliberative review process" by the board of directors. The board concluded
>that he was not "consistently candid in his communications". However, he
>returned as CEO just days after his ouster.
更新于 大约 1 年前
