in webhook/main.py [0:0]
def generate_questions(project: str, location: str, text: str) -> list[dict[str, str]]:
"""Extract questions & answers using a large language model (LLM).
For more information, see:
https://cloud.google.com/vertex-ai/generative-ai/docs/learn/models
Args:
project: Google Cloud project ID.
location: Google Cloud location.
text: the text to generate questions and answers for
Returns: A list of (question, answer) tuples
"""
# Ask the model to generate the questions and answers.
genai_client = genai.Client(vertexai=True, project=project, location=location)
response = genai_client.models.generate_content(
model="gemini-2.0-flash",
contents="List 20 self-contained questions and answers that can be answered from the text.",
config=GenerateContentConfig(
# https://cloud.google.com/vertex-ai/generative-ai/docs/learn/prompts/system-instructions
system_instruction=[
"Use simple language and words that are easy to understand.",
"Avoid technical terms in the answers.",
],
# https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/control-generated-output
response_mime_type="application/json",
response_schema={
"type": "ARRAY",
"items": {
"type": "OBJECT",
"properties": {
"question": {"type": "STRING"},
"answer": {"type": "STRING"},
},
"required": ["question", "answer"],
},
},
),
)
text = response.text or ""
# The response is sometimes in code blocks, so we need to extract it.
code_block_start = text.find("```")
if code_block_start == -1:
code_block = text
else:
code_block = "\n".join(text[code_block_start:].splitlines()[1:-1])
# Parse the response as JSON.
try:
return json.loads(code_block)
except json.decoder.JSONDecodeError:
logging.debug(f"Failed to parse response:\n{response}")
raise