Spaces:
Sleeping
Sleeping
File size: 8,928 Bytes
628936c 73f0bd9 628936c 73f0bd9 628936c 23b02f3 628936c 23b02f3 628936c 23b02f3 628936c 23b02f3 628936c 23b02f3 628936c 23b02f3 628936c 23b02f3 628936c 23b02f3 628936c 23b02f3 628936c 23b02f3 628936c 23b02f3 628936c 23b02f3 628936c 23b02f3 628936c 23b02f3 628936c |
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
import os
import gradio as gr
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import PyPDFLoader, WebBaseLoader
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_community.vectorstores import SKLearnVectorStore
from langchain_openai import ChatOpenAI
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_pinecone import PineconeVectorStore
from langchain.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from pydantic import BaseModel, Field
from typing import List, TypedDict, Optional
from langchain.schema import Document
from langgraph.graph import START, END, StateGraph
from dotenv import load_dotenv
load_dotenv()
url = [
"https://www.investopedia.com/",
"https://www.fool.com/",
"https://www.morningstar.com/",
"https://www.kiplinger.com/",
"https://www.nerdwallet.com/"
]
# Initialize Embedding and Vector DB
embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
# Initialize Pinecone connection
try:
pc = PineconeVectorStore(
pinecone_api_key=os.environ.get('PINCE_CONE_LIGHT'),
embedding=embedding_model,
index_name='rag-rubic',
namespace='vectors_lightmodel'
)
retriever = pc.as_retriever(search_kwargs={"k": 10})
except Exception as e:
print(f"Pinecone connection error: {e}")
# Fallback to SKLearn vector store if Pinecone fails
retriever = None
# Initialize the LLM
llm = ChatOpenAI(
model='gpt-4o-mini',
api_key=os.environ.get('OPEN_AI_KEY'),
temperature=0.2
)
# Schema for grading documents
class GradeDocuments(BaseModel):
binary_score: str = Field(description="Documents are relevant to the question, 'yes' or 'no'")
structured_llm_grader = llm.with_structured_output(GradeDocuments)
# Define System and Grading prompt
system = """You are a grader assessing relevance of a retrieved document to a user question.
If the document contains keyword(s) or semantic meaning related to the question, grade it as relevant.
Give a binary score 'yes' or 'no' score to indicate whether the document is relevant to the question."""
grade_prompt = ChatPromptTemplate.from_messages([
("system", system),
("human", "Retrieved document: \n\n {documents} \n\n User question: {question}")
])
retrieval_grader = grade_prompt | structured_llm_grader
# RAG Prompt template
prompt = PromptTemplate(
template='''
You are a Registered Investment Advisor with expertise in Indian financial markets and client relations.
You must understand what the user is asking about their financial investments and respond to their queries based on the information in the documents only.
Use the following documents to answer the question. If you do not know the answer, say you don't know.
Query: {question}
Documents: {context}
''',
input_variables=['question', 'context']
)
rag_chain = prompt | llm | StrOutputParser()
# Web search tool for adding data from websites
web_search_tool = TavilySearchResults(api_key=os.environ.get('TAVILY_API_KEY'), k=10)
# Define Graph states and transitions
class GraphState(TypedDict):
question: str
generation: Optional[str]
need_web_search: Optional[str] # Changed from 'web_search' to 'need_web_search'
documents: List
def retrieve_db(state):
"""Gather data for the query."""
question = state['question']
if retriever:
try:
results = retriever.invoke(question)
return {'documents': results, 'question': question}
except Exception as e:
print(f"Retriever error: {e}")
# If retriever fails or doesn't exist, return empty documents
return {'documents': [], 'question': question, 'need_web_search': 'yes'}
def grade_docs(state):
"""Grades the docs generated by the retriever_db
If 1, returns the docs if 0 proceeds for web search"""
question = state['question']
docs = state['documents']
filterd_data = []
web = "no"
for data in docs:
score = retrieval_grader.invoke({'question':question, 'documents':docs})
grade = score.binary_score
if grade == 'yes':
filterd_data.append(data)
else:
#print("----------Failed, proceeding with WebSearch------------------")
web = 'yes'
return {"documents": filterd_data, "question": question, "need_web_search": web}
def decide(state):
"""Decide if the generation should be based on DB or web search DATA"""
web = state.get('need_web_search', 'no') # Updated key name
if web == 'yes':
return 'web_search'
else:
return 'generate'
def web_search(state):
"""Perform a web search and store both content and source URLs."""
question = state['question']
documents = state["documents"]
# Get search results
results = web_search_tool.invoke({"query": question})
# Process results with sources
docs = []
for res in results:
content = res["content"] # Extract answer content
source = res["url"] # Extract source URL
# Create Document with metadata
doc = Document(page_content=content, metadata={"source": source})
docs.append(doc)
if not results:
#print("No results from web search. Returning default response.")
return {"documents": [], "question": question}
documents.extend(docs)
return {"documents": documents, "question": question}
def generate(state):
#print("Inside generate function") # Debugging
documents = state['documents']
question = state['question']
# Generate response using retrieved documents
response = rag_chain.invoke({'context': documents, 'question': question})
# Extract source URLs
sources = [doc.metadata.get("source", "Unknown source") for doc in documents if "source" in doc.metadata]
# Format response with citations
formatted_response = response + "\n\nSources:\n" + "\n".join(sources) if sources else response
#print("Generated response:", formatted_response) # Debugging
# Return response with sources
return {
'documents': documents,
'question': question,
'generation': formatted_response # Append sources to the response
}
# Compile Workflow
workflow = StateGraph(GraphState)
workflow.add_node("retrieve", retrieve_db)
workflow.add_node("grader", grade_docs)
workflow.add_node("web_search", web_search) # Now this won't conflict with the state key
workflow.add_node("generate", generate)
workflow.add_edge(START, "retrieve")
workflow.add_edge("retrieve", "grader")
workflow.add_conditional_edges(
"grader",
decide,
{
'web_search': 'web_search',
'generate': 'generate'
},
)
workflow.add_edge("web_search", "generate")
workflow.add_edge("generate", END)
# Compile the graph
crag = workflow.compile()
# Define Gradio Interface with proper chat history management
def process_query(user_input, history):
# Initialize history if it's None
if history is None:
history = []
# Add user input to history
history.append((user_input, ""))
# Process the query
inputs = {"question": user_input}
response = ""
try:
# Execute the graph
result = crag.invoke(inputs)
if result and 'generation' in result:
response = result['generation']
else:
response = "I couldn't find relevant information to answer your question."
except Exception as e:
#print(f"Error in crag execution: {e}")
response = "I encountered an error while processing your request. Please try again."
# Update the last response in history
history[-1] = (user_input, response)
return history, ""
# Gradio Interface
with gr.Blocks() as demo:
gr.Markdown("# 🤖 RAG-Powered Financial Advisor Chatbot")
chatbot = gr.Chatbot(
[],
elem_id="chatbot",
bubble_full_width=False,
height=600,
avatar_images=(None, "🤖")
)
with gr.Row():
msg = gr.Textbox(
placeholder="Ask me anything about Indian financial markets...",
label="Your question:",
scale=9
)
submit_btn = gr.Button("Send", scale=1)
clear_btn = gr.Button("Clear Chat")
# Set up event handlers
submit_click_event = submit_btn.click(
process_query,
inputs=[msg, chatbot],
outputs=[chatbot, msg]
)
msg.submit(
process_query,
inputs=[msg, chatbot],
outputs=[chatbot, msg]
)
clear_btn.click(lambda: [], outputs=[chatbot])
if __name__ == "__main__":
demo.launch() |