Skip to content

Commit 31a9d1f

Browse files
committed
modified search
1 parent fa932be commit 31a9d1f

File tree

3 files changed

+28
-49
lines changed

3 files changed

+28
-49
lines changed

app.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ def handle_auth(supabase):
4949
def render_sidebar_config():
5050
render_settings(st)
5151
api_key = st.sidebar.text_input("SambaNova API Key", type="password", disabled=st.session_state.is_processing_docs)
52-
st.sidebar.caption("Don't have an API key? [Get SambaNova API Key](https://cloud.sambanova.ai/)")
5352
model_choice = st.sidebar.selectbox("Choose model", options=[
5453
"Llama-3.3-Swallow-70B-Instruct-v0.4",
5554
"Llama-4-Maverick-17B-128E-Instruct",
@@ -65,10 +64,10 @@ def render_sidebar_config():
6564
"Qwen3-32B",
6665
"gpt-oss-120b"
6766
], index=5, disabled=st.session_state.is_processing_docs)
68-
firecrawl_api_key = st.sidebar.text_input("Firecrawl API Key", type="password", disabled=st.session_state.is_processing_docs)
69-
render_web_search_toggle(st, firecrawl_api_key)
67+
serper_api_key = st.sidebar.text_input("Serper API Key", type="password", disabled=st.session_state.is_processing_docs)
68+
render_web_search_toggle(st, serper_api_key)
7069
render_auth_buttons(st)
71-
return api_key, model_choice, firecrawl_api_key
70+
return api_key, model_choice, serper_api_key
7271

7372
def handle_file_upload(api_key, model_choice):
7473
uploaded_files = st.file_uploader("Upload up to 3 files each under 2MB", type=["pdf","txt","docx"], accept_multiple_files=True, disabled=st.session_state.is_processing_docs) or []
@@ -105,7 +104,7 @@ def render_chat_interface():
105104
st.session_state.memory = ensure_memory_from_chat(current_chat)
106105
render_chat_history(current_chat)
107106

108-
def handle_chat_interaction(api_key, model_choice, firecrawl_api_key, supabase):
107+
def handle_chat_interaction(api_key, model_choice, serper_api_key, supabase):
109108
if not st.session_state.is_processing_docs:
110109
q = st.chat_input("Your question")
111110
if q:
@@ -131,8 +130,8 @@ def handle_chat_interaction(api_key, model_choice, firecrawl_api_key, supabase):
131130
if not err:
132131
history_text = render_history_text(st.session_state.memory)
133132

134-
if st.session_state.use_web_search and firecrawl_api_key:
135-
final, sources = web_search_answer(llm, firecrawl_api_key, q)
133+
if st.session_state.use_web_search and serper_api_key:
134+
final, sources = web_search_answer(llm, serper_api_key, q)
136135
source_block = "\n\n**Web Sources used:**\n" + "\n".join(f"- {s}" for s in sources) if sources else ""
137136
else:
138137
if "vectorstore" in st.session_state:
@@ -173,12 +172,12 @@ def main():
173172

174173
handle_auth(supabase)
175174

176-
api_key, model_choice, firecrawl_api_key = render_sidebar_config()
175+
api_key, model_choice, serper_api_key = render_sidebar_config()
177176
handle_file_upload(api_key, model_choice)
178177
render_chat_interface()
179178

180179
if api_key:
181-
handle_chat_interaction(api_key, model_choice, firecrawl_api_key, supabase)
180+
handle_chat_interaction(api_key, model_choice, serper_api_key, supabase)
182181
else:
183182
st.info("Enter your SambaNova API key in the sidebar.")
184183

services/ai_processor.py

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from langchain_pinecone import PineconeVectorStore
99
from langchain.text_splitter import RecursiveCharacterTextSplitter
1010
from langchain.docstore.document import Document
11-
from langchain.docstore.document import Document
11+
from langchain_community.utilities import GoogleSerperAPIWrapper
1212
from config import PINECONE_API_KEY, PINECONE_INDEX_NAME
1313
from utils.common_utils import chunk_id, extract_text_from_file
1414

@@ -72,41 +72,21 @@ def index_docs(vectorstore, docs):
7272
vectorstore.add_documents(docs, ids=ids)
7373
return len(docs)
7474

75-
from firecrawl import FirecrawlApp
76-
7775
# --- Search ---
78-
def web_search_answer(llm, firecrawl_api_key: str, question: str) -> tuple[str, list[str]]:
79-
app = FirecrawlApp(api_key=firecrawl_api_key)
80-
try:
81-
# Using 'search' mode to get results similar to Google Search
82-
sr = app.search(question,limit=5)
83-
84-
# Firecrawl returns an object with a 'data' or 'web' attribute containing the list of results.
85-
# Based on testing, it returns an object with a 'web' attribute.
86-
if hasattr(sr, 'web'):
87-
results_data = sr.web
88-
elif hasattr(sr, 'data'):
89-
results_data = sr.data
90-
elif isinstance(sr, dict):
91-
results_data = sr.get('data', sr.get('web', []))
92-
else:
93-
results_data = []
94-
95-
search_results = "\n".join(
96-
f"Title: {getattr(r, 'title', 'No Title')}\nLink: {getattr(r, 'url', getattr(r, 'link', ''))}\nSnippet: {getattr(r, 'description', getattr(r, 'snippet', ''))}\n"
97-
for r in results_data
98-
)
99-
100-
prompt = PromptTemplate.from_template(
101-
"Based on these web search results, answer concisely and include key sources.\n\n{results}\n\nQuestion: {q}\n\nAnswer:"
102-
)
103-
chain = LLMChain(llm=llm, prompt=prompt, output_key="ans")
104-
ans = chain({"results": search_results, "q": question})["ans"].strip()
105-
106-
sources = [getattr(r, 'url', getattr(r, 'link', '')) for r in results_data if getattr(r, 'url', None) or getattr(r, 'link', None)]
107-
return ans, sources
108-
except Exception as e:
109-
return f"Error performing web search: {str(e)}", []
76+
def web_search_answer(llm, serper_api_key: str, question: str) -> tuple[str, list[str]]:
77+
search_tool = GoogleSerperAPIWrapper(serper_api_key=serper_api_key, k=5)
78+
sr = search_tool.results(question)
79+
organic = sr.get("organic", [])
80+
search_results = "\n".join(
81+
f"Snippet: {o.get('snippet','')}\nLink: {o.get('link','')}" for o in organic
82+
)
83+
prompt = PromptTemplate.from_template(
84+
"Based on these web search results, answer concisely and include key sources.\n\n{results}\n\nQuestion: {q}\n\nAnswer:"
85+
)
86+
chain = LLMChain(llm=llm, prompt=prompt, output_key="ans")
87+
ans = chain({"results": search_results, "q": question})["ans"].strip()
88+
sources = [o.get("link","") for o in organic if o.get("link")]
89+
return ans, sources
11090

11191
# --- Guardrails ---
11292
class InputGuardrails:

ui/components.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ def render_auth_buttons(st):
2222
st.session_state.show_signup = False
2323
st.rerun()
2424

25-
def render_web_search_toggle(st, firecrawl_api_key: str | None):
26-
enabled = st.session_state.use_web_search and bool(firecrawl_api_key)
25+
def render_web_search_toggle(st, serper_api_key: str | None):
26+
enabled = st.session_state.use_web_search and bool(serper_api_key)
2727
ws_label = f"Web search: {'On' if enabled else 'Off'}"
28-
new_val = st.sidebar.toggle(ws_label, value=enabled, disabled=not firecrawl_api_key, key="web_search_toggle")
28+
new_val = st.sidebar.toggle(ws_label, value=enabled, disabled=not serper_api_key, key="web_search_toggle")
2929
st.session_state.use_web_search = bool(new_val)
30-
31-
st.sidebar.caption("Don't have an API key? [Get Firecrawl API key](https://www.firecrawl.dev/)")
30+
if not serper_api_key:
31+
st.sidebar.caption("Add a Serper API key to enable web search.")
3232

3333
def render_chat_search(st):
3434
st.sidebar.title("Search Chats")

0 commit comments

Comments
 (0)