From 6dcc3f0309c6e37d11cfb83c270100485a4ea892 Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Tue, 27 May 2025 15:42:30 +0300 Subject: [PATCH] Fix: config & ds prompt --- config/config.go | 5 +++-- llmapi/main.go | 25 ++++++++++++++++++------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/config/config.go b/config/config.go index 6608d3e..4d19e86 100644 --- a/config/config.go +++ b/config/config.go @@ -20,8 +20,8 @@ type ServerConfig struct { } type LLMConfig struct { - URL string `toml:"LLM_URL"` - TOKEN string `toml:"LLM_TOKEN"` + URL string `toml:"URL"` + TOKEN string `toml:"TOKEN"` } func LoadConfigOrDefault(fn string) *Config { @@ -38,5 +38,6 @@ func LoadConfigOrDefault(fn string) *Config { config.ServerConfig.Host = "localhost" config.ServerConfig.Port = "3000" } + // fmt.Printf("config debug; config.LLMConfig.URL: %s\n", config.LLMConfig.URL) return config } diff --git a/llmapi/main.go b/llmapi/main.go index adaaa84..1aa6eb1 100644 --- a/llmapi/main.go +++ b/llmapi/main.go @@ -25,6 +25,8 @@ var ( // botname -> channel SignalChanMap = make(map[string]chan bool) DoneChanMap = make(map[string]chan bool) + // got prompt: control character (\\u0000-\\u001F) found while parsing a string at line 4 column 0 + MimePrompt = `we are playing alias;\nyou are a mime (player who gives a clue of one noun word and number of cards you expect them to open) of the red team (people who would guess by your clue want open the red cards);\nplease return your clue, number of cards to open and what words you mean them to find using that clue in json like:\n{\n\"clue\": \"one-word-noun\",\n\"number\": \"number-from-0-to-9\",\n\"word_I_mean_my_team_to_open\": [\"this\", \"that\", ...]\n}\nthe team who openes all their cards first wins.\nplease return json only.\nunopen Blue cards left: %d;\nunopen Red cards left: %d;\nhere is the game info in json:\n%s` ) type Bot struct { @@ -185,12 +187,21 @@ func (b *Bot) BuildPrompt(room *models.Room) string { } toText["cards"] = copiedCards } - data, err := json.MarshalIndent(toText, "", " ") + data, err := json.Marshal(toText) if err != nil { - // log + b.log.Error("failed to marshal", "error", err) return "" } - return string(data) + // Escape the JSON string for inclusion in another JSON field + escapedData := strings.ReplaceAll(string(data), `"`, `\"`) + if b.Role == models.UserRoleMime { + return fmt.Sprintf(MimePrompt, room.BlueCounter, room.RedCounter, escapedData) + } + if b.Role == models.UserRoleMime { + // TODO: + return "" + } + return "" } func (b *Bot) CallLLM(prompt string) error { @@ -213,7 +224,7 @@ func (b *Bot) CallLLM(prompt string) error { client := &http.Client{} req, err := http.NewRequest(method, b.cfg.LLMConfig.URL, payload) if err != nil { - fmt.Println(err) + b.log.Error("failed to make new request", "error", err, "url", b.cfg.LLMConfig.URL) return err } req.Header.Add("Content-Type", "application/json") @@ -221,15 +232,15 @@ func (b *Bot) CallLLM(prompt string) error { req.Header.Add("Authorization", "Bearer "+b.cfg.LLMConfig.TOKEN) res, err := client.Do(req) if err != nil { - fmt.Println(err) + b.log.Error("failed to make request", "error", err, "url", b.cfg.LLMConfig.URL) return err } defer res.Body.Close() body, err := io.ReadAll(res.Body) if err != nil { - fmt.Println(err) + b.log.Error("failed to read resp body", "error", err, "url", b.cfg.LLMConfig.URL) return err } - fmt.Println(string(body)) + b.log.Debug("llm resp", "body", string(body)) return nil }