marks
commited on
Commit
·
f71cd77
1
Parent(s):
94bdc12
Implemented pydantic
Browse files- api_clients.py +15 -11
- models.py +25 -0
api_clients.py
CHANGED
|
@@ -4,6 +4,7 @@ import aiohttp
|
|
| 4 |
import elevenlabs
|
| 5 |
from contextlib import asynccontextmanager
|
| 6 |
from logger import setup_logger, log_execution_time, log_async_execution_time
|
|
|
|
| 7 |
|
| 8 |
logger = setup_logger("api_clients")
|
| 9 |
|
|
@@ -60,9 +61,8 @@ class OpenRouterClient:
|
|
| 60 |
headers=self.headers
|
| 61 |
)
|
| 62 |
response.raise_for_status()
|
| 63 |
-
models = response.json()["data"]
|
| 64 |
-
|
| 65 |
-
return [(model["id"], model["name"]) for model in models]
|
| 66 |
|
| 67 |
@log_async_execution_time(logger)
|
| 68 |
async def generate_script(self, content: str, prompt: str, model_id: str) -> str:
|
|
@@ -84,19 +84,23 @@ class OpenRouterClient:
|
|
| 84 |
raise ValueError("Please provide a more detailed prompt")
|
| 85 |
|
| 86 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
response = requests.post(
|
| 88 |
f"{self.base_url}/chat/completions",
|
| 89 |
headers=self.headers,
|
| 90 |
-
json
|
| 91 |
-
"model": model_id,
|
| 92 |
-
"messages": [
|
| 93 |
-
{"role": "system", "content": "You are a podcast script writer."},
|
| 94 |
-
{"role": "user", "content": f"Create a podcast script from this content: {content}"}
|
| 95 |
-
]
|
| 96 |
-
}
|
| 97 |
)
|
| 98 |
response.raise_for_status()
|
| 99 |
-
|
|
|
|
|
|
|
| 100 |
except Exception as e:
|
| 101 |
logger.error(f"Script generation failed", exc_info=True)
|
| 102 |
raise
|
|
|
|
| 4 |
import elevenlabs
|
| 5 |
from contextlib import asynccontextmanager
|
| 6 |
from logger import setup_logger, log_execution_time, log_async_execution_time
|
| 7 |
+
from models import OpenRouterRequest, OpenRouterResponse, Message, OpenRouterModel
|
| 8 |
|
| 9 |
logger = setup_logger("api_clients")
|
| 10 |
|
|
|
|
| 61 |
headers=self.headers
|
| 62 |
)
|
| 63 |
response.raise_for_status()
|
| 64 |
+
models = [OpenRouterModel(**model) for model in response.json()["data"]]
|
| 65 |
+
return [(model.id, model.name) for model in models]
|
|
|
|
| 66 |
|
| 67 |
@log_async_execution_time(logger)
|
| 68 |
async def generate_script(self, content: str, prompt: str, model_id: str) -> str:
|
|
|
|
| 84 |
raise ValueError("Please provide a more detailed prompt")
|
| 85 |
|
| 86 |
try:
|
| 87 |
+
request = OpenRouterRequest(
|
| 88 |
+
model=model_id,
|
| 89 |
+
messages=[
|
| 90 |
+
Message(role="system", content="You are a podcast script writer."),
|
| 91 |
+
Message(role="user", content=f"Create a podcast script from this content: {content}")
|
| 92 |
+
]
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
response = requests.post(
|
| 96 |
f"{self.base_url}/chat/completions",
|
| 97 |
headers=self.headers,
|
| 98 |
+
data=request.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
)
|
| 100 |
response.raise_for_status()
|
| 101 |
+
|
| 102 |
+
router_response = OpenRouterResponse(**response.json())
|
| 103 |
+
return router_response.choices[0].message.content
|
| 104 |
except Exception as e:
|
| 105 |
logger.error(f"Script generation failed", exc_info=True)
|
| 106 |
raise
|
models.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel, Field
|
| 2 |
+
from typing import List, Optional
|
| 3 |
+
|
| 4 |
+
class Message(BaseModel):
|
| 5 |
+
role: str
|
| 6 |
+
content: str
|
| 7 |
+
|
| 8 |
+
class OpenRouterRequest(BaseModel):
|
| 9 |
+
model: str
|
| 10 |
+
messages: List[Message]
|
| 11 |
+
|
| 12 |
+
class Choice(BaseModel):
|
| 13 |
+
message: Message
|
| 14 |
+
index: int = 0
|
| 15 |
+
finish_reason: Optional[str] = None
|
| 16 |
+
|
| 17 |
+
class OpenRouterResponse(BaseModel):
|
| 18 |
+
id: str
|
| 19 |
+
choices: List[Choice]
|
| 20 |
+
model: str
|
| 21 |
+
|
| 22 |
+
class OpenRouterModel(BaseModel):
|
| 23 |
+
id: str
|
| 24 |
+
name: str
|
| 25 |
+
description: Optional[str] = None
|