-
Notifications
You must be signed in to change notification settings - Fork 25
✨ #172 Add Anthropic integration for chat streaming #182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,15 +2,102 @@ package anthropic | |||||
|
||||||
import ( | ||||||
"context" | ||||||
"bytes" | ||||||
"context" | ||||||
"encoding/json" | ||||||
"fmt" | ||||||
"io" | ||||||
"net/http" | ||||||
|
||||||
"glide/pkg/api/schemas" | ||||||
"glide/pkg/providers/clients" | ||||||
) | ||||||
|
||||||
type Client struct { | ||||||
APIKey string | ||||||
} | ||||||
|
||||||
func (c *Client) SupportChatStream() bool { | ||||||
return false | ||||||
return true | ||||||
} | ||||||
|
||||||
func (c *Client) ChatStream(ctx context.Context, chatReq *schemas.ChatRequest) (clients.ChatStream, error) { | ||||||
|
||||||
apiURL := "https://api.anthropic.com/v1/complete" | ||||||
|
||||||
requestBody := map[string]interface{}{ | ||||||
|
||||||
"model": "claude-2", | ||||||
"prompt": chatReq.Message, // Assuming chatReq.Message contains the user's message | ||||||
"max_tokens_to_sample": 256, | ||||||
"stream": true, | ||||||
} | ||||||
requestJSON, err := json.Marshal(requestBody) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
||||||
httpReq, err := http.NewRequestWithContext(ctx, "POST", apiURL, bytes.NewBuffer(requestJSON)) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
httpReq.Header.Set("Content-Type", "application/json") | ||||||
httpReq.Header.Set("anthropic-version", "2023-06-01") | ||||||
httpReq.Header.Set("x-api-key", c.APIKey) | ||||||
|
||||||
resp, err := http.DefaultClient.Do(httpReq) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
defer resp.Body.Close() | ||||||
|
||||||
if resp.StatusCode != http.StatusOK { | ||||||
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) | ||||||
} | ||||||
|
||||||
chatStream := &AnthropicChatStream{ | ||||||
responseBody: resp.Body, | ||||||
} | ||||||
|
||||||
return chatStream, nil | ||||||
} | ||||||
|
||||||
type AnthropicChatStream struct { | ||||||
responseBody io.ReadCloser | ||||||
} | ||||||
|
||||||
func (s *AnthropicChatStream) Receive() (string, error) { | ||||||
|
func (s *AnthropicChatStream) Receive() (string, error) { | |
func (s *AnthropicChatStream) Recv() (string, error) { |
It also should return (*schemas.ChatStreamChunk, error)
but I think you will get to that:
https://github.com/EinStack/glide/blob/develop/pkg/providers/clients/stream.go#L9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There has to be ChatStream()
method that would create an instance of AnthropicChatStream in this case.
For example, in OpenAI case, it looked this way:
https://github.com/EinStack/glide/blob/develop/pkg/providers/openai/chat_stream.go#L162-L177
Without that there is nothing that would use the AnthropicChatStream struct
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Golang uniquely, compared to other programming languages, allows struct/object methods can be separate across go files in the module. For example, in case of Anthropic client is spread across these three files:
Specifically, the client is defined in https://github.com/EinStack/glide/blob/develop/pkg/providers/anthropic/client.go#L22
and it has already pulling a lot of useful values like API key (e.g. c.config.APIKey) and a bunch of other configurations. So these three files are really worth checking and reuse that existing client.