from transformers import AutoTokenizer
tokenizer_path = "/root/ld/ld_model_pretrained/minicpm3"
url = "http://localhost:8000/v1/chat/completions" # 假设服务运行在本地机器上
"Content-Type": "application/json",
"Authorization": "Bearer token-abc123" # 使用上面命令中的API密钥
"name": "get_delivery_date",
"description": "Get the delivery date for a customer's order. Call this whenever you need to know the delivery date, for example when a customer asks 'Where is my package'",
"description": "The customer's order ID.",
"required": ["order_id"],
"additionalProperties": False,
"description": "image_gen is an AI painting (image generation) service that inputs a text description and returns the URL of the image drawn based on the text.",
"description": "Text description of the image the user wants to generate",
"required": ["description_image"],
"additionalProperties": False,
"content": "You are a helpful customer support assistant. Use the supplied tools to assist the user.",
"content": "Hi, can you tell me the delivery date for my order? The order id is 1234 and 4321.",
tokenizer = AutoTokenizer.from_pretrained(tokenizer_path, trust_remote_code=True)
prompt = tokenizer.apply_chat_template(
messages, tools=tools, tokenize=False, add_generation_prompt=True
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
def fake_tool_execute(toolcall):
tool_name=toolcall['name']
arguments=eval(toolcall['arguments'])
if tool_name == "get_delivery_date":
order_id=arguments['order_id']
get_data="2024-09-05" if int(order_id)%2==0 else "2024-09-06"
elif tool_name == "image_gen":
prompt=arguments['description_image']
prompt = urllib.parse.quote(prompt)
return json.dumps({'image_url': f'https://image.pollinations.ai/prompt/{prompt}'}, ensure_ascii=False)
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
for msg in response.json()["choices"][0]["message"]['tool_calls']:
and msg["function"] is not None
and len(msg["function"]) > 0
toolcall=msg["function"]
tool_response = fake_tool_execute(toolcall)
"content": tool_response,
"plugin": toolcall["name"],
"input_param":toolcall['arguments']
print(f"Error: {response.status_code}, {response.text}")