Enha: match tool call with regexp; clear panics

This commit is contained in:
Grail Finder
2024-11-21 20:16:47 +03:00
parent c35af03720
commit cc84c037ec
5 changed files with 55 additions and 52 deletions

View File

@@ -12,10 +12,13 @@
- basic tools: memorize and recall; - basic tools: memorize and recall;
- stop stream from the bot; + - stop stream from the bot; +
- sqlitedb instead of chatfiles; + - sqlitedb instead of chatfiles; +
- define tools and sys prompt for them to be used; - define tools and sys prompt for them to be used; +
- sqlite for the bot memory; - sqlite for the bot memory;
- fullscreen textarea option (bothersome to implement); - fullscreen textarea option (bothersome to implement);
- add system prompt without tools (for mistral);
- option to switch between predefined sys prompts; - option to switch between predefined sys prompts;
- consider adding use /completion of llamacpp, since openai endpoint clearly has template|format issues;
- change temp, min-p and other params from tui;
### FIX: ### FIX:
- bot responding (or haninging) blocks everything; + - bot responding (or haninging) blocks everything; +
@@ -24,6 +27,6 @@
- Tab is needed to copy paste text into textarea box, use shift+tab to switch focus; (changed tp pgup) + - 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)); - 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; - 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; - sometimes bots put additional info around the tool call, have a regexp to match tool call; +
- remove all panics from code; - remove all panics from code; +
- new chat is not saved in db; - new chat is not saved in db;

49
bot.go
View File

@@ -24,14 +24,13 @@ var httpClient = http.Client{
var ( var (
logger *slog.Logger logger *slog.Logger
APIURL = "http://localhost:8080/v1/chat/completions" APIURL = "http://localhost:8080/v1/chat/completions"
DB = map[string]map[string]any{}
userRole = "user" userRole = "user"
assistantRole = "assistant" assistantRole = "assistant"
toolRole = "tool" toolRole = "tool"
assistantIcon = "<🤖>: " assistantIcon = "<🤖>: "
userIcon = "<user>: " userIcon = "<user>: "
historyDir = "./history/" // TODO: pass as an cli arg or have config
// TODO: pass as an cli arg logFileName = "log.txt"
showSystemMsgs bool showSystemMsgs bool
chunkLimit = 1000 chunkLimit = 1000
activeChatName string activeChatName string
@@ -44,21 +43,12 @@ var (
{Role: "system", Content: systemMsg}, {Role: "system", Content: systemMsg},
{Role: assistantRole, Content: defaultFirstMsg}, {Role: assistantRole, Content: defaultFirstMsg},
} }
interruptResp = false defaultStarterBytes, _ = json.Marshal(chatBody.Messages)
interruptResp = false
) )
// ==== // ====
func getUserInput(userPrompt string) string {
fmt.Printf(userPrompt)
reader := bufio.NewReader(os.Stdin)
line, err := reader.ReadString('\n')
if err != nil {
panic(err) // think about it
}
return line
}
func formMsg(chatBody *models.ChatBody, newMsg, role string) io.Reader { func formMsg(chatBody *models.ChatBody, newMsg, role string) io.Reader {
if newMsg != "" { // otherwise let the bot continue if newMsg != "" { // otherwise let the bot continue
newMsg := models.MessagesStory{Role: role, Content: newMsg} newMsg := models.MessagesStory{Role: role, Content: newMsg}
@@ -66,7 +56,8 @@ func formMsg(chatBody *models.ChatBody, newMsg, role string) io.Reader {
} }
data, err := json.Marshal(chatBody) data, err := json.Marshal(chatBody)
if err != nil { if err != nil {
panic(err) logger.Error("failed to form a msg", "error", err)
return nil
} }
return bytes.NewReader(data) return bytes.NewReader(data)
} }
@@ -130,6 +121,9 @@ func sendMsgToLLM(body io.Reader) (any, error) {
func chatRound(userMsg, role string, tv *tview.TextView) { func chatRound(userMsg, role string, tv *tview.TextView) {
botRespMode = true botRespMode = true
reader := formMsg(chatBody, userMsg, role) reader := formMsg(chatBody, userMsg, role)
if reader == nil {
return // any notification in that case?
}
go sendMsgToLLM(reader) go sendMsgToLLM(reader)
fmt.Fprintf(tv, fmt.Sprintf("(%d) ", len(chatBody.Messages))) fmt.Fprintf(tv, fmt.Sprintf("(%d) ", len(chatBody.Messages)))
fmt.Fprintf(tv, assistantIcon) fmt.Fprintf(tv, assistantIcon)
@@ -161,18 +155,22 @@ out:
} }
func findCall(msg string, tv *tview.TextView) { func findCall(msg string, tv *tview.TextView) {
prefix := "__tool_call__\n" // prefix := "__tool_call__\n"
suffix := "\n__tool_call__" // 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{} fc := models.FuncCall{}
if !strings.HasPrefix(msg, prefix) || jsStr := toolCallRE.FindString(msg)
!strings.HasSuffix(msg, suffix) { if jsStr == "" {
// tool call not found
return return
} }
jsStr := strings.TrimSuffix(strings.TrimPrefix(msg, prefix), suffix)
if err := json.Unmarshal([]byte(jsStr), &fc); err != nil { 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)
return return
// panic(err)
} }
// call a func // call a func
f, ok := fnMap[fc.Name] f, ok := fnMap[fc.Name]
@@ -231,13 +229,10 @@ func textSliceToChat(chat []string) []models.MessagesStory {
} }
func init() { func init() {
file, err := os.OpenFile("log.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) file, err := os.OpenFile(logFileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil { if err != nil {
panic(err) logger.Error("failed to open log file", "error", err, "filename", logFileName)
} return
// create dir if does not exist
if err := os.MkdirAll(historyDir, os.ModePerm); err != nil {
panic(err)
} }
logger = slog.New(slog.NewTextHandler(file, nil)) logger = slog.New(slog.NewTextHandler(file, nil))
store = storage.NewProviderSQL("test.db", logger) store = storage.NewProviderSQL("test.db", logger)

21
main.go
View File

@@ -2,7 +2,6 @@ package main
import ( import (
"elefant/models" "elefant/models"
"encoding/json"
"fmt" "fmt"
"strconv" "strconv"
"time" "time"
@@ -63,7 +62,7 @@ func main() {
chatOpts := []string{"cancel", "new"} chatOpts := []string{"cancel", "new"}
fList, err := loadHistoryChats() fList, err := loadHistoryChats()
if err != nil { if err != nil {
panic(err) logger.Error("failed to load chat history", "error", err)
} }
chatOpts = append(chatOpts, fList...) chatOpts = append(chatOpts, fList...)
chatActModal := tview.NewModal(). chatActModal := tview.NewModal().
@@ -74,15 +73,10 @@ func main() {
case "new": case "new":
// set chat body // set chat body
chatBody.Messages = defaultStarter 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)) textView.SetText(chatToText(showSystemMsgs))
newChat := &models.Chat{ newChat := &models.Chat{
Name: fmt.Sprintf("%v_%v", "new", time.Now().Unix()), Name: fmt.Sprintf("%v_%v", "new", time.Now().Unix()),
Msgs: string(msgsBytes), Msgs: string(defaultStarterBytes),
} }
// activeChatName = path.Join(historyDir, fmt.Sprintf("%d_chat.json", time.Now().Unix())) // activeChatName = path.Join(historyDir, fmt.Sprintf("%d_chat.json", time.Now().Unix()))
activeChatName = newChat.Name activeChatName = newChat.Name
@@ -166,10 +160,10 @@ func main() {
textView.ScrollToEnd() textView.ScrollToEnd()
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyF1 { if event.Key() == tcell.KeyF1 {
// fList, err := listHistoryFiles(historyDir)
fList, err := loadHistoryChats() fList, err := loadHistoryChats()
if err != nil { if err != nil {
panic(err) logger.Error("failed to load chat history", "error", err)
return nil
} }
chatOpts = append(chatOpts, fList...) chatOpts = append(chatOpts, fList...)
pages.AddPage("history", chatActModal, true, true) pages.AddPage("history", chatActModal, true, true)
@@ -220,6 +214,10 @@ func main() {
pages.AddPage("getIndex", indexPickWindow, true, true) pages.AddPage("getIndex", indexPickWindow, true, true)
return nil return nil
} }
if event.Key() == tcell.KeyCtrlE {
textArea.SetText("pressed ctrl+e", true)
return nil
}
// cannot send msg in editMode or botRespMode // cannot send msg in editMode or botRespMode
if event.Key() == tcell.KeyEscape && !editMode && !botRespMode { if event.Key() == tcell.KeyEscape && !editMode && !botRespMode {
fromRow, fromColumn, _, _ := textArea.GetCursor() fromRow, fromColumn, _, _ := textArea.GetCursor()
@@ -251,6 +249,7 @@ func main() {
pages.AddPage("main", flex, true, true) pages.AddPage("main", flex, true, true)
if err := app.SetRoot(pages, if err := app.SetRoot(pages,
true).EnableMouse(true).Run(); err != nil { true).EnableMouse(true).Run(); err != nil {
panic(err) logger.Error("failed to start tview app", "error", err)
return
} }
} }

View File

@@ -69,9 +69,9 @@ func (p ProviderSQL) RemoveChat(id uint32) error {
func NewProviderSQL(dbPath string, logger *slog.Logger) FullRepo { func NewProviderSQL(dbPath string, logger *slog.Logger) FullRepo {
db, err := sqlx.Open("sqlite", dbPath) db, err := sqlx.Open("sqlite", dbPath)
if err != nil { if err != nil {
panic(err) logger.Error("failed to open db connection", "error", err)
return nil
} }
// get SQLite version
p := ProviderSQL{db: db, logger: logger} p := ProviderSQL{db: db, logger: logger}
p.Migrate() p.Migrate()
return p return p

View File

@@ -3,6 +3,7 @@ package main
import ( import (
"elefant/models" "elefant/models"
"encoding/json" "encoding/json"
"regexp"
"time" "time"
) )
@@ -29,7 +30,8 @@ var (
// When making function call avoid typing anything else. 'tool' user will respond with the results of the 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. // After that you are free to respond to the user.
// ` // `
systemMsg = `You're a helpful assistant. toolCallRE = regexp.MustCompile(`__tool_call__\s*([\s\S]*?)__tool_call__`)
systemMsg = `You're a helpful assistant.
# Tools # Tools
You can do functions call if needed. You can do functions call if needed.
Your current tools: Your current tools:
@@ -75,8 +77,8 @@ also:
*/ */
func memorise(args ...string) []byte { func memorise(args ...string) []byte {
agent := assistantRole agent := assistantRole
if len(args) < 1 { if len(args) < 2 {
// TODO: log logger.Warn("not enough args to call memorise tool")
return nil return nil
} }
memory := &models.Memory{ memory := &models.Memory{
@@ -92,12 +94,13 @@ func memorise(args ...string) []byte {
func recall(args ...string) []byte { func recall(args ...string) []byte {
agent := assistantRole agent := assistantRole
if len(args) < 1 { if len(args) < 1 {
// TODO: log logger.Warn("not enough args to call recall tool")
return nil return nil
} }
mind, err := store.Recall(agent, args[0]) mind, err := store.Recall(agent, args[0])
if err != nil { if err != nil {
panic(err) logger.Error("failed to use tool", "error", err, "args", args)
return nil
} }
return []byte(mind) return []byte(mind)
} }
@@ -106,11 +109,13 @@ func recallTopics(args ...string) []byte {
agent := assistantRole agent := assistantRole
topics, err := store.RecallTopics(agent) topics, err := store.RecallTopics(agent)
if err != nil { if err != nil {
panic(err) logger.Error("failed to use tool", "error", err, "args", args)
return nil
} }
data, err := json.Marshal(topics) data, err := json.Marshal(topics)
if err != nil { if err != nil {
panic(err) logger.Error("failed to use tool", "error", err, "args", args)
return nil
} }
return data return data
} }
@@ -118,7 +123,7 @@ func recallTopics(args ...string) []byte {
func fullMemoryLoad() {} func fullMemoryLoad() {}
// predifine funcs // predifine funcs
func getUserDetails(id ...string) []byte { func getUserDetails(args ...string) []byte {
// db query // db query
// return DB[id[0]] // return DB[id[0]]
m := map[string]any{ m := map[string]any{
@@ -129,7 +134,8 @@ func getUserDetails(id ...string) []byte {
} }
data, err := json.Marshal(m) data, err := json.Marshal(m)
if err != nil { if err != nil {
panic(err) logger.Error("failed to use tool", "error", err, "args", args)
return nil
} }
return data return data
} }