Sometimes we may need to make a second function calling based on the result of first function call, but now it is unable to do this. It will just call the function once, and tell you unable to find the information. Following code is an example:
def get_pepple_info(index):
print(index)
if (index == 1):
# "Birthday, please continue to search by the following index values:\n\n101 Father\n102 Mother\n103 Sister\n"
return "生日,请通过下列索引值继续查找:\n\n101 爸爸\n102 妈妈\n103 姐姐\n"
if (index == 101):
return "1970.12.10"
if (index == 102):
return "1975.06.06"
if (index == 103):
return "1993.09.22"
if (index == 2):
# "Occupation, please continue to search by the following index values:\n\n201 Father\n202 Mother\n203 Sister\n"
return "职业,请通过下列索引值继续查找:\n\n201 爸爸\n202 妈妈\n203 姐姐\n"
if (index == 201):
# "Ship designer"
return "船舶设计师"
if (index == 202):
# "Customer service manager"
return "客服经理"
if (index == 203):
# "Personnel"
return "人事"
return "Not found"
tools = [
{
"type": "function",
"function": {
"name": "get_pepple_info",
# "Find information about family members, if you encounter a secondary index value, please call the function again to find it",
"description": '查找家庭成员的信息,如果遇到二级索引值,请直接再次调用函数进行查找',
"parameters": {
"type": "object",
"properties": {
"index": {
"type": "integer",
# "Index value (primary directory, there may be secondary directory inside, you need to search it again), 1: birthday, 2: occupation\n",
"description": '索引值(一级目录,里面可能有次级目录还需要再次查找),1:生日,2:职业\n',
}
},
"required": ["index"],
},
},
}
]
messages = []
while True:
messages.append({"role": "user", "content": input('请输入内容:')})
response = client.chat.completions.create(
model="gpt-3.5-turbo-1106",
messages=messages,
tools=tools,
)
response_message = response.choices[0].message
messages.append(response_message)
tool_calls = response_message.tool_calls
while tool_calls:
available_functions = {
"get_pepple_info": get_pepple_info,
}
for tool_call in tool_calls:
function_name = tool_call.function.name
function_to_call = available_functions[function_name]
function_args = json.loads(tool_call.function.arguments)
function_response = function_to_call(
index=function_args.get("index"),
)
messages.append(
{
"tool_call_id": tool_call.id,
"role": "tool",
"name": function_name,
"content": function_response,
}
)
response = client.chat.completions.create(
model="gpt-3.5-turbo-1106",
messages=messages,
)
response_message=response.choices[0].message
messages.append(response_message)
tool_calls = response_message.tool_calls
print(response_message.content)