实际 VS 预期

在我的预期里,我希望 gpt function calling 能完美实现链式调用且不产生额外的 tokens 消耗。如果能按我的预期工作,我将使用它作为 API 的调度中心,使得通过自然语言随意调用相关函数成为可能,并且可以按任意组合进行加工处理,就像函数式编程一样。
然而实际情况是,gpt function calling实际上是提取自然语言中函数的相应参数。因此,要完成一次回复,它可能需要执行两次或更多次,这取决于所采取的步骤数量。从某种程度上说,它类似于 AutoGPT,但相比之下更加稳定。它能自动选择最佳匹配的自定义函数来获取参数,但 GPT3.5 并不能始终如预期地匹配相似函数,并且在链式调用时无法确保每次都正确输出 JSON 结构化数据,从而导致链式调用中断。
或许 GPT4 的表现会更好,但由于尚未获得 GPT4 的 API,无法进行测试。

function calling 的作用

它允许 ChatGPT 生成参数,并以结构化的数据类型与自定义函数进行交互,生成稳定的 JSON 输出。
最重要的是,它能够从自然语言中提取相应的函数参数,方便我们进行函数调用,而无需将具体执行函数传递给 GPT。这为我们的对话提供了更灵活的方式。

gpt3.5 的 function calling

首先,我们可自行编写或使用 langchain 来实践一下简单的函数调用。

单函数调用

定义函数描述以获取参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function_descriptions = [
{
"name": "get_student_score",
"description": "Get the student score by given his or her name",
"parameters": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "The student's name",
}
},
"required": ["name"],
}
}
]

定义执行函数以返回结果

1
2
3
4
5
6
7
8
def get_student_score(name):
"""Get the student score by given his or her name"""

score = {
"name": name,
"score": SCORES[name]
}
return json.dumps(score)

开始调用 chat

1
2
3
4
5
6
7
8
9
10
11
user_query = "What's the performance of Lucy in the scool this year?"
response = openai.ChatCompletion.create(
model=OPENAI_MODEL,
messages=[{"role": "user", "content": user_query}],
functions=function_descriptions,
function_call="auto"
)
ai_response_message = response["choices"][0]["message"]
print(ai_response_message)
name = eval(ai_response_message['function_call']['arguments']).get("name")
print(name)

执行以上 gpt 调用后,我们将获得提示词中的名字,其结果如下

1
2
3
4
5
6
7
8
9
{
"role": "assistant",
"content": null,
"function_call": {
"name": "get_student_score",
"arguments": "{\n\"name\": \"Lucy\"\n}"
}
}
Lucy

拿着对应参数 name=’Lucy’,去执行相应函数 get_student_score 获得 json 结果,再次调用 chat 函数完成自然语言的回复

1
2
3
4
5
6
7
8
9
10
11
12
13
second_response = openai.ChatCompletion.create(
model=OPENAI_MODEL,
messages=[
{"role": "user", "content": user_query},
ai_response_message,
{
"role": "function",
"name": "get_student_score",
"content": function_response,
},
],
)
print(second_response['choices'][0]['message']['content'])

假设 Lucy 的分数为 60,则它将返回

1
Lucy has achieved a score of 60 this year.

从上述过程我们可以看出,要完成一次正确且稳定的回复,我们需要对同个提示词做 2 次操作,一次是获取结构化的 json 函数所需参数,并自行完成函数调用,一次是携带函数执行结果,完成最终回复。

接下来我们看看多函数调用,中途发生过中断。

多函数调用

此处使用 langchain 完成该过程。

定义函数描述以获取参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function_descriptions = [
{
"name": "remove_word_from_string",
"description": "Remove a word from a string by given its index",
"parameters": {
"type": "object",
"properties": {
"string": {
"type": "string",
"description": "The original string to be processed",
},
"index": {
"type": "integer",
"description": "The index of the word to be removed"
},
},
"required": [
"string",
"index"
],
},
},
{
"name": "send_message_by_email",
"description": "Send an email with the text message to a recipient",
"parameters": {
"type": "object",
"properties": {
"recipient": {
"type": "string",
"description": "The email address of the recipient",
},
"message": {
"type": "string",
"description": "The message of the email content",
}
},
"required": [
"recipient",
"message"
],
},
}
]

以上两个函数,一个用来获取字符串和要移除单词的位置,一个用来获取接收者和消息

定义执行函数以返回结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def remove_word_from_string(string, index):
words = string.split()

if 0 <= index < len(words):
del words[index]

return ' '.join(words)
else:
return string


def send_message_by_email(recipient, message):
print(f'Sending {message} to {recipient}\n\n')
return f'Just sent email to {recipient}'

开始调用 chat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
question = """
I have a string as follows:

black yellow red blue green

Please do the following 2 operations on it:
1. Remove the third word in the string
2. Send the updated string to Alex via email [email protected]
"""
first_response = llm.predict_messages(
[HumanMessage(content=question)], functions=function_descriptions)
print(first_response.additional_kwargs, end='\n\n')

## 省略拿着对应参数调用函数的步骤,returned_value为其执行后返回的json数据

second_response = llm.predict_messages(
[
HumanMessage(content=question),
AIMessage(content=str(first_response.additional_kwargs)),
ChatMessage(
role='function',
additional_kwargs={'name': function_name},
content=returned_value
)
],
functions=function_descriptions
)
## 省略拿着对应参数调用函数的步骤,returned_value为其执行后返回的json数据
third_response = llm.predict_messages(
[
HumanMessage(content=question),
AIMessage(content=str(first_response.additional_kwargs)),
AIMessage(content=str(second_response.additional_kwargs)),
ChatMessage(
role='function',
additional_kwargs={'name': function_name},
content=returned_value
)
], functions=function_descriptions
)

最终结果如下

1
2
3
Sending black yellow blue green to [email protected]

I have removed the third word from the string and sent the updated string to Alex via email.