DEV Community

Daniel Dong
Daniel Dong

Posted on

Build a Multi-Model AI Chatbot in 50 Lines of Code

Want to build your own AI chatbot? Here's a complete, working Python app that lets users switch between 14 AI models instantly. Copy-paste and run.

Most AI chatbot tutorials are either too simple (single model) or too complex (Docker, databases, authentication).

Here's a complete, working chatbot in 50 lines of Python. No frameworks. No databases. Just openai SDK + one API.

The Complete Code

# chatbot.py - A multi-model AI chatbot in 50 lines
import os
from openai import OpenAI

# One client, 14 models
client = OpenAI(
    api_key="mb-your-key",  # Get free key at aibridge-api.com
    base_url="https://aibridge-api.com/v1"
)

# Available models
MODELS = [
    "deepseek-v4-pro", "deepseek-chat", "deepseek-reasoner",
    "deepseek-coder", "deepseek-v4-flash", "qwen3-235b-a22b",
    "qwen-max", "qwen-plus", "glm-4-plus", "glm-4-air",
    "glm-4-flash", "moonshot-v1-128k", "moonshot-v1-32k",
    "moonshot-v1-8k"
]

def chat(model, messages):
    """Send a chat request to any model."""
    response = client.chat.completions.create(
        model=model,
        messages=messages,
        stream=True
    )

    # Stream output
    full_response = ""
    for chunk in response:
        if chunk.choices[0].delta.content:
            content = chunk.choices[0].delta.content
            print(content, end="", flush=True)
            full_response += content
    print()
    return full_response

# Main loop
if __name__ == "__main__":
    print("🤖 Multi-Model AI Chatbot")
    print(f"Available models: {len(MODELS)}")
    print("Type 'switch' to change model, 'exit' to quit\n")

    model = MODELS[0]  # Default model
    messages = []

    while True:
        print(f"\n[Model: {model}]")
        user_input = input("You: ")

        if user_input.lower() == "exit":
            break
        elif user_input.lower() == "switch":
            print("\nAvailable models:")
            for i, m in enumerate(MODELS):
                print(f"  {i}: {m}")
            idx = int(input("Select model (0-13): "))
            model = MODELS[idx]
            continue

        messages.append({"role": "user", "content": user_input})

        print("Bot: ", end="")
        bot_response = chat(model, messages)

        messages.append({"role": "assistant", "content": bot_response})
Enter fullscreen mode Exit fullscreen mode

How to Run It

# 1. Install dependency
pip install openai

# 2. Get free API key (no credit card)
# → https://aibridge-api.com

# 3. Run the chatbot
python chatbot.py
Enter fullscreen mode Exit fullscreen mode

What You Can Do

  • Switch models mid-conversation (switch command)
  • Compare responses (same prompt, different models)
  • Test streaming (real-time output)
  • Keep conversation history (messages array)

Try It Now

  1. Copy the code above (all 50 lines)
  2. Get a free API key → aibridge-api.com
  3. Run it → python chatbot.py
  4. Start chatting with 14 AI models

Pro tip: Use deepseek-v4-flash for quick responses, deepseek-v4-pro for complex reasoning.

mainpage

models

playground

pricing

Top comments (0)