in functions/main.py [0:0]
def generate_aqa_answer(req: https_fn.Request) -> https_fn.Response:
# Grab the text parameter.
prompt = req.args.get("text")
if prompt is None:
return https_fn.Response("No text parameter provided", status=400)
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE_NAME)
scoped_credentials = credentials.with_scopes(
['https://www.googleapis.com/auth/cloud-platform', 'https://www.googleapis.com/auth/generative-language.retriever'])
generative_service_client = glm.GenerativeServiceClient(credentials=scoped_credentials)
# Prepare parameters for the AQA model
content = glm.Content(parts=[glm.Part(text=prompt)])
retriever_config = glm.SemanticRetrieverConfig(
source=CORPUS_NAME, query=content
)
# Create a request to the AQA model
req = glm.GenerateAnswerRequest(
model=AQA_MODEL,
contents=[content],
semantic_retriever=retriever_config,
answer_style=ANSWER_STYLE,
)
try:
aqa_response = generative_service_client.generate_answer(req)
answer = aqa_response.answer.content.parts[0].text
print(aqa_response)
except:
print('Generate AQA answer - in the exception')
try:
answer = convert_to_html(answer)
print(answer)
except:
print('Make HTML - in the exception')
try:
resource_url = get_url(aqa_response.answer.grounding_attributions[0].source_id.semantic_retriever_chunk.chunk)
except:
print('Resouce URL attempt - in the exception')
questions = get_genai_follow_up_questions(prompt, aqa_response.answer.grounding_attributions)
if (aqa_response.answerable_probability < .1):
answer = "Sorry, that question isn't answered on Angular.dev. Please try again!"
resource_url = ''
return jsonify({
'answer': answer,
'probability': aqa_response.answerable_probability,
'url': resource_url,
'questions': questions
})