Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var Cmd = &cobra.Command{ Use: "claudeai", RunE: func(cmd *cobra.Command, args []string) error { keyb, err := os.ReadFile(cfgPath) if err != nil { return err } for { prompt := promptui.Prompt{ Label: "Ask>", AllowEdit: true, } val, err := prompt.Run() if err != nil { return err } bdy := &Body{ Model: "claude-3-5-sonnet-20241022", MaxTokens: 1024, Messages: []*Message{ { Role: "user", Content: val, }, }, } b, err := json.Marshal(bdy) if err != nil { return err } req, err := http.NewRequest( http.MethodPost, "https://api.anthropic.com/v1/messages", bytes.NewReader(b), ) if err != nil { return err } req.Header.Add("x-api-key", string(keyb)) req.Header.Add("anthropic-version", "2023-06-01") req.Header.Add("content-type", "application/json") resp, err := http.DefaultClient.Do(req) if err != nil { return err } rbdy, err := io.ReadAll(resp.Body) if resp.StatusCode != 200 { if err != nil { return err } return fmt.Errorf("received non-200 response %d. body %s", resp.StatusCode, string(rbdy)) } rs := Response{} err = json.Unmarshal(rbdy, &rs) if err != nil { return err } for _, c := range rs.Content { fmt.Println(c.Text) } log.Printf("Usage %d %d\n", rs.Usage.InputTokens, rs.Usage.OutputTokens) } }, }
Functions ¶
This section is empty.
Types ¶
type Message ¶
Click to show internal directories.
Click to hide internal directories.