Skip to content

Commit

Permalink
rename a bunch of stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
dleviminzi committed Mar 7, 2024
1 parent 00799e7 commit abb4c3b
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 38 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
30 changes: 15 additions & 15 deletions complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -40,46 +40,46 @@ 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
}

// 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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
}
26 changes: 13 additions & 13 deletions complete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

func TestComplete(t *testing.T) {
ctx := context.Background()
mockRes := &CompleteResponse{
mockRes := &CompletionResponse{
Completion: "test",
StopReason: "test",
Model: "test",
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion examples/completion/basic_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions examples/stream/stream_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -64,5 +64,4 @@ func main() {
fmt.Printf("%s", event.Data.Completion)
}
}

}
5 changes: 2 additions & 3 deletions messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit abb4c3b

Please sign in to comment.