Skip to content
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
10 changes: 5 additions & 5 deletions app/explain.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

from anthropic import Anthropic
from anthropic import AsyncAnthropic

from app.cache import CacheProvider, cache_response, get_cached_response
from app.explain_api import CostBreakdown, ExplainRequest, ExplainResponse, TokenUsage
Expand All @@ -19,7 +19,7 @@

async def process_request(
body: ExplainRequest,
client: Anthropic,
client: AsyncAnthropic,
prompt: Prompt,
metrics_provider: MetricsProvider,
cache_provider: CacheProvider | None = None,
Expand All @@ -31,7 +31,7 @@ async def process_request(

Args:
body: The request body as a Pydantic model
client: Anthropic client instance
client: AsyncAnthropic client instance
prompt: Prompt instance for generating messages
metrics_provider: metrics provider for tracking stats
cache_provider: cache provider for storing/retrieving responses
Expand Down Expand Up @@ -69,7 +69,7 @@ async def process_request(

async def _call_anthropic_api(
body: ExplainRequest,
client: Anthropic,
client: AsyncAnthropic,
prompt: Prompt,
metrics_provider: MetricsProvider,
) -> ExplainResponse:
Expand All @@ -92,7 +92,7 @@ async def _call_anthropic_api(
# Call Claude API
LOGGER.info("Using Anthropic client with model: %s", {prompt_data["model"]})

message = client.messages.create(
message = await client.messages.create(
model=prompt_data["model"],
max_tokens=prompt_data["max_tokens"],
temperature=prompt_data["temperature"],
Expand Down
4 changes: 2 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from contextlib import asynccontextmanager
from pathlib import Path

from anthropic import Anthropic
from anthropic import AsyncAnthropic
from anthropic import __version__ as anthropic_version
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
Expand Down Expand Up @@ -42,7 +42,7 @@ async def lifespan(app: FastAPI):

# Store shared resources in app.state
app.state.settings = settings
app.state.anthropic_client = Anthropic(api_key=settings.anthropic_api_key)
app.state.anthropic_client = AsyncAnthropic(api_key=settings.anthropic_api_key)

# Load the prompt configuration
prompt_config_path = Path(__file__).parent / "prompt.yaml"
Expand Down
4 changes: 2 additions & 2 deletions app/test_explain.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
from pathlib import Path
from unittest.mock import MagicMock
from unittest.mock import AsyncMock, MagicMock

import pytest

Expand Down Expand Up @@ -65,7 +65,7 @@ def mock_anthropic_client():
mock_message.usage.input_tokens = 100
mock_message.usage.output_tokens = 50

mock_client.messages.create.return_value = mock_message
mock_client.messages.create = AsyncMock(return_value=mock_message)
return mock_client


Expand Down