|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + |
| 11 | + // Packages |
| 12 | + "github.com/djthorpe/go-tablewriter" |
| 13 | + "github.com/mutablelogic/go-client" |
| 14 | + "github.com/mutablelogic/go-client/pkg/anthropic" |
| 15 | + "github.com/mutablelogic/go-client/pkg/openai/schema" |
| 16 | +) |
| 17 | + |
| 18 | +/////////////////////////////////////////////////////////////////////////////// |
| 19 | +// GLOBALS |
| 20 | + |
| 21 | +var ( |
| 22 | + samName = "sam" |
| 23 | + samWeatherTool = schema.NewTool("get_weather", "Get the weather for a location") |
| 24 | + samSystemPrompt = "Your name is Samantha, you are a friendly AI assistant, here to help you with anything you need. Your responses should be short and to the point, and you should always be polite." |
| 25 | +) |
| 26 | + |
| 27 | +/////////////////////////////////////////////////////////////////////////////// |
| 28 | +// LIFECYCLE |
| 29 | + |
| 30 | +func samRegister(flags *Flags) { |
| 31 | + flags.Register(Cmd{ |
| 32 | + Name: samName, |
| 33 | + Description: "Interact with Samantha, a friendly AI assistant", |
| 34 | + Parse: samParse, |
| 35 | + Fn: []Fn{ |
| 36 | + {Name: "chat", Call: samChat, Description: "Chat with Sam"}, |
| 37 | + }, |
| 38 | + }) |
| 39 | +} |
| 40 | + |
| 41 | +func samParse(flags *Flags, opts ...client.ClientOpt) error { |
| 42 | + // Initialize weather |
| 43 | + if err := weatherapiParse(flags, opts...); err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + |
| 47 | + // Initialize anthropic |
| 48 | + opts = append(opts, client.OptHeader("Anthropic-Beta", "tools-2024-04-04")) |
| 49 | + if err := anthropicParse(flags, opts...); err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + // Add tool parameters |
| 54 | + if err := samWeatherTool.AddParameter("location", "The city to get the weather for", true); err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + |
| 58 | + // Return success |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +/////////////////////////////////////////////////////////////////////////////// |
| 63 | +// METHODS |
| 64 | + |
| 65 | +func samChat(ctx context.Context, w *tablewriter.Writer, _ []string) error { |
| 66 | + var toolResult bool |
| 67 | + |
| 68 | + messages := []*schema.Message{} |
| 69 | + for { |
| 70 | + if ctx.Err() != nil { |
| 71 | + return nil |
| 72 | + } |
| 73 | + |
| 74 | + // Read if there hasn't been any tool results yet |
| 75 | + if !toolResult { |
| 76 | + reader := bufio.NewReader(os.Stdin) |
| 77 | + fmt.Print("Chat: ") |
| 78 | + text, err := reader.ReadString('\n') |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + messages = append(messages, schema.NewMessage("user", schema.Text(strings.TrimSpace(text)))) |
| 83 | + } |
| 84 | + |
| 85 | + // Request -> Response |
| 86 | + responses, err := anthropicClient.Messages(ctx, messages, anthropic.OptSystem(samSystemPrompt), anthropic.OptTool(samWeatherTool)) |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + toolResult = false |
| 91 | + |
| 92 | + for _, response := range responses { |
| 93 | + switch response.Type { |
| 94 | + case "text": |
| 95 | + messages = samAppend(messages, schema.NewMessage("assistant", schema.Text(response.Text))) |
| 96 | + fmt.Println(response.Text) |
| 97 | + fmt.Println("") |
| 98 | + case "tool_use": |
| 99 | + messages = samAppend(messages, schema.NewMessage("assistant", response)) |
| 100 | + result := samCall(ctx, response) |
| 101 | + messages = samAppend(messages, schema.NewMessage("user", result)) |
| 102 | + toolResult = true |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func samCall(_ context.Context, content schema.Content) *schema.Content { |
| 109 | + if content.Type != "tool_use" { |
| 110 | + return schema.ToolResult(content.Id, fmt.Sprint("unexpected content type:", content.Type)) |
| 111 | + } |
| 112 | + switch content.Name { |
| 113 | + case samWeatherTool.Name: |
| 114 | + if location, exists := content.GetString(content.Name, "location"); exists { |
| 115 | + if weather, err := weatherapiClient.Current(location); err != nil { |
| 116 | + return schema.ToolResult(content.Id, fmt.Sprint("Unable to get the current weather, the error is ", err)) |
| 117 | + } else if data, err := json.MarshalIndent(weather, "", " "); err != nil { |
| 118 | + return schema.ToolResult(content.Id, fmt.Sprint("Unable to marshal the weather data, the error is ", err)) |
| 119 | + } else { |
| 120 | + return schema.ToolResult(content.Id, string(data)) |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + return schema.ToolResult(content.Id, fmt.Sprint("unable to call:", content.Name)) |
| 125 | +} |
| 126 | + |
| 127 | +func samAppend(messages []*schema.Message, message *schema.Message) []*schema.Message { |
| 128 | + // if the previous message was of the same role, then append the new message to the previous one |
| 129 | + if len(messages) > 0 && messages[len(messages)-1].Role == message.Role { |
| 130 | + messages[len(messages)-1].Add(message.Content) |
| 131 | + return messages |
| 132 | + } else { |
| 133 | + return append(messages, message) |
| 134 | + } |
| 135 | +} |
0 commit comments