CoolFace
Apppublic

SanketAI/Academic-Research-Paper-Assistant

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
qa_agent.py58 linesDownload Raw Back to agents
1import os2import streamlit as st 3from agents import  SearchAgent4from langchain.vectorstores import FAISS5from langchain_google_genai import GoogleGenerativeAIEmbeddings6from config.config import model7 8 9 10            11embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")12 13class QAAgent:14    def __init__(self):15        16        self.model = model17        self.prompt = """You are a research assistant answering questions about academic papers. Use the following context from papers and chat history to provide accurate, specific answers.18 19        Previous conversation:20        {chat_history}21 22        Paper context:23        {context}24 25        Question: {question}26 27        Guidelines:28        1. Reference specific papers when making claims29        2. Use direct quotes when relevant30        3. Acknowledge if information isn't available in the provided context31        4. Maintain academic tone and precision32        """33        self.papers = None34        self.search_agent_response  = ""35 36    def solve(self, query):37        38            39        # Load vector store40        vector_db = FAISS.load_local("vector_db", embeddings, index_name="base_and_adjacent", allow_dangerous_deserialization=True)41        42        # Get chat history43        chat_history = st.session_state.get("chat_history", [])44        chat_history_text = "".join([f"{sender}: {msg}" for sender, msg in chat_history[-5:]])  # Last 5 messages45        46        # Get relevant chunks47        retrieved = vector_db.as_retriever().get_relevant_documents(query)48        context = "".join([f"{doc.page_content}\n Source: {doc.metadata['source']}" for doc in retrieved])49        50        # Generate response51        full_prompt = self.prompt.format(52            chat_history=chat_history_text,53            context=context,54            question=query55        )56        57        response = self.model.generate_content(str(self.search_agent_response)  + full_prompt)58        return response.text , self.papers