I aim to develop a question answering system using Gemini. The question generator will prompt 10 general questions regarding the weather in various countries, while the answer generator will exclusively respond to questions present in the description. If a mentioned country's weather details are not provided in the description, the system will reply with "I don't have enough information" for that specific country. The provided code lacks conversational flow and exhibits repetitive questioning in the question generator. It fails to engage users in a natural dialogue, as the questions seem to lack variety and depth.
import google.generativeai as genai
import os
import random
from tenacity import retry, wait_random_exponential, stop_after_attempt
import json
# Set up Google API key
os.environ['GOOGLE_API_KEY'] = "GOOGLE_API_KEY"
genai.configure(api_key=os.environ['GOOGLE_API_KEY'])
# Select the model
model = genai.GenerativeModel('gemini-pro')
def generate_prompt_question():
return f"""
YOU ARE A CHATBOT HELPING USERS TO GAIN INFORMATION ABOUT WEATHER OF DIFFERENT CITIES.
ASK SINGLE QUESTION PER MESSAGE.
"""
def generate_prompt_answer(question, description):
return f"""
ANSWER THE QUESTION BASED ON THE DESCRIPTION.
IF THE DESCRIPTION DOES NOT INCLUDE THE REQUESTED INFORMATION, MENTION THAT YOU DON'T KNOW.
{question} and {description}
"""
@retry(wait=wait_random_exponential(multiplier=1, max=10), stop=stop_after_attempt(10))
def conversation(description, verbose=True):
dialogue_messages = []
for _ in range(10):
try:
prompt_question = generate_prompt_qustion()
generated_question = model.generate_content([prompt_question])
question = generated_question.text.strip()
dialogue_messages.append({"QUESTION": question})
print("QUESTION: \n{question}\n")
prompt_answer = generate_prompt_answer(question, description)
generated_response = model.generate_content([prompt_answer])
answer = generated_response.text.strip()
dialogue_messages.append({"ANSWER": answer})
print("ANSWER: \n{answer}\n")
except Exception as e:
print(f"Error occurred: {e}")
return dialogue_messages
description = """
As of the latest data available,
the temperatures across various countries exhibit a diverse range of climates and conditions.
In North America, the United States experiences a spectrum of temperatures,
ranging from the icy chill of Alaska to the sweltering heat of Arizona.
Moving southward, Mexico boasts warmer climates,
with regions such as the Yucatan Peninsula and Baja California experiencing consistently high temperatures.
Across the Atlantic, European nations like Norway and Sweden endure frigid winters,
while countries like Spain and Italy enjoy more moderate climates, with warm summers and mild winters.
"""
dialogue = conversation(description, verbose=True)
with open('conversation.json', 'w') as outfile:
json.dump(dialogue, outfile, indent=4)