跳转到主要内容
Build a fully functional AI chatbot in Python using Ace Data Cloud’s unified API. This tutorial works with Claude, GPT-4o, Gemini, DeepSeek, and any other supported model.

What you’ll build

A command-line chatbot that:
  • Maintains conversation history (multi-turn)
  • Supports streaming responses
  • Works with any AI model via one API

Prerequisites

pip install openai

Complete code

from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://api.acedata.cloud/v1",
)

def chat(model="claude-sonnet-4-20250514"):
    messages = []
    print(f"Chatbot ready ({model}). Type 'quit' to exit.\n")

    while True:
        user_input = input("You: ")
        if user_input.lower() in ("quit", "exit"):
            break

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

        stream = client.chat.completions.create(
            model=model,
            messages=messages,
            stream=True,
        )

        print("AI: ", end="", flush=True)
        full_response = ""
        for chunk in stream:
            if chunk.choices[0].delta.content:
                content = chunk.choices[0].delta.content
                print(content, end="", flush=True)
                full_response += content
        print()

        messages.append({"role": "assistant", "content": full_response})

if __name__ == "__main__":
    chat()

Switch models instantly

Change the model parameter to use any provider:
chat("claude-sonnet-4-20250514")  # Anthropic Claude
chat("gpt-4o")                    # OpenAI GPT-4o
chat("gemini-2.5-flash")          # Google Gemini
chat("deepseek-chat")             # DeepSeek
chat("grok-3")                    # xAI Grok
All models use the same OpenAI-compatible API format — no code changes needed.

Add system prompts

Customize your chatbot’s personality:
messages = [
    {
        "role": "system",
        "content": "You are a helpful coding assistant. Answer concisely with code examples.",
    }
]

Next steps