Skip to content

support anthropicBaseURL , openaiAPIKey, anthropicAPIKey cli params #6

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

Merged
merged 6 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@
aidocs/
.mcp.json
*.log
mcphost
.idea
test/
build/
scripts/
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,15 @@ mcphost -m openai:gpt-4
```

### Flags
- `--anthropic-url string`: Base URL for Anthropic API (defaults to api.anthropic.com)
- `--anthropic-api-key string`: Anthropic API key (can also be set via ANTHROPIC_API_KEY environment variable)
- `--config string`: Config file location (default is $HOME/mcp.json)
- `--debug`: Enable debug logging
- `--message-window int`: Number of messages to keep in context (default: 10)
- `-m, --model string`: Model to use (format: provider:model) (default "anthropic:claude-3-5-sonnet-latest")
- `--openai-url string`: Base URL for OpenAI API (defaults to api.openai.com)
- `--openai-api-key string`: OpenAI API key (can also be set via OPENAI_API_KEY environment variable)


### Interactive Commands

Expand Down Expand Up @@ -154,4 +158,4 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file

- Thanks to the Anthropic team for Claude and the MCP specification
- Thanks to the Ollama team for their local LLM runtime
- Thanks to all contributors who have helped improve this tool
- Thanks to all contributors who have helped improve this tool
39 changes: 27 additions & 12 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ import (
)

var (
renderer *glamour.TermRenderer
configFile string
messageWindow int
modelFlag string // New flag for model selection
openaiBaseURL string // Base URL for OpenAI API
renderer *glamour.TermRenderer
configFile string
messageWindow int
modelFlag string // New flag for model selection
openaiBaseURL string // Base URL for OpenAI API
anthropicBaseURL string // Base URL for Anthropic API
openaiAPIKey string
anthropicAPIKey string
)

const (
Expand Down Expand Up @@ -79,8 +82,12 @@ func init() {
// Add debug flag
rootCmd.PersistentFlags().
BoolVar(&debugMode, "debug", false, "enable debug logging")
rootCmd.PersistentFlags().
StringVar(&openaiBaseURL, "openai-url", "", "base URL for OpenAI API (defaults to api.openai.com)")

flags := rootCmd.PersistentFlags()
flags.StringVar(&openaiBaseURL, "openai-url", "", "base URL for OpenAI API (defaults to api.openai.com)")
flags.StringVar(&anthropicBaseURL, "anthropic-url", "", "base URL for Anthropic API (defaults to api.anthropic.com)")
flags.StringVar(&openaiAPIKey, "openai-api-key", "", "OpenAI API key")
flags.StringVar(&anthropicAPIKey, "anthropic-api-key", "", "Anthropic API key")
}

// Add new function to create provider
Expand All @@ -98,22 +105,30 @@ func createProvider(modelString string) (llm.Provider, error) {

switch provider {
case "anthropic":
apiKey := os.Getenv("ANTHROPIC_API_KEY")
apiKey := anthropicAPIKey
if apiKey == "" {
apiKey = os.Getenv("ANTHROPIC_API_KEY")
}

if apiKey == "" {
return nil, fmt.Errorf(
"ANTHROPIC_API_KEY environment variable not set",
"Anthropic API key not provided. Use --anthropic-api-key flag or ANTHROPIC_API_KEY environment variable",
)
}
return anthropic.NewProvider(apiKey), nil
return anthropic.NewProvider(apiKey, anthropicBaseURL, model), nil

case "ollama":
return ollama.NewProvider(model)

case "openai":
apiKey := os.Getenv("OPENAI_API_KEY")
apiKey := openaiAPIKey
if apiKey == "" {
apiKey = os.Getenv("OPENAI_API_KEY")
}

if apiKey == "" {
return nil, fmt.Errorf(
"OPENAI_API_KEY environment variable not set",
"OpenAI API key not provided. Use --openai-api-key flag or OPENAI_API_KEY environment variable",
)
}
return openai.NewProvider(apiKey, openaiBaseURL, model), nil
Expand Down
20 changes: 14 additions & 6 deletions pkg/llm/anthropic/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,25 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
)

type Client struct {
apiKey string
client *http.Client
apiKey string
client *http.Client
baseURL string
}

func NewClient(apiKey string) *Client {
func NewClient(apiKey string, baseURL string) *Client {
if baseURL == "" {
baseURL = "https://api.anthropic.com/v1"
} else if !strings.HasSuffix(baseURL, "/v1") {
baseURL = strings.TrimSuffix(baseURL, "/") + "/v1"
}
return &Client{
apiKey: apiKey,
client: &http.Client{},
apiKey: apiKey,
baseURL: baseURL,
client: &http.Client{},
}
}

Expand All @@ -26,7 +34,7 @@ func (c *Client) CreateMessage(ctx context.Context, req CreateRequest) (*APIMess
return nil, fmt.Errorf("error marshaling request: %w", err)
}

httpReq, err := http.NewRequestWithContext(ctx, "POST", "https://api.anthropic.com/v1/messages", bytes.NewReader(body))
httpReq, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/messages", c.baseURL), bytes.NewReader(body))
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}
Expand Down
9 changes: 6 additions & 3 deletions pkg/llm/anthropic/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ type Provider struct {
model string
}

func NewProvider(apiKey string) *Provider {
func NewProvider(apiKey string, baseURL string, model string) *Provider {
if model == "" {
model = "claude-3-5-sonnet-20240620" // 默认模型
}
return &Provider{
client: NewClient(apiKey),
model: "claude-3-5-sonnet-20240620",
client: NewClient(apiKey, baseURL),
model: model,
}
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/llm/openai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
)

type Client struct {
Expand All @@ -17,6 +18,8 @@ type Client struct {
func NewClient(apiKey string, baseURL string) *Client {
if baseURL == "" {
baseURL = "https://api.openai.com/v1"
} else if !strings.HasSuffix(baseURL, "/v1") {
baseURL = strings.TrimSuffix(baseURL, "/") + "/v1"
}
return &Client{
apiKey: apiKey,
Expand Down