on_question()
When a Guava agent is asked a question that it cannot answer from its context alone, it will invoke the on_question callback. Your Expert then has the chance to answer that question, typically using a RAG system. Our examples use the provided DocumentQA class, but you can use any RAG system you prefer.
See our Q&A example for a step-by-step walkthrough.
signature
@agent.on_question
def on_question(call: guava.Call, question: str) -> str:
# question: natural-language question from the caller
# return: answer to relay to caller
...If you want the agent to answer questions immediately, use add_info to pre-emptively add information to the context.
on_question, like all Guava callbacks, is invoked asynchronously and does not block dialog. The Guava voice agent continues to engage the caller until the question answer comes back.on_questionmay be invoked multiple times, for example, if a caller asks a question and then refines it.on_questionmay be invoked speculatively before a caller is done talking.on_questionmay be invoked simultaneously withon_action_request, as some requests can be both an "action" and a "question". For example, "Do you have a lost and found?" In this case, the agent will synthesize both responses: "Yes, we have a lost and found. Would you like me to transfer you there?"
Example
support_controller.py
import guava
from guava import Agent
from guava.helpers.rag import DocumentQA
from guava.examples.example_data import PROPERTY_INSURANCE_POLICY
agent = Agent(
organization="Harper Valley Property Insurance",
purpose="Answer questions regarding property insurance policy",
)
document_qa = DocumentQA(documents=PROPERTY_INSURANCE_POLICY)
@agent.on_question
def on_question(call: guava.Call, question: str) -> str:
return document_qa.ask(question)Questions? hi@goguava.ai
@agent.on_question
def on_question(call: guava.Call, question: str) -> str:
# question: natural-language question from the caller
# return: answer to relay to caller
...import guava
from guava import Agent
from guava.helpers.rag import DocumentQA
from guava.examples.example_data import PROPERTY_INSURANCE_POLICY
agent = Agent(
organization="Harper Valley Property Insurance",
purpose="Answer questions regarding property insurance policy",
)
document_qa = DocumentQA(documents=PROPERTY_INSURANCE_POLICY)
@agent.on_question
def on_question(call: guava.Call, question: str) -> str:
return document_qa.ask(question)