当前位置: 首页 > news >正文

用层做的网站/十大营销模式

用层做的网站,十大营销模式,花生壳https和WordPress,设计网站平台风格【GPT入门】第11课 FunctionCall调用代码入门 1. 手撕FunctionCall2.代码3.functionCall的结果 1. 手撕FunctionCall 为了了解,funcationCall底层,手写一个functionCall多方法,并调用,体验 思路: 任务:让…

【GPT入门】第11课 FunctionCall调用代码入门

  • 1. 手撕FunctionCall
  • 2.代码
  • 3.functionCall的结果

1. 手撕FunctionCall

为了了解,funcationCall底层,手写一个functionCall多方法,并调用,体验

思路:
任务:让openai调用sum方法,对加法进行求和
1.定义sum方法,给openAi接口
2.让大模型自动识别用户问题,解释参数,获取调用方法id、方法名称、方法参数
3.把第二步的结果,给大模型,让大模型调用函数,并返回结果

2.代码

from openai import OpenAI
from dotenv import load_dotenv, find_dotenv
import json_ = load_dotenv(find_dotenv())client = OpenAI()def print_json(data):"""打印参数。如果参数是有结构的(如字典或列表),则以格式化的 JSON 形式打印;否则,直接打印该值。"""if hasattr(data, 'model_dump_json'):data = json.loads(data.model_dump_json())if (isinstance(data, (list))):for item in data:print_json(item)elif (isinstance(data, (dict))):print(json.dumps(data,indent=4,ensure_ascii=False))else:print(data)def get_completion(messages, model="gpt-4o-mini"):response = client.chat.completions.create(model=model,messages=messages,temperature=0.7,tools=[{  # 用 JSON 描述函数。可以定义多个。由大模型决定调用谁。也可能都不调用"type": "function","function": {"name": "sum","description": "加法器,计算一组数的和","parameters": {"type": "object","properties": {"numbers": {"type": "array","items": {"type": "number"}}}}}}],)print("response:")print(response)return response.choices[0].messagefrom math import *prompt = "Tell me the sum of 1,2,3,4,5,6,7,8,9,10"messages = [{"role": "system", "content": "你是一个数学家"},{"role": "user", "content": prompt}
]response = get_completion(messages)# 把大模型的回复加入到对话历史中。必须有
messages.append(response)
print("------function call-----")
print(response)if (response.tool_calls is not None):# 是否要调用 sumtool_call = response.tool_calls[0]if(tool_call.function.name == 'sum'):args = json.loads(tool_call.function.arguments)result = sum(args["numbers"])#把函数调用结果加入到对话历史中messages.append({"tool_call_id":tool_call.id, #用于表示函数调用Id"role":"tool","name":"sum","content":str(result) #数值 result 必须转为字符串})#再次调用大模型response = get_completion(messages)messages.append(response)print("-------最终 GPT 回复-------")print(response.content)print("---------对话历史----------")
print_json(messages)

3.functionCall的结果

C:\ProgramData\anaconda3\envs\gptLearning\python.exe E:\workspace\gptLearning\gptLearning\les03\Lesson01_functionCalling.py 
response:
ChatCompletion(id='chatcmpl-B8xbekE9Xfke8t1AkftFpEzpcdtho', choices=[Choice(finish_reason='tool_calls', index=0, logprobs=None, message=ChatCompletionMessage(content=None, refusal=None, role='assistant', audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_CcEYaIPwl1Ru63XmMm6qSGFD', function=Function(arguments='{"numbers":[1,2,3,4,5,6,7,8,9,10]}', name='sum'), type='function')]), content_filter_results={})], created=1741475450, model='gpt-4o-mini-2024-07-18', object='chat.completion', service_tier=None, system_fingerprint='fp_b705f0c291', usage=CompletionUsage(completion_tokens=32, prompt_tokens=79, total_tokens=111, completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0, audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0), prompt_tokens_details=PromptTokensDetails(audio_tokens=0, cached_tokens=0)), prompt_filter_results=[{'prompt_index': 0, 'content_filter_results': {'hate': {'filtered': False, 'severity': 'safe'}, 'jailbreak': {'filtered': False, 'detected': False}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}}}])
------function call-----
ChatCompletionMessage(content=None, refusal=None, role='assistant', audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_CcEYaIPwl1Ru63XmMm6qSGFD', function=Function(arguments='{"numbers":[1,2,3,4,5,6,7,8,9,10]}', name='sum'), type='function')])
response:
ChatCompletion(id='chatcmpl-B8xbfvdhavJ9RTAVxfAk3SmkIjVOT', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='The sum of the numbers 1 through 10 is 55.', refusal=None, role='assistant', audio=None, function_call=None, tool_calls=None))], created=1741475451, model='gpt-4o-mini-2024-07-18', object='chat.completion', service_tier='default', system_fingerprint='fp_06737a9306', usage=CompletionUsage(completion_tokens=16, prompt_tokens=118, total_tokens=134, completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0, audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0), prompt_tokens_details=PromptTokensDetails(audio_tokens=0, cached_tokens=0)))
-------最终 GPT 回复-------
The sum of the numbers 1 through 10 is 55.
---------对话历史----------
{"role": "system","content": "你是一个数学家"
}
{"role": "user","content": "Tell me the sum of 1,2,3,4,5,6,7,8,9,10"
}
{"content": null,"refusal": null,"role": "assistant","audio": null,"function_call": null,"tool_calls": [{"id": "call_CcEYaIPwl1Ru63XmMm6qSGFD","function": {"arguments": "{\"numbers\":[1,2,3,4,5,6,7,8,9,10]}","name": "sum"},"type": "function"}]
}
{"tool_call_id": "call_CcEYaIPwl1Ru63XmMm6qSGFD","role": "tool","name": "sum","content": "55"
}
{"content": "The sum of the numbers 1 through 10 is 55.","refusal": null,"role": "assistant","audio": null,"function_call": null,"tool_calls": null
}Process finished with exit code 0
http://www.whsansanxincailiao.cn/news/30285192.html

相关文章:

  • 网店免费注册/安康地seo
  • 个人网站开发项目总结/搜索关键词查询
  • 学生做兼职的网站/怎么发帖子做推广
  • 惠州外贸网站建设公司/aso优化的主要内容
  • 做dw和ps的网站教学/网站是怎么做出来的
  • 广州市研发网站建设价格/域名地址查询
  • 如何开一个网站/郑州网络营销公司有哪些
  • dede网站/网站推广软件下载安装免费
  • 中国建设教育协会培训中心/建站优化公司
  • wordpress 网站小模块/aso优化{ }贴吧
  • 网站建设费计入什么科目/网络搜索引擎有哪些
  • 商务网站建设规划心得/长沙网站seo外包
  • 网站想要游览怎么做/seo商城
  • 婚恋咨询网站运营/十大经典口碑营销案例
  • 给有后台的网站做网页/网站推广方案策划
  • wordpress插件残留数据/seoul什么意思
  • 网站没有收录从哪开始做优化/百度竞价排名叫什么
  • ftp发布asp.net网站/品牌推广策划方案案例
  • 智联招聘网站怎么做微招聘信息吗/湖南网站定制
  • 网站制作计算机/站内推广的方法和工具
  • 哪个cms可以做交友网站/阿里指数查询
  • 苏州市相城区建设局网站/seo研究协会网
  • 网站活动模板/关键词查询工具
  • 移动网站建设生要女/关键词排名网站
  • 长沙 外贸网站建设公司价格/腾讯会议价格
  • 网站建设-设计/百度推广后台登录入口
  • 宁德市路桥建设有限公司网站/优化游戏卡顿的软件
  • 网站开发 报刊/嘉兴seo计费管理
  • 临沂网站建设哪家专业/香港服务器
  • 网页设计师认证/关键词优化软件