> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gnani.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Customer support bot

> Build a support chatbot that looks up orders and opens tickets by calling your functions.

Early access
**Gnani Evon v3.5** is now available as an API to early access registrants.

This recipe builds a command-line support bot for an online store. It answers in the customer's language and uses [tool calling](https://docs.gnani.ai/api/LLM/tool-calling.md) to look up orders and open support tickets in your systems.

## When to use this

* Customers ask questions that need live data from your systems, such as order status.
* The bot should take simple actions, such as opening a ticket, and confirm them in the chat.
* Customers write in Hindi, English or a mix, and expect a reply in the same language.

***

## The complete bot

Each script keeps the conversation, runs the tools the model asks for, and loops until the model answers. The order data is synthetic; replace the two functions with calls to your own systems.

```python Python (OpenAI SDK) expandable theme={null}
import json
import os

from openai import BadRequestError, OpenAI

client = OpenAI(
    base_url="https://api.vachana.ai/v1",
    api_key=os.environ["GNANI_API_KEY"],
)

# Synthetic sample data. Replace with calls to your own systems.
ORDERS = {
    "ORD-48213": {"status": "shipped", "expected_delivery": "2026-11-14", "amount": "₹1,499"},
    "ORD-51907": {"status": "processing", "expected_delivery": "2026-11-20", "amount": "₹3,250"},
}
TICKETS = []


def get_order_status(order_id: str) -> dict:
    order = ORDERS.get(order_id.strip().upper())
    if order is None:
        return {"error": f"No order found with ID {order_id}."}
    return {"order_id": order_id, **order}


def create_support_ticket(order_id: str, summary: str) -> dict:
    ticket_id = f"TKT-{1001 + len(TICKETS)}"
    TICKETS.append({"ticket_id": ticket_id, "order_id": order_id, "summary": summary})
    return {"ticket_id": ticket_id, "status": "open"}


FUNCTIONS = {
    "get_order_status": get_order_status,
    "create_support_ticket": create_support_ticket,
}

TOOLS = [
    {
        "type": "function",
        "function": {
            "name": "get_order_status",
            "description": "Look up the delivery status of a customer's order.",
            "parameters": {
                "type": "object",
                "properties": {
                    "order_id": {"type": "string", "description": "The order ID, for example ORD-48213."}
                },
                "required": ["order_id"],
            },
        },
    },
    {
        "type": "function",
        "function": {
            "name": "create_support_ticket",
            "description": "Open a support ticket when the customer reports a problem you cannot solve in the chat.",
            "parameters": {
                "type": "object",
                "properties": {
                    "order_id": {"type": "string", "description": "The order ID the problem is about."},
                    "summary": {"type": "string", "description": "One sentence describing the problem, in English."},
                },
                "required": ["order_id", "summary"],
            },
        },
    },
]

SYSTEM = (
    "You are an AI support assistant for an online store. Reply in the customer's language, in two or three "
    "short sentences. Use get_order_status to look up orders; never guess an order's status. If the customer "
    "reports a problem you cannot solve, open a ticket with create_support_ticket and give them the ticket ID."
)


def reply(messages: list) -> str:
    for _ in range(5):  # a safety limit on tool rounds
        completion = client.chat.completions.create(
            model="evon-v3.5",
            messages=messages,
            tools=TOOLS,
            reasoning_effort="none",
        )
        choice = completion.choices[0]
        message = choice.message

        if not message.tool_calls:  # no more calls: this is the answer
            if choice.finish_reason == "content_filter":
                return "Sorry, I can't help with that. Please ask in another way."
            messages.append({"role": "assistant", "content": message.content})
            return message.content

        messages.append({
            "role": "assistant",
            "content": message.content,
            "tool_calls": [call.model_dump() for call in message.tool_calls],
        })
        for call in message.tool_calls:
            function = FUNCTIONS.get(call.function.name)
            try:
                arguments = json.loads(call.function.arguments)
                result = function(**arguments) if function else {"error": f"Unknown function {call.function.name}"}
            except (json.JSONDecodeError, TypeError) as error:
                result = {"error": f"Invalid arguments: {error}"}
            messages.append({"role": "tool", "tool_call_id": call.id, "content": json.dumps(result, ensure_ascii=False)})

    return "Sorry, something went wrong. Please try again."


def main():
    messages = [{"role": "system", "content": SYSTEM}]
    print("Ask about an order (try ORD-48213). Press Ctrl+C to quit.")
    while True:
        text = input("> ").strip()
        if not text:
            continue
        checkpoint = len(messages)
        messages.append({"role": "user", "content": text})
        try:
            print(reply(messages))
        except BadRequestError as error:
            del messages[checkpoint:]  # drop the refused turn so the chat can go on
            if error.code == "content_filter":
                print("Sorry, I couldn't process that message. Please rephrase it.")
            else:
                raise


if __name__ == "__main__":
    main()
```

```javascript JavaScript (OpenAI SDK) expandable theme={null}
import OpenAI from "openai";
import readline from "node:readline/promises";
import { stdin as input, stdout as output } from "node:process";

const client = new OpenAI({
  baseURL: "https://api.vachana.ai/v1",
  apiKey: process.env.GNANI_API_KEY,
});

// Synthetic sample data. Replace with calls to your own systems.
const ORDERS = {
  "ORD-48213": { status: "shipped", expected_delivery: "2026-11-14", amount: "₹1,499" },
  "ORD-51907": { status: "processing", expected_delivery: "2026-11-20", amount: "₹3,250" },
};
const TICKETS = [];

const FUNCTIONS = {
  get_order_status({ order_id }) {
    const order = ORDERS[String(order_id).trim().toUpperCase()];
    return order ? { order_id, ...order } : { error: `No order found with ID ${order_id}.` };
  },
  create_support_ticket({ order_id, summary }) {
    const ticket_id = `TKT-${1001 + TICKETS.length}`;
    TICKETS.push({ ticket_id, order_id, summary });
    return { ticket_id, status: "open" };
  },
};

const TOOLS = [
  {
    type: "function",
    function: {
      name: "get_order_status",
      description: "Look up the delivery status of a customer's order.",
      parameters: {
        type: "object",
        properties: {
          order_id: { type: "string", description: "The order ID, for example ORD-48213." },
        },
        required: ["order_id"],
      },
    },
  },
  {
    type: "function",
    function: {
      name: "create_support_ticket",
      description: "Open a support ticket when the customer reports a problem you cannot solve in the chat.",
      parameters: {
        type: "object",
        properties: {
          order_id: { type: "string", description: "The order ID the problem is about." },
          summary: { type: "string", description: "One sentence describing the problem, in English." },
        },
        required: ["order_id", "summary"],
      },
    },
  },
];

const SYSTEM =
  "You are an AI support assistant for an online store. Reply in the customer's language, in two or three " +
  "short sentences. Use get_order_status to look up orders; never guess an order's status. If the customer " +
  "reports a problem you cannot solve, open a ticket with create_support_ticket and give them the ticket ID.";

async function reply(messages) {
  for (let round = 0; round < 5; round++) { // a safety limit on tool rounds
    const completion = await client.chat.completions.create({
      model: "evon-v3.5",
      messages,
      tools: TOOLS,
      reasoning_effort: "none",
    });
    const choice = completion.choices[0];
    const message = choice.message;

    if (!message.tool_calls?.length) { // no more calls: this is the answer
      if (choice.finish_reason === "content_filter") {
        return "Sorry, I can't help with that. Please ask in another way.";
      }
      messages.push({ role: "assistant", content: message.content });
      return message.content;
    }

    messages.push({ role: "assistant", content: message.content, tool_calls: message.tool_calls });
    for (const call of message.tool_calls) {
      const fn = FUNCTIONS[call.function.name];
      let result;
      try {
        const args = JSON.parse(call.function.arguments);
        result = fn ? fn(args) : { error: `Unknown function ${call.function.name}` };
      } catch (error) {
        result = { error: `Invalid arguments: ${error.message}` };
      }
      messages.push({ role: "tool", tool_call_id: call.id, content: JSON.stringify(result) });
    }
  }
  return "Sorry, something went wrong. Please try again.";
}

const messages = [{ role: "system", content: SYSTEM }];
const rl = readline.createInterface({ input, output });
console.log("Ask about an order (try ORD-48213). Type exit to quit.");

while (true) {
  const text = (await rl.question("> ")).trim();
  if (text === "exit") break;
  if (!text) continue;
  const checkpoint = messages.length;
  messages.push({ role: "user", content: text });
  try {
    console.log(await reply(messages));
  } catch (error) {
    messages.splice(checkpoint); // drop the refused turn so the chat can go on
    if (error instanceof OpenAI.BadRequestError && error.code === "content_filter") {
      console.log("Sorry, I couldn't process that message. Please rephrase it.");
    } else {
      throw error;
    }
  }
}
rl.close();
```

Save the script as `support_bot.py` or `support-bot.mjs` and run it with `python support_bot.py` or `node support-bot.mjs`. A session looks something like this (replies vary):

```text theme={null}
Ask about an order (try ORD-48213). Press Ctrl+C to quit.
> Where is my order ORD-48213?
Your order ORD-48213 has shipped and should arrive on 14 November.
> मेरा दूसरा ऑर्डर ORD-51907 टूटा हुआ आया है।
माफ़ कीजिए। मैंने आपके ऑर्डर ORD-51907 के लिए टिकट TKT-1001 खोल दिया है। हमारी टीम जल्द ही आपसे संपर्क करेगी।
```

***

## How it works

1. The customer's message is added to `messages`, after the `system` message.
2. The request sends `tools`. The model either answers, or replies with `tool_calls`.
3. For each call, your code parses `arguments`, runs the function, and adds a `tool` message whose `tool_call_id` matches the call's `id`.
4. The conversation is sent again, with the same `tools`, until the model answers in `content`.
5. The answer is kept in `messages`, so the next question has the whole conversation.

***

## Options

| Option | Use it to |
| - | - |
| `tool_choice: "auto"` (default) | Let the model decide when to call a tool. Right for a chat bot. |
| `tool_choice: {"type": "function", "function": {"name": "get_order_status"}}` | Force one tool, for example on a screen that only tracks orders. |
| `reasoning_effort: "none"` | Faster replies. Leave thinking on only if the bot must plan several steps. |
| `{"type": "web_search"}` in `tools` | Also answer general questions from the web. See [Web search](https://docs.gnani.ai/api/LLM/web-search.md). |

***

## Gotchas

> [!WARNING]
>
> **Check `message.tool_calls`, not `finish_reason`.** When you force a function with `tool_choice`, the reply can have `finish_reason: "stop"` even though it contains a tool call. And `tool_choice: "required"` is only allowed when `tools` has exactly one tool. Both differ from OpenAI. See [Tool calling](https://docs.gnani.ai/api/LLM/tool-calling.md).

* **Validate arguments before you act.** They are written by the model. Never pass them straight to a database query or a shell.
* **Return errors as tool results**, as `get_order_status` does for an unknown ID. The model can then explain the problem to the customer.
* **Tool results are screened like user messages.** If a tool returns text from customers or the web that tries to instruct the model, the request can be refused with `400 content_filter`. The scripts drop that turn so the chat can go on.
* **Personal data in replies is masked.** If a tool returns a phone number or UPI ID and the model repeats it, the reply contains a placeholder such as `[REDACTED_PHONE]`. See [Safety and privacy](https://docs.gnani.ai/api/LLM/safety.md).
* **Every tool round is a separate request.** Each counts toward your [rate limits and daily token quota](https://docs.gnani.ai/api/LLM/limits.md). Trim old turns in long chats.

***

## Related

> [card] [Tool calling](https://docs.gnani.ai/api/LLM/tool-calling.md)
>
> The tool-calling flow, step by step.

> [card] [Safety and privacy](https://docs.gnani.ai/api/LLM/safety.md)
>
> Blocked requests and masked personal data.

> [card] [Voice agent](https://docs.gnani.ai/api/LLM/recipes/voice-agent.md)
>
> Put the same bot behind Prisma and Timbre.

> [card] [Errors](https://docs.gnani.ai/api/LLM/errors.md)
>
> Status codes, error bodies and retries.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.