Skip to content

Commit 2d38015

Browse files
feat: Make OpenAI and Anthropic API base URLs configurable
- Add openai_api_base and anthropic_api_base fields to Settings - Update AnthropicClientWrapper to accept and use base_url parameter - Update get_model_client to use configuration settings instead of env vars directly - Fix import style to use absolute imports - Format code with ruff Co-authored-by: Andrew Brookins <[email protected]>
1 parent 2b623b1 commit 2d38015

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

agent_memory_server/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class Settings(BaseSettings):
5252
long_term_memory: bool = True
5353
openai_api_key: str | None = None
5454
anthropic_api_key: str | None = None
55+
openai_api_base: str | None = None
56+
anthropic_api_base: str | None = None
5557
generation_model: str = "gpt-4o"
5658
embedding_model: str = "text-embedding-3-small"
5759
port: int = 8000

agent_memory_server/llms.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from openai import AsyncOpenAI
1010
from pydantic import BaseModel
1111

12+
from agent_memory_server.config import settings
13+
1214

1315
logger = logging.getLogger(__name__)
1416

@@ -203,14 +205,21 @@ def total_tokens(self) -> int:
203205
class AnthropicClientWrapper:
204206
"""Wrapper for Anthropic client"""
205207

206-
def __init__(self, api_key: str | None = None):
208+
def __init__(self, api_key: str | None = None, base_url: str | None = None):
207209
"""Initialize the Anthropic client"""
208210
anthropic_api_key = api_key or os.environ.get("ANTHROPIC_API_KEY")
211+
anthropic_api_base = base_url or os.environ.get("ANTHROPIC_API_BASE")
209212

210213
if not anthropic_api_key:
211214
raise ValueError("Anthropic API key is required")
212215

213-
self.client = anthropic.AsyncAnthropic(api_key=anthropic_api_key)
216+
if anthropic_api_base:
217+
self.client = anthropic.AsyncAnthropic(
218+
api_key=anthropic_api_key,
219+
base_url=anthropic_api_base,
220+
)
221+
else:
222+
self.client = anthropic.AsyncAnthropic(api_key=anthropic_api_key)
214223

215224
async def create_chat_completion(
216225
self,
@@ -397,9 +406,15 @@ async def get_model_client(
397406
model_config = get_model_config(model_name)
398407

399408
if model_config.provider == ModelProvider.OPENAI:
400-
model = OpenAIClientWrapper(api_key=os.environ.get("OPENAI_API_KEY"))
409+
model = OpenAIClientWrapper(
410+
api_key=settings.openai_api_key,
411+
base_url=settings.openai_api_base,
412+
)
401413
if model_config.provider == ModelProvider.ANTHROPIC:
402-
model = AnthropicClientWrapper(api_key=os.environ.get("ANTHROPIC_API_KEY"))
414+
model = AnthropicClientWrapper(
415+
api_key=settings.anthropic_api_key,
416+
base_url=settings.anthropic_api_base,
417+
)
403418

404419
if model:
405420
_model_clients[model_name] = model

tests/test_full_integration.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -772,9 +772,9 @@ async def test_memory_prompt_with_long_term_search(
772772
)
773773
for msg in messages
774774
)
775-
assert (
776-
relevant_context_found
777-
), f"No relevant memory context found in messages: {messages}"
775+
assert relevant_context_found, (
776+
f"No relevant memory context found in messages: {messages}"
777+
)
778778

779779
# Cleanup
780780
await client.delete_long_term_memories([m.id for m in test_memories])
@@ -1078,9 +1078,9 @@ async def test_full_workflow_integration(
10781078
)
10791079
print(f"No topic filter search results: {no_topic_search}")
10801080

1081-
assert (
1082-
len(search_results["memories"]) > 0
1083-
), f"No memories found in search results: {search_results}"
1081+
assert len(search_results["memories"]) > 0, (
1082+
f"No memories found in search results: {search_results}"
1083+
)
10841084

10851085
# 6. Test tool integration with a realistic scenario
10861086
tool_call = {
@@ -1125,9 +1125,9 @@ async def test_full_workflow_integration(
11251125
m for m in long_term_memories.memories if m.id.startswith(memory_id_prefix)
11261126
]
11271127

1128-
assert (
1129-
len(our_memories) == 0
1130-
), f"Expected 0 of our memories but found {len(our_memories)}: {our_memories}"
1128+
assert len(our_memories) == 0, (
1129+
f"Expected 0 of our memories but found {len(our_memories)}: {our_memories}"
1130+
)
11311131

11321132

11331133
@pytest.mark.integration

0 commit comments

Comments
 (0)