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..41ef584a83 --- /dev/null +++ b/how-to-use-claude-api-in-python/basic_claude_call.py @@ -0,0 +1,13 @@ +import anthropic + +client = anthropic.Anthropic() + +response = client.messages.create( + model="claude-sonnet-4-5", + 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..4baf89f445 --- /dev/null +++ b/how-to-use-claude-api-in-python/coding_assistant.py @@ -0,0 +1,22 @@ +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-5", + 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..17c7671cd7 --- /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-5", + 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']}") \ No newline at end of file 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..f72ae9fc54 --- /dev/null +++ b/how-to-use-claude-api-in-python/structured_output_2.py @@ -0,0 +1,31 @@ +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-5", + 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}")