logging: pt1

This commit is contained in:
Ettore Di Giacinto
2024-04-15 00:13:10 +02:00
parent ac8f6e94ff
commit f0bc2be678
14 changed files with 118 additions and 126 deletions

View File

@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"log/slog"
"os"
"path/filepath"
"sync"
@@ -32,7 +33,6 @@ type AgentConfig struct {
Model string `json:"model" form:"model"`
Name string `json:"name" form:"name"`
HUD bool `json:"hud" form:"hud"`
Debug bool `json:"debug" form:"debug"`
StandaloneJob bool `json:"standalone_job" form:"standalone_job"`
RandomIdentity bool `json:"random_identity" form:"random_identity"`
InitiateConversations bool `json:"initiate_conversations" form:"initiate_conversations"`
@@ -175,14 +175,14 @@ func (a *AgentConfig) availableActions(ctx context.Context) []Action {
actions := []Action{}
for _, action := range a.Actions {
fmt.Println("Set Action", action)
slog.Info("Set Action", action)
var config map[string]string
if err := json.Unmarshal([]byte(action.Config), &config); err != nil {
fmt.Println("Error unmarshalling action config", err)
slog.Info("Error unmarshalling action config", err)
continue
}
fmt.Println("Config", config)
slog.Info("Config", config)
switch action.Name {
case ActionSearch:
@@ -218,20 +218,20 @@ func (a *AgentConfig) availableConnectors() []Connector {
connectors := []Connector{}
for _, c := range a.Connector {
fmt.Println("Set Connector", c)
slog.Info("Set Connector", c)
var config map[string]string
if err := json.Unmarshal([]byte(c.Config), &config); err != nil {
fmt.Println("Error unmarshalling connector config", err)
slog.Info("Error unmarshalling connector config", err)
continue
}
fmt.Println("Config", config)
slog.Info("Config", config)
switch c.Type {
case ConnectorTelegram:
cc, err := connector.NewTelegramConnector(config)
if err != nil {
fmt.Println("Error creating telegram connector", err)
slog.Info("Error creating telegram connector", err)
continue
}
@@ -266,15 +266,15 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
connectors := config.availableConnectors()
fmt.Println("Creating agent", name)
fmt.Println("Model", model)
fmt.Println("API URL", a.apiURL)
slog.Info("Creating agent", name)
slog.Info("Model", model)
slog.Info("API URL", a.apiURL)
actions := config.availableActions(ctx)
stateFile, characterFile := a.stateFiles(name)
fmt.Println("Actions", actions)
slog.Info("Actions", actions)
opts := []Option{
WithModel(model),
WithLLMAPIURL(a.apiURL),
@@ -288,7 +288,7 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
WithCharacterFile(characterFile),
WithRAGDB(a.ragDB),
WithAgentReasoningCallback(func(state ActionCurrentState) bool {
fmt.Println("Reasoning", state.Reasoning)
slog.Info("Reasoning", state.Reasoning)
manager.Send(
NewMessage(
fmt.Sprintf(`Thinking: %s`, htmlIfy(state.Reasoning)),
@@ -311,7 +311,7 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
a.agentStatus[name].addResult(state)
a.Unlock()
fmt.Println("Reasoning", state.Reasoning)
slog.Info("Reasoning", state.Reasoning)
text := fmt.Sprintf(`Reasoning: %s
Action taken: %+v
@@ -337,8 +337,8 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
if config.HUD {
opts = append(opts, EnableHUD)
}
if config.Debug {
opts = append(opts, DebugMode)
if os.Getenv("DEBUG") != "" {
opts = append(opts, LogLevel(slog.LevelDebug))
}
if config.StandaloneJob {
opts = append(opts, EnableStandaloneJob)
@@ -365,8 +365,7 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
opts = append(opts, EnableKnowledgeBaseWithResults(config.KnowledgeBaseResults))
}
fmt.Println("Starting agent", name)
fmt.Printf("Config %+v\n", config)
slog.Info("Starting agent", "name", name, "config", config)
agent, err := New(opts...)
if err != nil {
return err
@@ -377,7 +376,7 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
go func() {
if err := agent.Run(); err != nil {
fmt.Println("Agent stop: ", err.Error())
slog.Info("Agent stop: ", err.Error())
}
}()

View File

@@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"log/slog"
"net/http"
"os"
@@ -53,7 +54,7 @@ func (a *App) KnowledgeBaseFile(db *InMemoryDatabase) func(c *fiber.Ctx) error {
return err
}
fmt.Println("File uploaded to: " + destination)
slog.Info("File uploaded to: " + destination)
fmt.Printf("Payload: %+v\n", payload)
content, err := readPdf(destination) // Read local pdf file
@@ -61,7 +62,7 @@ func (a *App) KnowledgeBaseFile(db *InMemoryDatabase) func(c *fiber.Ctx) error {
panic(err)
}
fmt.Println("Content is", content)
slog.Info("Content is", content)
chunkSize := defaultChunkSize
if payload.ChunkSize > 0 {
chunkSize = payload.ChunkSize
@@ -130,7 +131,7 @@ func (a *App) Notify(pool *AgentPool) func(c *fiber.Ctx) error {
func (a *App) Delete(pool *AgentPool) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
if err := pool.Remove(c.Params("name")); err != nil {
fmt.Println("Error removing agent", err)
slog.Info("Error removing agent", err)
return c.Status(http.StatusInternalServerError).SendString(err.Error())
}
return c.Redirect("/agents")
@@ -139,7 +140,7 @@ func (a *App) Delete(pool *AgentPool) func(c *fiber.Ctx) error {
func (a *App) Pause(pool *AgentPool) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
fmt.Println("Pausing agent", c.Params("name"))
slog.Info("Pausing agent", c.Params("name"))
agent := pool.GetAgent(c.Params("name"))
if agent != nil {
agent.Pause()
@@ -217,7 +218,7 @@ func (a *App) ImportAgent(pool *AgentPool) func(c *fiber.Ctx) error {
return err
}
fmt.Println("Importing agent", config.Name)
slog.Info("Importing agent", config.Name)
if config.Name == "" {
c.Status(http.StatusBadRequest).SendString("Name is required")
@@ -257,13 +258,13 @@ func (a *App) Chat(pool *AgentPool) func(c *fiber.Ctx) error {
go func() {
agent := pool.GetAgent(agentName)
if agent == nil {
fmt.Println("Agent not found in pool", c.Params("name"))
slog.Info("Agent not found in pool", c.Params("name"))
return
}
res := agent.Ask(
WithText(query),
)
fmt.Println("response is", res.Response)
slog.Info("response is", res.Response)
manager.Send(
NewMessage(
chatDiv(res.Response, "blue"),

View File

@@ -1,7 +1,7 @@
package connector
import (
"fmt"
"log/slog"
"strings"
"github.com/bwmarrin/discordgo"
@@ -39,7 +39,7 @@ func (d *Discord) Start(a *agent.Agent) {
// Create a new Discord session using the provided bot token.
dg, err := discordgo.New(Token)
if err != nil {
fmt.Println("error creating Discord session,", err)
slog.Info("error creating Discord session,", err)
return
}
@@ -52,7 +52,7 @@ func (d *Discord) Start(a *agent.Agent) {
// Open a websocket connection to Discord and begin listening.
err = dg.Open()
if err != nil {
fmt.Println("error opening connection,", err)
slog.Info("error opening connection,", err)
return
}
@@ -84,7 +84,7 @@ func (d *Discord) messageCreate(a *agent.Agent) func(s *discordgo.Session, m *di
)
_, err := s.ChannelMessageSend(m.ChannelID, job.Response)
if err != nil {
fmt.Println("error sending message,", err)
slog.Info("error sending message,", err)
}
}

View File

@@ -2,6 +2,7 @@ package connector
import (
"fmt"
"log/slog"
"strings"
"time"
@@ -57,7 +58,7 @@ func (g *GithubIssues) Start(a *agent.Agent) {
for {
select {
case <-ticker.C:
fmt.Println("Looking into github issues...")
slog.Info("Looking into github issues...")
g.issuesService()
case <-a.Context().Done():
return
@@ -79,7 +80,7 @@ func (g *GithubIssues) issuesService() {
g.repository,
&github.IssueListByRepoOptions{})
if err != nil {
fmt.Println("Error listing issues", err)
slog.Info("Error listing issues", err)
}
for _, issue := range issues {
// Do something with the issue
@@ -99,7 +100,7 @@ func (g *GithubIssues) issuesService() {
}
if userName == user.GetLogin() {
fmt.Println("Ignoring issue opened by the bot")
slog.Info("Ignoring issue opened by the bot")
continue
}
messages := []openai.ChatCompletionMessage{
@@ -133,7 +134,7 @@ func (g *GithubIssues) issuesService() {
// if last comment is from the user and mentions the bot username, we must answer
if comment.User.GetName() != user.GetLogin() && len(comments)-1 == i {
if strings.Contains(comment.GetBody(), fmt.Sprintf("@%s", user.GetLogin())) {
fmt.Println("Bot was mentioned in the last comment")
slog.Info("Bot was mentioned in the last comment")
mustAnswer = true
}
}
@@ -141,9 +142,9 @@ func (g *GithubIssues) issuesService() {
if len(comments) == 0 || !botAnsweredAlready {
// if no comments, or bot didn't answer yet, we must answer
fmt.Println("No comments, or bot didn't answer yet")
fmt.Println("Comments:", len(comments))
fmt.Println("Bot answered already", botAnsweredAlready)
slog.Info("No comments, or bot didn't answer yet")
slog.Info("Comments:", len(comments))
slog.Info("Bot answered already", botAnsweredAlready)
mustAnswer = true
}
@@ -163,7 +164,7 @@ func (g *GithubIssues) issuesService() {
},
)
if err != nil {
fmt.Println("Error creating comment", err)
slog.Info("Error creating comment", err)
}
}
}

View File

@@ -3,6 +3,7 @@ package connector
import (
"fmt"
"log"
"log/slog"
"os"
"strings"
@@ -60,11 +61,11 @@ func (t *Slack) Start(a *agent.Agent) {
for evt := range client.Events {
switch evt.Type {
case socketmode.EventTypeConnecting:
fmt.Println("Connecting to Slack with Socket Mode...")
slog.Info("Connecting to Slack with Socket Mode...")
case socketmode.EventTypeConnectionError:
fmt.Println("Connection failed. Retrying later...")
slog.Info("Connection failed. Retrying later...")
case socketmode.EventTypeConnected:
fmt.Println("Connected to Slack with Socket Mode.")
slog.Info("Connected to Slack with Socket Mode.")
case socketmode.EventTypeEventsAPI:
eventsAPIEvent, ok := evt.Data.(slackevents.EventsAPIEvent)
if !ok {
@@ -91,7 +92,7 @@ func (t *Slack) Start(a *agent.Agent) {
if t.channelID == "" && !t.alwaysReply || // If we have set alwaysReply and no channelID
t.channelID != ev.Channel { // If we have a channelID and it's not the same as the event channel
// Skip messages from other channels
fmt.Println("Skipping reply to channel", ev.Channel, t.channelID)
slog.Info("Skipping reply to channel", ev.Channel, t.channelID)
continue
}
@@ -123,7 +124,7 @@ func (t *Slack) Start(a *agent.Agent) {
// strip our id from the message
message = strings.ReplaceAll(message, "<@"+b.UserID+"> ", "")
fmt.Println("Message", message)
slog.Info("Message", message)
go func() {
res := a.Ask(

View File

@@ -2,8 +2,8 @@ package main
import (
"embed"
"fmt"
"log"
"log/slog"
"net/http"
"os"
@@ -75,9 +75,9 @@ func main() {
}
if len(db.Database) > 0 && kbdisableIndexing != "true" {
fmt.Println("Loading knowledgebase from disk, to skip run with KBDISABLEINDEX=true")
slog.Info("Loading knowledgebase from disk, to skip run with KBDISABLEINDEX=true")
if err := db.SaveToStore(); err != nil {
fmt.Println("Error storing in the KB", err)
slog.Info("Error storing in the KB", err)
}
}

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"path/filepath"
@@ -120,7 +121,7 @@ func getWebPage(url string) (string, error) {
func Sitemap(url string) (res []string, err error) {
err = sitemap.ParseFromSite(url, func(e sitemap.Entry) error {
fmt.Println("Sitemap page: " + e.GetLocation())
slog.Info("Sitemap page: " + e.GetLocation())
content, err := getWebPage(e.GetLocation())
if err == nil {
res = append(res, content)
@@ -133,10 +134,10 @@ func Sitemap(url string) (res []string, err error) {
func WebsiteToKB(website string, chunkSize int, db *InMemoryDatabase) {
content, err := Sitemap(website)
if err != nil {
fmt.Println("Error walking sitemap for website", err)
slog.Info("Error walking sitemap for website", err)
}
fmt.Println("Found pages: ", len(content))
fmt.Println("ChunkSize: ", chunkSize)
slog.Info("Found pages: ", len(content))
slog.Info("ChunkSize: ", chunkSize)
StringsToKB(db, chunkSize, content...)
}
@@ -144,9 +145,9 @@ func WebsiteToKB(website string, chunkSize int, db *InMemoryDatabase) {
func StringsToKB(db *InMemoryDatabase, chunkSize int, content ...string) {
for _, c := range content {
chunks := splitParagraphIntoChunks(c, chunkSize)
fmt.Println("chunks: ", len(chunks))
slog.Info("chunks: ", len(chunks))
for _, chunk := range chunks {
fmt.Println("Chunk size: ", len(chunk))
slog.Info("Chunk size: ", len(chunk))
db.AddEntry(chunk)
}
@@ -154,7 +155,7 @@ func StringsToKB(db *InMemoryDatabase, chunkSize int, content ...string) {
}
if err := db.SaveToStore(); err != nil {
fmt.Println("Error storing in the KB", err)
slog.Info("Error storing in the KB", err)
}
}

View File

@@ -91,8 +91,6 @@
<input type="text" name="kb_results" id="kb_results" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-lg border-gray-300 rounded-md bg-gray-700 text-white" placeholder="3">
</div>
<label for="debug" class="block text-lg font-medium text-gray-400">Debug</label>
<input type="checkbox" name="debug" id="debug" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-lg border-gray-300 rounded-md bg-gray-700 text-white">
<label for="standalone_job" class="block text-lg font-medium text-gray-400">Standalone Job</label>
<input type="checkbox" name="standalone_job" id="standalone_job" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-lg border-gray-300 rounded-md bg-gray-700 text-white">