-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_shaping.py
106 lines (82 loc) · 2.69 KB
/
output_shaping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""
This example shows how to use output shaping with Anthropic models.
Download `uv` to run this example: https://github.com/astral-sh/uv
```
export ANTHROPIC_API_KEY="..."
uv run examples/output_shaping.py
```
"""
# /// script
# dependencies = [
# "anthropic",
# "hype @ git+https://github.com/mattt/hype.git",
# ]
# ///
from datetime import datetime
from typing import Annotated
from anthropic import Anthropic
from anthropic.types import MessageParam
from pydantic import BaseModel, Field, StringConstraints
from hype import create_anthropic_tools
AirportCode = Annotated[
str, StringConstraints(min_length=3, max_length=3, pattern=r"^[A-Z]+$")
]
class FlightDetails(BaseModel):
origin: AirportCode = Field(
description="Three-letter IATA airport code for the departure airport."
)
destination: AirportCode = Field(
description="Three-letter IATA airport code for the arrival airport."
)
departure_time: datetime = Field(
description="When the flight is scheduled to depart from its origin"
)
arrival_time: datetime = Field(
description="When the flight is scheduled to arrive at its destination"
)
model_config = {
"json_schema_extra": {
"examples": [
{
"origin": "LAX",
"destination": "JFK",
"departure_time": "2023-06-15T08:00:00Z",
"arrival_time": "2023-06-15T16:30:00Z",
}
]
}
}
if __name__ == "__main__":
client = Anthropic()
tools = create_anthropic_tools(result_type=FlightDetails)
messages: list[MessageParam] = [
{
"role": "user",
"content": """
Extract the flight details from following email:
It's time to check in for your flight.
Use the app for smooth sailing and we'll see you soon!
Confirmation code: ABCDEF
Your trip details
Flight 420
Seat 10D
5:00 PM 6:30 PM
SFO PDX
San Francisco Portland, OR
Departure Arrival
9/20/2023 9/20/2023
""",
}
]
response = client.messages.create(
model="claude-3-5-sonnet-20240620",
max_tokens=2046,
messages=messages,
tools=tools,
)
if response.stop_reason == "tool_use":
for block in response.content:
if block.type == "tool_use":
result = tools(block)
result = tools.future.result()
print(f"Final result: {result.model_dump_json(indent=2)}")