
Final Result
A beautiful, fast, and persistent terminal-based chat application powered by GLM-5-Turbo.


Key Features of My Setup
- Real-time Streaming — Words appear as they are generated (much more natural than waiting for full responses)
- Persistent Chat History — Conversations are saved automatically to a JSON file and restored when you reopen the app
- Beautiful Terminal UI — Colored text using the rich library
- Secure API Key Handling — Never hardcoded, loaded from environment variable
- Error Handling — Graceful handling of API issues
- Easy to Extend — Ready for future additions like tool calling or LangChain integration
Step-by-Step Setup Guide
1. Install Required Packages
Bash
pip install openai rich
2. Get Your API Key
- Go to OpenRouter
- Sign up / Log in
- Go to Settings → API Keys
- Create a new key and copy it
3. Set API Key Permanently
Bash
# Add to bashrc so it persists
echo 'export OPENROUTER_API_KEY="sk-or-v1-..."' >> ~/.bashrc
source ~/.bashrc
Verify it:
Bash
echo $OPENROUTER_API_KEY
4. Create the Chat Application
Bash
nano chat_glm.py
Paste the full code (same as before, but now included for completeness):
Python
import os
import json
from openai import OpenAI
from rich.console import Console
api_key = os.getenv("OPENROUTER_API_KEY")
if not api_key:
print("❌ Error: OPENROUTER_API_KEY is not set!")
exit(1)
client = OpenAI(api_key=api_key, base_url="https://openrouter.ai/api/v1")
console = Console()
HISTORY_FILE = "glm_chat_history.json"
def load_history():
if os.path.exists(HISTORY_FILE):
try:
with open(HISTORY_FILE, "r", encoding="utf-8") as f:
return json.load(f)
except:
return []
return []
def save_history(messages):
try:
with open(HISTORY_FILE, "w", encoding="utf-8") as f:
json.dump(messages, f, ensure_ascii=False, indent=2)
except:
pass
console.print("[bold cyan]🤖 GLM-5-Turbo Chat with History[/bold cyan] (type 'exit' to quit)\n")
messages = load_history()
if messages:
console.print(f"
[dim]Loaded {len(messages)//2} previous messages from history[/dim]
\n”) while True: console.print(“[bold green]You:[/bold green] “, end=””) user_input = input() if user_input.lower() in [‘exit’, ‘quit’, ‘bye’]: console.print(“[yellow]Goodbye! Chat history saved.[/yellow]”) save_history(messages) break messages.append({“role”: “user”, “content”: user_input}) console.print(“[bold blue]GLM:[/bold blue] “, end=””) try: response = client.chat.completions.create( model=”z-ai/glm-5-turbo”, messages=messages, temperature=0.7, max_tokens=2048, stream=True ) reply = “” for chunk in response: if chunk.choices[0].delta.content: content = chunk.choices[0].delta.content console.print(content, end=””) reply += content console.print(“\n”) messages.append({“role”: “assistant”, “content”: reply}) save_history(messages) except Exception as e: console.print(f”
[red]Error: {e}[/red]
“)
5. Run the Chat
Bash
python3 chat_glm.py