Build a Chatbot with Python
Tutorials
Build a Multi-Turn Chatbot in 5 Minutes
This tutorial walks through building a terminal-based AI chatbot using Barq's API.
1. Install Dependencies
pip install openai
2. Set Your API Key
export BARQ_API_KEY="barq-sk-your-key-here"
3. Complete Chatbot Code
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.barqapi.com/v1",
api_key=os.environ.get("BARQ_API_KEY")
)
messages = [
{"role": "system", "content": "You are a helpful AI assistant."}
]
print("π€ Barq Chatbot (type 'quit' to exit)\n")
while True:
user_input = input("You: ")
if user_input.lower() == 'quit':
break
messages.append({"role": "user", "content": user_input})
stream = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
stream=True
)
print("Bot: ", end="", flush=True)
full_response = ""
for chunk in stream:
content = chunk.choices[0].delta.content
if content:
print(content, end="", flush=True)
full_response += content
print("\n")
messages.append({"role": "assistant", "content": full_response})
4. Run It
python chatbot.py
What's Next?
- Swap
gpt-4o-miniforgpt-5.5for stronger reasoning - Add
temperature=0.7for more creative responses - Use
deepseek-v4-profor coding tasks - Deploy as a web app with Flask or FastAPI