Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -53,6 +53,7 @@ import logging
|
|
| 53 |
import asyncio
|
| 54 |
import re
|
| 55 |
import docx
|
|
|
|
| 56 |
|
| 57 |
# Set up logging to suppress Streamlit warnings about experimental functions
|
| 58 |
logging.getLogger('streamlit').setLevel(logging.ERROR)
|
|
@@ -1012,41 +1013,55 @@ def search_knowledge_base(query):
|
|
| 1012 |
|
| 1013 |
|
| 1014 |
def google_search(query):
|
| 1015 |
-
|
| 1016 |
-
|
| 1017 |
-
search_client = serpapi.Client(api_key=serper_api_key)
|
| 1018 |
|
| 1019 |
-
|
| 1020 |
-
|
| 1021 |
-
"q": query,
|
| 1022 |
-
"sort": "date", # Sort results by date for freshness
|
| 1023 |
-
"hl": "en", # Language: English
|
| 1024 |
-
"gl": "au", # Country: United States
|
| 1025 |
|
| 1026 |
-
|
|
|
|
| 1027 |
|
| 1028 |
-
|
| 1029 |
-
results
|
| 1030 |
-
|
| 1031 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1032 |
|
| 1033 |
-
|
| 1034 |
-
|
| 1035 |
-
snippet = result.get("snippet", "")
|
| 1036 |
|
| 1037 |
-
|
| 1038 |
-
|
| 1039 |
-
|
|
|
|
| 1040 |
|
| 1041 |
-
|
| 1042 |
-
|
| 1043 |
-
return ["No valid links found"]
|
| 1044 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1045 |
except Exception as e:
|
| 1046 |
-
|
|
|
|
| 1047 |
return ["Error occurred during Google search"]
|
| 1048 |
|
| 1049 |
|
|
|
|
| 1050 |
# RAG response function
|
| 1051 |
def rag_response(query):
|
| 1052 |
"""
|
|
@@ -1096,7 +1111,24 @@ def google_search_tool(query: str):
|
|
| 1096 |
"""Perform a Google search using the SERPER API."""
|
| 1097 |
return google_search(query)
|
| 1098 |
|
| 1099 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1100 |
|
| 1101 |
prompt_message = f"""
|
| 1102 |
** You are a Professional copywriter tasked with creating non-flowery fluid, interconnected marketing content that integrates Trust Builders into various formats for any organization. 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.
|
|
|
|
| 53 |
import asyncio
|
| 54 |
import re
|
| 55 |
import docx
|
| 56 |
+
from langchain_community.tools import TavilySearchResults
|
| 57 |
|
| 58 |
# Set up logging to suppress Streamlit warnings about experimental functions
|
| 59 |
logging.getLogger('streamlit').setLevel(logging.ERROR)
|
|
|
|
| 1013 |
|
| 1014 |
|
| 1015 |
def google_search(query):
|
| 1016 |
+
"""
|
| 1017 |
+
Performs a Google search using the SerpApi service and retrieves search result snippets.
|
|
|
|
| 1018 |
|
| 1019 |
+
This function uses the SerpApi client to perform a Google search based on the provided query.
|
| 1020 |
+
It extracts and returns the snippets from the organic search results.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1021 |
|
| 1022 |
+
Args:
|
| 1023 |
+
query (str): The search query to be used for the Google search.
|
| 1024 |
|
| 1025 |
+
Returns:
|
| 1026 |
+
list: A list of snippets from the organic search results. If an error occurs, returns a list with an error message.
|
| 1027 |
+
|
| 1028 |
+
Raises:
|
| 1029 |
+
requests.exceptions.HTTPError: If an HTTP error occurs during the search, it is logged and an error message is returned.
|
| 1030 |
+
Exception: For any other general errors, they are logged and an error message is returned.
|
| 1031 |
+
"""
|
| 1032 |
+
try:
|
| 1033 |
+
# Set up connection to google.serper.dev API
|
| 1034 |
+
conn = http.client.HTTPSConnection("google.serper.dev")
|
| 1035 |
+
payload = json.dumps({"q": query})
|
| 1036 |
+
headers = {
|
| 1037 |
+
"X-API-KEY": "07b4113c2730711b568623b13f7c88078bab9c78",
|
| 1038 |
+
"Content-Type": "application/json",
|
| 1039 |
+
}
|
| 1040 |
|
| 1041 |
+
# Send POST request to the API
|
| 1042 |
+
conn.request("POST", "/search", payload, headers)
|
|
|
|
| 1043 |
|
| 1044 |
+
# Get response and decode the data
|
| 1045 |
+
res = conn.getresponse()
|
| 1046 |
+
data = res.read()
|
| 1047 |
+
results = json.loads(data.decode("utf-8"))
|
| 1048 |
|
| 1049 |
+
# Extract snippets from organic search results
|
| 1050 |
+
snippets = [result["snippet"] for result in results.get("organic", [])]
|
|
|
|
| 1051 |
|
| 1052 |
+
# Return the list of snippets
|
| 1053 |
+
return snippets
|
| 1054 |
+
except http.client.HTTPException as http_err:
|
| 1055 |
+
# Log HTTP errors and return a specific error message
|
| 1056 |
+
print(f"HTTP error occurred: {http_err}")
|
| 1057 |
+
return ["HTTP error occurred during Google search"]
|
| 1058 |
except Exception as e:
|
| 1059 |
+
# Log any other general errors and return a generic error message
|
| 1060 |
+
print(f"General Error: {e}")
|
| 1061 |
return ["Error occurred during Google search"]
|
| 1062 |
|
| 1063 |
|
| 1064 |
+
|
| 1065 |
# RAG response function
|
| 1066 |
def rag_response(query):
|
| 1067 |
"""
|
|
|
|
| 1111 |
"""Perform a Google search using the SERPER API."""
|
| 1112 |
return google_search(query)
|
| 1113 |
|
| 1114 |
+
tavily_tool = TavilySearchResults(
|
| 1115 |
+
max_results=5,
|
| 1116 |
+
search_depth="advanced",
|
| 1117 |
+
include_answer=True,
|
| 1118 |
+
include_raw_content=True,
|
| 1119 |
+
include_images=True,
|
| 1120 |
+
# include_domains=[...],
|
| 1121 |
+
# exclude_domains=[...],
|
| 1122 |
+
# name="...", # overwrite default tool name
|
| 1123 |
+
# description="...", # overwrite default tool description
|
| 1124 |
+
# args_schema=..., # overwrite default args_schema: BaseModel
|
| 1125 |
+
)
|
| 1126 |
+
# Compile all tool functions into a list
|
| 1127 |
+
tools = [
|
| 1128 |
+
knowledge_base_tool, # Tool for querying the knowledge base and retrieving responses
|
| 1129 |
+
tavily_tool,
|
| 1130 |
+
# google_search_tool, # Tool for performing a Google search and retrieving search result snippets
|
| 1131 |
+
]
|
| 1132 |
|
| 1133 |
prompt_message = f"""
|
| 1134 |
** You are a Professional copywriter tasked with creating non-flowery fluid, interconnected marketing content that integrates Trust Builders into various formats for any organization. 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.
|