Skip to content

Commit 49a1af0

Browse files
committed
fix: fixed openai-compatible tests
1 parent 8252cfc commit 49a1af0

8 files changed

Lines changed: 127 additions & 66 deletions

File tree

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,31 @@ For all providers:
6161
pip install axm-agent[all]
6262
```
6363

64+
## 🔑 Configuration
65+
66+
AXM Agent uses environment variables for API credentials:
67+
68+
### OpenAI
69+
```bash
70+
export AXM_OPENAI_API_KEY="sk-..."
71+
```
72+
73+
### Anthropic (Claude)
74+
```bash
75+
export AXM_ANTHROPIC_API_KEY="sk-ant-..."
76+
```
77+
78+
### OpenAI-Compatible Providers (DeepSeek, local LLMs, etc.)
79+
```bash
80+
export AXM_OPENAI_COMPATIBLE_API_KEY="your-api-key"
81+
export AXM_OPENAI_COMPATIBLE_BASE_URL="https://your-endpoint.com/v1"
82+
```
83+
84+
You can also pass credentials directly when creating agents:
85+
```python
86+
agent = Agent("gpt-4", api_key="sk-...", base_url="https://custom-endpoint.com/v1")
87+
```
88+
6489
## 🎓 Quick Start
6590

6691
### Basic Agent

axm/llm/anthropic.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Anthropic Claude LLM provider"""
22

33
import json
4+
import os
45
from typing import Any, AsyncIterator, Dict, Iterator, List, Optional, Type
56

67
from pydantic import BaseModel
@@ -18,9 +19,20 @@
1819

1920

2021
class AnthropicProvider(LLMProvider):
21-
"""Anthropic Claude LLM provider"""
22+
"""Anthropic Claude LLM provider
23+
24+
Reads AXM_ANTHROPIC_API_KEY from environment if api_key not provided.
25+
"""
2226

2327
def __init__(self, api_key: Optional[str] = None, base_url: Optional[str] = None):
28+
"""
29+
Initialize Anthropic provider.
30+
31+
Args:
32+
api_key: Anthropic API key (default: $AXM_ANTHROPIC_API_KEY)
33+
base_url: Optional base URL for API
34+
"""
35+
api_key = api_key or os.environ.get("AXM_ANTHROPIC_API_KEY")
2436
self.client = Anthropic(api_key=api_key, base_url=base_url)
2537
self.async_client = AsyncAnthropic(api_key=api_key, base_url=base_url)
2638

axm/llm/openai.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""OpenAI LLM provider"""
22

3+
import os
34
from typing import Any, AsyncIterator, Dict, Iterator, List, Optional, Type
45

56
from pydantic import BaseModel
@@ -17,9 +18,20 @@
1718

1819

1920
class OpenAIProvider(LLMProvider):
20-
"""OpenAI LLM provider (GPT-4, GPT-3.5, etc.)"""
21+
"""OpenAI LLM provider (GPT-4, GPT-3.5, etc.)
22+
23+
Reads AXM_OPENAI_API_KEY from environment if api_key not provided.
24+
"""
2125

2226
def __init__(self, api_key: Optional[str] = None, base_url: Optional[str] = None):
27+
"""
28+
Initialize OpenAI provider.
29+
30+
Args:
31+
api_key: OpenAI API key (default: $AXM_OPENAI_API_KEY)
32+
base_url: Optional base URL for API
33+
"""
34+
api_key = api_key or os.environ.get("AXM_OPENAI_API_KEY")
2335
self.client = OpenAI(api_key=api_key, base_url=base_url)
2436
self.async_client = AsyncOpenAI(api_key=api_key, base_url=base_url)
2537

axm/llm/openai_compatible.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ class OpenAICompatibleProvider(LLMProvider):
4545
"""OpenAI-compatible LLM provider using requests library
4646
4747
Works with any API that follows the OpenAI chat completions format.
48-
Automatically reads environment variables in this order:
49-
- API Key: OPENAI_COMPATIBLE_API_KEY -> OPENAI_API_KEY
50-
- Base URL: OPENAI_COMPATIBLE_BASE_URL -> OPENAI_BASE_URL -> https://api.openai.com/v1
48+
Reads AXM_OPENAI_COMPATIBLE_API_KEY and AXM_OPENAI_COMPATIBLE_BASE_URL from environment.
5149
"""
5250

5351
def __init__(
@@ -60,24 +58,15 @@ def __init__(
6058
Initialize the OpenAI-compatible provider.
6159
6260
Args:
63-
api_key: API key for authentication (default: $OPENAI_COMPATIBLE_API_KEY, then $OPENAI_API_KEY)
64-
base_url: Base URL for the API (default: $OPENAI_COMPATIBLE_BASE_URL, then $OPENAI_BASE_URL, then https://api.openai.com/v1)
61+
api_key: API key for authentication (default: $AXM_OPENAI_COMPATIBLE_API_KEY)
62+
base_url: Base URL for the API (default: $AXM_OPENAI_COMPATIBLE_BASE_URL)
6563
timeout: Request timeout in seconds
6664
"""
67-
# Try OPENAI_COMPATIBLE_API_KEY first, fall back to OPENAI_API_KEY
68-
self.api_key = (
69-
api_key
70-
or os.environ.get("OPENAI_COMPATIBLE_API_KEY")
71-
or os.environ.get("OPENAI_API_KEY", "")
65+
self.api_key = api_key or os.environ.get("AXM_OPENAI_COMPATIBLE_API_KEY", "")
66+
self.base_url = (base_url or os.environ.get("AXM_OPENAI_COMPATIBLE_BASE_URL", "")).rstrip(
67+
"/"
7268
)
7369

74-
# Try OPENAI_COMPATIBLE_BASE_URL first, then OPENAI_BASE_URL, finally default
75-
default_base_url = (
76-
os.environ.get("OPENAI_COMPATIBLE_BASE_URL")
77-
or os.environ.get("OPENAI_BASE_URL")
78-
or "https://api.openai.com/v1"
79-
)
80-
self.base_url = (base_url or default_base_url).rstrip("/")
8170
self.timeout = timeout
8271

8372
def _get_headers(self) -> Dict[str, str]:

docs/getting_started.md

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,50 @@ pip install axm-agent[anthropic]
2121
pip install axm-agent[all]
2222
```
2323

24-
## Basic Setup
24+
## Configuration
25+
26+
### Environment Variables
2527

26-
### 1. Set up your API keys
28+
AXM Agent uses dedicated environment variables for each provider:
29+
30+
**OpenAI:**
31+
```bash
32+
export AXM_OPENAI_API_KEY="sk-..."
33+
```
2734

28-
Create a `.env` file in your project root:
35+
**Anthropic (Claude):**
36+
```bash
37+
export AXM_ANTHROPIC_API_KEY="sk-ant-..."
38+
```
2939

40+
**OpenAI-Compatible Providers (DeepSeek, Groq, local LLMs, etc.):**
3041
```bash
31-
OPENAI_API_KEY=your_openai_api_key
32-
ANTHROPIC_API_KEY=your_anthropic_api_key
42+
export AXM_OPENAI_COMPATIBLE_API_KEY="your-api-key"
43+
export AXM_OPENAI_COMPATIBLE_BASE_URL="https://your-endpoint.com/v1"
3344
```
3445

35-
Or set them in your code:
46+
### Passing Credentials Directly
47+
48+
You can also pass credentials directly when creating agents:
3649

3750
```python
38-
import os
39-
os.environ["OPENAI_API_KEY"] = "your_key_here"
51+
from axm import Agent
52+
53+
# OpenAI
54+
agent = Agent("gpt-4", api_key="sk-...")
55+
56+
# OpenAI-compatible endpoint
57+
agent = Agent(
58+
"deepseek-v3",
59+
api_key="your-api-key",
60+
base_url="https://ark.cn-beijing.volces.com/api/v3"
61+
)
62+
63+
# Anthropic
64+
agent = Agent("claude-3-opus-20240229", api_key="sk-ant-...")
4065
```
4166

42-
### 2. Create your first agent
67+
## Basic Setup
4368

4469
```python
4570
from axm import Agent
@@ -276,7 +301,9 @@ Check out the `examples/` directory for complete examples:
276301
```python
277302
# Check if key is set
278303
import os
279-
print(os.getenv("OPENAI_API_KEY"))
304+
print(os.getenv("AXM_OPENAI_API_KEY"))
305+
print(os.getenv("AXM_ANTHROPIC_API_KEY"))
306+
print(os.getenv("AXM_OPENAI_COMPATIBLE_API_KEY"))
280307

281308
# Or pass directly
282309
agent = Agent("gpt-4", api_key="your-key")

examples/openai_compatible_example.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,24 @@
33
This provider works with any API that follows the OpenAI chat completions format,
44
including local LLM servers, custom proxies, or alternative cloud providers.
55
6-
Environment Variable Priority:
7-
1. Direct parameters (api_key, base_url) - highest priority
8-
2. OPENAI_COMPATIBLE_API_KEY and OPENAI_COMPATIBLE_BASE_URL
9-
3. OPENAI_API_KEY and OPENAI_BASE_URL - fallback
10-
4. Default: https://api.openai.com/v1
6+
Environment Variables:
7+
- AXM_OPENAI_COMPATIBLE_API_KEY: API key for authentication
8+
- AXM_OPENAI_COMPATIBLE_BASE_URL: Base URL for the API endpoint
9+
10+
Examples:
11+
export AXM_OPENAI_COMPATIBLE_BASE_URL="https://your-endpoint.com/v1"
12+
export AXM_OPENAI_COMPATIBLE_API_KEY="your-api-key"
1113
"""
1214

1315
from axm.core.agent import Agent
1416
from axm.core.multi_agent import MultiAgent
1517
from axm.llm.openai_compatible import OpenAICompatibleProvider
1618

17-
# Example 1: Use OPENAI_COMPATIBLE_* environment variables (RECOMMENDED)
19+
# Example 1: Use environment variables (RECOMMENDED)
1820
# Set these in your shell or .env file:
19-
# export OPENAI_COMPATIBLE_BASE_URL="https://your-endpoint.com/v1"
20-
# export OPENAI_COMPATIBLE_API_KEY="your-api-key"
21-
print("=== Example 1: Using OPENAI_COMPATIBLE_* environment variables ===")
21+
# export AXM_OPENAI_COMPATIBLE_BASE_URL="https://your-endpoint.com/v1"
22+
# export AXM_OPENAI_COMPATIBLE_API_KEY="your-api-key"
23+
print("=== Example 1: Using environment variables ===")
2224
provider_env = OpenAICompatibleProvider()
2325
print(f"Base URL from env: {provider_env.base_url}")
2426
print()

qq.py renamed to tests/test_mixed_usage.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
"""
22
Quick Start Script - Get up and running with AXM Agent in seconds!
33
4-
Run this script to see AXM Agent in action.
5-
Set your OPENAI_COMPATIBLE_BASE_URL and OPENAI_COMPATIBLE_API_KEY environment variables.
6-
Or use OPENAI_BASE_URL and OPENAI_API_KEY as fallback.
4+
Set environment variables:
5+
export AXM_OPENAI_COMPATIBLE_BASE_URL="https://ark.cn-beijing.volces.com/api/v3"
6+
export AXM_OPENAI_COMPATIBLE_API_KEY="your-api-key"
7+
8+
Then run: python qq.py
79
"""
810

911
import os
@@ -12,20 +14,16 @@
1214
from axm.core.multi_agent import MultiAgent
1315
from axm.llm.openai_compatible import OpenAICompatibleProvider
1416

17+
# Configuration
1518
MODEL = "deepseek-v3-250324"
16-
# Note: The base_url should end with /v1 (or similar version endpoint)
17-
# not include /chat/completions - that's added automatically by the provider
18-
BASE_URL = "https://ark.cn-beijing.volces.com/api/v3"
19-
API_KEY = os.environ.get("OPENAI_API_KEY", "test-key-please-set-env-var")
20-
2119

2220
def demo_basic_agent():
2321
"""Demo 1: Basic agent usage"""
2422
print("\n" + "=" * 60)
2523
print("DEMO 1: Basic Agent")
2624
print("=" * 60 + "\n")
2725

28-
agent = Agent(model=MODEL, base_url=BASE_URL, api_key=API_KEY)
26+
agent = Agent(model=MODEL)
2927

3028
assert isinstance(agent.llm, OpenAICompatibleProvider)
3129

@@ -41,7 +39,7 @@ def demo_agent_with_tools():
4139
print("DEMO 2: Agent with Custom Tools")
4240
print("=" * 60 + "\n")
4341

44-
agent = Agent(model=MODEL, base_url=BASE_URL, api_key=API_KEY)
42+
agent = Agent(model=MODEL)
4543

4644
@agent.tool
4745
def get_user_info(user_id: int) -> dict:
@@ -78,7 +76,7 @@ class MovieRecommendation(BaseModel):
7876
rating: float
7977
why_recommended: str
8078

81-
agent = Agent(model=MODEL, base_url=BASE_URL, api_key=API_KEY)
79+
agent = Agent(model=MODEL)
8280
movie = agent.run(
8381
"Recommend a sci-fi movie for someone who loves AI themes",
8482
response_format=MovieRecommendation,
@@ -97,7 +95,7 @@ def demo_planning_agent():
9795
print("DEMO 4: Planning Agent")
9896
print("=" * 60 + "\n")
9997

100-
agent = PlanningAgent(model=MODEL, base_url=BASE_URL, api_key=API_KEY)
98+
agent = PlanningAgent(model=MODEL)
10199

102100
agent.execute_plan(
103101
"Research the benefits of Python for data science and create a summary",
@@ -111,14 +109,12 @@ def demo_multi_agent():
111109
print("DEMO 5: Multi-Agent Collaboration")
112110
print("=" * 60 + "\n")
113111

114-
researcher = Agent(model=MODEL, role="researcher", base_url=BASE_URL, api_key=API_KEY)
115-
writer = Agent(model=MODEL, role="writer", base_url=BASE_URL, api_key=API_KEY)
112+
researcher = Agent(model=MODEL, role="researcher")
113+
writer = Agent(model=MODEL, role="writer")
116114

117115
team = MultiAgent(
118116
[researcher, writer],
119-
orchestrator_model=MODEL,
120-
base_url=BASE_URL,
121-
api_key=API_KEY,
117+
orchestrator_model=MODEL
122118
)
123119

124120
result = team.collaborate(
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
MODEL = "deepseek-v3-250324"
99
BASE_URL = "https://ark.cn-beijing.volces.com/api/v3"
10-
API_KEY = os.environ.get("OPENAI_API_KEY", "test-key")
10+
API_KEY = os.environ.get("AXM_OPENAI_COMPATIBLE_API_KEY", "test-key")
1111

1212

1313
def test_multi_agent_with_custom_url():
@@ -17,9 +17,7 @@ def test_multi_agent_with_custom_url():
1717
print("=" * 60 + "\n")
1818

1919
# Create agents with custom base_url
20-
researcher = Agent(
21-
model=MODEL, role="researcher", base_url=BASE_URL, api_key=API_KEY
22-
)
20+
researcher = Agent(model=MODEL, role="researcher", base_url=BASE_URL, api_key=API_KEY)
2321
writer = Agent(model=MODEL, role="writer", base_url=BASE_URL, api_key=API_KEY)
2422

2523
# Verify agents are using OpenAICompatibleProvider
@@ -55,29 +53,29 @@ def test_multi_agent_with_custom_url():
5553

5654

5755
def test_environment_variables():
58-
"""Test that OPENAI_COMPATIBLE_* environment variables work"""
56+
"""Test that AXM_OPENAI_COMPATIBLE_* environment variables work"""
5957
print("\n" + "=" * 60)
60-
print("TEST: Environment Variables Priority")
58+
print("TEST: Environment Variables")
6159
print("=" * 60 + "\n")
6260

6361
# Set environment variables
64-
os.environ["OPENAI_COMPATIBLE_BASE_URL"] = BASE_URL
65-
os.environ["OPENAI_COMPATIBLE_API_KEY"] = API_KEY
62+
os.environ["AXM_OPENAI_COMPATIBLE_BASE_URL"] = BASE_URL
63+
os.environ["AXM_OPENAI_COMPATIBLE_API_KEY"] = API_KEY
6664

6765
# Create provider without explicit parameters
6866
provider = OpenAICompatibleProvider()
6967

7068
# Verify environment variables were used
7169
assert provider.base_url == BASE_URL
7270
assert provider.api_key == API_KEY
73-
print(f"✅ Provider used OPENAI_COMPATIBLE_BASE_URL: {BASE_URL}")
74-
print(f"✅ Provider used OPENAI_COMPATIBLE_API_KEY: {API_KEY[:10]}...")
71+
print(f"✅ Provider used AXM_OPENAI_COMPATIBLE_BASE_URL: {BASE_URL}")
72+
print(f"✅ Provider used AXM_OPENAI_COMPATIBLE_API_KEY: {API_KEY[:10]}...")
7573

7674
# Create agent without explicit parameters
7775
agent = Agent(model=MODEL)
7876
assert isinstance(agent.llm, OpenAICompatibleProvider)
7977
assert agent.llm.base_url == BASE_URL
80-
print(f"✅ Agent used environment variables correctly")
78+
print("✅ Agent used environment variables correctly")
8179

8280
print("\n✅ All environment variable tests passed!")
8381

0 commit comments

Comments
 (0)