Skip to content
This repository was archived by the owner on Feb 14, 2025. It is now read-only.

Commit d55bd5d

Browse files
committed
refactor: code cleanup
1 parent 40112d9 commit d55bd5d

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed

examples/anthrophic.py

+63-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import asyncio
33
from anthropic import Anthropic, AsyncAnthropic
44

5-
from lunary.anthrophic import monitor
5+
import lunary
6+
from lunary.anthrophic import monitor, parse_message
67

78
def sync_non_streaming():
89
client = Anthropic()
@@ -228,6 +229,65 @@ async def async_tool_calls():
228229
print(f"snapshot: {event.snapshot}")
229230

230231

232+
def reconcilliation_tool_calls():
233+
from anthropic import Anthropic
234+
from anthropic.types import ToolParam, MessageParam
235+
236+
thread = lunary.open_thread()
237+
client = monitor(Anthropic())
238+
239+
user_message: MessageParam = {
240+
"role": "user",
241+
"content": "What is the weather in San Francisco, California?",
242+
}
243+
tools: list[ToolParam] = [
244+
{
245+
"name": "get_weather",
246+
"description": "Get the weather for a specific location",
247+
"input_schema": {
248+
"type": "object",
249+
"properties": {"location": {"type": "string"}},
250+
},
251+
}
252+
]
253+
254+
message_id = thread.track_message(user_message)
255+
256+
with lunary.parent(message_id):
257+
message = client.messages.create(
258+
model="claude-3-opus-20240229",
259+
max_tokens=1024,
260+
messages=[user_message],
261+
tools=tools,
262+
)
263+
print(f"Initial response: {message.model_dump_json(indent=2)}")
264+
265+
assert message.stop_reason == "tool_use"
266+
267+
tool = next(c for c in message.content if c.type == "tool_use")
268+
response = client.messages.create(
269+
model="claude-3-opus-20240229",
270+
max_tokens=1024,
271+
messages=[
272+
user_message,
273+
{"role": message.role, "content": message.content},
274+
{
275+
"role": "user",
276+
"content": [
277+
{
278+
"type": "tool_result",
279+
"tool_use_id": tool.id,
280+
"content": [{"type": "text", "text": "The weather is 73f"}],
281+
}
282+
],
283+
},
284+
],
285+
tools=tools,
286+
)
287+
print(f"\nFinal response: {response.model_dump_json(indent=2)}")
288+
289+
290+
231291
# sync_non_streaming()
232292
# asyncio.run(async_non_streaming())
233293

@@ -243,3 +303,5 @@ async def async_tool_calls():
243303

244304
# tool_calls()
245305
# asyncio.run(async_tool_calls())
306+
307+
reconcilliation_tool_calls()

lunary/anthrophic.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def __params_parser(params: t.Dict[str, t.Any]) -> t.Dict[str, t.Any]:
7474
}
7575

7676

77-
def __parse_message_content(message: MessageParam):
77+
def parse_message(message: MessageParam):
7878
role = message.get("role")
7979
content = message.get("content")
8080

@@ -120,7 +120,7 @@ def __input_parser(kwargs: t.Dict):
120120
inputs.append({ "role": "system", "content": item.get("text") })
121121

122122
for message in kwargs.get("messages", []):
123-
inputs.extend(__parse_message_content(message))
123+
inputs.extend(parse_message(message))
124124

125125
return {"input": inputs, "name": kwargs.get("model")}
126126

@@ -176,7 +176,6 @@ def __iter__(self):
176176

177177
def __iterator__(self):
178178
for event in self.__stream.__stream__():
179-
print("\n", event)
180179
if event.type == "message_start":
181180
self.__messages.append(
182181
{
@@ -320,7 +319,6 @@ def __aiter__(self):
320319

321320
async def __iterator__(self):
322321
async for event in self.__stream.__stream__():
323-
print("\n", event)
324322
if event.type == "message_start":
325323
self.__messages.append(
326324
{
@@ -578,7 +576,6 @@ def __wrap_sync(
578576
is_openai=False,
579577
)
580578
except Exception as e:
581-
raise e
582579
return logging.exception(e)
583580

584581
if contextify_stream or kwargs.get("stream") == True:
@@ -672,7 +669,6 @@ async def __wrap_async(
672669
is_openai=False,
673670
)
674671
except Exception as e:
675-
raise e
676672
return logging.exception(e)
677673

678674
if contextify_stream or kwargs.get("stream") == True:

0 commit comments

Comments
 (0)