diff --git a/README.md b/README.md index b78f335..854001f 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ func main() { userPrompt, _ := reader.ReadString('\n') userPrompt = strings.TrimSuffix(userPrompt, "\n") - resp, err := c.Message(context.Background(), anthrogo.MessageRequest{ + resp, err := c.MessageRequest(context.Background(), anthrogo.MessagePayload{ Model: anthrogo.ModelClaude3Opus, Messages: []anthrogo.Message{{ Role: anthrogo.RoleTypeUser, @@ -70,7 +70,7 @@ func main() { conversation.AddMessage(anthrogo.RoleHuman, userPrompt) // Set up the payload and send completion stream request - resp, err := c.Complete(&anthrogo.CompletePayload{ + resp, err := c.CompletionRequest(context.Background(), anthrogo.CompletionPayload{ MaxTokensToSample: 256, Model: anthrogo.ModelClaude2, Prompt: conversation.GeneratePrompt(), @@ -114,7 +114,7 @@ func main() { conversation.AddMessage(anthrogo.RoleHuman, userPrompt) // Set up the payload and send completion stream request - completeStreamResp, err := c.CompleteStream(&anthrogo.CompletePayload{ + completeStreamResp, _ := c.StreamingCompletionRequest(context.Background(), anthrogo.CompletionPayload{ MaxTokensToSample: 256, Model: anthrogo.ModelClaude2, Prompt: conversation.GeneratePrompt(), diff --git a/complete.go b/complete.go index fbc3e27..fec1995 100644 --- a/complete.go +++ b/complete.go @@ -22,8 +22,8 @@ type CompletionMessage struct { Content string } -// CompletePayload contains the necessary data for the completion request. -type CompletePayload struct { +// CompletionPayload contains the necessary data for the completion request. +type CompletionPayload struct { MaxTokensToSample int `json:"max_tokens_to_sample"` Model AnthropicModel `json:"model"` Prompt string `json:"prompt"` @@ -40,16 +40,16 @@ type CompleteOptions struct { TopP float64 `json:"top_p,omitempty"` } -// CompleteResponse contains the completion result or error details. -type CompleteResponse struct { +// CompletionResponse contains the completion result or error details. +type CompletionResponse struct { Completion string `json:"completion"` StopReason string `json:"stop_reason"` Model string `json:"model"` } -// CompleteStreamResponse contains the server sent events decoder, the response body from the request, and a +// StreamingCompletionResponse contains the server sent events decoder, the response body from the request, and a // cancel function to enforce a timeout. -type CompleteStreamResponse struct { +type StreamingCompletionResponse struct { decoder *SSEDecoder body io.ReadCloser cancel context.CancelFunc @@ -57,29 +57,29 @@ type CompleteStreamResponse struct { // Decode is a method for CompleteStreamResponse that returns the next event // from the server-sent events decoder, or an error if one occurred. -func (c CompleteStreamResponse) Decode() (*Event, error) { +func (c StreamingCompletionResponse) Decode() (*Event, error) { return c.decoder.Decode() } // Cancel is a method for CompleteStreamResponse that invokes the associated // cancel function to stop the request prematurely. -func (c CompleteStreamResponse) Cancel() { +func (c StreamingCompletionResponse) Cancel() { c.cancel() } // Close is a method for CompleteStreamResponse that closes the response body. // If the response body has been read, Close returns nil. Otherwise, it returns // an error. -func (c CompleteStreamResponse) Close() error { +func (c StreamingCompletionResponse) Close() error { return c.body.Close() } -// Complete sends a complete request to the server and returns the response or error. -func (c *Client) Complete(ctx context.Context, payload CompletePayload) (CompleteResponse, error) { +// CompletionRequest sends a complete request to the server and returns the response or error. +func (c *Client) CompletionRequest(ctx context.Context, payload CompletionPayload) (CompletionResponse, error) { // force stream off if user uses this method payload.Stream = false - var resp CompleteResponse + var resp CompletionResponse req, cancel, err := c.createRequest(ctx, payload, RequestTypeComplete) if err != nil { return resp, err @@ -114,11 +114,11 @@ func (c *Client) Complete(ctx context.Context, payload CompletePayload) (Complet return resp, nil } -// CompleteStream is a method for Client that sends a request to the server with +// StreamingCompletionRequest is a method for Client that sends a request to the server with // streaming enabled. It marshals the payload into a JSON object and sends it // to the server in a POST request. If the request is successful, it returns a // pointer to a CompleteStreamResponse object. Otherwise, it returns an error. -func (c *Client) CompleteStream(ctx context.Context, payload CompletePayload) (*CompleteStreamResponse, error) { +func (c *Client) StreamingCompletionRequest(ctx context.Context, payload CompletionPayload) (*StreamingCompletionResponse, error) { // force stream to true if user calls this method payload.Stream = true @@ -146,5 +146,5 @@ func (c *Client) CompleteStream(ctx context.Context, payload CompletePayload) (* return nil, fmt.Errorf("%s: %s", errorResponse.Error.Type, errorResponse.Error.Message) } - return &CompleteStreamResponse{NewSSEDecoder(res.Body), res.Body, cancel}, nil + return &StreamingCompletionResponse{NewSSEDecoder(res.Body), res.Body, cancel}, nil } diff --git a/complete_test.go b/complete_test.go index d887b6a..728e8ed 100644 --- a/complete_test.go +++ b/complete_test.go @@ -19,7 +19,7 @@ import ( func TestComplete(t *testing.T) { ctx := context.Background() - mockRes := &CompleteResponse{ + mockRes := &CompletionResponse{ Completion: "test", StopReason: "test", Model: "test", @@ -38,13 +38,13 @@ func TestComplete(t *testing.T) { client.HttpClient = mockHTTPClient - payload := CompletePayload{ + payload := CompletionPayload{ MaxTokensToSample: 10, Model: "test", Prompt: "test", } - response, err := client.Complete(ctx, payload) + response, err := client.CompletionRequest(ctx, payload) assert.NoError(t, err) assert.NotNil(t, response) @@ -66,9 +66,9 @@ func TestComplete_HttpClientError(t *testing.T) { client.HttpClient = mockHTTPClient - payload := CompletePayload{} + payload := CompletionPayload{} - response, err := client.Complete(ctx, payload) + response, err := client.CompletionRequest(ctx, payload) assert.Empty(t, response) assert.Error(t, err) @@ -89,9 +89,9 @@ func TestComplete_Non200StatusCode(t *testing.T) { client.HttpClient = mockHTTPClient - payload := CompletePayload{} + payload := CompletionPayload{} - _, err = client.Complete(ctx, payload) + _, err = client.CompletionRequest(ctx, payload) assert.NotNil(t, err) @@ -111,9 +111,9 @@ func TestComplete_UnmarshalError(t *testing.T) { client.HttpClient = mockHTTPClient - payload := CompletePayload{} + payload := CompletionPayload{} - response, err := client.Complete(ctx, payload) + response, err := client.CompletionRequest(ctx, payload) assert.Empty(t, response) assert.Error(t, err) @@ -137,9 +137,9 @@ func TestCompleteStream(t *testing.T) { client.HttpClient = mockHTTPClient - payload := CompletePayload{} + payload := CompletionPayload{} - response, err := client.CompleteStream(ctx, payload) + response, err := client.StreamingCompletionRequest(ctx, payload) defer response.Close() assert.NoError(t, err) @@ -161,9 +161,9 @@ func TestCompleteStream_HttpClientError(t *testing.T) { client.HttpClient = mockHTTPClient - payload := CompletePayload{} + payload := CompletionPayload{} - response, err := client.CompleteStream(context.Background(), payload) + response, err := client.StreamingCompletionRequest(context.Background(), payload) assert.Nil(t, response) assert.Error(t, err) diff --git a/examples/completion/basic_example.go b/examples/completion/basic_example.go index 170be49..57ae17f 100644 --- a/examples/completion/basic_example.go +++ b/examples/completion/basic_example.go @@ -29,7 +29,7 @@ func main() { conversation.AddMessage(anthrogo.RoleHuman, userPrompt) // Set up the payload and send completion stream request - resp, err := c.Complete(context.Background(), anthrogo.CompletePayload{ + resp, err := c.CompletionRequest(context.Background(), anthrogo.CompletionPayload{ MaxTokensToSample: 256, Model: anthrogo.ModelClaude2, Prompt: conversation.GeneratePrompt(), diff --git a/examples/messages/main.go b/examples/messages/basic_message.go similarity index 91% rename from examples/messages/main.go rename to examples/messages/basic_message.go index f87b5a9..9e0b054 100644 --- a/examples/messages/main.go +++ b/examples/messages/basic_message.go @@ -26,7 +26,7 @@ func main() { userPrompt, _ := reader.ReadString('\n') userPrompt = strings.TrimSuffix(userPrompt, "\n") - resp, err := c.Message(context.Background(), anthrogo.MessageRequest{ + resp, err := c.MessageRequest(context.Background(), anthrogo.MessagePayload{ Model: anthrogo.ModelClaude3Opus, Messages: []anthrogo.Message{{ Role: anthrogo.RoleTypeUser, diff --git a/examples/stream/stream_example.go b/examples/stream/stream_example.go index f427f01..1b76dca 100644 --- a/examples/stream/stream_example.go +++ b/examples/stream/stream_example.go @@ -32,7 +32,7 @@ func main() { conversation.AddMessage(anthrogo.RoleHuman, userPrompt) // Set up the payload and send completion stream request - completeStreamResp, _ := c.CompleteStream(context.Background(), anthrogo.CompletePayload{ + completeStreamResp, _ := c.StreamingCompletionRequest(context.Background(), anthrogo.CompletionPayload{ MaxTokensToSample: 256, Model: anthrogo.ModelClaude2, Prompt: conversation.GeneratePrompt(), @@ -64,5 +64,4 @@ func main() { fmt.Printf("%s", event.Data.Completion) } } - } diff --git a/messages.go b/messages.go index 11f5df3..86826a6 100644 --- a/messages.go +++ b/messages.go @@ -18,7 +18,7 @@ const ( RoleTypeAssistant RoleType = "assistant" ) -type MessageRequest struct { +type MessagePayload struct { Model AnthropicModel `json:"model"` Messages []Message `json:"messages"` MaxTokens int `json:"max_tokens"` @@ -73,8 +73,7 @@ type Usage struct { OutputTokens int `json:"output_tokens"` } -func (c *Client) Message(ctx context.Context, payload MessageRequest) (MessageResponse, error) { - +func (c *Client) MessageRequest(ctx context.Context, payload MessagePayload) (MessageResponse, error) { var resp MessageResponse req, cancel, err := c.createRequest(ctx, payload, RequestTypeMessages)