Update main.py
Browse files
main.py
CHANGED
|
@@ -11,46 +11,44 @@ TELEGRAM_API_URL = f"https://api.telegram.org/bot{TELEGRAM_TOKEN}"
|
|
| 11 |
|
| 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}
|
| 27 |
-
],
|
| 28 |
temperature=0.2
|
| 29 |
)
|
| 30 |
-
return response.choices[0].message.content or "No response
|
| 31 |
except Exception as e:
|
| 32 |
-
return f"⚠️
|
| 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"}
|
| 45 |
|
|
|
|
|
|
|
| 46 |
@app.post("/webhook")
|
| 47 |
async def telegram_webhook(request: Request, background_tasks: BackgroundTasks):
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
if __name__ == "__main__":
|
| 56 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 11 |
|
| 12 |
app = FastAPI()
|
| 13 |
|
| 14 |
+
system_prompt = "You are Phoebus AI. Keep replies concise and use Markdown."
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
async def run_agent(prompt: str) -> str:
|
| 17 |
try:
|
|
|
|
| 18 |
response = await acompletion(
|
| 19 |
model="gemini/gemini-1.5-pro",
|
| 20 |
+
messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": prompt}],
|
|
|
|
|
|
|
|
|
|
| 21 |
temperature=0.2
|
| 22 |
)
|
| 23 |
+
return response.choices[0].message.content or "No response."
|
| 24 |
except Exception as e:
|
| 25 |
+
return f"⚠️ Engine Error: {str(e)}"
|
| 26 |
|
| 27 |
async def process_and_reply(chat_id: int, text: str):
|
| 28 |
response_text = await run_agent(text)
|
| 29 |
async with httpx.AsyncClient() as client:
|
| 30 |
+
await client.post(f"{TELEGRAM_API_URL}/sendMessage", json={"chat_id": chat_id, "text": response_text, "parse_mode": "Markdown"})
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
@app.get("/health")
|
| 33 |
async def health_check():
|
| 34 |
return {"status": "alive"}
|
| 35 |
|
| 36 |
+
# Listen on BOTH root and /webhook to be safe
|
| 37 |
+
@app.post("/")
|
| 38 |
@app.post("/webhook")
|
| 39 |
async def telegram_webhook(request: Request, background_tasks: BackgroundTasks):
|
| 40 |
+
try:
|
| 41 |
+
data = await request.json()
|
| 42 |
+
print(f"DEBUG RECEIVE: {data}") # This will show up in your HF logs!
|
| 43 |
+
|
| 44 |
+
if "message" in data and "text" in data["message"]:
|
| 45 |
+
chat_id = data["message"]["chat"]["id"]
|
| 46 |
+
user_msg = data["message"]["text"]
|
| 47 |
+
background_tasks.add_task(process_and_reply, chat_id, user_msg)
|
| 48 |
+
return {"status": "ok"}
|
| 49 |
+
except Exception as e:
|
| 50 |
+
print(f"DEBUG ERROR: {e}")
|
| 51 |
+
return {"status": "error"}
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|
| 54 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|