File size: 1,284 Bytes
d1d1d6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from langchain_core.messages import AIMessage, HumanMessage
from fastapi import FastAPI
from langchain_pinecone.vectorstores import Pinecone
from pydantic import BaseModel
from rag import Rag
from retriever import AskMeAboutRagRetriever
from langchain_huggingface import HuggingFaceEmbeddings
from dotenv import load_dotenv
import os

load_dotenv()

api_key=os.getenv('PINECONE_KEY')
index_name="askmeaboutrag" 

vectorstore = Pinecone(pinecone_api_key=api_key, index_name=index_name, embedding=HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2"))
retriever = AskMeAboutRagRetriever(vectorstore)
rag_llm = Rag(vectorstore, retriever);

rag_llm.createRagChain()

chat_history = []

class ChatInput(BaseModel):
    question: str

app = FastAPI() 

@app.get("/")
async def root():
    return {"message": "Hello World"}

@app.post("/generatechat/")
async def generateResponse(chat_input: ChatInput):
    ai_msg = rag_llm.generateResponse(chat_input.question, chat_history)
    chat_history.extend(
        [
            HumanMessage(content=chat_input.question),
            AIMessage(content=ai_msg["answer"]),
        ]
    )
    return {"response": ai_msg}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="127.0.0.1", port=8000)
    print("Server is running")