Wajahat698 commited on
Commit
1ea607d
·
verified ·
1 Parent(s): 6fe8f4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -6
app.py CHANGED
@@ -8,6 +8,7 @@ from langchain_community.vectorstores import FAISS
8
  from langchain_openai import OpenAIEmbeddings
9
  from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
10
  from langchain.text_splitter import RecursiveCharacterTextSplitter
 
11
 
12
  from langchain.agents import tool, AgentExecutor
13
  from langchain.agents.output_parsers.openai_tools import OpenAIToolsAgentOutputParser
@@ -1042,24 +1043,25 @@ def google_search(query):
1042
 
1043
  # Process the results to extract links and snippets
1044
  organic_results = results.get("organic_results", [])
1045
- valid_results = []
1046
  for result in organic_results:
1047
  link = result.get("link", "")
1048
  snippet = result.get("snippet", "")
1049
- # Validate the link (basic check to avoid blank or invalid links)
 
1050
  if link.startswith("http"):
1051
- valid_results.append({"link": link, "snippet": snippet})
 
1052
 
1053
  # Ensure we return meaningful results
1054
- if valid_results:
1055
- return valid_results
1056
  return ["No valid links found"]
1057
 
1058
  except Exception as e:
1059
  logger.error(f"Error in Google search: {e}")
1060
  return ["Error occurred during Google search"]
1061
 
1062
-
1063
  # RAG response function
1064
  def rag_response(query):
1065
  try:
 
8
  from langchain_openai import OpenAIEmbeddings
9
  from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
10
  from langchain.text_splitter import RecursiveCharacterTextSplitter
11
+ from urllib.parse import urlparse
12
 
13
  from langchain.agents import tool, AgentExecutor
14
  from langchain.agents.output_parsers.openai_tools import OpenAIToolsAgentOutputParser
 
1043
 
1044
  # Process the results to extract links and snippets
1045
  organic_results = results.get("organic_results", [])
1046
+ formatted_results = []
1047
  for result in organic_results:
1048
  link = result.get("link", "")
1049
  snippet = result.get("snippet", "")
1050
+
1051
+ # Shorten the URL to its domain
1052
  if link.startswith("http"):
1053
+ domain = urlparse(link).netloc
1054
+ formatted_results.append(f"{snippet} - [{domain}]({link})")
1055
 
1056
  # Ensure we return meaningful results
1057
+ if formatted_results:
1058
+ return formatted_results
1059
  return ["No valid links found"]
1060
 
1061
  except Exception as e:
1062
  logger.error(f"Error in Google search: {e}")
1063
  return ["Error occurred during Google search"]
1064
 
 
1065
  # RAG response function
1066
  def rag_response(query):
1067
  try: