Wajahat698 commited on
Commit
3f2ccce
·
verified ·
1 Parent(s): a3ce15b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -25
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
- try:
1016
- # Initialize the search client and parameters
1017
- search_client = serpapi.Client(api_key=serper_api_key)
1018
 
1019
- search_params = {
1020
- "engine": "google",
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
- # Perform the search
1029
- results = search_client.search(search_params)
1030
- organic_results = results.get("organic_results", [])
1031
- formatted_results = []
 
 
 
 
 
 
 
 
 
 
 
1032
 
1033
- for result in organic_results:
1034
- link = result.get("link", "")
1035
- snippet = result.get("snippet", "")
1036
 
1037
- # Ensure proper Markdown formatting with only "Source" as the clickable text
1038
- if link.startswith("http"):
1039
- formatted_results.append(f"{snippet} - [Source]({link})")
 
1040
 
1041
- if formatted_results:
1042
- return formatted_results
1043
- return ["No valid links found"]
1044
 
 
 
 
 
 
 
1045
  except Exception as e:
1046
- print(f"Error in Google search: {e}")
 
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
- tools = [knowledge_base_tool, google_search_tool]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.