From f0fb6a31370024f7bd4a711856f9af21e6e322c4 Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Tue, 11 Feb 2025 09:21:46 +0300 Subject: Fix: /v1 chat endpoint; linter --- README.md | 9 ++++----- bot.go | 7 +++++-- main.go | 4 ++-- server.go | 23 +++++++++++++++++------ tables.go | 4 +++- tui.go | 22 ++++++++++------------ 6 files changed, 41 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index b21bd9a..f17c436 100644 --- a/README.md +++ b/README.md @@ -39,9 +39,9 @@ - option to remove from chat history; + - connection to a model status; (need to be tied to some event, perhaps its own shortcut even) + - char card is the sys message, but how about giving tools to char that does not have it? + +- boolean flag to use/not use tools. I see it as a msg from a tool to an llm "Hey, it might be good idea to use me!"; + - lets say we have two (or more) agents with the same name across multiple chats. These agents go and ask db for topics they memorised. Now they can access topics that aren't meant for them. (so memory should have an option: shareable; that indicates if that memory can be shared across chats); - server mode: no tui but api calls with the func calling, rag, other middleware; -- boolean flag to use/not use tools. I see it as a msg from a tool to an llm "Hey, it might be good idea to use me!"; - multirole support? ### FIX: @@ -68,9 +68,8 @@ - table selection does not work; (ctrl+m is enter, it breakes all the tables) + - name split for llamacpp completion. user msg should end with 'bot_name:'; + - remove icons for agents/user; use only : + +- F4 after edit mode no colors; + +- sql memory upsert fails with msg="failed to insert memory" query="INSERT INTO memories (agent, topic, mind) VALUES (:agent, :topic, :mind) RETURNING *;" error="constraint failed: UNIQUE constraint failed: memories.agent, memories.topic (1555); + +- model info shold be an event and show disconnect status when fails; + - add retry on failed call (and EOF); -- model info shold be an event and show disconnect status when fails; -- message editing broke ( runtime error: index out of range [-1]); out of index; -- sql memory upsert fails with msg="failed to insert memory" query="INSERT INTO memories (agent, topic, mind) VALUES (:agent, :topic, :mind) RETURNING *;" error="constraint failed: UNIQUE constraint failed: memories.agent, memories.topic (1555); - F5 broke formatting and messages somehow; -- F4 after edit mode no colors; diff --git a/bot.go b/bot.go index 4e080ee..7fc4a4b 100644 --- a/bot.go +++ b/bot.go @@ -43,11 +43,11 @@ var ( "min_p": 0.05, "n_predict": -1.0, } - toolUseText = "consider making a tool call." ) func fetchModelName() *models.LLMModels { api := "http://localhost:8080/v1/models" + //nolint resp, err := httpClient.Get(api) if err != nil { logger.Warn("failed to get model", "link", api, "error", err) @@ -60,7 +60,7 @@ func fetchModelName() *models.LLMModels { return nil } if resp.StatusCode != 200 { - currentModel = "none" + currentModel = "disconnected" return nil } currentModel = path.Base(llmModel.Data[0].ID) @@ -94,6 +94,9 @@ func sendMsgToLLM(body io.Reader) { resp, err := httpClient.Post(cfg.CurrentAPI, "application/json", body) if err != nil { logger.Error("llamacpp api", "error", err) + if err := notifyUser("error", "apicall failed"); err != nil { + logger.Error("failed to notify", "error", err) + } streamDone <- true return } diff --git a/main.go b/main.go index df9e5ba..0b3bae5 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,7 @@ package main import ( "flag" - "fmt" + "strconv" "unicode" "github.com/rivo/tview" @@ -30,7 +30,7 @@ func main() { flag.Parse() if apiPort != nil && *apiPort > 3000 { srv := Server{} - srv.ListenToRequests(fmt.Sprintf("%d", *apiPort)) + srv.ListenToRequests(strconv.Itoa(*apiPort)) return } pages.AddPage("main", flex, true, true) diff --git a/server.go b/server.go index 79aeb2f..5a1a1c3 100644 --- a/server.go +++ b/server.go @@ -9,6 +9,7 @@ import ( ) type Server struct { + // nolint config config.Config } @@ -25,13 +26,17 @@ func (srv *Server) ListenToRequests(port string) { mux.HandleFunc("GET /model", modelHandler) mux.HandleFunc("POST /completion", completionHandler) fmt.Println("Listening", "addr", server.Addr) - server.ListenAndServe() + if err := server.ListenAndServe(); err != nil { + panic(err) + } } // create server // listen to the completion endpoint handler func pingHandler(w http.ResponseWriter, req *http.Request) { - w.Write([]byte("pong")) + if _, err := w.Write([]byte("pong")); err != nil { + logger.Error("server ping", "error", err) + } } func completionHandler(w http.ResponseWriter, req *http.Request) { @@ -44,20 +49,26 @@ out: for { select { case chunk := <-chunkChan: - fmt.Println(chunk) - w.Write([]byte(chunk)) + fmt.Print(chunk) + if _, err := w.Write([]byte(chunk)); err != nil { + logger.Warn("failed to write chunk", "value", chunk) + continue + } case <-streamDone: break out } } - return } func modelHandler(w http.ResponseWriter, req *http.Request) { llmModel := fetchModelName() payload, err := json.Marshal(llmModel) if err != nil { + logger.Error("model handler", "error", err) // return err + return + } + if _, err := w.Write(payload); err != nil { + logger.Error("model handler", "error", err) } - w.Write(payload) } diff --git a/tables.go b/tables.go index 35a2e60..b520cd9 100644 --- a/tables.go +++ b/tables.go @@ -109,7 +109,9 @@ func makeChatTable(chatMap map[string]models.Chat) *tview.Table { if !ok { logger.Warn("no such card", "agent", agentName) //no:lint - notifyUser("error", "no such card: "+agentName) + if err := notifyUser("error", "no such card: "+agentName); err != nil { + logger.Warn("failed ot notify", "error", err) + } } if err := pngmeta.WriteToPng(cc.ToSpec(cfg.UserRole), cc.FilePath, cc.FilePath); err != nil { logger.Error("failed to write charcard", diff --git a/tui.go b/tui.go index db7a62e..d07a545 100644 --- a/tui.go +++ b/tui.go @@ -25,19 +25,16 @@ var ( // sysModal *tview.Modal indexPickWindow *tview.InputField renameWindow *tview.InputField - // - longJobStatusCh = make(chan string, 1) // pages - historyPage = "historyPage" - agentPage = "agentPage" - editMsgPage = "editMsgPage" - indexPage = "indexPage" - helpPage = "helpPage" - renamePage = "renamePage" - RAGPage = "RAGPage " - longStatusPage = "longStatusPage" - propsPage = "propsPage" - codeBlockPage = "codeBlockPage" + historyPage = "historyPage" + agentPage = "agentPage" + editMsgPage = "editMsgPage" + indexPage = "indexPage" + helpPage = "helpPage" + renamePage = "renamePage" + RAGPage = "RAGPage " + propsPage = "propsPage" + codeBlockPage = "codeBlockPage" // help text helpText = ` [yellow]Esc[white]: send msg @@ -525,6 +522,7 @@ func init() { } cfg.APIMap[newAPI] = prevAPI cfg.CurrentAPI = newAPI + initChunkParser() updateStatusLine() return nil } -- cgit v1.2.3