-
Notifications
You must be signed in to change notification settings - Fork 0
Amir deris/configure ai agent max token and other properties #24
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
Changes from 4 commits
80c0cdb
6cb728a
4f4872b
44c7f08
f8531f8
ead7c01
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 |
|---|---|---|
|
|
@@ -4,8 +4,11 @@ import ( | |
| "context" | ||
| "fmt" | ||
| "github.com/anthropics/anthropic-sdk-go" | ||
| anthropicOption "github.com/anthropics/anthropic-sdk-go/option" | ||
| "github.com/openai/openai-go" | ||
| openaiOption "github.com/openai/openai-go/option" | ||
| "google.golang.org/genai" | ||
| "os" | ||
| ) | ||
|
|
||
| const ( | ||
|
|
@@ -20,26 +23,34 @@ const ( | |
| geminiFlash = "gemini-2.5-flash" | ||
| ) | ||
|
|
||
| const ( | ||
| defaultMaxTokens = 1024 | ||
| ) | ||
|
|
||
| type Assister interface { | ||
| GetTerminalCommand(ctx context.Context, userMessage string, systemMessage string) (string, error) | ||
| GetTerminalCommand(ctx context.Context, userMessage string) (string, error) | ||
| } | ||
|
|
||
| var _ Assister = (*OpenAIAssister)(nil) | ||
| var _ Assister = (*AnthropicAIAssister)(nil) | ||
| var _ Assister = (*GeminiAIAssister)(nil) | ||
|
|
||
| type OpenAIAssister struct { | ||
| model string | ||
| aiParameters | ||
| } | ||
|
|
||
| func (o *OpenAIAssister) GetTerminalCommand(ctx context.Context, userMessage string, systemMessage string) (string, error) { | ||
| client := openai.NewClient() | ||
| func (o *OpenAIAssister) GetTerminalCommand(ctx context.Context, userMessage string) (string, error) { | ||
| apiKey := o.aiParameters.apiKey | ||
| if apiKey == "" { | ||
| apiKey, _ = os.LookupEnv("OPENAI_API_KEY") | ||
| } | ||
| client := openai.NewClient(openaiOption.WithAPIKey(apiKey)) | ||
| chatCompletion, err := client.Chat.Completions.New(ctx, openai.ChatCompletionNewParams{ | ||
| Messages: []openai.ChatCompletionMessageParamUnion{ | ||
| openai.UserMessage(userMessage), | ||
| openai.SystemMessage(systemMessage), | ||
| openai.SystemMessage(o.aiParameters.systemPrompt), | ||
| }, | ||
| Model: o.model, | ||
| Model: o.aiParameters.model, | ||
| }) | ||
| if err != nil { | ||
| return "", err | ||
|
|
@@ -48,15 +59,23 @@ func (o *OpenAIAssister) GetTerminalCommand(ctx context.Context, userMessage str | |
| } | ||
|
|
||
| type AnthropicAIAssister struct { | ||
| model string | ||
| aiParameters | ||
| } | ||
|
|
||
| func (c *AnthropicAIAssister) GetTerminalCommand(ctx context.Context, userMessage string, systemMessage string) (string, error) { | ||
| client := anthropic.NewClient() //defaults to os.LookupEnv("ANTHROPIC_API_KEY") | ||
| func (c *AnthropicAIAssister) GetTerminalCommand(ctx context.Context, userMessage string) (string, error) { | ||
| apiKey := c.aiParameters.apiKey | ||
| if apiKey == "" { | ||
| apiKey, _ = os.LookupEnv("ANTHROPIC_API_KEY") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to set this? I wonder if the SDK would internally infer this if unset.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a great point. The SDK by default looks at those env variables. I will fix this. |
||
| } | ||
| maxTokens := c.aiParameters.maxTokens | ||
| if maxTokens == 0 { | ||
| maxTokens = defaultMaxTokens | ||
| } | ||
| client := anthropic.NewClient(anthropicOption.WithAPIKey(apiKey)) | ||
| message, err := client.Messages.New(ctx, anthropic.MessageNewParams{ | ||
| MaxTokens: 1024, | ||
| MaxTokens: maxTokens, | ||
| System: []anthropic.TextBlockParam{ | ||
| {Text: systemMessage}, | ||
| {Text: c.aiParameters.systemPrompt}, | ||
| }, | ||
| Messages: []anthropic.MessageParam{ | ||
| anthropic.NewUserMessage(anthropic.NewTextBlock(userMessage)), | ||
|
|
@@ -77,20 +96,25 @@ func (c *AnthropicAIAssister) GetTerminalCommand(ctx context.Context, userMessag | |
| } | ||
|
|
||
| type GeminiAIAssister struct { | ||
| model string | ||
| aiParameters | ||
| } | ||
|
|
||
| func (g *GeminiAIAssister) GetTerminalCommand(ctx context.Context, userMessage string, systemMessage string) (string, error) { | ||
| func (g *GeminiAIAssister) GetTerminalCommand(ctx context.Context, userMessage string) (string, error) { | ||
| apiKey := g.aiParameters.apiKey | ||
| if apiKey == "" { | ||
| apiKey, _ = os.LookupEnv("GEMINI_API_KEY") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
| client, err := genai.NewClient(ctx, &genai.ClientConfig{ | ||
| Backend: genai.BackendGeminiAPI, | ||
| APIKey: apiKey, | ||
| }) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| config := &genai.GenerateContentConfig{ | ||
| SystemInstruction: &genai.Content{ | ||
| Parts: []*genai.Part{ | ||
| {Text: systemMessage}, | ||
| {Text: g.aiParameters.systemPrompt}, | ||
| }, | ||
| }, | ||
| } | ||
|
|
@@ -106,38 +130,32 @@ func (g *GeminiAIAssister) GetTerminalCommand(ctx context.Context, userMessage s | |
| } | ||
|
|
||
| type AssisterCreator interface { | ||
| GetAssister(agent string, model string) (Assister, error) | ||
| GetAssister(parameters aiParameters) (Assister, error) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is generally a bad smell: having a exported interface with unexported types. I would either unexport the interface or export the
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Totally missed that! Thanks for the great point. |
||
| } | ||
|
|
||
| var _ AssisterCreator = (*defaultAIAssisterCreator)(nil) | ||
|
|
||
| type defaultAIAssisterCreator struct{} | ||
|
|
||
| func (d *defaultAIAssisterCreator) GetAssister(agent, model string) (Assister, error) { | ||
| func (d *defaultAIAssisterCreator) GetAssister(p aiParameters) (Assister, error) { | ||
| switch { | ||
| case agent == "" || agent == openAIAgent: | ||
| if model == "" { | ||
| model = openai.ChatModelGPT4o | ||
| case p.agent == "" || p.agent == openAIAgent: | ||
| if p.model == "" { | ||
| p.model = openai.ChatModelGPT4o | ||
| } | ||
| return &OpenAIAssister{ | ||
| model: model, | ||
| }, nil | ||
| return &OpenAIAssister{p}, nil | ||
|
|
||
| case agent == anthropicAIAgent: | ||
| if model == "" { | ||
| model = string(anthropic.ModelClaude3_5HaikuLatest) | ||
| case p.agent == anthropicAIAgent: | ||
| if p.model == "" { | ||
| p.model = string(anthropic.ModelClaude3_5HaikuLatest) | ||
| } | ||
| return &AnthropicAIAssister{ | ||
| model: model, | ||
| }, nil | ||
| case agent == geminiAIAgent: | ||
| if model == "" { | ||
| model = geminiFlashLite | ||
| return &AnthropicAIAssister{p}, nil | ||
| case p.agent == geminiAIAgent: | ||
| if p.model == "" { | ||
| p.model = geminiFlashLite | ||
| } | ||
| return &GeminiAIAssister{ | ||
| model, | ||
| }, nil | ||
| return &GeminiAIAssister{p}, nil | ||
| default: | ||
| return nil, fmt.Errorf("cannot create AI agent for %s and model %s", agent, model) | ||
| return nil, fmt.Errorf("cannot create AI agent for %s and model %s", p.agent, p.model) | ||
| } | ||
| } | ||
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.
Group this with the rest of the constants above?