Fix: /v1 chat endpoint; linter

This commit is contained in:
Grail Finder
2025-02-11 09:21:46 +03:00
parent 5468053908
commit f0fb6a3137
6 changed files with 41 additions and 28 deletions

View File

@@ -39,9 +39,9 @@
- option to remove <thinking> from chat history; + - option to remove <thinking> from chat history; +
- connection to a model status; (need to be tied to some event, perhaps its own shortcut even) + - 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? + - 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); - 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; - 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? - multirole support?
### FIX: ### FIX:
@@ -68,9 +68,8 @@
- table selection does not work; (ctrl+m is enter, it breakes all the tables) + - 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:'; + - name split for llamacpp completion. user msg should end with 'bot_name:'; +
- remove icons for agents/user; use only <role>: + - remove icons for agents/user; use only <role>: +
- 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); - 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; - F5 broke formatting and messages somehow;
- F4 after edit mode no colors;

7
bot.go
View File

@@ -43,11 +43,11 @@ var (
"min_p": 0.05, "min_p": 0.05,
"n_predict": -1.0, "n_predict": -1.0,
} }
toolUseText = "consider making a tool call."
) )
func fetchModelName() *models.LLMModels { func fetchModelName() *models.LLMModels {
api := "http://localhost:8080/v1/models" api := "http://localhost:8080/v1/models"
//nolint
resp, err := httpClient.Get(api) resp, err := httpClient.Get(api)
if err != nil { if err != nil {
logger.Warn("failed to get model", "link", api, "error", err) logger.Warn("failed to get model", "link", api, "error", err)
@@ -60,7 +60,7 @@ func fetchModelName() *models.LLMModels {
return nil return nil
} }
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
currentModel = "none" currentModel = "disconnected"
return nil return nil
} }
currentModel = path.Base(llmModel.Data[0].ID) 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) resp, err := httpClient.Post(cfg.CurrentAPI, "application/json", body)
if err != nil { if err != nil {
logger.Error("llamacpp api", "error", err) logger.Error("llamacpp api", "error", err)
if err := notifyUser("error", "apicall failed"); err != nil {
logger.Error("failed to notify", "error", err)
}
streamDone <- true streamDone <- true
return return
} }

View File

@@ -2,7 +2,7 @@ package main
import ( import (
"flag" "flag"
"fmt" "strconv"
"unicode" "unicode"
"github.com/rivo/tview" "github.com/rivo/tview"
@@ -30,7 +30,7 @@ func main() {
flag.Parse() flag.Parse()
if apiPort != nil && *apiPort > 3000 { if apiPort != nil && *apiPort > 3000 {
srv := Server{} srv := Server{}
srv.ListenToRequests(fmt.Sprintf("%d", *apiPort)) srv.ListenToRequests(strconv.Itoa(*apiPort))
return return
} }
pages.AddPage("main", flex, true, true) pages.AddPage("main", flex, true, true)

View File

@@ -9,6 +9,7 @@ import (
) )
type Server struct { type Server struct {
// nolint
config config.Config config config.Config
} }
@@ -25,13 +26,17 @@ func (srv *Server) ListenToRequests(port string) {
mux.HandleFunc("GET /model", modelHandler) mux.HandleFunc("GET /model", modelHandler)
mux.HandleFunc("POST /completion", completionHandler) mux.HandleFunc("POST /completion", completionHandler)
fmt.Println("Listening", "addr", server.Addr) fmt.Println("Listening", "addr", server.Addr)
server.ListenAndServe() if err := server.ListenAndServe(); err != nil {
panic(err)
}
} }
// create server // create server
// listen to the completion endpoint handler // listen to the completion endpoint handler
func pingHandler(w http.ResponseWriter, req *http.Request) { 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) { func completionHandler(w http.ResponseWriter, req *http.Request) {
@@ -44,20 +49,26 @@ out:
for { for {
select { select {
case chunk := <-chunkChan: case chunk := <-chunkChan:
fmt.Println(chunk) fmt.Print(chunk)
w.Write([]byte(chunk)) if _, err := w.Write([]byte(chunk)); err != nil {
logger.Warn("failed to write chunk", "value", chunk)
continue
}
case <-streamDone: case <-streamDone:
break out break out
} }
} }
return
} }
func modelHandler(w http.ResponseWriter, req *http.Request) { func modelHandler(w http.ResponseWriter, req *http.Request) {
llmModel := fetchModelName() llmModel := fetchModelName()
payload, err := json.Marshal(llmModel) payload, err := json.Marshal(llmModel)
if err != nil { if err != nil {
logger.Error("model handler", "error", err)
// return err // return err
return
}
if _, err := w.Write(payload); err != nil {
logger.Error("model handler", "error", err)
} }
w.Write(payload)
} }

View File

@@ -109,7 +109,9 @@ func makeChatTable(chatMap map[string]models.Chat) *tview.Table {
if !ok { if !ok {
logger.Warn("no such card", "agent", agentName) logger.Warn("no such card", "agent", agentName)
//no:lint //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 { if err := pngmeta.WriteToPng(cc.ToSpec(cfg.UserRole), cc.FilePath, cc.FilePath); err != nil {
logger.Error("failed to write charcard", logger.Error("failed to write charcard",

4
tui.go
View File

@@ -25,8 +25,6 @@ var (
// sysModal *tview.Modal // sysModal *tview.Modal
indexPickWindow *tview.InputField indexPickWindow *tview.InputField
renameWindow *tview.InputField renameWindow *tview.InputField
//
longJobStatusCh = make(chan string, 1)
// pages // pages
historyPage = "historyPage" historyPage = "historyPage"
agentPage = "agentPage" agentPage = "agentPage"
@@ -35,7 +33,6 @@ var (
helpPage = "helpPage" helpPage = "helpPage"
renamePage = "renamePage" renamePage = "renamePage"
RAGPage = "RAGPage " RAGPage = "RAGPage "
longStatusPage = "longStatusPage"
propsPage = "propsPage" propsPage = "propsPage"
codeBlockPage = "codeBlockPage" codeBlockPage = "codeBlockPage"
// help text // help text
@@ -525,6 +522,7 @@ func init() {
} }
cfg.APIMap[newAPI] = prevAPI cfg.APIMap[newAPI] = prevAPI
cfg.CurrentAPI = newAPI cfg.CurrentAPI = newAPI
initChunkParser()
updateStatusLine() updateStatusLine()
return nil return nil
} }