Build a Multi-Model Chatbot with 1vy + Python

Tutorial: build a chatbot that automatically routes to different AI models based on the task. Uses 1vy's unified API with the OpenAI SDK.

What we'll build

A Python chatbot that intelligently routes requests to different AI models: - Complex reasoning → DeepSeek R1 (best cost/quality for thinking) - Code generation → Claude Sonnet 4.6 (top coding model) - Quick answers → GPT-4o Mini (fast and cheap)

All through one 1vy API key.

Prerequisites

- Python 3.9+ - A 1vy API key (sign up free)

``bash pip install openai ``

The code

```python import openai

client = openai.OpenAI( base_url="https://api.1vy.ai/v1", api_key="sk-1vy-your-key-here" )

def classify_task(message: str) -> str: """Simple keyword-based task classification.""" msg = message.lower() if any(w in msg for w in ["code", "function", "debug", "implement", "refactor"]): return "code" if any(w in msg for w in ["why", "explain", "analyze", "reason", "think"]): return "reasoning" return "quick"

MODEL_MAP = { "code": "claude-sonnet-4-6", "reasoning": "deepseek-r1", "quick": "gpt-4o-mini", }

def chat(message: str) -> str: task = classify_task(message) model = MODEL_MAP[task] print(f"[routing to {model}]")

response = client.chat.completions.create( model=model, messages=[{"role": "user", "content": message}], ) return response.choices[0].message.content

# Try it print(chat("Write a Python function to merge two sorted lists")) print(chat("Why does the moon cause tides?")) print(chat("What's the capital of France?")) ```

How it works

1. The classify_task function checks for keywords to determine the type of request 2. Each task type maps to the best model for that job 3. All requests go through the same 1vy endpoint — no SDK changes, no API key juggling

Extending the router

For production, replace keyword matching with an LLM-based classifier:

``python def classify_with_llm(message: str) -> str: response = client.chat.completions.create( model="gpt-4o-mini", # cheap classifier messages=[{ "role": "system", "content": "Classify the user's request. Reply with exactly one word: code, reasoning, or quick." }, { "role": "user", "content": message }], max_tokens=5, ) return response.choices[0].message.content.strip().lower() ``

Cost savings

By routing to the right model, you avoid overpaying: - Simple factual questions → GPT-4o Mini ($0.17/1M input) instead of GPT-4o ($2.88/1M) - That's a 17× cost reduction for tasks that don't need a flagship model

Next steps

- Add streaming support with stream=True - Implement conversation history (multi-turn) - Add fallback logic (if one model is slow, try another)

All of this works with the standard OpenAI SDK — 1vy handles the multi-provider routing behind the scenes.

Get your API key →