summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuval Adam <_@yuv.al>2025-03-06 13:58:42 +0100
committerYuval Adam <_@yuv.al>2025-03-06 13:58:42 +0100
commit3a448eea3b01d2a4f3f0e703fae396a93fcbf243 (patch)
treec7f9fc8b10a4c6bac8ba6239adb3a8f2922ecfbd
parentceec45a6d8fad9d8750b8719b9bc3aa206a5ee9d (diff)
Use anthropic api key from config
-rw-r--r--README.md26
-rw-r--r--strangeloop/llm.py15
2 files changed, 32 insertions, 9 deletions
diff --git a/README.md b/README.md
index 07b2487..7fc1d75 100644
--- a/README.md
+++ b/README.md
@@ -32,10 +32,10 @@ uvx strangeloop process FILE_PATH [--output OUTPUT_PATH]
uvx strangeloop ask "What is recursive self-improvement in AI?"
# Configuration management
-uvx strangeloop config set api_key "your-api-key"
-uvx strangeloop config get api_key
+uvx strangeloop config set anthropic_api_key "your-api-key"
+uvx strangeloop config get anthropic_api_key
uvx strangeloop config list
-uvx strangeloop config delete api_key
+uvx strangeloop config delete anthropic_api_key
uvx strangeloop config path
```
@@ -49,11 +49,19 @@ strangeloop info
strangeloop ask "What is the meaning of life?" --max-tokens 2048 --temperature 0.8
```
-## Environment Variables
+## API Keys and Configuration
-The following environment variables are required:
+To use the Claude Sonnet 3.7 integration, you need to provide your Anthropic API key in one of these ways (in order of precedence):
-- `ANTHROPIC_API_KEY`: Your Anthropic API key for accessing Claude Sonnet 3.7
+1. Set in configuration:
+ ```bash
+ strangeloop config set anthropic_api_key "your-api-key"
+ ```
+
+2. Set as environment variable:
+ ```bash
+ export ANTHROPIC_API_KEY="your-api-key"
+ ```
## Configuration
@@ -79,3 +87,9 @@ strangeloop config delete model
# Show the configuration file path
strangeloop config path
+```
+
+### Common Configuration Options
+
+- `anthropic_api_key`: Your Anthropic API key
+- `model`: The Claude model to use (default: "claude-3-sonnet-20240229")
diff --git a/strangeloop/llm.py b/strangeloop/llm.py
index 900b01f..c3e5516 100644
--- a/strangeloop/llm.py
+++ b/strangeloop/llm.py
@@ -6,22 +6,31 @@ import os
import requests
import json
from typing import Dict, Any, Optional
+from .config import get_config
class ClaudeClient:
"""Client for interacting with Anthropic's Claude API."""
- def __init__(self, api_key: Optional[str] = None, model: str = "claude-3-sonnet-20240229"):
+ def __init__(self, api_key: Optional[str] = None, model: str = "claude-3-7-sonnet-20250219"):
"""
Initialize the Claude client.
Args:
- api_key: Anthropic API key. If None, will try to get from ANTHROPIC_API_KEY env var.
+ api_key: Anthropic API key. If None, will try to get from config, then ANTHROPIC_API_KEY env var.
model: The Claude model to use. Defaults to Claude Sonnet 3.7.
"""
+ # Try to get API key from different sources in order of priority:
+ # 1. Directly provided api_key parameter
+ # 2. Configuration file
+ # 3. Environment variable
+ if api_key is None:
+ config = get_config()
+ api_key = config.get("anthropic_api_key")
+
self.api_key = api_key or os.environ.get("ANTHROPIC_API_KEY")
if not self.api_key:
- raise ValueError("Anthropic API key must be provided or set as ANTHROPIC_API_KEY environment variable")
+ raise ValueError("Anthropic API key must be provided, set in configuration with 'config set anthropic_api_key YOUR_KEY', or set as ANTHROPIC_API_KEY environment variable")
self.model = model
self.api_url = "https://api.anthropic.com/v1/messages"