-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsync_demo.cc
More file actions
100 lines (81 loc) · 2.47 KB
/
sync_demo.cc
File metadata and controls
100 lines (81 loc) · 2.47 KB
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
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include "llm_client.h"
using namespace wfai;
volatile bool stop_flag = false;
FunctionManager func_mgr;
void get_current_weather(const std::string& arguments, FunctionResult *result)
{
printf("function calling... get_current_weather(%s)\n", arguments.c_str());
result->name = "get_current_weather";
result->success = true;
result->result = "Temperature: 25°C, Sunny"; // whaterver the location is
}
void register_functions()
{
FunctionDefinition weather_func = {
.name = "get_weather",
.description = "Get ",
};
ParameterProperty location_prop = {
.type = "string",
.description = "Name of the City, e.g. Beijing, Shenzhen.",
};
weather_func.add_parameter("location", location_prop, true);
func_mgr.register_function(weather_func, get_current_weather);
printf("registered weather function successfully.\n");
}
void sig_handler(int signo)
{
stop_flag = true;
}
int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "USAGE: %s <api_key>\n", argv[0]);
exit(1);
}
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
LLMClient client(argv[1]);
client.set_function_manager(&func_mgr);
register_functions();
printf("=== Synchronous LLMClient DEMO ===\n\n");
printf("1. Basic Chat in NO Streaming Mode:\n");
{
ChatCompletionRequest request;
request.model = "deepseek-chat";
request.messages.push_back({"user", "hi"});
ChatCompletionResponse response;
auto result = client.chat_completion_sync(request, response);
if (result.success) {
printf("✓ Request Success (Status Code : %d)\n", result.status_code);
printf("Response : %s\n\n", response.choices[0].message.content.c_str());
} else {
printf("✗ Request Failed : %s\n\n", result.error_message.c_str());
}
}
printf("2. Chat With Tool Calls:\n");
{
ChatCompletionRequest request;
request.model = "deepseek-chat";
request.stream = false;
request.tool_choice = "auto"; // enable tool calls
request.messages.push_back({"user", "What's the weather in Shenzhen?"});
ChatCompletionResponse response;
auto result = client.chat_completion_sync(request, response);
if (result.success)
{
printf("✓ Request Success (Status Code : %d)\n", result.status_code);
printf("Response : %s\n\n", response.choices[0].message.content.c_str());
} else {
printf("✗ Request Failed : %s\n\n", result.error_message.c_str());
}
}
return 0;
}