From cbfd34822d20dfb4221988f0325d37de9c52586c Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 23 Apr 2026 06:00:40 +0000 Subject: [PATCH] Add multi-turn Claude chatbot with streaming and prompt caching https://claude.ai/code/session_01JQizR2iVW4aizawZQsXFym --- claude_chat.py | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 claude_chat.py diff --git a/claude_chat.py b/claude_chat.py new file mode 100644 index 0000000..6d29ad9 --- /dev/null +++ b/claude_chat.py @@ -0,0 +1,82 @@ +""" +Multi-turn conversation chatbot using the Anthropic Claude API. +Usage: python claude_chat.py +Requires: pip install anthropic +Set ANTHROPIC_API_KEY environment variable before running. +""" + +import os +import anthropic + +SYSTEM_PROMPT = """You are a helpful AI assistant with expertise in Python and data science. +You help the user understand their code, debug issues, and learn new concepts. +Be concise, clear, and provide working code examples when relevant.""" + +MODEL = "claude-opus-4-7" + + +def chat(): + client = anthropic.Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY")) + messages = [] + + print("Claude Chat — tapez 'quit' ou 'exit' pour quitter, 'reset' pour effacer l'historique.") + print("-" * 60) + + while True: + try: + user_input = input("\nVous: ").strip() + except (EOFError, KeyboardInterrupt): + print("\nAu revoir!") + break + + if not user_input: + continue + + if user_input.lower() in ("quit", "exit"): + print("Au revoir!") + break + + if user_input.lower() == "reset": + messages = [] + print("Historique effacé.") + continue + + messages.append({"role": "user", "content": user_input}) + + print("\nClaude: ", end="", flush=True) + + with client.messages.stream( + model=MODEL, + max_tokens=4096, + thinking={"type": "adaptive"}, + system=[ + { + "type": "text", + "text": SYSTEM_PROMPT, + "cache_control": {"type": "ephemeral"}, + } + ], + messages=messages, + ) as stream: + response_text = "" + for event in stream: + if event.type == "content_block_delta": + if event.delta.type == "text_delta": + print(event.delta.text, end="", flush=True) + response_text += event.delta.text + + print() + messages.append({"role": "assistant", "content": response_text}) + + final = stream.get_final_message() + usage = final.usage + cache_read = getattr(usage, "cache_read_input_tokens", 0) or 0 + if cache_read: + print(f" [tokens: in={usage.input_tokens}, out={usage.output_tokens}, cache_hit={cache_read}]") + + +if __name__ == "__main__": + if not os.environ.get("ANTHROPIC_API_KEY"): + print("Erreur: définissez la variable d'environnement ANTHROPIC_API_KEY") + raise SystemExit(1) + chat()