Spaces:
Paused
Paused
Commit ·
5845cbf
1
Parent(s): ba18c3d
new search query generator, removes js render in zen rows
Browse files- app.py +7 -6
- generate_response.py +3 -3
- web_search.py +9 -15
app.py
CHANGED
|
@@ -5,8 +5,8 @@ from time import asctime
|
|
| 5 |
import gradio as gr
|
| 6 |
from llama_index.core import Document, VectorStoreIndex
|
| 7 |
|
| 8 |
-
from generate_response import generate_chat_response_with_history, set_llm, is_search_query,
|
| 9 |
-
generate_chat_response_with_history_rag_return_response
|
| 10 |
from read_write_index import read_write_index
|
| 11 |
from web_search import search
|
| 12 |
|
|
@@ -17,12 +17,13 @@ rag_similarity = False
|
|
| 17 |
|
| 18 |
|
| 19 |
def google_search_chat(message, history):
|
| 20 |
-
|
| 21 |
-
if is_search_query(
|
| 22 |
-
search_results = search(message,
|
| 23 |
print(f'Search results returned: {len(search_results)}')
|
|
|
|
| 24 |
relevant_content = ""
|
| 25 |
-
for
|
| 26 |
relevant_content = relevant_content + "\n" + ''.join(result['html'])
|
| 27 |
|
| 28 |
if relevant_content != "":
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
from llama_index.core import Document, VectorStoreIndex
|
| 7 |
|
| 8 |
+
from generate_response import generate_chat_response_with_history, set_llm, is_search_query, \
|
| 9 |
+
generate_chat_response_with_history_rag_return_response, google_question
|
| 10 |
from read_write_index import read_write_index
|
| 11 |
from web_search import search
|
| 12 |
|
|
|
|
| 17 |
|
| 18 |
|
| 19 |
def google_search_chat(message, history):
|
| 20 |
+
gquestion = google_question(message, history)
|
| 21 |
+
if is_search_query(gquestion):
|
| 22 |
+
search_results = search(message, gquestion)
|
| 23 |
print(f'Search results returned: {len(search_results)}')
|
| 24 |
+
|
| 25 |
relevant_content = ""
|
| 26 |
+
for result in enumerate(search_results):
|
| 27 |
relevant_content = relevant_content + "\n" + ''.join(result['html'])
|
| 28 |
|
| 29 |
if relevant_content != "":
|
generate_response.py
CHANGED
|
@@ -152,11 +152,11 @@ def collect_history(message, history):
|
|
| 152 |
return messages
|
| 153 |
|
| 154 |
|
| 155 |
-
def
|
| 156 |
DEFAULT_TEMPLATE = """\
|
| 157 |
Given a conversation (between Human and Assistant) and a follow up message from Human, \
|
| 158 |
-
rewrite the message to be a standalone
|
| 159 |
-
from the conversation.
|
| 160 |
|
| 161 |
<Chat History>
|
| 162 |
{chat_history}
|
|
|
|
| 152 |
return messages
|
| 153 |
|
| 154 |
|
| 155 |
+
def google_question(message, history):
|
| 156 |
DEFAULT_TEMPLATE = """\
|
| 157 |
Given a conversation (between Human and Assistant) and a follow up message from Human, \
|
| 158 |
+
rewrite the message to be a standalone web engine search query that captures all relevant context \
|
| 159 |
+
from the conversation in keywords.
|
| 160 |
|
| 161 |
<Chat History>
|
| 162 |
{chat_history}
|
web_search.py
CHANGED
|
@@ -18,18 +18,12 @@ logger = logging.getLogger("agent_logger")
|
|
| 18 |
|
| 19 |
|
| 20 |
def search(msg, query_phrase):
|
|
|
|
| 21 |
try:
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
google_text
|
| 25 |
-
try:
|
| 26 |
-
print(f"asking google {msg}; rephrased: {query_phrase}")
|
| 27 |
-
google_text = search_google(msg, query_phrase)
|
| 28 |
-
except:
|
| 29 |
-
traceback.print_exc()
|
| 30 |
-
|
| 31 |
print("\n\nFinal response: ")
|
| 32 |
-
|
| 33 |
for item in google_text:
|
| 34 |
print(
|
| 35 |
f"\n##############################################################################################\nSource: {item['source']}"
|
|
@@ -42,7 +36,7 @@ def search(msg, query_phrase):
|
|
| 42 |
raise KeyboardInterrupt
|
| 43 |
except:
|
| 44 |
traceback.print_exc()
|
| 45 |
-
return
|
| 46 |
|
| 47 |
|
| 48 |
# Define a function to make a single URL request and process the response
|
|
@@ -54,10 +48,10 @@ def process_url(url):
|
|
| 54 |
warnings.simplefilter("ignore")
|
| 55 |
try:
|
| 56 |
client = ZenRowsClient(os.getenv('zenrows_api_key'))
|
| 57 |
-
params = {"js_render": "true",
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
response = client.get(url
|
| 61 |
print(f'got response, status: {response.status_code}')
|
| 62 |
result = response.text
|
| 63 |
if len(result) > 0:
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
def search(msg, query_phrase):
|
| 21 |
+
google_text = ""
|
| 22 |
try:
|
| 23 |
+
print(f"asking google {msg}; rephrased: {query_phrase}")
|
| 24 |
+
google_text = search_google(msg, query_phrase)
|
| 25 |
+
print(f"Found {len(google_text)} items")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
print("\n\nFinal response: ")
|
|
|
|
| 27 |
for item in google_text:
|
| 28 |
print(
|
| 29 |
f"\n##############################################################################################\nSource: {item['source']}"
|
|
|
|
| 36 |
raise KeyboardInterrupt
|
| 37 |
except:
|
| 38 |
traceback.print_exc()
|
| 39 |
+
return google_text
|
| 40 |
|
| 41 |
|
| 42 |
# Define a function to make a single URL request and process the response
|
|
|
|
| 48 |
warnings.simplefilter("ignore")
|
| 49 |
try:
|
| 50 |
client = ZenRowsClient(os.getenv('zenrows_api_key'))
|
| 51 |
+
# params = {"js_render": "true", "json_response": "true",
|
| 52 |
+
# "js_instructions": "%5B%7B%22click%22%3A%22.selector%22%7D%2C%7B%22wait%22%3A500%7D%2C%7B%22fill%22%3A%5B%22.input%22%2C%22value%22%5D%7D%2C%7B%22wait_for%22%3A%22.slow_selector%22%7D%5D",
|
| 53 |
+
# }
|
| 54 |
+
response = client.get(url)
|
| 55 |
print(f'got response, status: {response.status_code}')
|
| 56 |
result = response.text
|
| 57 |
if len(result) > 0:
|