Skip to navigation

Get JSON, Summaries and Answers

View as Markdown

output_rules asks for one new text made from the whole recording. You get it in output.text, next to the normal text. The output can only use what was said, and it sees the finished text, so a name that your vocabulary fixed has the same spelling in output.

A sales call into a CRM record

Describe every key: its type, what it holds, and what to write when it was not said.

import json, os
from sarvamai import SarvamAI
client = SarvamAI(api_subscription_key=os.environ["SARVAM_API_KEY"])
CRM = """Return only one JSON object with exactly these keys, and
no other text. Use null for a value that was not said.
{
"buyer_name": string | null, // full name, in English letters
"buyer_city": string | null, // city, in English
"buyer_phone": string | null, // phone number, digits only
"product": string | null, // what the buyer wants, with size
"quantity": number | null, // how many units
"unit_price_inr": number | null // price of one unit, in rupees
}"""
with open("sales_call.wav", "rb") as f:
res = client.dictate.transcribe(file=f, language_code="hi-IN", output_rules=CRM)
record = json.loads(res.output.text) # output.text is a string: parse it
print(json.dumps(record, indent=2, ensure_ascii=False))
total = record["quantity"] * record["unit_price_inr"]
print("order total (computed in code): ₹", total)
Output
{
"buyer_name": "Sunita Kulkarni",
"buyer_city": "Nashik",
"buyer_phone": "9822041735",
"product": "water tanks of 1000 liters",
"quantity": 40,
"unit_price_inr": 6800
}
order total (computed in code): ₹ 272000

Answers to set questions

Give a way out for questions the call does not answer:

output_rules for questions
QUESTIONS = """Answer each question on its own numbered line, in
English, in one short sentence. If the call does not answer a
question, write "Not mentioned".
1. Which city is the buyer from?
2. How many tanks does the buyer want?
3. Did the buyer ask for a discount?"""

Pass it as output_rules the same way, and print res.output.text.

Write rules that work

DoWhy
Describe every key: type (string | null, number | null), what it holdsA bare JSON shape gets keys wrong more often
Say what to write when a value was not said (null, “Not mentioned”)Otherwise the model may invent a value
For Indian-language audio, write “Write every value in English” at the top, or “in English” in each keyWith no language named, the values come back in the speaker’s script
Name the language of a summary: “in English”Otherwise it can follow the clip
Ask for one output per requestSeveral outputs in one request can mix up
Leave output_rules out when you do not want an outputAn empty string, or only spaces, means no output
Do sums in your own codeThe model’s sums are not reliable
Parse output.text yourself, and check output.degradedIt is a string, and it can fail on its own while text is fine

The output call can take up to 90 s on the live socket, so give it time. Over a WebSocket, the output comes in an output.final message after the last turn.