Repository navigation
Expand file tree
/
Copy pathllm_command_generator.c
More file actions
399 lines (334 loc) · 13.4 KB
/
Copy pathllm_command_generator.c
File metadata and controls
399 lines (334 loc) · 13.4 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <unistd.h>
#include <arpa/inet.h>
#define MAX_PARAGRAPH_LENGTH 1500
#define MAX_COMMAND_LENGTH 1000
#define MAX_COMMANDS 1000
#define API_URL "https://api-inference.huggingface.co/models/meta-llama/Llama-3.2-1B-Instruct" //meta-llama/Llama-3.2-3B-Instruct"
#define API_KEY "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" // Replace with your actual API key
#define PORT 8080
#define BUFFER_SIZE 1024
struct MemoryStruct {
char *memory;
size_t size;
};
static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) {
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
char *ptr = realloc(mem->memory, mem->size + realsize + 1);
if (!ptr) {
printf("Not enough memory (realloc returned NULL)\n");
return 0;
}
mem->memory = ptr;
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
char** read_commands_from_file(const char* filename, int* num_commands) {
FILE* file = fopen(filename, "r");
if (file == NULL) {
fprintf(stderr, "Error opening file: %s\n", filename);
exit(EXIT_FAILURE);
}
char** commands = malloc(MAX_COMMANDS * sizeof(char*));
char line[MAX_COMMAND_LENGTH];
int i = 0;
while (fgets(line, sizeof(line), file) && i < MAX_COMMANDS) {
line[strcspn(line, "\n")] = 0; // Remove newline
commands[i] = strdup(line);
i++;
}
fclose(file);
*num_commands = i;
return commands;
}
char* replace_newlines(const char* input) {
int count = 0;
const char* tmp = input;
while (*tmp) {
if (*tmp == '\n') count++;
tmp++;
}
char* result = malloc(strlen(input) + count + 1);
if (result == NULL) {
return NULL; // Memory allocation failed
}
char* dst = result;
while (*input) {
if (*input == '\n') {
*dst++ = '\\';
*dst++ = 'n';
} else {
*dst++ = *input;
}
input++;
}
*dst = '\0';
return result;
}
char* read_input_file(const char* filename) {
FILE* file = fopen(filename, "rb");
if (file == NULL) {
fprintf(stderr, "Error: Could not open file %s\n", filename);
return NULL;
}
fseek(file, 0, SEEK_END);
long length = ftell(file);
fseek(file, 0, SEEK_SET);
char* buffer = malloc(length + 1);
if (buffer == NULL) {
fprintf(stderr, "Error: Memory allocation failed\n");
fclose(file);
return NULL;
}
size_t read_length = fread(buffer, 1, length, file);
if (read_length != (size_t)length) {
fprintf(stderr, "Error: Could not read entire file\n");
free(buffer);
fclose(file);
return NULL;
}
buffer[length] = '\0';
fclose(file);
// Remove or replace control characters
for (long i = 0; i < length; i++) {
if (iscntrl((unsigned char)buffer[i]) && buffer[i] != '\n' && buffer[i] != '\r' && buffer[i] != '\t') {
buffer[i] = ' '; // Replace with space
}
}
buffer = replace_newlines(buffer);
return buffer;
}
char *trim_whitespace(char *str) {
char *end;
char *src, *dst;
// Trim leading space
while (isspace((unsigned char)*str)) str++;
if (*str == 0) { // All spaces?
return strdup("");
}
// Trim trailing space
end = str + strlen(str) - 1;
while (end > str && isspace((unsigned char)*end)) end--;
end[1] = '\0';
// Remove newlines and copy
src = str;
dst = str;
while (*src) {
if (*src != '\n') {
*dst++ = *src;
}
src++;
}
*dst = '\0';
// Allocate memory for the trimmed string
char *trimmed = strdup(str);
if (trimmed == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
return trimmed;
}
char* sanitize_json_string(const char* input) {
if (input == NULL) return NULL;
size_t input_len = strlen(input);
char* output = malloc(input_len * 2 + 1); // Worst case: every char needs escaping
if (output == NULL) return NULL;
size_t j = 0;
for (size_t i = 0; i < input_len; i++) {
unsigned char c = input[i];
if (iscntrl(c)) {
switch (c) {
case '\b': strcpy(&output[j], "\\b"); j += 2; break;
case '\f': strcpy(&output[j], "\\f"); j += 2; break;
case '\n': strcpy(&output[j], "\\n"); j += 2; break;
case '\r': strcpy(&output[j], "\\r"); j += 2; break;
case '\t': strcpy(&output[j], "\\t"); j += 2; break;
default:
// For other control characters, use \u00XX format
sprintf(&output[j], "\\u%04x", c);
j += 6;
}
} else if (c == '"' || c == '\\') {
output[j++] = '\\';
output[j++] = c;
} else {
output[j++] = c;
}
}
output[j] = '\0';
return output;
}
char* generate_commands_huggingface(const char* paragraph, char** valid_commands, int num_commands) {
CURL *curl;
CURLcode res;
struct MemoryStruct chunk;
chunk.memory = malloc(1); // Start with a small allocation
chunk.size = 0;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl) {
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
char auth_header[256];
snprintf(auth_header, sizeof(auth_header), "Authorization: Bearer %s", API_KEY);
headers = curl_slist_append(headers, auth_header);
// Prepare the prompt
char *prompt = malloc(MAX_PARAGRAPH_LENGTH + (MAX_COMMAND_LENGTH * num_commands) + 1000);
if (prompt == NULL) {
fprintf(stderr, "Failed to allocate memory for prompt\n");
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
return NULL;
}
sprintf(prompt, "{ \"parameters\": { \"max_new_tokens\": 256, \"return_full_text\": false }, \"inputs\": \"You are a car command generator. Your task is to generate a sequence of commands based on a user's description of their actions or a request for random commands. Use ONLY the relevant commands from the following predefined list: ");
//strcat(prompt, "all_door unlocked; aircondition on; radio on; front_left_window unlock; satelitenavigation on; wiper on; sunroof on; defroster on; horn on; hazard on; front_right_window unlock; head_light on; bluetooth on; radio off; rear_left_window unlock; wiper off; airbag on; sidemirror on; trunk on; aircondition off; rear_right_window unlock; all_door locked; steering right; abs on; bluetooth off; radio on; defroster_front on; all_window locked; throttle on; aircondition up; brakecontrol on; radio nextstation; steering left; brakecontrol off; radio volumeup; defroster_rear off; radio previousstation; aircondition down; all_window unlocked; radio volumedown; cruisecontrol on; left_signal ON; left_signal off; handbrake on; throttle off; airbag off; front_left_door unlocked; front_left_door locked; front_right_door unlocked; front_right_door locked; rear_right_door unlocked; right_signal on; rear_right_door locked; rear_left_door unlocked; rear_left_door locked; cruisecontrol off; satelitenavigation off; right_signal off; handbrake off; head_light Off; defroster off; sidemirror off; trunk off; hazard off; horn off ");
for (int i = 0; i < num_commands; i++) {
strcat(prompt, valid_commands[i]);
strcat(prompt, "; ");
}
strcat(prompt, "Rules: ");
strcat(prompt, "1. Generate commands based on the user input paragraph. ");
strcat(prompt, "2. Use only the relevant commands listed above, exactly as they appear. ");
strcat(prompt, "3. Include 'delay 2000' between each action command. ");
strcat(prompt, "4. Do not add any sequence numbers or explanations. ");
strcat(prompt, "5. Separate each command with a semicolon (;). ");
strcat(prompt, "6. Ensure logical sequence of actions (e.g., unlock door before opening it). ");
strcat(prompt, "7. Do not repeat commands unnecessarily. ");
strcat(prompt, "8. If an action is not explicitly mentioned, do not include related commands. ");
strcat(prompt, "9. Do not use line breaks for separating commands. Present all commands on a single line. ");
strcat(prompt, "User input paragraph: ");
strcat(prompt, paragraph);
strcat(prompt, " Generated commands: \"}");
printf("\nprompt: %s\n", prompt);
curl_easy_setopt(curl, CURLOPT_URL, API_URL);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, prompt);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
free(chunk.memory);
chunk.memory = NULL;
}
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
free(prompt);
}
curl_global_cleanup();
return chunk.memory;
}
char** extract_commands(const char* input, int* num_commands) {
char** commands = malloc(MAX_COMMANDS * sizeof(char*));
*num_commands = 0;
// Find the start of the generated commands
const char* start = strstr(input, "\"generated_text\":\"");
if (start == NULL) {
printf("Generated commands not found in the input.\n");
return commands;
}
// Move to the start of the actual commands
// start = strchr(start, ':');
start += strlen("\"generated_text\":\"");
printf("\n%s\n", start);
if (start == NULL) {
printf("No commands found after 'Generated commands:'\n");
return commands;
}
start++; // Move past the newline
// Extract commands
char* line = strtok((char*)start, ";");
while (line != NULL && *num_commands < MAX_COMMANDS) {
// Check if we've reached the end of the JSON object
if (strstr(line, "\"}]") != NULL) {
break;
}
// Allocate memory for the command and copy it
commands[*num_commands] = malloc(MAX_COMMAND_LENGTH * sizeof(char));
strncpy(commands[*num_commands], line, MAX_COMMAND_LENGTH - 1);
commands[*num_commands][MAX_COMMAND_LENGTH - 1] = '\0'; // Ensure null-termination
(*num_commands)++;
line = strtok(NULL, ";");
}
return commands;
}
int main(int argc, char *argv[]) {
// if (argc != 2) {
// printf("Usage: %s <input_file>\n", argv[0]);
// return 1;
// }
int sock = 0;
int is_first_run = 1;
struct sockaddr_in serv_addr;
char buffer[BUFFER_SIZE] = {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
fprintf(stderr, "Socket creation error\n");
return EXIT_FAILURE;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
printf("\nConnection Failed \n");
return -1;
}
while(1) {
int num_commands;
char *filename = "./scripts/sample_commands.txt";
char** valid_commands = read_commands_from_file(filename, &num_commands);
char *paragraph = malloc(MAX_PARAGRAPH_LENGTH);
if(is_first_run == 1 && argc > 1)
{
is_first_run = 0;
paragraph = read_input_file(argv[1]);
printf("paragraph=%s", paragraph);
}
else
{
printf("Enter the paragraph describing the actions:\n");
fgets(paragraph, MAX_PARAGRAPH_LENGTH, stdin);
}
printf("Processing...\n");
paragraph = sanitize_json_string(paragraph);
printf("paragraph=%s", paragraph);
char* generated_commands = generate_commands_huggingface(paragraph, valid_commands, num_commands);
if (generated_commands != NULL) {
printf("\n\nGenerated commands:\n%s\n\n\n", generated_commands);
char** commands = extract_commands(generated_commands, &num_commands);
printf("Extracted commands:\n");
for (int i = 0; i < num_commands; i++) {
printf("%s\n", trim_whitespace(commands[i]));
send(sock, commands[i], strlen(commands[i]), 0);
memset(buffer, 0, BUFFER_SIZE);
read(sock, buffer, BUFFER_SIZE);
printf("Server response: %s\n", buffer);
}
// Free allocated memory
for (int i = 0; i < num_commands; i++) {
free(commands[i]);
}
free(commands);
free(generated_commands);
} else {
printf("Failed to generate commands.\n");
}
// Clean up
for (int i = 0; i < num_commands; i++) {
free(valid_commands[i]);
}
free(valid_commands);
}
close(sock);
return 0;
}