BUGFIX:chat robot添加internet_search
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
|
|
||||||
from dashscope import Generation
|
from dashscope import Generation
|
||||||
from retry import retry
|
from retry import retry
|
||||||
@@ -159,10 +160,11 @@ def search_from_internet(message):
|
|||||||
model='qwen-turbo',
|
model='qwen-turbo',
|
||||||
api_key=QWEN_API_KEY,
|
api_key=QWEN_API_KEY,
|
||||||
messages=message,
|
messages=message,
|
||||||
tools=tools,
|
prompt='The output must be in English.Keep the final result under 200 words.'
|
||||||
|
# tools=tools,
|
||||||
# seed=random.randint(1, 10000), # 设置随机数种子seed,如果没有设置,则随机数种子默认为1234
|
# seed=random.randint(1, 10000), # 设置随机数种子seed,如果没有设置,则随机数种子默认为1234
|
||||||
result_format='message', # 将输出设置为message形式
|
# result_format='message', # 将输出设置为message形式
|
||||||
enable_search='True'
|
# enable_search='True'
|
||||||
)
|
)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@@ -198,14 +200,9 @@ def get_response(messages):
|
|||||||
|
|
||||||
|
|
||||||
def call_with_messages(message, gender):
|
def call_with_messages(message, gender):
|
||||||
|
global tool_info
|
||||||
user_input = message
|
user_input = message
|
||||||
print('\n')
|
print('\n')
|
||||||
# messages = [
|
|
||||||
# {
|
|
||||||
# "content": input('请输入:'), # 提问示例:"现在几点了?" "一个小时后几点" "北京天气如何?"
|
|
||||||
# "role": "user"
|
|
||||||
# }
|
|
||||||
# ]
|
|
||||||
|
|
||||||
messages = [
|
messages = [
|
||||||
{
|
{
|
||||||
@@ -223,14 +220,10 @@ def call_with_messages(message, gender):
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
# 模型的第一轮调用
|
|
||||||
# first_response = get_response(messages)
|
|
||||||
# assistant_output = first_response.output.choices[0].message
|
|
||||||
# print(f"\n大模型第一轮输出信息:{first_response}\n")
|
|
||||||
# messages.append(assistant_output)
|
|
||||||
flag = True
|
flag = True
|
||||||
count = 1
|
count = 1
|
||||||
result_content = "我是一个时尚AI助手,请问有什么可以帮您"
|
# result_content = "我是一个时尚AI助手,请问有什么可以帮您"
|
||||||
|
result_content = "I am a fashion AI assistant, how can I help you?"
|
||||||
response_type = "chat"
|
response_type = "chat"
|
||||||
|
|
||||||
while flag and count <= 3:
|
while flag and count <= 3:
|
||||||
@@ -244,11 +237,15 @@ def call_with_messages(message, gender):
|
|||||||
print(f"最终答案:{assistant_output.content}") # 此处直接返回模型的回复,您可以根据您的业务,选择当无需调用工具时最终回复的内容
|
print(f"最终答案:{assistant_output.content}") # 此处直接返回模型的回复,您可以根据您的业务,选择当无需调用工具时最终回复的内容
|
||||||
result_content = assistant_output.content
|
result_content = assistant_output.content
|
||||||
break
|
break
|
||||||
# 如果模型选择的工具是search_from_internet
|
# 如果模型选择的工具是internet_search
|
||||||
# elif assistant_output.tool_calls[0]['function']['name'] == 'search_from_internet':
|
elif assistant_output.tool_calls[0]['function']['name'] == 'internet_search':
|
||||||
# tool_info = {"name": "search_from_internet", "role": "tool"}
|
tool_info = {"name": "search_from_internet", "role": "tool"}
|
||||||
# user_input = json.loads(assistant_output.tool_calls[0]['function']['arguments'])['user_input']
|
message = [
|
||||||
# tool_info['content'] = search_from_internet(user_input)
|
{'role': 'assistant', 'content': user_input}
|
||||||
|
]
|
||||||
|
tool_info['content'] = search_from_internet(message)
|
||||||
|
flag = False
|
||||||
|
result_content = tool_info['content'].output.text
|
||||||
# 如果模型选择的工具是get_database_table
|
# 如果模型选择的工具是get_database_table
|
||||||
elif assistant_output.tool_calls[0]['function']['name'] == 'get_database_table':
|
elif assistant_output.tool_calls[0]['function']['name'] == 'get_database_table':
|
||||||
tool_info = {"name": "get_database_table", "role": "tool", 'content': get_database_table()}
|
tool_info = {"name": "get_database_table", "role": "tool", 'content': get_database_table()}
|
||||||
@@ -275,13 +272,16 @@ def call_with_messages(message, gender):
|
|||||||
flag = False
|
flag = False
|
||||||
result_content = tool_info['content']
|
result_content = tool_info['content']
|
||||||
response_type = "image"
|
response_type = "image"
|
||||||
|
else :
|
||||||
|
tool_info = {"name": assistant_output.tool_calls[0]['function']['name'], 'content': 'null'}
|
||||||
|
logging.info(assistant_output.tool_calls[0]['function']['name'] + "(unknown tools)")
|
||||||
|
flag = False
|
||||||
|
|
||||||
print(f"工具输出信息:{tool_info['content']}\n")
|
print(f"工具输出信息:{tool_info['content']}\n")
|
||||||
messages.append(tool_info)
|
messages.append(tool_info)
|
||||||
count += 1
|
count += 1
|
||||||
|
|
||||||
final_output = {"output": result_content}
|
final_output = {"output": result_content, "response_type": response_type}
|
||||||
final_output["response_type"] = response_type
|
|
||||||
QWenCallbackHandler.on_chain_end(qwen, final_output)
|
QWenCallbackHandler.on_chain_end(qwen, final_output)
|
||||||
|
|
||||||
# 模型的第二轮调用,对工具的输出进行总结
|
# 模型的第二轮调用,对工具的输出进行总结
|
||||||
|
|||||||
Reference in New Issue
Block a user