当前位置:   article > 正文

调试文心大模型或chatgpt的function Calling函数应用场景_大模型function call

大模型function call

沉默了一段时间,最近都在研究AI大模型的产品落地应用,我觉得这个function calling出来后,对目前辅助办公有革命性的改变,可以它来做什么呢?我们先来调试看看,chatgpt和文心大模型的ERNIE Bot支持这个,chatgpt申请有一定门槛,先以文心的为例,账号的access_token申请我就不说了,直接去飞桨飞桨AI Studio星河社区-人工智能学习与实训注册申请。安装那些自己直接看官方文档,上代码:

  1. #coding:utf-8
  2. import erniebot,json
  3. #记载模型
  4. models = erniebot.Model.list()
  5. #参数
  6. erniebot.api_type = "aistudio"
  7. erniebot.access_token = "你的token码"
  8. messages = [
  9. {
  10. 'role': 'user',
  11. 'content': "搜索用户李大锤的信息",
  12. },
  13. ]
  14. functions = [
  15. {
  16. 'name': 'get_current_temperature',
  17. 'description': "获取指定城市的气温",
  18. 'parameters': {
  19. 'type': 'object',
  20. 'properties': {
  21. 'location': {
  22. 'type': 'string',
  23. 'description': "城市名称",
  24. },
  25. 'time': {
  26. 'type': 'string',
  27. 'description': "时间",
  28. },
  29. 'unit': {
  30. 'type': 'string',
  31. 'enum': [
  32. '摄氏度',
  33. '华氏度',
  34. ],
  35. },
  36. },
  37. 'required': [
  38. 'location',
  39. 'time',
  40. 'unit',
  41. ],
  42. },
  43. 'responses': {
  44. 'type': 'object',
  45. 'properties': {
  46. 'temperature': {
  47. 'type': 'integer',
  48. 'description': "城市气温",
  49. },
  50. 'time': {
  51. 'type': 'string',
  52. 'description': "时间",
  53. },
  54. 'unit': {
  55. 'type': 'string',
  56. 'enum': [
  57. '摄氏度',
  58. '华氏度',
  59. ],
  60. },
  61. },
  62. },
  63. },
  64. {
  65. 'name': 'get_user_info',
  66. 'description': "获取用户信息",
  67. 'parameters': {
  68. 'type': 'object',
  69. 'properties': {
  70. 'username': {
  71. 'type': 'string',
  72. 'description': "用户名",
  73. },
  74. },
  75. 'required': [
  76. 'username',
  77. ],
  78. },
  79. 'responses': {
  80. 'type': 'object',
  81. 'properties': {
  82. 'username': {
  83. 'type': 'string',
  84. 'description': "用户名",
  85. },
  86. 'birthday': {
  87. 'type': 'string',
  88. 'description': "用户生日",
  89. },
  90. 'hobby': {
  91. 'type': 'string',
  92. 'description': "用户爱好",
  93. },
  94. },
  95. },
  96. },
  97. ]
  98. response = erniebot.ChatCompletion.create(
  99. model='ernie-bot',
  100. messages=messages,
  101. functions=functions,
  102. )
  103. print("one:",response.get_result())

funcitons是一个数组,里面可以定义多组方法类的数据,上面我设置了2个方法,一个是查询天气的,一个是查询用户信息的,我们截取一个片段看看:

  1. {
  2. 'name': 'get_user_info', #我们自己定义的函数名称
  3. 'description': "获取用户信息", #描述
  4. 'parameters': {
  5. 'type': 'object',
  6. 'properties': {
  7. 'username': {
  8. 'type': 'string',
  9. 'description': "用户名",
  10. },
  11. },
  12. 'required': [
  13. 'username',
  14. ],
  15. },
  16. 'responses': {
  17. 'type': 'object',
  18. 'properties': {
  19. 'username': {
  20. 'type': 'string',
  21. 'description': "用户名",
  22. },
  23. 'birthday': {
  24. 'type': 'string',
  25. 'description': "用户生日",
  26. },
  27. 'hobby': {
  28. 'type': 'string',
  29. 'description': "用户爱好",
  30. },
  31. },
  32. },
  33. },
'
运行

这格式是固定的,parameters就是设置的参数,可定义参数的名称,类型,描述,responses就是返回的结果参数,这里主要就是定义好方法类及参数,这是执行的调用写法:

  1. response = erniebot.ChatCompletion.create(
  2. model='ernie-bot',
  3. messages=messages, #消息
  4. functions=functions, #匹配函数
  5. )

执行上面的代码会返回:

大模型会根据你的问题智能匹配是否符合函数的关键词(我估计可能也会存在一定的误差,整体测试还行),匹配上后会返回以上信息,get_user_info是你方法类的名称,arguments里面就是你定义的参数,它智能匹配到了用户名叫李大锤的。

接下来就可以根据返回的信息去调用本地对应的方法函数:

  1. #获取天气
  2. def get_current_temperature(location,time,unit):
  3. #这里可以自定义执行方法,下面是演示返回结果
  4. return {"location":location,"temperature": 36, "time": time, "unit": "℃"}
  5. #获取用户信息
  6. def get_user_info(username):
  7. #这里可以自定义执行方法,下面是演示返回结果
  8. return {"username": username, "birthday": "1998-08-08", "hobby": "踢球,打游戏,吃火锅"}
  9. #如果匹配成功则执行下面代码
  10. if response.is_function_response:
  11. function_call = response.get_result()
  12. name2function = {'get_current_temperature': get_current_temperature,'get_user_info': get_user_info}
  13. func = name2function[function_call['name']]
  14. func_name=function_call['name']
  15. args = json.loads(function_call['arguments'])
  16. res=""
  17. #根据name判断调用本地那个方法类
  18. if func_name=='get_current_temperature':
  19. res = func(location=args['location'],time=args['time'],unit=args['unit'])
  20. elif func_name=='get_user_info':
  21. res = func(username=args['username'])
  22. print("res:", res)

上面执行一下,看看结果

到这里基本上都走通了,开始的问题:搜索用户李大锤的信息,匹配后就执行了你的get_user_info的方法返回了结果。

通过以上思考,这样的话是不是可以在自己的公司产品或者项目开发一个辅助工具,更有效便捷的帮助用户摆脱一些繁琐复杂的操作流程,这个想象空间我感觉很大。

得到json后我们可以再次通过模型美化一下:

  1. #再次通过Ai执行
  2. messages.append(
  3. {
  4. "role": "assistant",
  5. "content": None,
  6. "function_call": function_call,
  7. }
  8. )
  9. messages.append(
  10. {
  11. "role": "function",
  12. "name": function_call["name"],
  13. "content": json.dumps(res, ensure_ascii=False),
  14. }
  15. )
  16. secendresponse = erniebot.ChatCompletion.create(
  17. model='ernie-bot',
  18. messages=messages,
  19. functions=functions,
  20. )
  21. print(secendresponse.result)

来执行看看:

这功能还是挺强大的,它的原理简单的可以理解为关键词匹配,有点类似小爱,小度智能控制的赶脚,然后在加上模型本身的文案编写,图片生成等功能,真的可以让自己的产品得到一个飞一般的提升,然后在想象一下,把它结合到软件工具中,是不是可以智能化的执行系统的命令,智能回复及应答,搜索等等,我目前第一步就是先集成到公司的产品中去,好了,记录就先到这里。

如果有需要的源代码的同学,请关注“小白一起学编程”公众号回复:functioncalling 下载源码

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

闽ICP备14008679号