mirror of
https://github.com/mudler/cogito.git
synced 2026-07-23 18:35:23 -04:00
b8aaf55307
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
93 lines
2.4 KiB
Go
93 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/mudler/cogito/clients"
|
|
"github.com/mudler/cogito/examples/internal/search"
|
|
|
|
"github.com/mudler/cogito"
|
|
)
|
|
|
|
func main() {
|
|
|
|
model := os.Getenv("MODEL")
|
|
apiKey := os.Getenv("API_KEY")
|
|
baseURL := os.Getenv("BASE_URL")
|
|
|
|
defaultLLM := clients.NewLocalAILLM(model, apiKey, baseURL)
|
|
|
|
// Create tool definition - this automatically generates openai.Tool via Tool() method
|
|
searchTool := cogito.NewToolDefinition(
|
|
&search.SearchTool{},
|
|
search.SearchArgs{},
|
|
"search",
|
|
"A search engine to find information about a topic",
|
|
)
|
|
|
|
f := cogito.NewEmptyFragment()
|
|
for {
|
|
reader := bufio.NewReader(os.Stdin)
|
|
fmt.Print("> ")
|
|
text, _ := reader.ReadString('\n')
|
|
fmt.Println(strings.TrimSpace(text))
|
|
|
|
f = f.AddMessage("user", strings.TrimSpace(text))
|
|
var err error
|
|
f, err = cogito.ExecuteTools(
|
|
defaultLLM, f,
|
|
cogito.WithMaxAttempts(10),
|
|
cogito.WithMaxRetries(10),
|
|
cogito.WithIterations(10),
|
|
cogito.DisableSinkState,
|
|
//cogito.WithForceReasoningTool(),
|
|
//cogito.WithForceReasoning(),
|
|
cogito.WithReasoningCallback(func(s string) {
|
|
fmt.Println("___________________ START REASONING _________________")
|
|
fmt.Println(s)
|
|
fmt.Println("___________________ END REASONING _________________")
|
|
}),
|
|
cogito.WithStatusCallback(func(s string) {
|
|
fmt.Println("___________________ START STATUS _________________")
|
|
fmt.Println(s)
|
|
fmt.Println("___________________ END STATUS _________________")
|
|
}),
|
|
cogito.WithTools(searchTool),
|
|
|
|
cogito.WithToolCallBack(func(tool *cogito.ToolChoice, state *cogito.SessionState) cogito.ToolCallDecision {
|
|
args, err := json.Marshal(tool.Arguments)
|
|
if err != nil {
|
|
return cogito.ToolCallDecision{Approved: false}
|
|
}
|
|
fmt.Println("The agent wants to run the tool " + tool.Name + " with the following arguments: " + string(args))
|
|
fmt.Println("Do you want to run the tool? (y/n/adjust)")
|
|
reader := bufio.NewReader(os.Stdin)
|
|
text, _ := reader.ReadString('\n')
|
|
text = strings.TrimSpace(text)
|
|
switch text {
|
|
case "y":
|
|
return cogito.ToolCallDecision{Approved: true}
|
|
case "n":
|
|
return cogito.ToolCallDecision{Approved: false}
|
|
default:
|
|
return cogito.ToolCallDecision{
|
|
Approved: true,
|
|
Adjustment: text,
|
|
}
|
|
}
|
|
}),
|
|
)
|
|
if err != nil && !errors.Is(err, cogito.ErrNoToolSelected) {
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Println(f.LastMessage().Content)
|
|
|
|
}
|
|
}
|