虽然大语言模型具备完成很多任务的能力,但是在专业细分领域并不擅长。此外,大模型本身的幻觉问题也很难通过模型自身的能力来解决,需要引入额外的组件。

因此,我们需要通过一些工具辅助来帮助大模型来完成相关任务。

在DB-GPT所支持的模型列表中,绝大多数都具备工具调用的能力。在开源模型中也有相当一部分模型表现出色,比如glm-4-9b-chat、 Yi-1.5-34B-Chat、Qwen2-72B-Instruct等

工具编写

在有些情况下,大模型没有能力直接完成任务的计算,所以我们需要编写一些计算工具,来辅助大模型完成对应的目标。

  1. from dbgpt.agent.resource import tool
  2. @tool
  3. def simple_calculator(first_number: int, second_number: int, operator: str) -> float:
  4. """Simple calculator tool. Just support +, -, *, /."""
  5. if isinstance(first_number, str):
  6. first_number = int(first_number)
  7. if isinstance(second_number, str):
  8. second_number = int(second_number)
  9. if operator == "+":
  10. return first_number + second_number
  11. elif operator == "-":
  12. return first_number - second_number
  13. elif operator == "*":
  14. return first_number * second_number
  15. elif operator == "/":
  16. return first_number / second_number
  17. else:
  18. raise ValueError(f"Invalid operator: {operator}")

为了测试多工具使用,我们继续编写一个工具来帮助LLM计算目录中的文件数量。

  1. import os
  2. from typing_extensions import Annotated, Doc
  3. @tool
  4. def count_directory_files(path: Annotated[str, Doc("The directory path")]) -> int:
  5. """Count the number of files in a directory."""
  6. if not os.path.isdir(path):
  7. raise ValueError(f"Invalid directory path: {path}")
  8. return len(os.listdir(path))

将工具打包到ToolPack中

绝大多数情况下,尤其是在生产使用中。我们我要具备多工具的能力,所以需要由ToolPack来进行工具的管理。 ToolPack是一组工具的集合,你可以使用它来进行工具管理。 智能体可以根据具体的任务从工具包中选择对应的工具来完成任务。

  1. from dbgpt.agent.resource import ToolPack
  2. tools = ToolPack([simple_calculator, count_directory_files])

在Agent中使用工具

  1. import asyncio
  2. import os
  3. from dbgpt.agent import AgentContext, AgentMemory, LLMConfig, UserProxyAgent
  4. from dbgpt.agent.expand.tool_assistant_agent import ToolAssistantAgent
  5. from dbgpt.model.proxy import OpenAILLMClient
  6. async def main():
  7. llm_client = OpenAILLMClient(
  8. model_alias="gpt-3.5-turbo", # or other models, eg. "gpt-4o"
  9. api_base=os.getenv("OPENAI_API_BASE"),
  10. api_key=os.getenv("OPENAI_API_KEY"),
  11. )
  12. context: AgentContext = AgentContext(
  13. conv_id="test123", language="en", temperature=0.5, max_new_tokens=2048
  14. )
  15. agent_memory = AgentMemory()
  16. user_proxy = await UserProxyAgent().bind(agent_memory).bind(context).build()
  17. tool_man = (
  18. await ToolAssistantAgent()
  19. .bind(context)
  20. .bind(LLMConfig(llm_client=llm_client))
  21. .bind(agent_memory)
  22. .bind(tools)
  23. .build()
  24. )
  25. await user_proxy.initiate_chat(
  26. recipient=tool_man,
  27. reviewer=user_proxy,
  28. message="Calculate the product of 10 and 99",
  29. )
  30. await user_proxy.initiate_chat(
  31. recipient=tool_man,
  32. reviewer=user_proxy,
  33. message="Count the number of files in /tmp",
  34. )
  35. # dbgpt-vis message infos
  36. print(await agent_memory.gpts_memory.one_chat_completions("test123"))
  37. if __name__ == "__main__":
  38. asyncio.run(main())

输出如下

  1. --------------------------------------------------------------------------------
  2. User (to LuBan)-[]:
  3. "Calculate the product of 10 and 99"
  4. --------------------------------------------------------------------------------
  5. un_stream ai response: {
  6. "thought": "To calculate the product of 10 and 99, we need to use a tool that can perform multiplication operation.",
  7. "tool_name": "simple_calculator",
  8. "args": {
  9. "first_number": 10,
  10. "second_number": 99,
  11. "operator": "*"
  12. }
  13. }
  14. --------------------------------------------------------------------------------
  15. LuBan (to User)-[gpt-3.5-turbo]:
  16. "{\n \"thought\": \"To calculate the product of 10 and 99, we need to use a tool that can perform multiplication operation.\",\n \"tool_name\": \"simple_calculator\",\n \"args\": {\n \"first_number\": 10,\n \"second_number\": 99,\n \"operator\": \"*\"\n }\n}"
  17. >>>>>>>>LuBan Review info:
  18. Pass(None)
  19. >>>>>>>>LuBan Action report:
  20. execution succeeded,
  21. 990
  22. --------------------------------------------------------------------------------
  23. --------------------------------------------------------------------------------
  24. User (to LuBan)-[]:
  25. "Count the number of files in /tmp"
  26. --------------------------------------------------------------------------------
  27. un_stream ai response: {
  28. "thought": "To count the number of files in /tmp directory, we should use a tool that can perform this operation.",
  29. "tool_name": "count_directory_files",
  30. "args": {
  31. "path": "/tmp"
  32. }
  33. }
  34. --------------------------------------------------------------------------------
  35. LuBan (to User)-[gpt-3.5-turbo]:
  36. "{\n \"thought\": \"To count the number of files in /tmp directory, we should use a tool that can perform this operation.\",\n \"tool_name\": \"count_directory_files\",\n \"args\": {\n \"path\": \"/tmp\"\n }\n}"
  37. >>>>>>>>LuBan Review info:
  38. Pass(None)
  39. >>>>>>>>LuBan Action report:
  40. execution succeeded,
  41. 19
  42. --------------------------------------------------------------------------------

在上面的代码中,我们使用了ToolAssistantAgent 工具助手智能体来选择并且调用工具,你可以将相关的能力应用到自己的业务当中。

更多细节

在上面代码中,我们用到了tool这个装饰器来定义工具函数。它将函数包装到FunctionTool对象,而FunctionToolBaseTool的子类,BaseTool是所有工具的基类。

事实上,工具也是DB-GPT中Agent的一类特殊资源。如果想了解更多细节,可以查看资源介绍。

附录