Model / main.py
nicolaydef's picture
Update main.py
0a8a0f3 verified
import os
import google.generativeai as genai
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
app = FastAPI()
# Получаем ключ
genai.configure(api_key=os.environ.get("GEMINI_API_KEY"))
# ВАЖНО: Убедись, что в requirements.txt версия >=0.7.2, иначе flash не заработает
model = genai.GenerativeModel("gemini-1.5-flash")
class RequestData(BaseModel):
context: str
mood: str
@app.post("/complete")
async def complete_text(data: RequestData):
try:
# Настройка личности
tone_instruction = ""
if data.mood == "fun":
tone_instruction = "Отвечай весело, используй сленг, эмодзи."
elif data.mood == "serious":
tone_instruction = "Отвечай официально, сухо, деловой стиль."
elif data.mood == "angry":
tone_instruction = "Отвечай пассивно-агрессивно, с сарказмом."
else:
tone_instruction = "Отвечай спокойно и нейтрально."
# ТВОЙ НОВЫЙ ПРОМПТ
prompt = (
f"System: {tone_instruction}. "
f"User input might be in wrong keyboard layout (e.g. 'ghbdtn' instead of 'привет'). "
f"If input looks like encoded Russian, decode it internally and complete the Russian phrase. "
f"If input is English, just complete it. "
f"Do not repeat the input. Output ONLY the completion text.\n\n"
f"User Input: {data.context}\nOutput:"
)
response = model.generate_content(prompt)
return {"completion": response.text.strip()}
except Exception as e:
# Вывод ошибки в ответ, чтобы клиент увидел причину
raise HTTPException(status_code=500, detail=str(e))