|
| 1 | +# *-* coding: utf-8 *-* |
| 2 | +from uuid import uuid4 |
| 3 | +import datetime |
| 4 | +import logging |
| 5 | + |
| 6 | +from dialog.db import engine, get_session |
| 7 | +from dialog.schemas import ChatModel |
| 8 | +from dialog.llm import process_user_message |
| 9 | + |
| 10 | +from sqlalchemy.orm import Session |
| 11 | + |
| 12 | +from fastapi import APIRouter, Depends |
| 13 | + |
| 14 | +open_ai_api_router = APIRouter() |
| 15 | + |
| 16 | +@open_ai_api_router.get("/models") |
| 17 | +async def get_models(): |
| 18 | + """ |
| 19 | + Returns the model that is available inside Dialog in the OpenAI format. |
| 20 | + """ |
| 21 | + return [{ |
| 22 | + "id": "talkd-ai", |
| 23 | + "object": "model", |
| 24 | + "created": datetime.datetime.now().timestamp(), |
| 25 | + "owned_by": "system" |
| 26 | + }] |
| 27 | + |
| 28 | +@open_ai_api_router.post("/chat/completions") |
| 29 | +async def ask_question_to_llm(message: ChatModel, session: Session = Depends(get_session)): |
| 30 | + """ |
| 31 | + This posts a single message to the LLM and returns the response without |
| 32 | + using memory. |
| 33 | + """ |
| 34 | + start_time = datetime.datetime.now() |
| 35 | + ai_message = process_user_message(message, chat_id=None) |
| 36 | + duration = datetime.datetime.now() - start_time |
| 37 | + logging.info(f"Request processing time: {duration}") |
| 38 | + return { |
| 39 | + "choices": [ |
| 40 | + { |
| 41 | + "finish_reason": "stop", |
| 42 | + "index": 0, |
| 43 | + "message": { |
| 44 | + "content": ai_message["text"], |
| 45 | + "role": "assistant" |
| 46 | + }, |
| 47 | + "logprobs": None |
| 48 | + } |
| 49 | + ], |
| 50 | + "created": datetime.datetime.now().timestamp(), |
| 51 | + "id": f"talkdai-{str(uuid4())}", |
| 52 | + "model": "talkdai", |
| 53 | + "object": "chat.completion", |
| 54 | + "usage": { |
| 55 | + "completion_tokens": None, |
| 56 | + "prompt_tokens": None, |
| 57 | + "total_tokens": None |
| 58 | + } |
| 59 | + } |
0 commit comments