marks
commited on
Commit
·
7e5d5c2
1
Parent(s):
25262d5
Fixed OpenRouter formatting
Browse files- api_clients.py +24 -9
api_clients.py
CHANGED
|
@@ -18,6 +18,8 @@ class OpenRouterClient:
|
|
| 18 |
self.base_url = "https://openrouter.ai/api/v1"
|
| 19 |
self.headers = {
|
| 20 |
"Authorization": f"Bearer {api_key}",
|
|
|
|
|
|
|
| 21 |
"Content-Type": "application/json"
|
| 22 |
}
|
| 23 |
logger.debug("OpenRouter client initialized successfully")
|
|
@@ -35,6 +37,8 @@ class OpenRouterClient:
|
|
| 35 |
# Update headers when API key changes
|
| 36 |
self.headers = {
|
| 37 |
"Authorization": f"Bearer {value}",
|
|
|
|
|
|
|
| 38 |
"Content-Type": "application/json",
|
| 39 |
}
|
| 40 |
logger.info("OpenRouter API key updated successfully")
|
|
@@ -84,24 +88,35 @@ class OpenRouterClient:
|
|
| 84 |
logger.error("Prompt too short or missing")
|
| 85 |
raise ValueError("Please provide a more detailed prompt")
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
try:
|
| 88 |
-
|
| 89 |
-
model
|
| 90 |
-
messages
|
| 91 |
-
|
| 92 |
-
|
| 93 |
]
|
| 94 |
-
|
| 95 |
|
| 96 |
async with self.get_session() as session:
|
| 97 |
async with session.post(
|
| 98 |
f"{self.base_url}/chat/completions",
|
| 99 |
-
json=
|
| 100 |
) as response:
|
| 101 |
response.raise_for_status()
|
| 102 |
data = await response.json()
|
| 103 |
-
|
| 104 |
-
return router_response.choices[0].message.content
|
| 105 |
except Exception as e:
|
| 106 |
logger.error(f"Script generation failed", exc_info=True)
|
| 107 |
raise
|
|
|
|
| 18 |
self.base_url = "https://openrouter.ai/api/v1"
|
| 19 |
self.headers = {
|
| 20 |
"Authorization": f"Bearer {api_key}",
|
| 21 |
+
"HTTP-Referer": "https://github.com/your-repo", # Required by OpenRouter
|
| 22 |
+
"X-Title": "URL to Podcast Generator", # Required by OpenRouter
|
| 23 |
"Content-Type": "application/json"
|
| 24 |
}
|
| 25 |
logger.debug("OpenRouter client initialized successfully")
|
|
|
|
| 37 |
# Update headers when API key changes
|
| 38 |
self.headers = {
|
| 39 |
"Authorization": f"Bearer {value}",
|
| 40 |
+
"HTTP-Referer": "https://github.com/your-repo", # Required by OpenRouter
|
| 41 |
+
"X-Title": "URL to Podcast Generator", # Required by OpenRouter
|
| 42 |
"Content-Type": "application/json",
|
| 43 |
}
|
| 44 |
logger.info("OpenRouter API key updated successfully")
|
|
|
|
| 88 |
logger.error("Prompt too short or missing")
|
| 89 |
raise ValueError("Please provide a more detailed prompt")
|
| 90 |
|
| 91 |
+
# Combine prompt and content into a clear instruction
|
| 92 |
+
full_prompt = f"""
|
| 93 |
+
Based on the following content, create an engaging podcast script:
|
| 94 |
+
|
| 95 |
+
Context: {prompt if prompt else 'Create a natural, conversational podcast script.'}
|
| 96 |
+
|
| 97 |
+
Content to transform:
|
| 98 |
+
{content}
|
| 99 |
+
|
| 100 |
+
Generate a well-structured podcast script that explains this content in a clear, engaging way.
|
| 101 |
+
"""
|
| 102 |
+
|
| 103 |
try:
|
| 104 |
+
request_data = {
|
| 105 |
+
"model": model_id,
|
| 106 |
+
"messages": [
|
| 107 |
+
{"role": "system", "content": "You are a professional podcast script writer who creates engaging, conversational content."},
|
| 108 |
+
{"role": "user", "content": full_prompt}
|
| 109 |
]
|
| 110 |
+
}
|
| 111 |
|
| 112 |
async with self.get_session() as session:
|
| 113 |
async with session.post(
|
| 114 |
f"{self.base_url}/chat/completions",
|
| 115 |
+
json=request_data
|
| 116 |
) as response:
|
| 117 |
response.raise_for_status()
|
| 118 |
data = await response.json()
|
| 119 |
+
return data['choices'][0]['message']['content']
|
|
|
|
| 120 |
except Exception as e:
|
| 121 |
logger.error(f"Script generation failed", exc_info=True)
|
| 122 |
raise
|