From 55007d27f808426128a7b2d86169e86c4cf01b57 Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Wed, 27 Nov 2024 15:09:43 +0300 Subject: Fix: tool calls --- README.md | 2 ++ bot.go | 24 ++++++++---------------- models/models.go | 4 ++-- tools.go | 48 +++++++++++++++--------------------------------- 4 files changed, 27 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 9fe6a32..f84eeee 100644 --- a/README.md +++ b/README.md @@ -34,3 +34,5 @@ - new chat replaces old ones in db; + - empty input to continue bot msg gens new msg index and bot icon; + - delete last msg: can have unexpected behavior (deletes what appears to be two messages if last bot msg was not generated (should only delete icon in that case)) (should use regen instead of delete in that case); +- lets say we have two (or more) agents with the same name across multiple chats. These agents go and ask db for topics they memoriesed. Now they can access topics that aren't meant for them. (so memory should have an option: shareble; that indicates if that memory can be shared across chats); +- if option to show sys msg enabled: it show display new tool responses; diff --git a/bot.go b/bot.go index 245e806..40706c3 100644 --- a/bot.go +++ b/bot.go @@ -31,7 +31,7 @@ var ( // TODO: pass as an cli arg or have config APIURL = "http://localhost:8080/v1/chat/completions" logFileName = "log.txt" - showSystemMsgs bool + showSystemMsgs = true chunkLimit = 1000 activeChatName string chunkChan = make(chan string, 10) @@ -158,21 +158,16 @@ out: } func findCall(msg string, tv *tview.TextView) { - // prefix := "__tool_call__\n" - // suffix := "\n__tool_call__" - // if !strings.HasPrefix(msg, prefix) || - // !strings.HasSuffix(msg, suffix) { - // return - // } - // jsStr := strings.TrimSuffix(strings.TrimPrefix(msg, prefix), suffix) fc := models.FuncCall{} jsStr := toolCallRE.FindString(msg) if jsStr == "" { - // tool call not found return } + prefix := "__tool_call__\n" + suffix := "\n__tool_call__" + jsStr = strings.TrimSuffix(strings.TrimPrefix(jsStr, prefix), suffix) if err := json.Unmarshal([]byte(jsStr), &fc); err != nil { - logger.Error("failed to unmarshal tool call", "error", err) + logger.Error("failed to unmarshal tool call", "error", err, "json_string", jsStr) return } // call a func @@ -182,12 +177,9 @@ func findCall(msg string, tv *tview.TextView) { chatRound(m, toolRole, tv) return } - resp := f(fc.Args) - toolMsg := fmt.Sprintf("tool response: %+v", resp) - // reader := formMsg(chatBody, toolMsg, toolRole) - // sendMsgToLLM() + resp := f(fc.Args...) + toolMsg := fmt.Sprintf("tool response: %+v", string(resp)) chatRound(toolMsg, toolRole, tv) - // return func result to the llm } func chatToTextSlice(showSys bool) []string { @@ -248,7 +240,7 @@ func init() { // load all chats in memory loadHistoryChats() lastChat := loadOldChatOrGetNew() - logger.Info("loaded history", "chat", lastChat) + logger.Info("loaded history") chatBody = &models.ChatBody{ Model: "modl_name", Stream: true, diff --git a/models/models.go b/models/models.go index 880779f..02bec00 100644 --- a/models/models.go +++ b/models/models.go @@ -12,8 +12,8 @@ import ( // } type FuncCall struct { - Name string `json:"name"` - Args string `json:"args"` + Name string `json:"name"` + Args []string `json:"args"` } type LLMResp struct { diff --git a/tools.go b/tools.go index b752790..b32d8cb 100644 --- a/tools.go +++ b/tools.go @@ -2,8 +2,9 @@ package main import ( "elefant/models" - "encoding/json" + "fmt" "regexp" + "strings" "time" ) @@ -30,7 +31,7 @@ Your current tools: { "name":"recall_topics", "args": null, -"when_to_use": "once in a while" +"when_to_use": "to see what topics are saved in memory" } ] @@ -42,7 +43,8 @@ __tool_call__ "args": "Adam" } __tool_call__ -When done right, tool call will be delivered to the 'tool' agent. 'tool' agent will respond with the results of the call. +Tool call is addressed to the tool agent, avoid sending more info than tool call itself, while making a call. +When done right, tool call will be delivered to the tool agent. tool agent will respond with the results of the call. After that you are free to respond to the user. ` systemMsg = toolSysMsg @@ -61,8 +63,9 @@ also: func memorise(args ...string) []byte { agent := assistantRole if len(args) < 2 { - logger.Warn("not enough args to call memorise tool") - return nil + msg := "not enough args to call memorise tool; need topic and data to remember" + logger.Error(msg) + return []byte(msg) } memory := &models.Memory{ Agent: agent, @@ -71,7 +74,8 @@ func memorise(args ...string) []byte { UpdatedAt: time.Now(), } store.Memorise(memory) - return nil + msg := fmt.Sprintf("info saved under the topic: %s", args[0]) + return []byte(msg) } func recall(args ...string) []byte { @@ -82,8 +86,9 @@ func recall(args ...string) []byte { } mind, err := store.Recall(agent, args[0]) if err != nil { - logger.Error("failed to use tool", "error", err, "args", args) - return nil + msg := fmt.Sprintf("failed to recall; error: %v; args: %v", err, args) + logger.Error(msg) + return []byte(msg) } return []byte(mind) } @@ -95,38 +100,15 @@ func recallTopics(args ...string) []byte { logger.Error("failed to use tool", "error", err, "args", args) return nil } - data, err := json.Marshal(topics) - if err != nil { - logger.Error("failed to use tool", "error", err, "args", args) - return nil - } - return data + joinedS := strings.Join(topics, ";") + return []byte(joinedS) } func fullMemoryLoad() {} -// predifine funcs -func getUserDetails(args ...string) []byte { - // db query - // return DB[id[0]] - m := map[string]any{ - "username": "fm11", - "id": 24983, - "reputation": 911, - "balance": 214.73, - } - data, err := json.Marshal(m) - if err != nil { - logger.Error("failed to use tool", "error", err, "args", args) - return nil - } - return data -} - type fnSig func(...string) []byte var fnMap = map[string]fnSig{ - "get_id": getUserDetails, "recall": recall, "recall_topics": recallTopics, "memorise": memorise, -- cgit v1.2.3