From 920f59e630502fda601a3e8fba9e9fcc3a93ba60 Mon Sep 17 00:00:00 2001 From: alejandro-angulo Date: Tue, 3 Jun 2025 22:05:37 -0700 Subject: [PATCH] Add conversation loop --- go.mod | 12 +++++++++ go.sum | 12 +++++++++ main.go | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b51242c --- /dev/null +++ b/go.mod @@ -0,0 +1,12 @@ +module agent + +go 1.24.2 + +require github.com/anthropics/anthropic-sdk-go v1.3.0 + +require ( + github.com/tidwall/gjson v1.14.4 // indirect + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.1 // indirect + github.com/tidwall/sjson v1.2.5 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..3a7142b --- /dev/null +++ b/go.sum @@ -0,0 +1,12 @@ +github.com/anthropics/anthropic-sdk-go v1.3.0 h1:KroW4oDT3KzFT71d3bnu4DxLFAEPvY+d1c6z2CrOz/s= +github.com/anthropics/anthropic-sdk-go v1.3.0/go.mod h1:AapDW22irxK2PSumZiQXYUFvsdQgkwIWlpESweWZI/c= +github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= +github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= +github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY= +github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28= diff --git a/main.go b/main.go new file mode 100644 index 0000000..b1443f4 --- /dev/null +++ b/main.go @@ -0,0 +1,81 @@ +package main + +import ( + "bufio" + "context" + "fmt" + "os" + + "github.com/anthropics/anthropic-sdk-go" +) + +func main() { + client := anthropic.NewClient() + + scanner := bufio.NewScanner(os.Stdin) + getUserMessage := func() (string, bool) { + if !scanner.Scan() { + return "", false + } + return scanner.Text(), true + } + + agent := NewAgent(&client, getUserMessage) + err := agent.Run(context.TODO()) + if err != nil { + fmt.Printf("Error: %s\n", err.Error()) + } +} + +func NewAgent(client *anthropic.Client, getUserMessage func() (string, bool)) *Agent { + return &Agent{ + client: client, + getUserMessage: getUserMessage, + } +} + +type Agent struct { + client *anthropic.Client + getUserMessage func() (string, bool) +} + +func (a *Agent) Run(ctx context.Context) error { + conversation := []anthropic.MessageParam{} + + fmt.Println("Chat with Claude (use 'ctrl-c' to quit)") + + for { + fmt.Print("\u001b[94mYou\u001b[0m: ") + userInput, ok := a.getUserMessage() + if !ok { + break + } + + userMessage := anthropic.NewUserMessage(anthropic.NewTextBlock(userInput)) + conversation = append(conversation, userMessage) + + message, err := a.runInference(ctx, conversation) + if err != nil { + return err + } + conversation = append(conversation, message.ToParam()) + + for _, content := range message.Content { + switch content.Type { + case "text": + fmt.Printf("\u001b[93mClaude\u001b[0m: %s\n", content.Text) + } + } + } + + return nil +} + +func (a *Agent) runInference(ctx context.Context, conversation []anthropic.MessageParam) (*anthropic.Message, error) { + message, err := a.client.Messages.New(ctx, anthropic.MessageNewParams{ + Model: anthropic.ModelClaude3_7SonnetLatest, + MaxTokens: int64(1024), + Messages: conversation, + }) + return message, err +}