on question.md

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.

@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
    ...
agent.onQuestion(async (call: guava.Call, question: string) => string);

Example

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)
import * as guava from "@guava-ai/guava-sdk";
import { DocumentQA } from "@guava-ai/guava-sdk/helpers/openai";
import { PROPERTY_INSURANCE_POLICY } from "@guava-ai/guava-sdk/example-data";

const agent = new guava.Agent({
  organization: "Harper Valley Property Insurance",
  purpose: "Answer questions regarding property insurance policy",
});

const documentQA = new DocumentQA(
  "harper-valley-property-insurance",
  PROPERTY_INSURANCE_POLICY,
);

agent.onQuestion(async (call: guava.Call, question: string) => {
  return await documentQA.ask(question);
});