From 777df6a6394ab6e077f773c16d636b5798cff84c Mon Sep 17 00:00:00 2001 From: AquibPy Date: Tue, 23 Apr 2024 14:16:17 +0530 Subject: [PATCH] ADDED: streaming response endpoint and chatbot endpoint --- api.py | 22 +++++++++-- helper_functions.py | 30 +++++++++++++++ templates/index.html | 92 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 templates/index.html diff --git a/api.py b/api.py index 09e9642..26c4530 100644 --- a/api.py +++ b/api.py @@ -1,7 +1,8 @@ import os -from fastapi import FastAPI,Form,File,UploadFile +from fastapi import FastAPI,Form,File,UploadFile, Request +from fastapi.templating import Jinja2Templates from fastapi.encoders import jsonable_encoder -from fastapi.responses import JSONResponse,RedirectResponse +from fastapi.responses import JSONResponse,RedirectResponse,StreamingResponse from typing import List,Optional from pydantic import BaseModel import google.generativeai as genai @@ -10,7 +11,7 @@ from mongo import MongoDB from helper_functions import get_qa_chain,get_gemini_response,get_url_doc_qa,extract_transcript_details,\ get_gemini_response_health,get_gemini_pdf,read_sql_query,remove_substrings,questions_generator,groq_pdf,\ - summarize_audio + summarize_audio,chatbot_send_message from langchain_groq import ChatGroq from langchain.chains import ConversationChain from langchain.chains.conversation.memory import ConversationBufferWindowMemory @@ -19,6 +20,8 @@ app = FastAPI(title="Genify By Mohd Aquib", summary="This API contains routes of different Gen AI usecases") +templates = Jinja2Templates(directory="templates") + app.allow_dangerous_deserialization = True app.add_middleware( @@ -37,6 +40,11 @@ class ResponseText(BaseModel): async def home(): return RedirectResponse("/docs") + +@app.get("/chatbot",description=" Talk to chatbot") +async def chat(request: Request): + return templates.TemplateResponse("index.html", {"request": request}) + @app.post("/invoice_extractor",description="This route extracts information from invoices based on provided images and prompts.") async def gemini(image_file: UploadFile = File(...), prompt: str = Form(...)): image = image_file.file.read() @@ -325,4 +333,10 @@ async def summarize_audio_endpoint(audio_file: UploadFile = File(...)): print(result) return ResponseText(response=summary_text) except Exception as e: - return {"error": str(e)} \ No newline at end of file + return {"error": str(e)} + + +@app.post("/stream_chat",description="This route provide the data from LLM in the streaming response.") +async def stream_chat(message: str = Form(...)): + generator = chatbot_send_message(message) + return StreamingResponse(generator, media_type="text/event-stream") \ No newline at end of file diff --git a/helper_functions.py b/helper_functions.py index d9b94ce..8c22a47 100644 --- a/helper_functions.py +++ b/helper_functions.py @@ -21,6 +21,10 @@ import sqlite3 from langchain_community.embeddings import GooglePalmEmbeddings import tempfile +from langchain.callbacks import AsyncIteratorCallbackHandler +from typing import AsyncIterable +import asyncio +from langchain.schema import HumanMessage load_dotenv() genai.configure(api_key=os.getenv("GOOGLE_API_KEY")) @@ -235,6 +239,32 @@ async def summarize_audio(audio_file): return response.text + +async def chatbot_send_message(content: str) -> AsyncIterable[str]: + callback = AsyncIteratorCallbackHandler() + model = ChatGroq( + temperature=0, + groq_api_key=os.environ['GROQ_API_KEY'], + model_name="llama3-70b-8192", + streaming=True, + verbose=True, + callbacks=[callback], + ) + + task = asyncio.create_task( + model.agenerate(messages=[[HumanMessage(content=content)]]) + ) + + try: + async for token in callback.aiter(): + yield token + except Exception as e: + print(f"Caught exception: {e}") + finally: + callback.done.set() + + await task + if __name__ == "__main__": create_vector_db() chain = get_qa_chain() diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..e9ecadd --- /dev/null +++ b/templates/index.html @@ -0,0 +1,92 @@ + + + + + + + + + +
+

Chat with AI

+
+ + + +
+ + + + + + \ No newline at end of file