|
from fastapi import APIRouter, Request, Depends |
|
from App.Users.Model import User |
|
from App.Users.UserRoutes import get_token_owner |
|
from App.Users.Schemas import UserSchema |
|
from App.Transcription.Model import Transcriptions |
|
from App.Transcription.Schemas import * |
|
from App import bot |
|
from .utils.Summarize import Summarizer |
|
from .utils.RAG import GenerativeAIAssistant |
|
from .Schema import ChatRequest |
|
import aiohttp |
|
import os |
|
|
|
chat_router = APIRouter(tags=["chat"]) |
|
|
|
|
|
P_API_KEY = os.environ.get("API_KEY") |
|
P_API_BASE = os.environ.get("P_API_BASE") |
|
API_URL = f"{P_API_BASE}chat-bison-001:generateMessage?key={P_API_KEY}" |
|
|
|
|
|
async def fetch_and_forward_request(request_body): |
|
async with aiohttp.ClientSession() as session: |
|
async with session.post(API_URL, data=request_body) as response: |
|
response_text = await response.text() |
|
return response_text, response.status |
|
|
|
|
|
@chat_router.post("/Chat") |
|
async def generate_response(req:ChatRequest): |
|
assistant=GenerativeAIAssistant() |
|
return await assistant.generate_message(req.messages,req.taskId) |
|
|
|
|
|
@chat_router.get("/summarize/{task_id}") |
|
async def generate_message( |
|
task_id: str, |
|
|
|
): |
|
entry: Transcriptions = await Transcriptions.objects.filter(task_id=task_id).first() |
|
result = BaseTranscription(**entry.__dict__) |
|
print(result) |
|
text = "" |
|
for item in result.content: |
|
text += item["text"] |
|
summary = await Summarizer(text) |
|
return [summary] |
|
|