marks commited on
Commit ·
f71cd77
1
Parent(s): 94bdc12
Implemented pydantic
Browse filesFiles changed (2)
- api_clients.py+15-11
- models.pyAdded+25
3 unmodified lines45678950 unmodified lines60616263646566676815 unmodified lines848586878889909192939495969798991001011023 unmodified linesimport elevenlabsfrom contextlib import asynccontextmanagerfrom logger import setup_logger, log_execution_time, log_async_execution_timelogger = setup_logger("api_clients")50 unmodified linesheaders=self.headers)response.raise_for_status()models = response.json()["data"]# Return tuple of (id, display_name) for each modelreturn [(model["id"], model["name"]) for model in models]@log_async_execution_time(logger)async def generate_script(self, content: str, prompt: str, model_id: str) -> str:15 unmodified linesraise ValueError("Please provide a more detailed prompt")try:response = requests.post(f"{self.base_url}/chat/completions",headers=self.headers,json={"model": model_id,"messages": [{"role": "system", "content": "You are a podcast script writer."},{"role": "user", "content": f"Create a podcast script from this content: {content}"}]})response.raise_for_status()return response.json()["choices"][0]["message"]["content"]except Exception as e:logger.error(f"Script generation failed", exc_info=True)raise3 unmodified lines4567891050 unmodified lines616263646566676815 unmodified lines848586878889909192939495969798991001011021031041051063 unmodified linesimport elevenlabsfrom contextlib import asynccontextmanagerfrom logger import setup_logger, log_execution_time, log_async_execution_timefrom models import OpenRouterRequest, OpenRouterResponse, Message, OpenRouterModellogger = setup_logger("api_clients")50 unmodified linesheaders=self.headers)response.raise_for_status()models = [OpenRouterModel(**model) for model in response.json()["data"]]return [(model.id, model.name) for model in models]@log_async_execution_time(logger)async def generate_script(self, content: str, prompt: str, model_id: str) -> str:15 unmodified linesraise ValueError("Please provide a more detailed prompt")try:request = OpenRouterRequest(model=model_id,messages=[Message(role="system", content="You are a podcast script writer."),Message(role="user", content=f"Create a podcast script from this content: {content}")])response = requests.post(f"{self.base_url}/chat/completions",headers=self.headers,data=request.json())response.raise_for_status()router_response = OpenRouterResponse(**response.json())return router_response.choices[0].message.contentexcept Exception as e:logger.error(f"Script generation failed", exc_info=True)raise
12345678910111213141516171819202122232425from pydantic import BaseModel, Fieldfrom typing import List, Optional
class Message(BaseModel): role: str content: str
class OpenRouterRequest(BaseModel): model: str messages: List[Message]
class Choice(BaseModel): message: Message index: int = 0 finish_reason: Optional[str] = None
class OpenRouterResponse(BaseModel): id: str choices: List[Choice] model: str
class OpenRouterModel(BaseModel): id: str name: str description: Optional[str] = Noneapi_clients.py+15-11
models.pyAdded+25