Skip to content

Instantly share code, notes, and snippets.

@beratcmn
Created March 20, 2024 14:28
Show Gist options
  • Save beratcmn/6c564b9eb784cab744f114f0a583df60 to your computer and use it in GitHub Desktop.
Save beratcmn/6c564b9eb784cab744f114f0a583df60 to your computer and use it in GitHub Desktop.
Perplexity like real time search with Gemini 1.0 Pro and Duckduckgo API
"""
Requirements:
annotated-types==0.6.0
cachetools==5.3.3
certifi==2024.2.2
cffi==1.16.0
charset-normalizer==3.3.2
click==8.1.7
colorama==0.4.6
curl-cffi==0.6.2
duckduckgo-search==5.1.0
google-ai-generativelanguage==0.4.0
google-api-core==2.17.1
google-auth==2.28.2
google-generativeai==0.4.1
googleapis-common-protos==1.63.0
grpcio==1.62.1
grpcio-status==1.62.1
idna==3.6
lxml==5.1.0
proto-plus==1.23.0
protobuf==4.25.3
pyasn1==0.5.1
pyasn1-modules==0.3.0
pycparser==2.21
pydantic==2.6.4
pydantic-core==2.16.3
requests==2.31.0
rsa==4.9
tqdm==4.66.2
typing-extensions==4.10.0
urllib3==2.2.1
"""
import google.generativeai as genai
from duckduckgo_search import DDGS
genai.configure(api_key="GOOGLE_API_KEY")
duckduckgo = DDGS(timeout=20)
generation_config = {
"temperature": 0,
"max_output_tokens": 2048,
}
safety_settings = [
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
]
model = genai.GenerativeModel(
"gemini-pro",
generation_config=generation_config,
safety_settings=safety_settings,
)
search_template = """Convert the following to a search engine query: {question}
Search query:
""".strip()
question = input("Enter a question: ")
search_query = model.generate_content(search_template.format(question=question)).text
print("-- LLM Generated search query:", search_query)
search_result = duckduckgo.text(search_query, max_results=7)
search_results = "\n\n".join(
[
"{_index}. {_title}\n{_body}\nSource: {_href}".format(
_index=i + 1,
_title=result["title"],
_body=result["body"],
_href=result["href"],
)
for i, result in enumerate(search_result)
]
)
template = """
You are a helpful search assistant. Be nice, talkative and informative.
Answer the user's question based on the search results below:
Search results:
{search_results}
User question:
{question}
Answer:
""".strip()
response = model.generate_content(
template.format(search_results=search_results, question=question)
)
print(response.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment