-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain.c
More file actions
127 lines (112 loc) · 4.11 KB
/
main.c
File metadata and controls
127 lines (112 loc) · 4.11 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
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
//
// tool-calling/main.c
// Custom tool registration and agent tool use
//
#include "adam.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char *env_load(const char *key) {
FILE *f = fopen(".env", "r");
if (!f) return NULL;
size_t klen = strlen(key);
char line[1024];
while (fgets(line, sizeof(line), f)) {
if (strncmp(line, key, klen) == 0 && line[klen] == '=') {
fclose(f);
char *val = line + klen + 1;
size_t len = strlen(val);
while (len > 0 && (val[len-1] == '\n' || val[len-1] == '\r'))
val[--len] = '\0';
return strdup(val);
}
}
fclose(f);
return NULL;
}
// Mock weather tool implementation
static adam_tool_result_t get_weather(arena_t *arena, void *ctx,
const char *args_json, size_t args_len) {
UNUSED_PARAM(ctx);
UNUSED_PARAM(args_len);
// Extract the "location" field from the JSON arguments.
// In a real application you would use a JSON parser; here we do a simple check.
const char *response;
if (strstr(args_json, "San Francisco") || strstr(args_json, "SF")) {
response = "{\"location\":\"San Francisco, CA\","
"\"temperature_f\":62,"
"\"condition\":\"Foggy\","
"\"humidity\":78,"
"\"wind_mph\":12}";
} else if (strstr(args_json, "New York") || strstr(args_json, "NYC")) {
response = "{\"location\":\"New York, NY\","
"\"temperature_f\":45,"
"\"condition\":\"Cloudy\","
"\"humidity\":65,"
"\"wind_mph\":8}";
} else {
response = "{\"location\":\"Unknown\","
"\"temperature_f\":70,"
"\"condition\":\"Sunny\","
"\"humidity\":50,"
"\"wind_mph\":5}";
}
// Allocate the response string in the arena (valid until arena reset)
size_t len = strlen(response);
char *buf = arena_alloc(arena, len + 1);
memcpy(buf, response, len + 1);
return (adam_tool_result_t){
.for_llm = buf,
.for_user = NULL,
.success = 1,
};
}
int main(void) {
adam_status_t status = adam_init();
if (status != ADAM_OK) {
fprintf(stderr, "adam_init failed: %s\n", adam_status_string(status));
return 1;
}
char *api_key = env_load("ANTHROPIC_API_KEY");
if (!api_key) {
fprintf(stderr, "Missing ANTHROPIC_API_KEY in .env file\n");
adam_cleanup();
return 1;
}
adam_settings_t *settings = adam_create_settings();
adam_settings_set_provider(settings, ADAM_API_ANTHROPIC, api_key, "claude-sonnet-4-20250514");
// Register the get_weather tool with its JSON schema
adam_tool_def_t weather_tool = {
.name = "get_weather",
.description = "Get the current weather for a location.",
.parameters_json = "{"
"\"type\":\"object\","
"\"properties\":{"
"\"location\":{\"type\":\"string\",\"description\":\"City name, e.g. San Francisco, CA\"}"
"},"
"\"required\":[\"location\"]"
"}",
.execute = get_weather,
.ctx = NULL,
};
adam_settings_add_tool(settings, weather_tool);
adam_history_t *history = adam_history_create();
printf("User: What's the weather like in San Francisco right now?\n\n");
adam_run_result_t result = adam_run(settings, history,
"What's the weather like in San Francisco right now?");
if (result.status != ADAM_OK) {
fprintf(stderr, "adam_run failed: %s\n", adam_status_string(result.status));
} else {
printf("Assistant: %s\n\n", result.final_response);
printf(" [iterations: %d, tokens: %d in / %d out, %.1f ms]\n",
result.total_iterations,
result.input_tokens, result.output_tokens,
result.elapsed_ms);
}
adam_run_result_free(&result);
adam_history_destroy(history);
adam_settings_destroy(settings);
free(api_key);
adam_cleanup();
return 0;
}