Skip to content

Commit bcf7bfd

Browse files
committed
added materials for How to Use the Claude API in Python for AI-Powered Applications
1 parent 890dd11 commit bcf7bfd

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import anthropic
2+
3+
client = anthropic.Anthropic()
4+
5+
response = client.messages.create(
6+
model="claude-sonnet-4-5",
7+
max_tokens=1024,
8+
messages=[
9+
{"role": "user", "content": "What is the Zen of Python?"}
10+
],
11+
)
12+
13+
print(response.content[0].text)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import anthropic
2+
3+
client = anthropic.Anthropic()
4+
5+
system_prompt = """
6+
You are a Python coding assistant. You only answer questions about Python.
7+
If the user asks about any other programming language or unrelated topic,
8+
politely explain that you can only help with Python questions.
9+
"""
10+
11+
user_input = input("Ask me anything about Python: ")
12+
13+
response = client.messages.create(
14+
model="claude-sonnet-4-5",
15+
max_tokens=1024,
16+
system=system_prompt,
17+
messages=[
18+
{"role": "user", "content": user_input}
19+
],
20+
)
21+
22+
print(f"\n{response.content[0].text}")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
anthropic
2+
pydantic
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import anthropic
2+
import json
3+
4+
client = anthropic.Anthropic()
5+
6+
response = client.messages.create(
7+
model="claude-sonnet-4-5",
8+
max_tokens=1024,
9+
system="You are a Python coding assistant.",
10+
messages=[
11+
{
12+
"role": "user",
13+
"content": "Write a Python function that adds two numbers.",
14+
}
15+
],
16+
output_config={
17+
"format": {
18+
"type": "json_schema",
19+
"schema": {
20+
"type": "object",
21+
"properties": {
22+
"function_name": {"type": "string"},
23+
"code": {"type": "string"},
24+
"explanation": {"type": "string"},
25+
},
26+
"required": ["function_name", "code", "explanation"],
27+
"additionalProperties": False,
28+
},
29+
}
30+
},
31+
)
32+
33+
result = json.loads(response.content[0].text)
34+
35+
print("--- Approach 1: Hand-written JSON schema ---")
36+
print(f"Function: {result['function_name']}")
37+
print(f"\nCode:\n{result['code']}")
38+
print(f"\nExplanation: {result['explanation']}")
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import anthropic
2+
3+
from pydantic import BaseModel
4+
5+
6+
class FunctionDescription(BaseModel):
7+
function_name: str
8+
code: str
9+
explanation: str
10+
11+
client = anthropic.Anthropic()
12+
13+
response = client.messages.parse(
14+
model="claude-sonnet-4-5",
15+
max_tokens=1024,
16+
system="You are a Python coding assistant.",
17+
messages=[
18+
{
19+
"role": "user",
20+
"content": "Write a Python function that adds two numbers.",
21+
}
22+
],
23+
output_format=FunctionDescription,
24+
)
25+
26+
result = response.parsed_output
27+
28+
print("--- Approach 2: Pydantic + client.messages.parse() ---")
29+
print(f"Function: {result.function_name}")
30+
print(f"\nCode:\n{result.code}")
31+
print(f"\nExplanation: {result.explanation}")

0 commit comments

Comments
 (0)