Skip to content

Commit

Permalink
chore(weave): capture request id for openai completions
Browse files Browse the repository at this point in the history
  • Loading branch information
bcsherma committed Dec 6, 2024
1 parent a259e4a commit 7f917fa
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
6 changes: 3 additions & 3 deletions tests/integrations/instructor/instructor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_instructor_openai(
assert op_name_from_ref(call.op_name) == "openai.chat.completions.create"
output = call.output
output_arguments = json.loads(
output.choices[0].message.tool_calls[0].function.arguments
output["choices"][0]["message"]["tool_calls"][0]["function"]["arguments"]
)
assert "person_name" in output_arguments
assert "age" in output_arguments
Expand Down Expand Up @@ -112,7 +112,7 @@ async def extract_person(text: str) -> Person:
assert op_name_from_ref(call.op_name) == "openai.chat.completions.create"
output = call.output
output_arguments = json.loads(
output.choices[0].message.tool_calls[0].function.arguments
output["choices"][0]["message"]["tool_calls"][0]["function"]["arguments"]
)
assert "person_name" in output_arguments
assert "age" in output_arguments
Expand Down Expand Up @@ -166,7 +166,7 @@ def test_instructor_iterable(
assert call.started_at < call.ended_at
assert op_name_from_ref(call.op_name) == "openai.chat.completions.create"
output = call.output
output_arguments = json.loads(output.choices[0].message.content)
output_arguments = json.loads(output["choices"][0]["message"]["content"])
assert "tasks" in output_arguments
assert "person_name" in output_arguments["tasks"][0]
assert "age" in output_arguments["tasks"][0]
Expand Down
36 changes: 18 additions & 18 deletions tests/integrations/openai/openai_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ def test_openai_quickstart(client: weave.trace.weave_client.WeaveClient) -> None
assert call.started_at < call.ended_at # type: ignore

output = call.output
assert output.model == "gpt-4o-2024-05-13"
assert output.object == "chat.completion"
assert output["model"] == "gpt-4o-2024-05-13"
assert output["object"] == "chat.completion"

usage = call.summary["usage"][output.model] # type: ignore
usage = call.summary["usage"][output["model"]] # type: ignore
assert usage["requests"] == 1
assert usage["completion_tokens"] == 28
assert usage["prompt_tokens"] == 11
Expand Down Expand Up @@ -86,10 +86,10 @@ async def test_openai_async_quickstart(
assert call.started_at < call.ended_at # type: ignore

output = call.output
assert output.model == "gpt-4o-2024-05-13"
assert output.object == "chat.completion"
assert output["model"] == "gpt-4o-2024-05-13"
assert output["object"] == "chat.completion"

usage = call.summary["usage"][output.model] # type: ignore
usage = call.summary["usage"][output["model"]] # type: ignore
assert usage["requests"] == 1
assert usage["completion_tokens"] == 28
assert usage["prompt_tokens"] == 11
Expand Down Expand Up @@ -315,10 +315,10 @@ def test_openai_function_call(client: weave.trace.weave_client.WeaveClient) -> N
assert call.started_at < call.ended_at # type: ignore

output = call.output
assert output.model == "gpt-4o-2024-05-13"
assert output.object == "chat.completion"
assert output["model"] == "gpt-4o-2024-05-13"
assert output["object"] == "chat.completion"

usage = call.summary["usage"][output.model] # type: ignore
usage = call.summary["usage"][output["model"]] # type: ignore
assert usage["total_tokens"] == 117
assert usage["completion_tokens"] == 18
assert usage["prompt_tokens"] == 99
Expand Down Expand Up @@ -401,10 +401,10 @@ async def test_openai_function_call_async(
assert call.started_at < call.ended_at # type: ignore

output = call.output
assert output.model == "gpt-4o-2024-05-13"
assert output.object == "chat.completion"
assert output["model"] == "gpt-4o-2024-05-13"
assert output["object"] == "chat.completion"

usage = call.summary["usage"][output.model] # type: ignore
usage = call.summary["usage"][output["model"]] # type: ignore
assert usage["total_tokens"] == 117
assert usage["completion_tokens"] == 18
assert usage["prompt_tokens"] == 99
Expand Down Expand Up @@ -577,10 +577,10 @@ def test_openai_tool_call(client: weave.trace.weave_client.WeaveClient) -> None:
assert call.started_at < call.ended_at # type: ignore

output = call.output
assert output.model == "gpt-4o-2024-05-13"
assert output.object == "chat.completion"
assert output["model"] == "gpt-4o-2024-05-13"
assert output["object"] == "chat.completion"

usage = call.summary["usage"][output.model] # type: ignore
usage = call.summary["usage"][output["model"]] # type: ignore
assert usage["total_tokens"] == 117
assert usage["completion_tokens"] == 27
assert usage["prompt_tokens"] == 90
Expand Down Expand Up @@ -664,10 +664,10 @@ async def test_openai_tool_call_async(
assert call.started_at < call.ended_at # type: ignore

output = call.output
assert output.model == "gpt-4o-2024-05-13"
assert output.object == "chat.completion"
assert output["model"] == "gpt-4o-2024-05-13"
assert output["object"] == "chat.completion"

usage = call.summary["usage"][output.model] # type: ignore
usage = call.summary["usage"][output["model"]] # type: ignore
assert usage["total_tokens"] == 117
assert usage["completion_tokens"] == 27
assert usage["prompt_tokens"] == 90
Expand Down
13 changes: 9 additions & 4 deletions weave/integrations/openai/openai_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ def _get_tool_calls(
)
return _tool_calls

dump = None
if isinstance(value, ChatCompletionChunk):
final_value = ChatCompletion(
dump = ChatCompletion(
id=value.id,
choices=[
{
Expand All @@ -116,10 +117,14 @@ def _get_tool_calls(
object="chat.completion",
system_fingerprint=value.system_fingerprint,
usage=value.usage if hasattr(value, "usage") else None,
)
return final_value.model_dump(exclude_unset=True, exclude_none=True)
else:
).model_dump(exclude_unset=True, exclude_none=True)
elif not hasattr(value, "model_dump"):
return value
else:
dump = value.model_dump(exclude_unset=True, exclude_none=True)
if hasattr(value, "_request_id"):
dump["request_id"] = value._request_id
return dump


def openai_accumulator(
Expand Down

0 comments on commit 7f917fa

Please sign in to comment.