Update main.py
Browse files
main.py
CHANGED
|
@@ -12,15 +12,15 @@ TELEGRAM_API_URL = f"https://api.telegram.org/bot{TELEGRAM_TOKEN}"
|
|
| 12 |
app = FastAPI()
|
| 13 |
|
| 14 |
system_prompt = (
|
| 15 |
-
"You are an expert coding
|
| 16 |
-
"Keep replies concise and under 1000 chars. Use Markdown
|
| 17 |
)
|
| 18 |
|
| 19 |
-
# --- NATIVE AGENT ENGINE ---
|
| 20 |
async def run_agent(prompt: str) -> str:
|
| 21 |
try:
|
|
|
|
| 22 |
response = await acompletion(
|
| 23 |
-
model="gemini/gemini-
|
| 24 |
messages=[
|
| 25 |
{"role": "system", "content": system_prompt},
|
| 26 |
{"role": "user", "content": prompt}
|
|
@@ -29,24 +29,16 @@ async def run_agent(prompt: str) -> str:
|
|
| 29 |
)
|
| 30 |
return response.choices[0].message.content or "No response generated."
|
| 31 |
except Exception as e:
|
| 32 |
-
return f"
|
| 33 |
|
| 34 |
-
# --- BACKGROUND TASK: PROCESS AND REPLY ---
|
| 35 |
async def process_and_reply(chat_id: int, text: str):
|
| 36 |
response_text = await run_agent(text)
|
| 37 |
-
|
| 38 |
-
# Send the final response back to Telegram
|
| 39 |
async with httpx.AsyncClient() as client:
|
| 40 |
await client.post(
|
| 41 |
f"{TELEGRAM_API_URL}/sendMessage",
|
| 42 |
-
json={
|
| 43 |
-
"chat_id": chat_id,
|
| 44 |
-
"text": response_text,
|
| 45 |
-
"parse_mode": "Markdown"
|
| 46 |
-
}
|
| 47 |
)
|
| 48 |
|
| 49 |
-
# --- ENDPOINTS ---
|
| 50 |
@app.get("/health")
|
| 51 |
async def health_check():
|
| 52 |
return {"status": "alive"}
|
|
@@ -54,16 +46,10 @@ async def health_check():
|
|
| 54 |
@app.post("/webhook")
|
| 55 |
async def telegram_webhook(request: Request, background_tasks: BackgroundTasks):
|
| 56 |
data = await request.json()
|
| 57 |
-
|
| 58 |
-
# Extract the message from Telegram's JSON payload
|
| 59 |
if "message" in data and "text" in data["message"]:
|
| 60 |
chat_id = data["message"]["chat"]["id"]
|
| 61 |
user_msg = data["message"]["text"]
|
| 62 |
-
|
| 63 |
-
# Run the AI generation in the background so we can instantly reply "OK" to Telegram
|
| 64 |
background_tasks.add_task(process_and_reply, chat_id, user_msg)
|
| 65 |
-
|
| 66 |
-
# Telegram requires an immediate 200 OK so it doesn't get stuck in a retry loop
|
| 67 |
return {"status": "ok"}
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
|
|
|
| 12 |
app = FastAPI()
|
| 13 |
|
| 14 |
system_prompt = (
|
| 15 |
+
"You are Phoebus AI, an expert coding agent. "
|
| 16 |
+
"Keep replies concise and under 1000 chars. Use Markdown."
|
| 17 |
)
|
| 18 |
|
|
|
|
| 19 |
async def run_agent(prompt: str) -> str:
|
| 20 |
try:
|
| 21 |
+
# Switched to 1.5-pro for maximum stability with LiteLLM
|
| 22 |
response = await acompletion(
|
| 23 |
+
model="gemini/gemini-1.5-pro",
|
| 24 |
messages=[
|
| 25 |
{"role": "system", "content": system_prompt},
|
| 26 |
{"role": "user", "content": prompt}
|
|
|
|
| 29 |
)
|
| 30 |
return response.choices[0].message.content or "No response generated."
|
| 31 |
except Exception as e:
|
| 32 |
+
return f"⚠️ Phoebus Engine Error: {str(e)}"
|
| 33 |
|
|
|
|
| 34 |
async def process_and_reply(chat_id: int, text: str):
|
| 35 |
response_text = await run_agent(text)
|
|
|
|
|
|
|
| 36 |
async with httpx.AsyncClient() as client:
|
| 37 |
await client.post(
|
| 38 |
f"{TELEGRAM_API_URL}/sendMessage",
|
| 39 |
+
json={"chat_id": chat_id, "text": response_text, "parse_mode": "Markdown"}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
)
|
| 41 |
|
|
|
|
| 42 |
@app.get("/health")
|
| 43 |
async def health_check():
|
| 44 |
return {"status": "alive"}
|
|
|
|
| 46 |
@app.post("/webhook")
|
| 47 |
async def telegram_webhook(request: Request, background_tasks: BackgroundTasks):
|
| 48 |
data = await request.json()
|
|
|
|
|
|
|
| 49 |
if "message" in data and "text" in data["message"]:
|
| 50 |
chat_id = data["message"]["chat"]["id"]
|
| 51 |
user_msg = data["message"]["text"]
|
|
|
|
|
|
|
| 52 |
background_tasks.add_task(process_and_reply, chat_id, user_msg)
|
|
|
|
|
|
|
| 53 |
return {"status": "ok"}
|
| 54 |
|
| 55 |
if __name__ == "__main__":
|