diff --git a/how-to-use-claude-api-in-python/README.md b/how-to-use-claude-api-in-python/README.md new file mode 100644 index 0000000000..508fe054d0 --- /dev/null +++ b/how-to-use-claude-api-in-python/README.md @@ -0,0 +1,3 @@ +# How to Use the Claude API in Python + +This folder contains supporting materials for the Real Python tutorial [How to Use the Claude API in Python](https://realpython.com/claude-api-python/). diff --git a/how-to-use-claude-api-in-python/basic_claude_call.py b/how-to-use-claude-api-in-python/basic_claude_call.py new file mode 100644 index 0000000000..59a3f3f417 --- /dev/null +++ b/how-to-use-claude-api-in-python/basic_claude_call.py @@ -0,0 +1,11 @@ +import anthropic + +client = anthropic.Anthropic() + +response = client.messages.create( + model="claude-sonnet-4-6", + max_tokens=1024, + messages=[{"role": "user", "content": "What is the Zen of Python?"}], +) + +print(response.content[0].text) diff --git a/how-to-use-claude-api-in-python/coding_assistant.py b/how-to-use-claude-api-in-python/coding_assistant.py new file mode 100644 index 0000000000..84990445c6 --- /dev/null +++ b/how-to-use-claude-api-in-python/coding_assistant.py @@ -0,0 +1,20 @@ +import anthropic + +client = anthropic.Anthropic() + +system_prompt = """ +You are a Python coding assistant. You only answer questions about Python. +If the user asks about any other programming language or unrelated topic, +politely explain that you can only help with Python questions. +""" + +user_input = input("Ask me anything about Python: ") + +response = client.messages.create( + model="claude-sonnet-4-6", + max_tokens=1024, + system=system_prompt, + messages=[{"role": "user", "content": user_input}], +) + +print(f"\n{response.content[0].text}") diff --git a/how-to-use-claude-api-in-python/requirements.txt b/how-to-use-claude-api-in-python/requirements.txt new file mode 100644 index 0000000000..8ba2115811 --- /dev/null +++ b/how-to-use-claude-api-in-python/requirements.txt @@ -0,0 +1,2 @@ +anthropic +pydantic \ No newline at end of file diff --git a/how-to-use-claude-api-in-python/structured_output_1.py b/how-to-use-claude-api-in-python/structured_output_1.py new file mode 100644 index 0000000000..59e3c7b583 --- /dev/null +++ b/how-to-use-claude-api-in-python/structured_output_1.py @@ -0,0 +1,38 @@ +import anthropic +import json + +client = anthropic.Anthropic() + +response = client.messages.create( + model="claude-sonnet-4-6", + max_tokens=1024, + system="You are a Python coding assistant.", + messages=[ + { + "role": "user", + "content": "Write a Python function that adds two numbers.", + } + ], + output_config={ + "format": { + "type": "json_schema", + "schema": { + "type": "object", + "properties": { + "function_name": {"type": "string"}, + "code": {"type": "string"}, + "explanation": {"type": "string"}, + }, + "required": ["function_name", "code", "explanation"], + "additionalProperties": False, + }, + } + }, +) + +result = json.loads(response.content[0].text) + +print("--- Approach 1: Hand-written JSON schema ---") +print(f"Function: {result['function_name']}") +print(f"\nCode:\n{result['code']}") +print(f"\nExplanation: {result['explanation']}") diff --git a/how-to-use-claude-api-in-python/structured_output_2.py b/how-to-use-claude-api-in-python/structured_output_2.py new file mode 100644 index 0000000000..af392ddd62 --- /dev/null +++ b/how-to-use-claude-api-in-python/structured_output_2.py @@ -0,0 +1,32 @@ +import anthropic + +from pydantic import BaseModel + + +class FunctionDescription(BaseModel): + function_name: str + code: str + explanation: str + + +client = anthropic.Anthropic() + +response = client.messages.parse( + model="claude-sonnet-4-6", + max_tokens=1024, + system="You are a Python coding assistant.", + messages=[ + { + "role": "user", + "content": "Write a Python function that adds two numbers.", + } + ], + output_format=FunctionDescription, +) + +result = response.parsed_output + +print("--- Approach 2: Pydantic + client.messages.parse() ---") +print(f"Function: {result.function_name}") +print(f"\nCode:\n{result.code}") +print(f"\nExplanation: {result.explanation}")