当前位置:   article > 正文

ChatGLM3调用工具 Demo_chatglm3 tool

chatglm3 tool

前言:在ChatGLM3-6B模型中,我们调用了tool工具(ChatGLM3-6B-Base,ChatGLM3-6B-32K没有调用tool功能),这里我们进行简单的demo。

如何制作tools(下列代码中有两个tool:“track”,“text-to-speech”):

  1. tools = [
  2. {
  3. "name": "track",
  4. "description": "追踪指定股票的实时价格",
  5. "parameters": {
  6. "type": "object",
  7. "properties": {
  8. "symbol": {
  9. "description": "需要追踪的股票代码"
  10. }
  11. },
  12. "required": ['symbol']
  13. }
  14. },
  15. {
  16. "name": "text-to-speech",
  17. "description": "将文本转换为语音",
  18. "parameters": {
  19. "type": "object",
  20. "properties": {
  21. "text": {
  22. "description": "需要转换成语音的文本"
  23. },
  24. "voice": {
  25. "description": "要使用的语音类型(男声、女声等)"
  26. },
  27. "speed": {
  28. "description": "语音的速度(快、中等、慢等)"
  29. }
  30. },
  31. "required": ['text']
  32. }
  33. }
  34. ]
  35. system_info = {"role": "system", "content": "Answer the following questions as best as you can. You have access to the following tools:", "tools": tools}
'
运行

tools参数解释:

"name":为tool工具名;

"description":对工具的作用进行描述;

"parameters":

        "type":默认为"object";

        "properties":在此定义工具的属性以及对属性值的描述;

        "required": 需要返回的属性;

加载模型

  1. from transformers import AutoTokenizer, AutoModel
  2. tokenizer = AutoTokenizer.from_pretrained("path_to_model", trust_remote_code=True)
  3. model = AutoModel.from_pretrained("path_to_model", trust_remote_code=True, device='cuda')
  4. model = model.eval()

在调用模型时,我们的query和tool相关时,模型会自动调用tool并反馈

  1. history = [system_info]
  2. query = "帮我查询股票10111的价格"
  3. response, history = model.chat(tokenizer, query, history=history)
  4. print(response)

response输出为{'name': 'track', 'parameters': {'symbol': '10111'}} #模型的反馈 

这里result模拟的是外部的反馈数据,将反馈和history一起传递给模型

(这里 role="observation" 表示输入的是工具调用的返回值而不是用户输入,不能省略。)

  1. import json
  2. result = json.dumps({"price": 12412}, ensure_ascii=False)
  3. response, history = model.chat(tokenizer, result, history=history, role="observation")
  4. print(response)

这是repsonse的值

“根据您的查询,我已经成功调用了追踪股票价格的API,并返回了股票代码为10111的实时股价。根据API调用结果,当前股票的价格为12412元。希望这个信息对您有所帮助! ”

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/990162
推荐阅读
  

闽ICP备14008679号