diff options
| author | Sadeep Madurange <sadeep@asciimx.com> | 2026-07-29 14:42:38 +0800 |
|---|---|---|
| committer | Sadeep Madurange <sadeep@asciimx.com> | 2026-07-29 14:42:38 +0800 |
| commit | c5f4c607883a41cee09bd3ea6674113ef937bfa0 (patch) | |
| tree | 5c25b49502aedf5a5ef32fec56d77cdaf847d92e | |
| parent | 6aa618ad8ddcc5b449e1c946f4a506a72505e739 (diff) | |
| download | lex-c5f4c607883a41cee09bd3ea6674113ef937bfa0.tar.gz | |
Fix OBSOLETE hallucination.
| -rw-r--r-- | main.c | 47 |
1 files changed, 29 insertions, 18 deletions
@@ -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); |
