diff options
author | Grail Finder <wohilas@gmail.com> | 2024-11-21 18:15:01 +0300 |
---|---|---|
committer | Grail Finder <wohilas@gmail.com> | 2024-11-21 18:15:01 +0300 |
commit | c35af037203ac5c39a4f704d5343bc2b5cc56a0c (patch) | |
tree | 0f392fc7788e3b2632ceeabe3b85af771c74fd2d | |
parent | 35340d88863cb2a6a1b883bd3312abfef3a24c7a (diff) |
Feat: add tools to tool map
-rw-r--r-- | README.md | 6 | ||||
-rw-r--r-- | bot.go | 8 | ||||
-rw-r--r-- | main.go | 17 | ||||
-rw-r--r-- | storage/storage.go | 7 | ||||
-rw-r--r-- | tools.go | 115 |
5 files changed, 130 insertions, 23 deletions
@@ -8,12 +8,13 @@ - edit message? (including from bot); + - ability to copy message; + - menu with old chats (chat files); + -- fullscreen textarea option (for long prompt); - tab to switch selection between textview and textarea (input and chat); + - basic tools: memorize and recall; - stop stream from the bot; + - sqlitedb instead of chatfiles; + +- define tools and sys prompt for them to be used; - sqlite for the bot memory; +- fullscreen textarea option (bothersome to implement); - option to switch between predefined sys prompts; ### FIX: @@ -23,3 +24,6 @@ - Tab is needed to copy paste text into textarea box, use shift+tab to switch focus; (changed tp pgup) + - 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)); - empty input to continue bot msg gens new msg index and bot icon; +- sometimes bots put additional info around the tool call, have a regexp to match tool call; +- remove all panics from code; +- new chat is not saved in db; @@ -33,11 +33,12 @@ var ( historyDir = "./history/" // TODO: pass as an cli arg showSystemMsgs bool + chunkLimit = 1000 activeChatName string chunkChan = make(chan string, 10) streamDone = make(chan bool, 1) chatBody *models.ChatBody - store storage.ChatHistory + store storage.FullRepo defaultFirstMsg = "Hello! What can I do for you?" defaultStarter = []models.MessagesStory{ {Role: "system", Content: systemMsg}, @@ -89,14 +90,15 @@ func sendMsgToLLM(body io.Reader) (any, error) { break } llmchunk := models.LLMRespChunk{} - if counter > 2000 { + if counter > chunkLimit { + logger.Warn("response hit chunk limit", "limit", chunkLimit) streamDone <- true break } line, err := reader.ReadBytes('\n') if err != nil { streamDone <- true - panic(err) + logger.Error("error reading response body", "error", err) } // logger.Info("linecheck", "line", string(line), "len", len(line), "counter", counter) if len(line) <= 1 { @@ -1,8 +1,9 @@ package main import ( + "elefant/models" + "encoding/json" "fmt" - "path" "strconv" "time" "unicode" @@ -73,8 +74,19 @@ func main() { case "new": // set chat body chatBody.Messages = defaultStarter + // TODO: use predefined var since it is the same each time + msgsBytes, err := json.Marshal(chatBody.Messages) + if err != nil { + logger.Error(err.Error()) + } textView.SetText(chatToText(showSystemMsgs)) - activeChatName = path.Join(historyDir, fmt.Sprintf("%d_chat.json", time.Now().Unix())) + newChat := &models.Chat{ + Name: fmt.Sprintf("%v_%v", "new", time.Now().Unix()), + Msgs: string(msgsBytes), + } + // activeChatName = path.Join(historyDir, fmt.Sprintf("%d_chat.json", time.Now().Unix())) + activeChatName = newChat.Name + chatMap[newChat.Name] = newChat pages.RemovePage("history") return // set text @@ -141,7 +153,6 @@ func main() { editArea.SetText(m.Content, true) } if !editMode && event.Key() == tcell.KeyEnter { - // TODO: add notification that text was copied copyToClipboard(m.Content) notification := fmt.Sprintf("msg '%s' was copied to the clipboard", m.Content[:30]) notifyUser("copied", notification) diff --git a/storage/storage.go b/storage/storage.go index 67b8dd8..7d0d941 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -8,6 +8,11 @@ import ( "github.com/jmoiron/sqlx" ) +type FullRepo interface { + ChatHistory + Memories +} + type ChatHistory interface { ListChats() ([]models.Chat, error) GetChatByID(id uint32) (*models.Chat, error) @@ -61,7 +66,7 @@ func (p ProviderSQL) RemoveChat(id uint32) error { return err } -func NewProviderSQL(dbPath string, logger *slog.Logger) ChatHistory { +func NewProviderSQL(dbPath string, logger *slog.Logger) FullRepo { db, err := sqlx.Open("sqlite", dbPath) if err != nil { panic(err) @@ -1,59 +1,144 @@ package main +import ( + "elefant/models" + "encoding/json" + "time" +) + var ( // TODO: form that message based on existing funcs + // systemMsg = `You're a helpful assistant. + // # Tools + // You can do functions call if needed. + // Your current tools: + // <tools> + // { + // "name":"get_id", + // "args": "username" + // } + // </tools> + // To make a function call return a json object within __tool_call__ tags; + // Example: + // __tool_call__ + // { + // "name":"get_id", + // "args": "Adam" + // } + // __tool_call__ + // When making function call avoid typing anything else. 'tool' user will respond with the results of the call. + // After that you are free to respond to the user. + // ` systemMsg = `You're a helpful assistant. # Tools You can do functions call if needed. Your current tools: <tools> +[ +{ +"name":"recall", +"args": "topic", +"when_to_use": "when asked about topic that user previously asked to memorise" +}, { -"name":"get_id", -"args": "username" +"name":"memorise", +"args": ["topic", "info"], +"when_to_use": "when asked to memorise something" +}, +{ +"name":"recall_topics", +"args": null, +"when_to_use": "once in a while" } +] </tools> To make a function call return a json object within __tool_call__ tags; Example: __tool_call__ { -"name":"get_id", +"name":"recall", "args": "Adam" } __tool_call__ -When making function call avoid typing anything else. 'tool' user will respond with the results of the 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. ` ) -func memorize(topic, info string) { - // +/* +consider cases: +- append mode (treat it like a journal appendix) +- replace mode (new info/mind invalidates old ones) +also: +- some writing can be done without consideration of previous data; +- others do; +*/ +func memorise(args ...string) []byte { + agent := assistantRole + if len(args) < 1 { + // TODO: log + return nil + } + memory := &models.Memory{ + Agent: agent, + Topic: args[0], + Mind: args[1], + UpdatedAt: time.Now(), + } + store.Memorise(memory) + return nil } -func recall(topic string) string { - // - return "" +func recall(args ...string) []byte { + agent := assistantRole + if len(args) < 1 { + // TODO: log + return nil + } + mind, err := store.Recall(agent, args[0]) + if err != nil { + panic(err) + } + return []byte(mind) } -func recallTopics() []string { - return []string{} +func recallTopics(args ...string) []byte { + agent := assistantRole + topics, err := store.RecallTopics(agent) + if err != nil { + panic(err) + } + data, err := json.Marshal(topics) + if err != nil { + panic(err) + } + return data } func fullMemoryLoad() {} // predifine funcs -func getUserDetails(id ...string) map[string]any { +func getUserDetails(id ...string) []byte { // db query // return DB[id[0]] - return map[string]any{ + m := map[string]any{ "username": "fm11", "id": 24983, "reputation": 911, "balance": 214.73, } + data, err := json.Marshal(m) + if err != nil { + panic(err) + } + return data } -type fnSig func(...string) map[string]any +type fnSig func(...string) []byte var fnMap = map[string]fnSig{ - "get_id": getUserDetails, + "get_id": getUserDetails, + "recall": recall, + "recall_topics": recallTopics, + "memorise": memorise, } |