Spaces:
Runtime error
Runtime error
Dustin Haring
commited on
Commit
·
b6b8370
1
Parent(s):
41c0e52
Integrate Google Custom Search tool
Browse files- app.py +7 -7
- google_custom_search.py +54 -0
app.py
CHANGED
@@ -13,6 +13,7 @@ from langchain_core.messages import AIMessage, HumanMessage
|
|
13 |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
14 |
from langchain_core.pydantic_v1 import BaseModel, Field
|
15 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
|
|
16 |
|
17 |
GOOGLE_API = "AIzaSyAz7e9gxDpUomG1YrE1W0evKC16cHvqgKc"
|
18 |
|
@@ -104,8 +105,8 @@ def main():
|
|
104 |
agent_chain = create_agent_chain(tools, llm)
|
105 |
user_input = get_user_input()
|
106 |
if user_input:
|
107 |
-
response = llm.invoke(user_input)
|
108 |
-
display_response(response)
|
109 |
# prompt = """
|
110 |
# You are a fact-checker. You are asked to verify the following statement based on the information you get from your tool, the search result we provided,
|
111 |
# and your knowledge. You should provide a response that is based on the information you have and that is as accurate as possible.
|
@@ -118,11 +119,10 @@ News Article Title:
|
|
118 |
"""
|
119 |
prompt += f'"{response}"\n'
|
120 |
prompt += "\n5 Strings from reputable news sites:\n"
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
prompt += '"dummy text"'
|
126 |
|
127 |
new_prompt = st.text_area(prompt)
|
128 |
|
|
|
13 |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
14 |
from langchain_core.pydantic_v1 import BaseModel, Field
|
15 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
16 |
+
from google_custom_search import custom_google_search
|
17 |
|
18 |
GOOGLE_API = "AIzaSyAz7e9gxDpUomG1YrE1W0evKC16cHvqgKc"
|
19 |
|
|
|
105 |
agent_chain = create_agent_chain(tools, llm)
|
106 |
user_input = get_user_input()
|
107 |
if user_input:
|
108 |
+
# response = llm.invoke(user_input)
|
109 |
+
# display_response(response)
|
110 |
# prompt = """
|
111 |
# You are a fact-checker. You are asked to verify the following statement based on the information you get from your tool, the search result we provided,
|
112 |
# and your knowledge. You should provide a response that is based on the information you have and that is as accurate as possible.
|
|
|
119 |
"""
|
120 |
prompt += f'"{response}"\n'
|
121 |
prompt += "\n5 Strings from reputable news sites:\n"
|
122 |
+
|
123 |
+
customSearchResults = custom_google_search(response)
|
124 |
+
for result in customSearchResults:
|
125 |
+
prompt += result
|
|
|
126 |
|
127 |
new_prompt = st.text_area(prompt)
|
128 |
|
google_custom_search.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# This file/function uses a custom implemented Google Custom Search tool to search and return the top 5 news article titles for a given prompt
|
2 |
+
|
3 |
+
from googleapiclient.discovery import build
|
4 |
+
import pprint
|
5 |
+
|
6 |
+
# Defaults to using Dustin's GCP API keys
|
7 |
+
def custom_google_search(search_term, api_key="AIzaSyA4oDDFtPxAfmPC8EcfQrkByb9xKm2QfMc", cse_id="31e85635d41bd4040", num_results=5):
|
8 |
+
"""Performs a Google Custom Search and returns titles of top results.
|
9 |
+
|
10 |
+
Args:
|
11 |
+
search_term: The search query.
|
12 |
+
api_key: Your Google API Key.
|
13 |
+
cse_id: Your Google Custom Search Engine ID.
|
14 |
+
num_results: The number of desired results (default: 5).
|
15 |
+
|
16 |
+
Returns:
|
17 |
+
A list of titles for the top search results.
|
18 |
+
"""
|
19 |
+
|
20 |
+
service = build("customsearch", "v1", developerKey=api_key)
|
21 |
+
results = service.cse().list(
|
22 |
+
q=search_term,
|
23 |
+
cx=cse_id,
|
24 |
+
num=num_results # Adjust the number of results as needed
|
25 |
+
).execute()
|
26 |
+
|
27 |
+
search_result_titles = []
|
28 |
+
for item in results['items']:
|
29 |
+
# extended_title = item['title'] + item['snippet']
|
30 |
+
try:
|
31 |
+
# extended_title = item['pagemap']['metatags'][0]['og:description']
|
32 |
+
extended_title = "* '" + item['pagemap']['metatags'][0]['og:description'] + "'"
|
33 |
+
except:
|
34 |
+
# extended_title = item['title']
|
35 |
+
extended_title = "* '" + item['title'] + "'"
|
36 |
+
|
37 |
+
# pprint.pprint(f"dah=={item['pagemap']['metatags'][0]['og:description']}")
|
38 |
+
search_result_titles.append(extended_title)
|
39 |
+
# search_result_titles = [item['title'] for item in results['items']]
|
40 |
+
return search_result_titles
|
41 |
+
|
42 |
+
if __name__ == "__main__":
|
43 |
+
# Examples
|
44 |
+
# search_phrase = "Californias governor rejects a bill to give unemployment checks to striking workers" # True
|
45 |
+
# search_phrase = "1,000 US troops deploying to build offshore port for Gaza aid" # True
|
46 |
+
# search_phrase = "More than 2500 migrants crossing the Mediterranean died or went missing this year" # True
|
47 |
+
# search_phrase = "Everything JK Rowling Would Be Willing To Do To Protect Her AntiTrans Views" # False
|
48 |
+
search_phrase = "Pat McAfee thought USC coach Lincoln Riley died because of President Biden's State of the Union address" # Unknown
|
49 |
+
top_titles = custom_google_search(search_phrase, my_api_key, my_cse_id)
|
50 |
+
|
51 |
+
print("Top Search Results:")
|
52 |
+
print("===================")
|
53 |
+
for title in top_titles:
|
54 |
+
print(f'{title}')
|