Voice + Automation
Customer Service Voicebot
Deploy an intelligent phone or web voice agent that handles customer queries, checks order status, and escalates to humans — powered by OneInfer's speech and chat APIs.
OneInfer Chat APIOneInfer Audio APITwilioFastAPIPython
Step-by-step guide
1
Define your system prompt with business context
python
SYSTEM_PROMPT = """
You are a customer service agent for Acme Corp.
- Help users track orders, request refunds, or escalate issues.
- Always be polite and concise.
- If you cannot resolve the issue, respond with: [ESCALATE]
- Do not invent order details — ask the customer for their order ID.
"""2
Handle an inbound customer message
python
import openai
client = openai.OpenAI(
api_key="your-oneinfer-api-key",
base_url="https://api.oneinfer.ai/v1"
)
def handle_message(conversation: list, user_text: str) -> dict:
conversation.append({"role": "user", "content": user_text})
response = client.chat.completions.create(
model="meta-llama/Llama-3.3-70B-Instruct-Turbo",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
*conversation
]
)
reply = response.choices[0].message.content
conversation.append({"role": "assistant", "content": reply})
escalate = "[ESCALATE]" in reply
return {"reply": reply.replace("[ESCALATE]", "").strip(), "escalate": escalate}3
Integrate with Twilio for phone calls
python
from fastapi import FastAPI, Request
from fastapi.responses import Response
from twilio.twiml.voice_response import VoiceResponse, Gather
app = FastAPI()
sessions: dict = {}
@app.post("/voice")
async def voice_webhook(request: Request):
form = await request.form()
call_sid = form.get("CallSid")
speech_result = form.get("SpeechResult", "")
if call_sid not in sessions:
sessions[call_sid] = []
vr = VoiceResponse()
if speech_result:
result = handle_message(sessions[call_sid], speech_result)
if result["escalate"]:
vr.say("I'm connecting you to a human agent now.")
vr.dial("+1-800-SUPPORT")
return Response(content=str(vr), media_type="text/xml")
gather = Gather(input="speech", action="/voice", timeout=3)
gather.say(result["reply"])
vr.append(gather)
else:
gather = Gather(input="speech", action="/voice", timeout=3)
gather.say("Hello, this is Acme Corp support. How can I help you today?")
vr.append(gather)
return Response(content=str(vr), media_type="text/xml")4
Run the server
bash
pip install fastapi uvicorn twilio openai
uvicorn main:app --host 0.0.0.0 --port 8000Point your Twilio phone number's webhook to your server's /voice endpoint. Use ngrok for local testing.