-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathtest_function_calling.cc
157 lines (132 loc) · 5.5 KB
/
test_function_calling.cc
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <memory>
#include "gtest/gtest.h"
#include "json/json.h"
#include "utils/function_calling/common.h"
class FunctionCallingUtilsTest : public ::testing::Test {
protected:
std::shared_ptr<Json::Value> createTestRequest() {
auto request = std::make_shared<Json::Value>();
(*request)["tools"] = Json::Value(Json::arrayValue);
return request;
}
};
TEST_F(FunctionCallingUtilsTest, ReplaceCustomFunctions) {
std::string original = "Test <CUSTOM_FUNCTIONS> placeholder";
std::string replacement = "Custom function";
std::string result =
function_calling_utils::ReplaceCustomFunctions(original, replacement);
EXPECT_EQ(result, "Test Custom function placeholder");
}
TEST_F(FunctionCallingUtilsTest, HasTools) {
auto request = createTestRequest();
EXPECT_FALSE(function_calling_utils::HasTools(request));
(*request)["tools"].append(Json::Value());
EXPECT_TRUE(function_calling_utils::HasTools(request));
(*request)["tools"] = "random";
EXPECT_FALSE(function_calling_utils::HasTools(request));
(*request)["tools"] = Json::Value::null;
EXPECT_FALSE(function_calling_utils::HasTools(request));
}
TEST_F(FunctionCallingUtilsTest, ProcessTools) {
auto request = createTestRequest();
Json::Value tool;
tool["type"] = "function";
tool["function"]["name"] = "test_function";
tool["function"]["description"] = "Test description";
(*request)["tools"].append(tool);
std::string result = function_calling_utils::ProcessTools(request);
EXPECT_TRUE(
result.find("Use the function 'test_function' to: Test description") !=
std::string::npos);
}
TEST_F(FunctionCallingUtilsTest, ParseMultipleFunctionStrings) {
std::string input =
"<function=func1>{\"arg\":\"value1\"}</"
"function><function=func2>{\"arg\":\"value2\"}</function>";
Json::Value result =
function_calling_utils::ParseMultipleFunctionStrings(input);
ASSERT_EQ(result.size(), 2);
EXPECT_EQ(result[0]["function"]["name"].asString(), "func1");
EXPECT_EQ(result[0]["function"]["arguments"].asString(),
"{\"arg\":\"value1\"}");
EXPECT_EQ(result[1]["function"]["name"].asString(), "func2");
EXPECT_EQ(result[1]["function"]["arguments"].asString(),
"{\"arg\":\"value2\"}");
}
TEST_F(FunctionCallingUtilsTest, ConvertJsonToFunctionStrings) {
Json::Value jsonArray(Json::arrayValue);
Json::Value function1, function2;
function1["function"]["name"] = "func1";
function1["function"]["arguments"] = "{\"arg\":\"value1\"}";
function2["function"]["name"] = "func2";
function2["function"]["arguments"] = "{\"arg\":\"value2\"}";
jsonArray.append(function1);
jsonArray.append(function2);
std::string result =
function_calling_utils::ConvertJsonToFunctionStrings(jsonArray);
EXPECT_EQ(result,
"<function=func1>{\"arg\":\"value1\"}</"
"function><function=func2>{\"arg\":\"value2\"}</function>");
}
TEST_F(FunctionCallingUtilsTest, CreateCustomFunctionsString) {
auto request = createTestRequest();
Json::Value tool;
tool["type"] = "function";
tool["function"]["name"] = "test_function";
tool["function"]["description"] = "Test description";
(*request)["tools"].append(tool);
std::string result =
function_calling_utils::CreateCustomFunctionsString(request);
EXPECT_TRUE(result.find("```") != std::string::npos);
EXPECT_TRUE(
result.find("Use the function 'test_function' to: Test description") !=
std::string::npos);
}
TEST_F(FunctionCallingUtilsTest, IsValidToolChoiceFormat) {
Json::Value validTool;
validTool["type"] = "function";
validTool["function"]["name"] = "test_function";
EXPECT_TRUE(function_calling_utils::IsValidToolChoiceFormat(validTool));
Json::Value invalidTool;
EXPECT_FALSE(function_calling_utils::IsValidToolChoiceFormat(invalidTool));
}
TEST_F(FunctionCallingUtilsTest, UpdateMessages) {
auto request = createTestRequest();
std::string system_prompt = "Original prompt";
(*request)["messages"] = Json::Value(Json::arrayValue);
function_calling_utils::UpdateMessages(system_prompt, request);
ASSERT_TRUE((*request)["messages"].isArray());
EXPECT_EQ((*request)["messages"][0]["role"].asString(), "system");
EXPECT_EQ((*request)["messages"][0]["content"].asString(), system_prompt);
}
TEST_F(FunctionCallingUtilsTest, PreprocessRequest) {
auto request = createTestRequest();
Json::Value tool;
tool["type"] = "function";
tool["function"]["name"] = "test_function";
tool["function"]["description"] = "Test description";
(*request)["tools"].append(tool);
function_calling_utils::PreprocessRequest(request);
ASSERT_TRUE((*request)["messages"].isArray());
EXPECT_TRUE((*request)["messages"][0]["content"].asString().find(
"Test description") != std::string::npos);
}
TEST_F(FunctionCallingUtilsTest, PostProcessResponse) {
Json::Value response;
response["choices"] = Json::Value(Json::arrayValue);
Json::Value choice;
choice["message"]["content"] =
"<function=test_function>{\"arg\":\"value\"}</function>";
response["choices"].append(choice);
function_calling_utils::PostProcessResponse(response);
EXPECT_EQ(response["choices"][0]["message"]["content"].asString(), "");
EXPECT_TRUE(response["choices"][0]["message"]["tool_calls"].isArray());
EXPECT_EQ(
response["choices"][0]["message"]["tool_calls"][0]["function"]["name"]
.asString(),
"test_function");
EXPECT_EQ(response["choices"][0]["message"]["tool_calls"][0]["function"]
["arguments"]
.asString(),
"{\"arg\":\"value\"}");
}