微信扫码
添加专属顾问
我要投稿
import openai
import os
import json
# 加载 .env 文件
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())
# 从环境变量中获得你的 OpenAI Key和配置URL
openai.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"]=None
msg.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-04-25
OpenAI 白送 200 美元的深度研究功能?实测后发现这个「阉割版」不如不用
2025-04-25
为什么一定要做Agent智能体?
2025-04-25
哇!首个MCPBench来了,MCP竟然不比Function Calls更有优势? | 最新
2025-04-25
医疗大模型案例分析(一):Google Med-PaLM
2025-04-25
vLLM+Qwen-32B+Open Web UI构建本地私有大模型
2025-04-25
AI产品经理思考MCP(3):MCP的未来可能
2025-04-25
AI产品经理思考MCP协议(2):标准化的必要性
2025-04-25
AI产品经理思考MCP协议(1):预见MCP——我的“万能库”与标准化之路
2024-08-13
2024-06-13
2024-08-21
2024-09-23
2024-07-31
2024-05-28
2024-08-04
2024-04-26
2024-07-09
2024-09-17