Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -55,6 +55,9 @@ st.set_page_config(layout="wide")
|
|
| 55 |
import logging
|
| 56 |
import asyncio
|
| 57 |
import re
|
|
|
|
|
|
|
|
|
|
| 58 |
# Set up logging to suppress Streamlit warnings about experimental functions
|
| 59 |
logging.getLogger('streamlit').setLevel(logging.ERROR)
|
| 60 |
INITIAL_MESSAGE_LIMIT = 100
|
|
@@ -1025,39 +1028,51 @@ def search_knowledge_base(query):
|
|
| 1025 |
return retrieved_docs
|
| 1026 |
|
| 1027 |
def google_search(query):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1028 |
try:
|
| 1029 |
-
#
|
| 1030 |
-
|
| 1031 |
-
|
| 1032 |
-
|
| 1033 |
-
"
|
| 1034 |
-
"
|
| 1035 |
-
"hl": "en", # Language: English
|
| 1036 |
-
"gl": "us", # Geolocation: United States
|
| 1037 |
}
|
| 1038 |
|
| 1039 |
-
#
|
| 1040 |
-
|
| 1041 |
-
organic_results = results.get("organic_results", [])
|
| 1042 |
-
formatted_results = []
|
| 1043 |
|
| 1044 |
-
|
| 1045 |
-
|
| 1046 |
-
|
|
|
|
| 1047 |
|
| 1048 |
-
|
| 1049 |
-
|
| 1050 |
-
formatted_results.append(f"{snippet} - [Source]({link})")
|
| 1051 |
-
|
| 1052 |
-
if formatted_results:
|
| 1053 |
-
return formatted_results
|
| 1054 |
-
return ["No valid links found"]
|
| 1055 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1056 |
except Exception as e:
|
| 1057 |
-
|
|
|
|
| 1058 |
return ["Error occurred during Google search"]
|
| 1059 |
|
| 1060 |
|
|
|
|
| 1061 |
def rag_response(query):
|
| 1062 |
"""
|
| 1063 |
Handle queries by searching both static and dynamically uploaded knowledge bases.
|
|
@@ -1093,7 +1108,6 @@ def rag_response(query):
|
|
| 1093 |
return "An error occurred during the RAG response generation process."
|
| 1094 |
|
| 1095 |
|
| 1096 |
-
# Define tools
|
| 1097 |
@tool
|
| 1098 |
def knowledge_base_tool(query: str):
|
| 1099 |
"""Query the knowledge base and retrieve a response."""
|
|
@@ -1104,7 +1118,24 @@ def google_search_tool(query: str):
|
|
| 1104 |
"""Perform a Google search using the SERPER API."""
|
| 1105 |
return google_search(query)
|
| 1106 |
|
| 1107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1108 |
|
| 1109 |
prompt_message = f"""
|
| 1110 |
** You are a Professional copywriter tasked with creating non-flowery fluid, interconnected marketing content that integrates Trust Builders into various formats for food for the hungry. Your content should be compelling, factual, well-structured, concise, and based on the knowledgebase. Write in an active voice using the first-person perspective ("we"), and avoid the third-person perspective. Creatively interconnect trust-building elements to enhance flow and impact. Avoid using terms like Stability, Development, Competence, Relationship, Benefit, Vision, trust, beacon, beacon of hope, and realm, except where specified.
|
|
|
|
| 55 |
import logging
|
| 56 |
import asyncio
|
| 57 |
import re
|
| 58 |
+
from langchain_community.tools import TavilySearchResults
|
| 59 |
+
|
| 60 |
+
|
| 61 |
# Set up logging to suppress Streamlit warnings about experimental functions
|
| 62 |
logging.getLogger('streamlit').setLevel(logging.ERROR)
|
| 63 |
INITIAL_MESSAGE_LIMIT = 100
|
|
|
|
| 1028 |
return retrieved_docs
|
| 1029 |
|
| 1030 |
def google_search(query):
|
| 1031 |
+
"""
|
| 1032 |
+
Performs a Google search using the SerpApi service and retrieves search result snippets.
|
| 1033 |
+
This function uses the SerpApi client to perform a Google search based on the provided query.
|
| 1034 |
+
It extracts and returns the snippets from the organic search results.
|
| 1035 |
+
Args:
|
| 1036 |
+
query (str): The search query to be used for the Google search.
|
| 1037 |
+
Returns:
|
| 1038 |
+
list: A list of snippets from the organic search results. If an error occurs, returns a list with an error message.
|
| 1039 |
+
Raises:
|
| 1040 |
+
requests.exceptions.HTTPError: If an HTTP error occurs during the search, it is logged and an error message is returned.
|
| 1041 |
+
Exception: For any other general errors, they are logged and an error message is returned.
|
| 1042 |
+
"""
|
| 1043 |
try:
|
| 1044 |
+
# Set up connection to google.serper.dev API
|
| 1045 |
+
conn = http.client.HTTPSConnection("google.serper.dev")
|
| 1046 |
+
payload = json.dumps({"q": query})
|
| 1047 |
+
headers = {
|
| 1048 |
+
"X-API-KEY": "07b4113c2730711b568623b13f7c88078bab9c78",
|
| 1049 |
+
"Content-Type": "application/json",
|
|
|
|
|
|
|
| 1050 |
}
|
| 1051 |
|
| 1052 |
+
# Send POST request to the API
|
| 1053 |
+
conn.request("POST", "/search", payload, headers)
|
|
|
|
|
|
|
| 1054 |
|
| 1055 |
+
# Get response and decode the data
|
| 1056 |
+
res = conn.getresponse()
|
| 1057 |
+
data = res.read()
|
| 1058 |
+
results = json.loads(data.decode("utf-8"))
|
| 1059 |
|
| 1060 |
+
# Extract snippets from organic search results
|
| 1061 |
+
snippets = [result["snippet"] for result in results.get("organic", [])]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1062 |
|
| 1063 |
+
# Return the list of snippets
|
| 1064 |
+
return snippets
|
| 1065 |
+
except http.client.HTTPException as http_err:
|
| 1066 |
+
# Log HTTP errors and return a specific error message
|
| 1067 |
+
print(f"HTTP error occurred: {http_err}")
|
| 1068 |
+
return ["HTTP error occurred during Google search"]
|
| 1069 |
except Exception as e:
|
| 1070 |
+
# Log any other general errors and return a generic error message
|
| 1071 |
+
print(f"General Error: {e}")
|
| 1072 |
return ["Error occurred during Google search"]
|
| 1073 |
|
| 1074 |
|
| 1075 |
+
|
| 1076 |
def rag_response(query):
|
| 1077 |
"""
|
| 1078 |
Handle queries by searching both static and dynamically uploaded knowledge bases.
|
|
|
|
| 1108 |
return "An error occurred during the RAG response generation process."
|
| 1109 |
|
| 1110 |
|
|
|
|
| 1111 |
@tool
|
| 1112 |
def knowledge_base_tool(query: str):
|
| 1113 |
"""Query the knowledge base and retrieve a response."""
|
|
|
|
| 1118 |
"""Perform a Google search using the SERPER API."""
|
| 1119 |
return google_search(query)
|
| 1120 |
|
| 1121 |
+
tavily_tool = TavilySearchResults(
|
| 1122 |
+
max_results=5,
|
| 1123 |
+
search_depth="advanced",
|
| 1124 |
+
include_answer=True,
|
| 1125 |
+
include_raw_content=True,
|
| 1126 |
+
include_images=True,
|
| 1127 |
+
# include_domains=[...],
|
| 1128 |
+
# exclude_domains=[...],
|
| 1129 |
+
# name="...", # overwrite default tool name
|
| 1130 |
+
# description="...", # overwrite default tool description
|
| 1131 |
+
# args_schema=..., # overwrite default args_schema: BaseModel
|
| 1132 |
+
)
|
| 1133 |
+
# Compile all tool functions into a list
|
| 1134 |
+
tools = [
|
| 1135 |
+
knowledge_base_tool, # Tool for querying the knowledge base and retrieving responses
|
| 1136 |
+
tavily_tool,
|
| 1137 |
+
# google_search_tool, # Tool for performing a Google search and retrieving search result snippets
|
| 1138 |
+
]
|
| 1139 |
|
| 1140 |
prompt_message = f"""
|
| 1141 |
** You are a Professional copywriter tasked with creating non-flowery fluid, interconnected marketing content that integrates Trust Builders into various formats for food for the hungry. Your content should be compelling, factual, well-structured, concise, and based on the knowledgebase. Write in an active voice using the first-person perspective ("we"), and avoid the third-person perspective. Creatively interconnect trust-building elements to enhance flow and impact. Avoid using terms like Stability, Development, Competence, Relationship, Benefit, Vision, trust, beacon, beacon of hope, and realm, except where specified.
|