微信扫码
添加专属顾问
我要投稿
import openaiimport osimport json# 加载 .env 文件from dotenv import load_dotenv, find_dotenv_ = load_dotenv(find_dotenv())# 从环境变量中获得你的 OpenAI Key和配置URLopenai.api_key = os.getenv('OPENAI_API_KEY')openai.api_base = os.getenv('OPENAI_API_URL')model = os.getenv('OPENAI_API_MODEL')def get_current_cluster_state(cluster_name):print(f"cluster:{cluster_name}")return"""ERROR: Failed to pull image "tanjp/docker/dsp:latest"""funcs = {"get_current_cluster_state": get_current_cluster_state}def run(input):msg=[{"role":"user","content":input}]ret = run_conversation(msg)return ret["content"]#function 接受一个名为 "cluster_name" 的参数,用于指定要查询状态的集群名称#temperature=0,# 模型输出的随机性,0 表示随机性最小。需要填写为0,否则会出现其他问题def run_conversation(msg):response = openai.ChatCompletion.create(model=model,messages=msg,temperature=0,# 模型输出的随机性,0 表示随机性最小functions=[{"name": "get_current_cluster_state","description": "Get the current state in a given cluster", #⽅法⽤途描述"parameters": {"type": "object","properties": {"cluster_name": {"type": "string","description": "the name of the cluster",#参数描述},},"required": ["cluster_name"],},}],function_call="auto",#"""控制模型如何响应函数调⽤• “none” 表示模型不调⽤函数• “auto” 表示模型可以⾃动调⽤函数• 通过 {"name": “my_function”} 强制模型调⽤指定函数• 默认值为 "auto"""")message =response["choices"][0]["message"]print("----- message ----")print(message)print("----- message ----")# 如果不需要调用function,则直接返回结果if not message.get("function_call"):return message# 获取调用的方法function_name = message["function_call"]["name"]function_args = json.loads(message["function_call"]["arguments"])print(function_args)res = funcs[function_name](function_args["cluster_name"])message["content"]=Nonemsg.append(message)msg.append({"role": "function","name": function_name,"content": res,})return run_conversation(msg)#python入口函数if __name__ == "__main__":#print(run("Hello"))#集群“DSP”出了什么问题?如果有错误,给我一些建议。print(run("What's wrong with the cluster 'DSP'? And if there's an error, give me some suggestion. "))
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2025-10-27
Claude Code接入飞书mcp,保姆级教程来啦
2025-10-27
GLM-4.6:355B参数的AI巨兽,代码能力爆表,推理性能全面升级,国产AI的新突破!
2025-10-27
放弃ES+Mongo,如何用Milvus一套系统搞定千万用户视频检索*关键词
2025-10-27
AIO Sandbox:为 AI Agent 打造的一体化、可定制的沙箱环境
2025-10-27
豆包视频生成模型1.0 pro fast正式发布:提速3倍,价格直降72%
2025-10-27
当AI学会“听”:声音识别到声音理解,AI到底是怎么听懂的?
2025-10-27
LongCat-Video 视频生成模型正式发布,探索世界模型的第一步
2025-10-27
AI出码率70%+的背后:高德团队如何实现AI研发效率的量化与优化
2025-08-21
2025-08-21
2025-08-19
2025-09-16
2025-09-08
2025-10-02
2025-09-17
2025-08-19
2025-09-29
2025-08-20
2025-10-27
2025-10-26
2025-10-25
2025-10-23
2025-10-23
2025-10-22
2025-10-22
2025-10-20