1
1
import os
2
2
import asyncio
3
- from dotenv import load_dotenv
4
3
from anthropic import Anthropic , AsyncAnthropic
5
4
6
5
from lunary .anthrophic import monitor
7
6
8
- load_dotenv ()
9
-
10
-
11
- def test_sync_non_streaming ():
7
+ def sync_non_streaming ():
12
8
client = Anthropic ()
13
9
monitor (client )
14
10
@@ -23,7 +19,7 @@ def test_sync_non_streaming():
23
19
print (message .content )
24
20
25
21
26
- async def test_async_non_streaming ():
22
+ async def async_non_streaming ():
27
23
client = monitor (AsyncAnthropic ())
28
24
29
25
message = await client .messages .create (
@@ -37,7 +33,7 @@ async def test_async_non_streaming():
37
33
print (message .content )
38
34
39
35
40
- def test_sync_streaming ():
36
+ def sync_streaming ():
41
37
client = monitor (Anthropic ())
42
38
43
39
stream = client .messages .create (
@@ -53,7 +49,7 @@ def test_sync_streaming():
53
49
print (event )
54
50
55
51
56
- async def test_async_streaming ():
52
+ async def async_streaming ():
57
53
client = monitor (AsyncAnthropic ())
58
54
59
55
stream = await client .messages .create (
@@ -69,7 +65,7 @@ async def test_async_streaming():
69
65
print (event )
70
66
71
67
72
- def test_sync_stream_helper ():
68
+ def sync_stream_helper ():
73
69
client = Anthropic ()
74
70
monitor (client )
75
71
@@ -84,7 +80,7 @@ def test_sync_stream_helper():
84
80
for event in stream :
85
81
print (event )
86
82
87
- async def test_async_stream_helper ():
83
+ async def async_stream_helper ():
88
84
client = monitor (AsyncAnthropic ())
89
85
90
86
async with client .messages .stream (
@@ -97,14 +93,15 @@ async def test_async_stream_helper():
97
93
],
98
94
model = "claude-3-opus-20240229" ,
99
95
) as stream :
100
- async for event in stream :
101
- print (event )
96
+ async for text in stream .text_stream :
97
+ print (text , end = "" , flush = True )
98
+ print ()
102
99
103
100
message = await stream .get_final_message ()
104
101
print (message .to_json ())
105
102
106
103
107
- def test_extra_arguments ():
104
+ def extra_arguments ():
108
105
client = Anthropic ()
109
106
monitor (client )
110
107
@@ -129,13 +126,120 @@ def test_extra_arguments():
129
126
print (message .content )
130
127
131
128
132
- # test_sync_non_streaming()
133
- # test_asyncio.run(async_non_streaming())
129
+ def anthrophic_bedrock ():
130
+ from anthropic import AnthropicBedrock
131
+
132
+ client = AnthropicBedrock ()
133
+
134
+ message = client .messages .create (
135
+ max_tokens = 1024 ,
136
+ messages = [
137
+ {
138
+ "role" : "user" ,
139
+ "content" : "Hello!" ,
140
+ }
141
+ ],
142
+ model = "anthropic.claude-3-sonnet-20240229-v1:0" ,
143
+ )
144
+ print (message )
145
+
146
+ def tool_calls ():
147
+ from anthropic import Anthropic
148
+ from anthropic .types import ToolParam , MessageParam
149
+
150
+ client = monitor (Anthropic ())
151
+
152
+ user_message : MessageParam = {
153
+ "role" : "user" ,
154
+ "content" : "What is the weather in San Francisco, California?" ,
155
+ }
156
+ tools : list [ToolParam ] = [
157
+ {
158
+ "name" : "get_weather" ,
159
+ "description" : "Get the weather for a specific location" ,
160
+ "input_schema" : {
161
+ "type" : "object" ,
162
+ "properties" : {"location" : {"type" : "string" }},
163
+ },
164
+ }
165
+ ]
166
+
167
+ message = client .messages .create (
168
+ model = "claude-3-opus-20240229" ,
169
+ max_tokens = 1024 ,
170
+ messages = [user_message ],
171
+ tools = tools ,
172
+ )
173
+ print (f"Initial response: { message .model_dump_json (indent = 2 )} " )
174
+
175
+ assert message .stop_reason == "tool_use"
176
+
177
+ tool = next (c for c in message .content if c .type == "tool_use" )
178
+ response = client .messages .create (
179
+ model = "claude-3-opus-20240229" ,
180
+ max_tokens = 1024 ,
181
+ messages = [
182
+ user_message ,
183
+ {"role" : message .role , "content" : message .content },
184
+ {
185
+ "role" : "user" ,
186
+ "content" : [
187
+ {
188
+ "type" : "tool_result" ,
189
+ "tool_use_id" : tool .id ,
190
+ "content" : [{"type" : "text" , "text" : "The weather is 73f" }],
191
+ }
192
+ ],
193
+ },
194
+ ],
195
+ tools = tools ,
196
+ )
197
+ print (f"\n Final response: { response .model_dump_json (indent = 2 )} " )
198
+
199
+
200
+ async def async_tool_calls ():
201
+ client = monitor (AsyncAnthropic ())
202
+ async with client .messages .stream (
203
+ max_tokens = 1024 ,
204
+ model = "claude-3-haiku-20240307" ,
205
+ tools = [
206
+ {
207
+ "name" : "get_weather" ,
208
+ "description" : "Get the weather at a specific location" ,
209
+ "input_schema" : {
210
+ "type" : "object" ,
211
+ "properties" : {
212
+ "location" : {"type" : "string" , "description" : "The city and state, e.g. San Francisco, CA" },
213
+ "unit" : {
214
+ "type" : "string" ,
215
+ "enum" : ["celsius" , "fahrenheit" ],
216
+ "description" : "Unit for the output" ,
217
+ },
218
+ },
219
+ "required" : ["location" ],
220
+ },
221
+ }
222
+ ],
223
+ messages = [{"role" : "user" , "content" : "What is the weather in SF?" }],
224
+ ) as stream :
225
+ async for event in stream :
226
+ if event .type == "input_json" :
227
+ print (f"delta: { repr (event .partial_json )} " )
228
+ print (f"snapshot: { event .snapshot } " )
229
+
230
+
231
+ # sync_non_streaming()
232
+ # asyncio.run(async_non_streaming())
233
+
234
+ # sync_streaming()
235
+ # asyncio.run(async_streaming())
236
+
237
+ # extra_arguments()
134
238
135
- # test_sync_streaming ()
136
- # test_asyncio .run(async_streaming ())
239
+ # sync_stream_helper ()
240
+ # asyncio .run(async_stream_helper ())
137
241
138
- # test_extra_arguments ()
242
+ # # anthrophic_bedrock ()
139
243
140
- # test_sync_stream_helper ()
141
- asyncio .run (test_async_stream_helper ())
244
+ # tool_calls ()
245
+ # asyncio.run(async_tool_calls ())
0 commit comments