app.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from fastapi import Body, FastAPI
  2. from ollama import ChatResponse, Client
  3. from config import OLLAMA_MODEL, OLLAMA_URL
  4. client = Client(host=f"{OLLAMA_URL}")
  5. # ollama.create(model='example', from_='gemma3', system="You are Mario from Super Mario Bros.") // TODO: Для асистента?
  6. def message(text: str) -> ChatResponse:
  7. return chat(
  8. [
  9. {
  10. "role": "system",
  11. "content": "Отвечай строго в формате Markdown. Не нужно пихать везде большие заголовки! Пиши как обычный человек, но красиво оформляй ответ",
  12. },
  13. {"role": "user", "content": text},
  14. ]
  15. )
  16. def chat(messages: list) -> ChatResponse:
  17. return client.chat(model=f"{OLLAMA_MODEL}", messages=messages, think=True)
  18. app = FastAPI()
  19. @app.post("/chat")
  20. def _(prompt: str = Body(..., embed=True)):
  21. print(prompt)
  22. response = message(prompt)
  23. return response
  24. @app.post("/generate")
  25. def generate(prompt: str = Body(..., embed=True)):
  26. response = client.generate(
  27. model=f"{OLLAMA_MODEL}",
  28. prompt=prompt,
  29. think=True,
  30. options={"temperature": 0.15},
  31. )
  32. return response
  33. @app.get("/health")
  34. def health():
  35. return {"status": "ok"}