当前位置:   article > 正文

AI菜鸟向前飞 — LangChain系列之十三 - 关于Tool的必知必会_langchain tool 返回的结果

langchain tool 返回的结果

图片

之前给大家介绍了对大语言模型知识助力的一种方式RAG,大家可以回顾

AI菜鸟向前飞 — LangChain系列之十 - RAG(上篇)

AI菜鸟向前飞 — LangChain系列之十一 - RAG(中篇)

AI菜鸟向前飞 — LangChain系列之十二 - RAG(下篇)

接下来 介绍如何使用Tool为大语言模型助力。

同时,Tool也是学会Agent和LangGraph的重要基础哈

Tool三要素

图片

实现方式(三种)

Class定义

  1. class CustomToolClass(BaseTool):
  2. name = "Custom tool"
  3. description = "这是一个处理XXX问题的工具"
  4. def _run(self, text: str) -> str:
  5. …………………………
  6. return "你好啊,我的处理结果是xxxxxx"
  7. async def _arun(self, text: str) -> str:
  8. return "你好啊,我的处理结果是xxxxxx"

StructuredTool

  1. def dealwithX(text: str) -> str:
  2. '''这是一个处理XXX问题的工具'''
  3. …………………………
  4. return "你好啊,我的处理结果是xxxxx"
  5. CustomTool = StructuredTool.from_function(
  6. func=dealwithX,
  7. name = "处理函数",
  8. description="你好啊,我是一个处理函数",
  9. )

装饰器Decorater

  1. @tool
  2. def dealwithX(text1: str) -> str:
  3. '''这是一个处理XXX问题的工具'''
  4. …………………………
  5. return "你好啊,我的处理结果是xxxxx"

与大语言模型交互

先看一段代码

  1. @tool
  2. def get_citations(docs: str):
  3. ''' 这是一个询问文章出处的处理函数 '''
  4. return f"{docs}的文章出自于 Song榆钱儿公众号,欢迎关注。"
  5. @tool
  6. def contain_docs(series: str):
  7. """查询包含多少篇文章的函数"""
  8. return f"这个{series}里面已包含20多篇文章。"
  9. tools = [get_citations, contain_docs]
  10. model_with_tool = model.bind_tools(tools=tools)
  11. res = model_with_tool.invoke("你正在阅读的'AI菜鸟系列'文章出自哪里?这个公众号一共有多少篇文章?")
  12. print(res)

返回的内容很多很多,我挑两个个重点内容介绍下

  • additional_kwargs

包含了tool_calls,即:把上面写的那两个Tool(函数)转成json格式返回

图片

  • Tool_calls

一个单独的tool_calls列表,此处划重点,后面会用到它

  1. [{'name': 'get_citations', 'args': {'docs': 'AI菜鸟系列'}, 'id': 'a8ff6f2f78c84bc3a3999ae2b0356c0a'},
  2. {'name': 'contain_docs', 'args': {'series': 'AI菜鸟系列'}, 'id': 'ff4beda02a61404a922affae5aff5099'}]

聪明的小伙伴能想到,我们也可以把上面的程序改成这种形式,就不会看那么多繁琐的内容

print(res.tool_calls)

函数还没被真正调用起来

    因为还没开始介绍Agent和LangGraph,这里我们可以“曲线救国”,用最原始的方式,代码如下:

  1. func_with_args = res.tool_calls
  2. # 映射
  3. func_map = {"get_citations": get_citations, "contain_docs": contain_docs}
  4. # 挨个调用
  5. for each in func_with_args:
  6. func_res = func_map[each["name"]].invoke(input=each["args"])
  7. print(func_res)

输出结果

  1. AI菜鸟系列的文章出自于 Song榆钱儿公众号,欢迎关注。
  2. 这个AI菜鸟系列里面已包含20多篇文章。

如果这么做,那要LangChain有何用...后面讲到Agent你就懂了:)

扩展知识

在早期的版本中(直接用OpenAI)在bind函数时(上面例子我们直接用的bind_tools),需要把函数转成json才能“绑”成功,这里就扩展一个这方面的知识点,使用

  1. from langchain_core.utils.function_calling import convert_to_openai_function
  2. convert_to_openai_function(get_citations)

让我们看看返回结果,有没有觉得跟上面的additional_kwargs很像

  1. {'name': 'get_citations',
  2. 'description': '这是一个询问文章出处的处理函数',
  3. 'parameters': {
  4. 'type': 'object',
  5. 'properties': {
  6. 'docs': {
  7. 'type': 'string'}
  8. },
  9. 'required': ['docs']
  10. }
  11. }

    聪明的小伙伴会想到,上面bind_tool是不是可以用bind替换掉, 那...当然没问题

  1. # model_with_tool = model.bind_tools(tools=[get_citations, contain_docs])
  2. model_with_tool = model.bind(tools=[convert_to_openai_function(get_citations), convert_to_openai_function(contain_docs)])

基础打牢了之后,下一篇将开始介绍Agent :)

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号