|
| 1 | +package anthropic |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | +) |
| 10 | + |
| 11 | +// StreamMessageOptions defines the options available when streaming server-sent |
| 12 | +// events from the Anthropic REST API, the StreamMessageOptions definition is |
| 13 | +// currently identical to the CreateMessageOptions definition, but the |
| 14 | +// StreamMessageOptions type has a custom MarshalJSON implementation that will |
| 15 | +// append a stream field and set it to true. |
| 16 | +type StreamMessageOptions struct { |
| 17 | + // Temperature defines the amount of randomness injected into the response. |
| 18 | + // Note that even with a temperature of 0.0, results will not be fully |
| 19 | + // deterministic. |
| 20 | + Temperature *float64 `json:"temperature,omitempty"` |
| 21 | + // TopK is used to remove long tail low probability responses by only |
| 22 | + // sampling from the top K options for each subsequent token. |
| 23 | + // Recommended for advanced use cases only. You usually only need to use |
| 24 | + // Temperature. |
| 25 | + TopK *int `json:"top_k,omitempty"` |
| 26 | + // TopP is the nucleus-sampling parameter. Temperature or TopP should be |
| 27 | + // used, but not both. |
| 28 | + // Recommended for advanced use cases only. You usually only need to use |
| 29 | + // Temperature. |
| 30 | + TopP *float64 `json:"top_p,omitempty"` |
| 31 | + // Model defines the language model that will be used to complete the |
| 32 | + // prompt. See model.go for a list of available models. |
| 33 | + Model LanguageModel `json:"model"` |
| 34 | + // System provides a means of specifying context and instructions to the |
| 35 | + // model, such as specifying a particular goal or role. |
| 36 | + System string `json:"system,omitempty"` |
| 37 | + // Messages are the input messages, models are trained to operate on |
| 38 | + // alternating user and assistant conversational turns. When creating a new |
| 39 | + // message, prior conversational turns can be specified with this field, |
| 40 | + // and the model generates the next Message in the conversation. |
| 41 | + Messages []Message `json:"messages"` |
| 42 | + // StopSequences defines custom text sequences that will cause the model to |
| 43 | + // stop generating. If the model encounters any of the sequences, the |
| 44 | + // StopReason field will be set to "stop_sequence" and the response |
| 45 | + // StopSequence field will be set to the sequence that caused the model to |
| 46 | + // stop. |
| 47 | + StopSequences []string `json:"stop_sequences,omitempty"` |
| 48 | + // MaxTokens defines the maximum number of tokens to generate before |
| 49 | + // stopping. Token generation may stop before reaching this limit, this only |
| 50 | + // specifies the absolute maximum number of tokens to generate. Different |
| 51 | + // models have different maximum token limits. |
| 52 | + MaxTokens int `json:"max_tokens"` |
| 53 | +} |
| 54 | + |
| 55 | +// MarshalJSON implements the json.Marshaler interface for StreamMessageOptions. |
| 56 | +// When StreamMessageOptions is marshalled to JSON, a stream field will be added |
| 57 | +// and set to a boolean value of true. |
| 58 | +func (c *StreamMessageOptions) MarshalJSON() ([]byte, error) { |
| 59 | + return json.Marshal(struct { |
| 60 | + Temperature *float64 `json:"temperature,omitempty"` |
| 61 | + TopK *int `json:"top_k,omitempty"` |
| 62 | + TopP *float64 `json:"top_p,omitempty"` |
| 63 | + Model LanguageModel `json:"model"` |
| 64 | + System string `json:"system,omitempty"` |
| 65 | + Messages []Message `json:"messages"` |
| 66 | + StopSequences []string `json:"stop_sequences,omitempty"` |
| 67 | + MaxTokens int `json:"max_tokens"` |
| 68 | + Stream bool `json:"stream"` |
| 69 | + }{ |
| 70 | + Temperature: c.Temperature, |
| 71 | + TopK: c.TopK, |
| 72 | + TopP: c.TopP, |
| 73 | + Model: c.Model, |
| 74 | + System: c.System, |
| 75 | + Messages: c.Messages, |
| 76 | + StopSequences: c.StopSequences, |
| 77 | + MaxTokens: c.MaxTokens, |
| 78 | + Stream: true, |
| 79 | + }) |
| 80 | +} |
| 81 | + |
| 82 | +// ServerSentEvent defines a server-sent event. |
| 83 | +type ServerSentEvent struct { |
| 84 | + Event *string |
| 85 | + Data string |
| 86 | + Raw []string |
| 87 | +} |
| 88 | + |
| 89 | +// Stream creates a new message using the provided options and streams the |
| 90 | +// response using server-sent events. This is a convenience method that |
| 91 | +// combines the Create and Stream methods. |
| 92 | +func (c *MessagesService) Stream( |
| 93 | + ctx context.Context, |
| 94 | + opts *StreamMessageOptions, |
| 95 | +) (*<-chan ServerSentEvent, *http.Response, error) { |
| 96 | + req, err := c.client.NewRequest(http.MethodPost, "messages", opts) |
| 97 | + if err != nil { |
| 98 | + return nil, nil, err |
| 99 | + } |
| 100 | + |
| 101 | + resp, err := c.client.Do(ctx, req, nil) |
| 102 | + if err != nil { |
| 103 | + return nil, resp, err |
| 104 | + } |
| 105 | + //goland:noinspection GoUnhandledErrorResult |
| 106 | + defer resp.Body.Close() |
| 107 | + |
| 108 | + output, err := newServerSentEventStream(resp.Body) |
| 109 | + |
| 110 | + return output, resp, err |
| 111 | +} |
| 112 | + |
| 113 | +func newServerSentEventStream(body io.ReadCloser) (*<-chan ServerSentEvent, error) { |
| 114 | + scanner := bufio.NewScanner(body) |
| 115 | + scanner.Buffer(make([]byte, 4096), bufio.MaxScanTokenSize) |
| 116 | + scanner.Split(func(data []byte, atEOF bool) (int, []byte, error) { |
| 117 | + return 0, nil, nil |
| 118 | + }) |
| 119 | + |
| 120 | + // TODO |
| 121 | + |
| 122 | + return new(<-chan ServerSentEvent), nil |
| 123 | +} |
0 commit comments