Loading page...Загружаем страницу...正在加载页面...
AtlasRepo

First Steps in AI Engineering: Building a Simple CLI Chatbot

说明与用途

使用Python和Anthropic Claude构建的轻量级CLI聊天机器人,具有令牌限制和对话记忆功能。

问题

Build a lightweight, turn‑based chatbot without heavy frameworks, while managing token usage and ensuring deterministic greetings.

结果

A minimal, cost‑aware CLI chatbot that can converse with Claude‑Sonnet‑5, print responses, and maintain conversation history.

工作流

  • Install Anthropic Python library
  • Import os and Anthropic
  • Set ANTHROPIC_API_KEY in environment
  • Define SYSTEM_PROMPT and STARTING_MESSAGE
  • Prompt user for input
  • Create messages list with assistant greeting and user question
  • Instantiate Anthropic client with api_key
  • Call client.messages.create with system, messages, model, max_tokens
  • Iterate over response.content blocks to extract text
  • Print assistant response
  • Append assistant response to messages list for next turn

证据

I wanted to organise myself by placing the constants in one area, and I knew I needed to starting directive for the client/chatbot to know the whole context

来源

response = client.messages.create(max_tokens=1024, system=SYSTEM_PROMPT, messages=messages, model="claude-sonnet-5")

来源

  • 记录日期: 2026-09-20T10:15:33.269Z