Langchain-Chatchat icon indicating copy to clipboard operation
Langchain-Chatchat copied to clipboard

langchain-chatchat如何自定义工具,像已有的天气查询一样,提供实例并讲述如何调用

Open Andy1018 opened this issue 11 months ago • 1 comments

langchain-chatchat如何自定义工具,像已有的天气查询一样,提供实例并讲述如何调用

Andy1018 avatar Mar 08 '24 09:03 Andy1018

嗨,@Andy1018!很高兴再次见到你。希望你一切都好!🚀

在Langchain-Chatchat中自定义工具,如同现有的天气查询工具,需要遵循一些步骤。让我们创建一个名为currency_converter的自定义工具作为示例。

  1. 定义您的工具:首先,您需要为您的工具创建一个新的Python文件。我们将其命名为currency_converter.py。在此文件中,您将定义工具的逻辑。为了简单起见,假设您正在使用一个名为FictionalCurrencyAPI的虚构API来执行转换。
# currency_converter.py

class CurrencyInput:
    def __init__(self, from_currency, to_currency, amount):
        self.from_currency = from_currency
        self.to_currency = to_currency
        self.amount = amount

def currency_converter(input: CurrencyInput):
    # 假设FictionalCurrencyAPI存在并有一个convert方法
    result = FictionalCurrencyAPI.convert(input.from_currency, input.to_currency, input.amount)
    return f"{input.amount} {input.from_currency} 等于 {result} {input.to_currency}"
  1. 注册您的工具:定义工具后,您需要在__init__.py文件中导入并注册它,所有的工具都在这里被导入并可用。
# 在__init__.py中添加此导入语句
from .currency_converter import currency_converter, CurrencyInput

您的__init__.py文件现在应该是这样的:

## 导入所有的工具类
from .search_knowledgebase_simple import search_knowledgebase_simple
from .search_knowledgebase_once import search_knowledgebase_once, KnowledgeSearchInput
from .search_knowledgebase_complex import search_knowledgebase_complex, KnowledgeSearchInput
from .calculate import calculate, CalculatorInput
from .weather_check import weathercheck, WeatherInput
from .shell import shell, ShellInput
from .search_internet import search_internet, SearchInternetInput
from .wolfram import wolfram, WolframInput
from .search_youtube import search_youtube, YoutubeInput
from .arxiv import arxiv, ArxivInput
# 您的自定义工具
from .currency_converter import currency_converter, CurrencyInput
  1. 调用您的工具:要调用您的工具,通常使用工具的函数以及所需的输入。以下是如何调用currency_converter工具的示例:
from server.agent.tools import currency_converter, CurrencyInput

# 示例用法
input_data = CurrencyInput(from_currency="USD", to_currency="EUR", amount=100)
result = currency_converter(input_data)
print(result)

此示例假设您有一个用于货币转换的虚构API FictionalCurrencyAPI。在实际情况中,您将用实际的逻辑或API调用替换这个,以执行所需的转换。

参考源码:

Sources

About Dosu

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] avatar Mar 08 '24 09:03 dosubot[bot]

这个问题已经被标记为 stale ,因为它已经超过 30 天没有任何活动。

github-actions[bot] avatar Apr 07 '24 21:04 github-actions[bot]