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

OpenAPIServiceConnector

OpenAPIServiceConnector 是一个充当 Haystack 生态系统与 OpenAPI 服务之间接口的组件。

pipeline 中的最常见位置灵活
强制运行变量“messages”: 一个 ChatMessage 对象列表,其中最后一个消息预计包含参数调用负载。

”service_openapi_spec”: 被调用服务的 OpenAPI 规范。它可以是 YAML/JSON,并且所有 ref 值都必须已解析。

”service_credentials”: 服务的身份验证凭据。我们目前支持两种 OpenAPI spec v3 安全方案

1. http – 用于 Basic、Bearer 和其他 HTTP 身份验证方案;2. apiKey – 用于 API 密钥和 cookie 身份验证。
输出变量“service_response”: 一个字典,它是 ChatMessage 对象的列表,其中每个消息对应一个函数调用。
如果用户指定了多个函数调用请求,将会有多个响应。
API 参考Connectors (连接器)
GitHub 链接https://github.com/deepset-ai/haystack/blob/main/haystack/components/connectors/openapi_service.py

概述

OpenAPIServiceConnector 作为 Haystack 生态系统和 OpenAPI 服务之间的桥梁。此组件通过使用来自ChatMessage 的信息来动态调用服务方法。它处理来自ChatMessage 的参数负载解析、服务身份验证、方法调用和响应格式化,从而更轻松地集成 OpenAPI 服务。

要使用OpenAPIServiceConnector,您需要安装可选的openapi3 依赖项,使用

pip install openapi3

OpenAPIServiceConnector 组件没有任何初始化参数。

用法

单独使用

此组件主要用于管道中,作为 OpenAPIServiceToFunctions,与函数调用模型配合使用,解析实际的函数调用参数,这些参数作为调用参数注入到OpenAPIServiceConnector.

在 pipeline 中

假设我们要将 Serper 搜索引擎链接到管道。在此,OpenAPIServiceConnector 利用了OpenAPIServiceToFunctions. OpenAPIServiceToFunctions 的能力,首先获取并以 OpenAI 的函数调用机制可以理解的格式更改 Serper 的 OpenAPI 规范。然后,OpenAPIServiceConnector 使用此规范激活 Serper 服务。

更具体地说,OpenAPIServiceConnector 动态调用 Serper OpenAPI 规范中定义的方法。这包括读取聊天消息或其他输入以提取函数调用参数,处理与 Serper 服务的身份验证,以及进行正确的 API 调用。该连接器确保方法调用遵循 Serper API 的要求,例如正确的请求格式化和响应处理。

请注意,我们此处仅以 Serper 为例。任何符合 OpenAPI 标准的服务都可以。

📘

要运行以下代码段,请注意您必须拥有自己的 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.

相关链接

在我们的 API 参考中查看参数详情