parent
1db093b2a8
commit
b4e0395871
|
|
@ -105,3 +105,11 @@ def alltool_stream():
|
||||||
if chunk:
|
if chunk:
|
||||||
yield chunk
|
yield chunk
|
||||||
return Response(event_stream(), content_type='text/event-stream')
|
return Response(event_stream(), content_type='text/event-stream')
|
||||||
|
|
||||||
|
@zhipu_controller.route('/zhipu/alltool/classify/non-stream', methods=['POST'])
|
||||||
|
def alltool_classify_non_stream():
|
||||||
|
data = request.json
|
||||||
|
message = data.get('message', '')
|
||||||
|
response = zhipu_alltool_service.func_call_classify(message)
|
||||||
|
print(f'response: {response}')
|
||||||
|
return response
|
||||||
|
|
@ -10,60 +10,44 @@ class ZhipuAlltoolService:
|
||||||
self.app_secret_key = "d54f764a1d67c17d857bd3983b772016.GRjowY0fyiMNurLc"
|
self.app_secret_key = "d54f764a1d67c17d857bd3983b772016.GRjowY0fyiMNurLc"
|
||||||
logger.info("ZhipuAlltoolService initialized with model: %s", self.model_name)
|
logger.info("ZhipuAlltoolService initialized with model: %s", self.model_name)
|
||||||
|
|
||||||
def func_call(self, message):
|
def func_call_classify(self, message):
|
||||||
logger.info("Starting web_search call")
|
logger.info("Starting web_search call")
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
client = ZhipuAI(api_key=self.app_secret_key)
|
client = ZhipuAI(api_key=self.app_secret_key)
|
||||||
try:
|
tools = [
|
||||||
response = client.chat.completions.create(
|
|
||||||
model="glm-4-alltools", # 填写需要调用的模型名称
|
|
||||||
messages=[
|
|
||||||
{
|
|
||||||
"role": "user",
|
|
||||||
"content":[
|
|
||||||
{
|
|
||||||
"type":"text",
|
|
||||||
"text":"帮我查询2018年至2024年,每年五一假期全国旅游出行数据,并绘制成柱状图展示数据趋势。"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
stream=True,
|
|
||||||
tools=[
|
|
||||||
{
|
{
|
||||||
"type": "function",
|
"type": "function",
|
||||||
"function": {
|
"function": {
|
||||||
"name": "get_tourist_data_by_year",
|
"name": "classify_user_input",
|
||||||
"description": "用于查询每一年的全国出行数据,输入年份范围(from_year,to_year),返回对应的出行数据,包括总出行人次、分交通方式的人次等。",
|
"description": "根据用户输入,判断用户意图,返回意图类型",
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"type": {
|
"category": {
|
||||||
"description": "交通方式,默认为by_all,火车=by_train,飞机=by_plane,自驾=by_car",
|
"type": "string",
|
||||||
"type": "string"
|
"description": "用户的意图有以下选项:web_search,retrive_knowledge,generate_report",
|
||||||
},
|
|
||||||
"from_year": {
|
|
||||||
"description": "开始年份,格式为yyyy",
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"to_year": {
|
|
||||||
"description": "结束年份,格式为yyyy",
|
|
||||||
"type": "string"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"required": ["from_year","to_year"]
|
"required": ["category"],
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
{
|
}
|
||||||
"type": "code_interpreter"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
messages = [
|
||||||
|
{
|
||||||
|
"role": "user",
|
||||||
|
"content": f"判断以下用户输入的意图并将其分类返回意图类型:{message}"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
try:
|
||||||
|
response = client.chat.completions.create(
|
||||||
|
model="glm-4-flash", # 填写需要调用的模型名称
|
||||||
|
messages= messages,
|
||||||
|
tools= tools,
|
||||||
|
tool_choice="auto"
|
||||||
)
|
)
|
||||||
|
print(response)
|
||||||
for chunk in response:
|
return response.choices[0].message.tool_calls[0].function.arguments
|
||||||
print(chunk)
|
|
||||||
return response
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error("Error in web_search call: %s", str(e))
|
logger.error("Error in web_search call: %s", str(e))
|
||||||
raise e
|
raise e
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue