Spaces:
Sleeping
Sleeping
Hasnan Ramadhan
commited on
Commit
·
49d345d
1
Parent(s):
3296119
fixing get key api
Browse files
app.py
CHANGED
|
@@ -5,15 +5,15 @@ from langchain_community.document_loaders import PyMuPDFLoader
|
|
| 5 |
import requests
|
| 6 |
from groq import Groq
|
| 7 |
import os
|
| 8 |
-
from dotenv import load_dotenv
|
| 9 |
import tempfile
|
| 10 |
from googlesearch import search
|
| 11 |
from bs4 import BeautifulSoup
|
| 12 |
from urllib.parse import urljoin, urlparse
|
| 13 |
import re
|
| 14 |
|
| 15 |
-
load_dotenv()
|
| 16 |
-
|
| 17 |
class DocumentState(TypedDict):
|
| 18 |
documents: list[dict]
|
| 19 |
summaries: list[str]
|
|
@@ -63,7 +63,8 @@ def get_groq_response(prompt):
|
|
| 63 |
|
| 64 |
def google_search_agent(state: DocumentState) -> DocumentState:
|
| 65 |
"""Performs Google search and extracts content from results."""
|
| 66 |
-
|
|
|
|
| 67 |
return state
|
| 68 |
|
| 69 |
try:
|
|
@@ -110,7 +111,8 @@ def google_search_agent(state: DocumentState) -> DocumentState:
|
|
| 110 |
|
| 111 |
def search_analyzer_agent(state: DocumentState) -> DocumentState:
|
| 112 |
"""Analyzes user query to determine if web search is needed."""
|
| 113 |
-
|
|
|
|
| 114 |
return state
|
| 115 |
|
| 116 |
# Keywords that typically indicate need for current information
|
|
@@ -120,26 +122,30 @@ def search_analyzer_agent(state: DocumentState) -> DocumentState:
|
|
| 120 |
'explain', 'information about', 'tell me about', 'research'
|
| 121 |
]
|
| 122 |
|
| 123 |
-
query_lower =
|
| 124 |
state['needs_search'] = any(indicator in query_lower for indicator in search_indicators)
|
| 125 |
|
| 126 |
return state
|
| 127 |
|
| 128 |
def search_response_agent(state: DocumentState) -> DocumentState:
|
| 129 |
"""Generates response based on search results."""
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
| 131 |
# Fallback to regular LLM response
|
| 132 |
-
|
| 133 |
-
|
|
|
|
| 134 |
return state
|
| 135 |
|
| 136 |
# Prepare search results for LLM
|
| 137 |
search_context = "\n\n".join([
|
| 138 |
f"Source: {result['title']} ({result['url']})\nContent: {result['content']}"
|
| 139 |
-
for result in
|
| 140 |
])
|
| 141 |
|
| 142 |
-
prompt = f"""Based on the following search results, provide a comprehensive and accurate answer to the user's question: "{
|
| 143 |
|
| 144 |
Search Results:
|
| 145 |
{search_context}
|
|
|
|
| 5 |
import requests
|
| 6 |
from groq import Groq
|
| 7 |
import os
|
| 8 |
+
# from dotenv import load_dotenv
|
| 9 |
import tempfile
|
| 10 |
from googlesearch import search
|
| 11 |
from bs4 import BeautifulSoup
|
| 12 |
from urllib.parse import urljoin, urlparse
|
| 13 |
import re
|
| 14 |
|
| 15 |
+
# load_dotenv()
|
| 16 |
+
print(os.getenv("GROQ_API_KEY"))
|
| 17 |
class DocumentState(TypedDict):
|
| 18 |
documents: list[dict]
|
| 19 |
summaries: list[str]
|
|
|
|
| 63 |
|
| 64 |
def google_search_agent(state: DocumentState) -> DocumentState:
|
| 65 |
"""Performs Google search and extracts content from results."""
|
| 66 |
+
search_query = state.get('search_query')
|
| 67 |
+
if not search_query or not isinstance(search_query, str):
|
| 68 |
return state
|
| 69 |
|
| 70 |
try:
|
|
|
|
| 111 |
|
| 112 |
def search_analyzer_agent(state: DocumentState) -> DocumentState:
|
| 113 |
"""Analyzes user query to determine if web search is needed."""
|
| 114 |
+
search_query = state.get('search_query')
|
| 115 |
+
if not search_query or not isinstance(search_query, str):
|
| 116 |
return state
|
| 117 |
|
| 118 |
# Keywords that typically indicate need for current information
|
|
|
|
| 122 |
'explain', 'information about', 'tell me about', 'research'
|
| 123 |
]
|
| 124 |
|
| 125 |
+
query_lower = search_query.lower()
|
| 126 |
state['needs_search'] = any(indicator in query_lower for indicator in search_indicators)
|
| 127 |
|
| 128 |
return state
|
| 129 |
|
| 130 |
def search_response_agent(state: DocumentState) -> DocumentState:
|
| 131 |
"""Generates response based on search results."""
|
| 132 |
+
search_results = state.get('search_results')
|
| 133 |
+
search_query = state.get('search_query')
|
| 134 |
+
|
| 135 |
+
if not search_results or not isinstance(search_results, list):
|
| 136 |
# Fallback to regular LLM response
|
| 137 |
+
if search_query and isinstance(search_query, str):
|
| 138 |
+
llm_response = get_llm_response(search_query)
|
| 139 |
+
state['summaries'] = [llm_response['response']]
|
| 140 |
return state
|
| 141 |
|
| 142 |
# Prepare search results for LLM
|
| 143 |
search_context = "\n\n".join([
|
| 144 |
f"Source: {result['title']} ({result['url']})\nContent: {result['content']}"
|
| 145 |
+
for result in search_results
|
| 146 |
])
|
| 147 |
|
| 148 |
+
prompt = f"""Based on the following search results, provide a comprehensive and accurate answer to the user's question: "{search_query}"
|
| 149 |
|
| 150 |
Search Results:
|
| 151 |
{search_context}
|