基于智普AI使用functioncall实现。#11248284577cs #113

Open
opened 2024-09-21 12:52:03 +08:00 by 11248284577cs · 0 comments

`import json

from dotenv import load_dotenv
load_dotenv() # 这将加载项目根目录下的.env文件中的环境变量

from zhipuai import ZhipuAI

初始化客户端

client = ZhipuAI()

获取产品列表

def getProducts() :
products = {
"products": [
{
"product_name": "产品A",
"product_price": 100.0,
"remaining_quantity": 50
},
{
"product_name": "产品B",
"product_price": 200.5,
"remaining_quantity": 75
},
{
"product_name": "产品C",
"product_price": 55.75,
"remaining_quantity": 100
},
{
"product_name": "产品D",
"product_price": 300.0,
"remaining_quantity": 25
},
{
"product_name": "产品E",
"product_price": 99.99,
"remaining_quantity": 0
},
{
"product_name": "产品F",
"product_price": 150.25,
"remaining_quantity": 30
},
{
"product_name": "产品G",
"product_price": 89.95,
"remaining_quantity": 60
},
{
"product_name": "产品H",
"product_price": 250.0,
"remaining_quantity": 45
}
]
}
return products

定义可调用的函数列表

tools = [{
"type": "function",
"function": {
"name": "getProducts",
"description": "返回店铺的全部产品",
"parameters":{
"type": "object",
"properties": {} # 即使没有参数,也需要定义空的properties对象
}
}
}]

构建对话消息

messages = [
{"role": "system", "content": "你能够语义分析,判断是否要用道具,如果要则返回到tool名称和参数"},
{"role": "user", "content": "1.列出所有产品及其价格和库存.2.产品D的多少钱"},
]
model_response = client.chat.completions.create(
model="glm-4", # 填写需要调用的模型编码
messages=messages,
tools=tools,
tool_choice="auto"
)
print("model_response", model_response)

处理模型返回的函数调用信息

if model_response.choices[0].message.tool_calls:
for tool_call in model_response.choices[0].message.tool_calls:
if tool_call.function.name == "getProducts":
# 这里模拟调用 getProducts 函数并获取结果
products = getProducts()
# 将函数调用结果添加到消息中
messages.append({
"role": "tool",
"content": json.dumps(products),
"tool_call_id": tool_call.id
})

再次调用模型以生成最终回复

final_response = client.chat.completions.create(
model="glm-4",
messages=messages,
tools=tools
)

输出最终结果

print("Final response:", final_response)

#根据您的请求,以下是所有产品的列表及其价格和库存情况:

1. 产品A:价格100.0元,库存50件

2. 产品B:价格200.5元,库存75件

3. 产品C:价格55.75元,库存100件

4. 产品D:价格300.0元,库存25件

5. 产品E:价格99.99元,库存0件

6. 产品F:价格150.25元,库存30件

7. 产品G:价格89.95元,库存60件

8. 产品H:价格250.0元,库存45件

您查询的产品D的价格为300.0元。`

`import json from dotenv import load_dotenv load_dotenv() # 这将加载项目根目录下的.env文件中的环境变量 from zhipuai import ZhipuAI # 初始化客户端 client = ZhipuAI() # 获取产品列表 def getProducts() : products = { "products": [ { "product_name": "产品A", "product_price": 100.0, "remaining_quantity": 50 }, { "product_name": "产品B", "product_price": 200.5, "remaining_quantity": 75 }, { "product_name": "产品C", "product_price": 55.75, "remaining_quantity": 100 }, { "product_name": "产品D", "product_price": 300.0, "remaining_quantity": 25 }, { "product_name": "产品E", "product_price": 99.99, "remaining_quantity": 0 }, { "product_name": "产品F", "product_price": 150.25, "remaining_quantity": 30 }, { "product_name": "产品G", "product_price": 89.95, "remaining_quantity": 60 }, { "product_name": "产品H", "product_price": 250.0, "remaining_quantity": 45 } ] } return products # 定义可调用的函数列表 tools = [{ "type": "function", "function": { "name": "getProducts", "description": "返回店铺的全部产品", "parameters":{ "type": "object", "properties": {} # 即使没有参数,也需要定义空的properties对象 } } }] # 构建对话消息 messages = [ {"role": "system", "content": "你能够语义分析,判断是否要用道具,如果要则返回到tool名称和参数"}, {"role": "user", "content": "1.列出所有产品及其价格和库存.2.产品D的多少钱"}, ] model_response = client.chat.completions.create( model="glm-4", # 填写需要调用的模型编码 messages=messages, tools=tools, tool_choice="auto" ) print("model_response", model_response) # 处理模型返回的函数调用信息 if model_response.choices[0].message.tool_calls: for tool_call in model_response.choices[0].message.tool_calls: if tool_call.function.name == "getProducts": # 这里模拟调用 getProducts 函数并获取结果 products = getProducts() # 将函数调用结果添加到消息中 messages.append({ "role": "tool", "content": json.dumps(products), "tool_call_id": tool_call.id }) # 再次调用模型以生成最终回复 final_response = client.chat.completions.create( model="glm-4", messages=messages, tools=tools ) # 输出最终结果 print("Final response:", final_response) #根据您的请求,以下是所有产品的列表及其价格和库存情况: # 1. 产品A:价格100.0元,库存50件 # 2. 产品B:价格200.5元,库存75件 # 3. 产品C:价格55.75元,库存100件 # 4. 产品D:价格300.0元,库存25件 # 5. 产品E:价格99.99元,库存0件 # 6. 产品F:价格150.25元,库存30件 # 7. 产品G:价格89.95元,库存60件 # 8. 产品H:价格250.0元,库存45件 # 您查询的产品D的价格为300.0元。`
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: HswOAuth/llm_course#113
No description provided.