How to fetch data from a third-party API #985

Closed
opened 2026-02-21 17:29:21 -05:00 by yindo · 3 comments
Owner

Originally created by @fatcatloversknnymonkey on GitHub (Jan 27, 2024).

Self Checks

Provide a description of the help you need

我想要调用三方的API,包括不限于自己服务器的接口API或者其他的API。返回的内容不可控,可能会返回一些数据集或者用户信息亦或者其他的内容。请问有什么办法集成进去吗?

Originally created by @fatcatloversknnymonkey on GitHub (Jan 27, 2024). ### Self Checks - [X] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [X] I confirm that I am using English to file this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). ### Provide a description of the help you need 我想要调用三方的API,包括不限于自己服务器的接口API或者其他的API。返回的内容不可控,可能会返回一些数据集或者用户信息亦或者其他的内容。请问有什么办法集成进去吗?
yindo added the 💪 enhancement label 2026-02-21 17:29:21 -05:00
yindo closed this issue 2026-02-21 17:29:22 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jan 27, 2024):

🤖

您好 @fatcatloversknnymonkey,很高兴再次看到你的问题!希望你一切都好。

要集成第三方API,包括您自己的服务器API和其他API,您可以参考DatasetRetrievalFeature类中使用的模式。这个类使用retrieve方法通过AgentExecutor运行查询并返回结果。

AgentExecutor使用一个包含工具列表的AgentConfiguration,这些工具是BaseTool的实例。在这种情况下,工具是DatasetRetrieverToolDatasetMultiRetrieverTool的实例,用于从数据集中检索信息。

要集成您自己的API,您需要创建一个新的类,继承自BaseTool,并实现与您的API交互所需的方法。然后,这个新类将在AgentConfiguration中代替或者添加到DatasetRetrieverToolDatasetMultiRetrieverTool

以下是一个简化的示例,展示如何创建一个新的工具来与您的API交互:

from core.tools.tool import BaseTool

class MyAPITool(BaseTool):
    def __init__(self, api_url, api_key):
        self.api_url = api_url
        self.api_key = api_key

    def run(self, query):
        # 在这里实现与您的API交互的逻辑。
        # 这可能涉及向API发送请求并处理响应。
        pass

然后,您可以像这样在AgentConfiguration中使用这个工具:

my_api_tool = MyAPITool(api_url='https://my-api.com', api_key='my-api-key')

agent_configuration = AgentConfiguration(
    strategy=planning_strategy,
    model_config=model_config,
    tools=[my_api_tool],
    memory=memory,
    max_iterations=10,
    max_execution_time=400.0,
    early_stopping_method="generate"
)

agent_executor = AgentExecutor(agent_configuration)
result = agent_executor.run(query)

这只是一个简化的示例,实际的实现将取决于您的API的具体情况以及您希望如何与它交互。

相关资源:

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

@dosubot[bot] commented on GitHub (Jan 27, 2024): _🤖_ <!-- Greeting --> 您好 @fatcatloversknnymonkey,很高兴再次看到你的问题!希望你一切都好。 <!-- Answer --> 要集成第三方API,包括您自己的服务器API和其他API,您可以参考`DatasetRetrievalFeature`类中使用的模式。这个类使用`retrieve`方法通过`AgentExecutor`运行查询并返回结果。 `AgentExecutor`使用一个包含工具列表的`AgentConfiguration`,这些工具是`BaseTool`的实例。在这种情况下,工具是`DatasetRetrieverTool`或`DatasetMultiRetrieverTool`的实例,用于从数据集中检索信息。 要集成您自己的API,您需要创建一个新的类,继承自`BaseTool`,并实现与您的API交互所需的方法。然后,这个新类将在`AgentConfiguration`中代替或者添加到`DatasetRetrieverTool`或`DatasetMultiRetrieverTool`。 以下是一个简化的示例,展示如何创建一个新的工具来与您的API交互: ```py from core.tools.tool import BaseTool class MyAPITool(BaseTool): def __init__(self, api_url, api_key): self.api_url = api_url self.api_key = api_key def run(self, query): # 在这里实现与您的API交互的逻辑。 # 这可能涉及向API发送请求并处理响应。 pass ``` 然后,您可以像这样在`AgentConfiguration`中使用这个工具: ```py my_api_tool = MyAPITool(api_url='https://my-api.com', api_key='my-api-key') agent_configuration = AgentConfiguration( strategy=planning_strategy, model_config=model_config, tools=[my_api_tool], memory=memory, max_iterations=10, max_execution_time=400.0, early_stopping_method="generate" ) agent_executor = AgentExecutor(agent_configuration) result = agent_executor.run(query) ``` 这只是一个简化的示例,实际的实现将取决于您的API的具体情况以及您希望如何与它交互。 相关资源: - [api/core/features/dataset_retrieval.py](https://github.com/langgenius/dify/blob/main/api/core/features/dataset_retrieval.py) ## Sources - [api/core/features/dataset_retrieval.py](https://github.com/langgenius/dify/blob/main/api/core/features/dataset_retrieval.py) --- This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot.
Author
Owner

@takatost commented on GitHub (Jan 27, 2024):

image

You can now try using API-based Variable to fetch data from your own API and use it as a variable in your Prompt. In the next month, we'll be rolling out Workflow as a more powerful alternative.

@takatost commented on GitHub (Jan 27, 2024): <img width="738" alt="image" src="https://github.com/langgenius/dify/assets/5485478/55853961-0dd6-4849-bd1f-98b95e340b55"> You can now try using API-based Variable to fetch data from your own API and use it as a variable in your Prompt. In the next month, we'll be rolling out Workflow as a more powerful alternative.
Author
Owner

@crazywoola commented on GitHub (Jan 27, 2024):

See this and this for how to build an API tool

@crazywoola commented on GitHub (Jan 27, 2024): See [this](https://docs.dify.ai/features/extension/api_based_extension/external_data_tool#api-response) and [this](https://docs.dify.ai/tutorials/cloudflare_workers) for how to build an API tool
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#985