summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--main.c47
1 files changed, 29 insertions, 18 deletions
diff --git a/main.c b/main.c
index 602a8fd..42db9c6 100644
--- a/main.c
+++ b/main.c
@@ -1,16 +1,16 @@
#include <stdio.h>
#include <stdbool.h>
+#include <ctype.h>
#include <err.h>
#include <unistd.h>
-#include "llama.h"
#include "mem.h"
+#include "llama.h"
#define MODEL_PATH "Qwen2.5-7B-Instruct-Q4_K_M.gguf"
#define SYSTEM_PROMPT \
- "You are a precise lexicographer. You are defining the EXACT target word provided.\n" \
- "Do NOT confuse the word with phonetically or visually similar words.\n\n" \
+ "You are a lexicographer. You are defining the EXACT target word provided.\n" \
"Output format strictly as follows:\n" \
"<WORD IN UPPERCASE> (<part of speech>) — <Formality: Conversational|Formal|Literary|Archaic>\n\n" \
"DEFINITION:\n" \
@@ -25,32 +25,37 @@
#define CHATML_FMT \
"<|im_start|>system\n%s<|im_end|>\n" \
"<|im_start|>user\nTarget word: %s\nDefine this specific word:<|im_end|>\n" \
- "<|im_start|>assistant\n"
+ "<|im_start|>assistant\n%s"
#define MAX_TOKENS 300
+static void upcase(char *dest, const char *src, size_t n)
+{
+ size_t i;
+
+ for (i = 0; i < n - 1 && src[i] != '\0'; i++)
+ dest[i] = (char)toupper((unsigned char)src[i]);
+
+ dest[i] = '\0';
+}
+
static int build_prompt(char **buf, const char *word)
{
- char *p;
- int len, written;
+ int len;
+ char upper_word[128];
if (!buf || !word)
return -1;
- len = snprintf(NULL, 0, CHATML_FMT, SYSTEM_PROMPT, word);
- if (len <= 0)
- return -1;
-
- p = MALLOC((size_t)len + 1);
+ upcase(upper_word, word, sizeof(upper_word));
- written = snprintf(p, (size_t)len + 1, CHATML_FMT, SYSTEM_PROMPT, word);
- if (written <= 0) {
- free(p);
+ len = asprintf(buf, CHATML_FMT, SYSTEM_PROMPT, word, upper_word);
+ if (len < 0) {
+ *buf = NULL;
return -1;
}
-
- *buf = p;
- return written;
+
+ return len;
}
static void process_request(struct llama_model *model, const char *word)
@@ -141,6 +146,12 @@ static void process_request(struct llama_model *model, const char *word)
free(prompt_tokens);
+ /* Response formatting: print the pre-filled word right as generation begins */
+ char upper_word[128];
+ upcase(upper_word, word, sizeof(upper_word));
+ printf("%s", upper_word);
+ fflush(stdout);
+
/* Generation loop */
sparams = llama_sampler_chain_default_params();
smpl = llama_sampler_chain_init(sparams);
@@ -201,7 +212,7 @@ static void process_request(struct llama_model *model, const char *word)
}
}
- printf("\n");
+ printf("\n\n");
fflush(stdout);
llama_perf_context_print(ctx);