Skip to content

Commit

Permalink
Merge branch 'development' of github.com:Scale3-Labs/langtrace-python…
Browse files Browse the repository at this point in the history
…-sdk into development
  • Loading branch information
alizenhom committed Jun 24, 2024
2 parents 71b9d9a + 467b2b0 commit 5686642
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 200,803 deletions.
81 changes: 55 additions & 26 deletions src/langtrace_python_sdk/instrumentation/openai/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,54 +276,83 @@ def traced_method(wrapped, instance, args, kwargs):


class StreamWrapper:
def __init__(self, stream, span, prompt_tokens, function_call=False, tool_calls=False):
def __init__(
self, stream, span, prompt_tokens, function_call=False, tool_calls=False
):
self.stream = stream
self.span = span
self.prompt_tokens = prompt_tokens
self.function_call = function_call
self.tool_calls = tool_calls
self.result_content = []
self.completion_tokens = 0
self._span_started = False
self._start_span()

def _start_span(self):
if not self._span_started:
self.span.add_event(Event.STREAM_START.value)
self._span_started = True

def _end_span(self):
if self._span_started:
self.span.add_event(Event.STREAM_END.value)
self.span.set_attribute(
"llm.token.counts",
json.dumps(
{
"input_tokens": self.prompt_tokens,
"output_tokens": self.completion_tokens,
"total_tokens": self.prompt_tokens + self.completion_tokens,
}
),
)
self.span.set_attribute(
"llm.responses",
json.dumps(
[
{
"role": "assistant",
"content": "".join(self.result_content),
}
]
),
)
self.span.set_status(StatusCode.OK)
self.span.end()
self._span_started = False

def __enter__(self):
self.span.add_event(Event.STREAM_START.value)
self._start_span()
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.span.add_event(Event.STREAM_END.value)
self.span.set_attribute(
"llm.token.counts",
json.dumps(
{
"input_tokens": self.prompt_tokens,
"output_tokens": self.completion_tokens,
"total_tokens": self.prompt_tokens + self.completion_tokens,
}
),
)
self.span.set_attribute(
"llm.responses",
json.dumps(
[
{
"role": "assistant",
"content": "".join(self.result_content),
}
]
),
)
self.span.set_status(StatusCode.OK)
self.span.end()
self._end_span()

def __iter__(self):
self._start_span()
return self

def __aiter__(self):
self._start_span()
return self

async def __anext__(self):
try:
chunk = await self.stream.__anext__()
self.process_chunk(chunk)
return chunk
except StopIteration:
self._end_span()
raise

def __next__(self):
try:
chunk = next(self.stream)
self.process_chunk(chunk)
return chunk
except StopIteration:
self._end_span()
raise

def process_chunk(self, chunk):
Expand Down
2 changes: 1 addition & 1 deletion src/langtrace_python_sdk/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.1.22"
__version__ = "2.1.23"
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interactions:
host:
- api.openai.com
user-agent:
- AsyncOpenAI/Python 1.23.2
- AsyncOpenAI/Python 1.34.0
x-stainless-arch:
- arm64
x-stainless-async:
Expand All @@ -26,75 +26,75 @@ interactions:
x-stainless-os:
- MacOS
x-stainless-package-version:
- 1.23.2
- 1.34.0
x-stainless-runtime:
- CPython
x-stainless-runtime-version:
- 3.11.5
- 3.12.3
method: POST
uri: https://api.openai.com/v1/chat/completions
response:
body:
string: 'data: {"id":"chatcmpl-9G7iUffCm37bRsnrZcMjcD55dwZcN","object":"chat.completion.chunk","created":1713629938,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"role":"assistant","content":""},"logprobs":null,"finish_reason":null}]}
string: 'data: {"id":"chatcmpl-9deeLdQVwFn9pGfxPrHrnGhzMINed","object":"chat.completion.chunk","created":1719238077,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"role":"assistant","content":""},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9G7iUffCm37bRsnrZcMjcD55dwZcN","object":"chat.completion.chunk","created":1713629938,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"This"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9deeLdQVwFn9pGfxPrHrnGhzMINed","object":"chat.completion.chunk","created":1719238077,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"This"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9G7iUffCm37bRsnrZcMjcD55dwZcN","object":"chat.completion.chunk","created":1713629938,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"
data: {"id":"chatcmpl-9deeLdQVwFn9pGfxPrHrnGhzMINed","object":"chat.completion.chunk","created":1719238077,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"
is"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9G7iUffCm37bRsnrZcMjcD55dwZcN","object":"chat.completion.chunk","created":1713629938,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"
data: {"id":"chatcmpl-9deeLdQVwFn9pGfxPrHrnGhzMINed","object":"chat.completion.chunk","created":1719238077,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"
a"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9G7iUffCm37bRsnrZcMjcD55dwZcN","object":"chat.completion.chunk","created":1713629938,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"
data: {"id":"chatcmpl-9deeLdQVwFn9pGfxPrHrnGhzMINed","object":"chat.completion.chunk","created":1719238077,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"
test"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9G7iUffCm37bRsnrZcMjcD55dwZcN","object":"chat.completion.chunk","created":1713629938,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"."},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9deeLdQVwFn9pGfxPrHrnGhzMINed","object":"chat.completion.chunk","created":1719238077,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"."},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9G7iUffCm37bRsnrZcMjcD55dwZcN","object":"chat.completion.chunk","created":1713629938,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"
data: {"id":"chatcmpl-9deeLdQVwFn9pGfxPrHrnGhzMINed","object":"chat.completion.chunk","created":1719238077,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"
This"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9G7iUffCm37bRsnrZcMjcD55dwZcN","object":"chat.completion.chunk","created":1713629938,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"
data: {"id":"chatcmpl-9deeLdQVwFn9pGfxPrHrnGhzMINed","object":"chat.completion.chunk","created":1719238077,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"
is"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9G7iUffCm37bRsnrZcMjcD55dwZcN","object":"chat.completion.chunk","created":1713629938,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"
data: {"id":"chatcmpl-9deeLdQVwFn9pGfxPrHrnGhzMINed","object":"chat.completion.chunk","created":1719238077,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"
a"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9G7iUffCm37bRsnrZcMjcD55dwZcN","object":"chat.completion.chunk","created":1713629938,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"
data: {"id":"chatcmpl-9deeLdQVwFn9pGfxPrHrnGhzMINed","object":"chat.completion.chunk","created":1719238077,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"
test"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9G7iUffCm37bRsnrZcMjcD55dwZcN","object":"chat.completion.chunk","created":1713629938,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"."},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9deeLdQVwFn9pGfxPrHrnGhzMINed","object":"chat.completion.chunk","created":1719238077,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"."},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9G7iUffCm37bRsnrZcMjcD55dwZcN","object":"chat.completion.chunk","created":1713629938,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"
data: {"id":"chatcmpl-9deeLdQVwFn9pGfxPrHrnGhzMINed","object":"chat.completion.chunk","created":1719238077,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"
This"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9G7iUffCm37bRsnrZcMjcD55dwZcN","object":"chat.completion.chunk","created":1713629938,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"
data: {"id":"chatcmpl-9deeLdQVwFn9pGfxPrHrnGhzMINed","object":"chat.completion.chunk","created":1719238077,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"
is"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9G7iUffCm37bRsnrZcMjcD55dwZcN","object":"chat.completion.chunk","created":1713629938,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"
data: {"id":"chatcmpl-9deeLdQVwFn9pGfxPrHrnGhzMINed","object":"chat.completion.chunk","created":1719238077,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"
a"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9G7iUffCm37bRsnrZcMjcD55dwZcN","object":"chat.completion.chunk","created":1713629938,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"
data: {"id":"chatcmpl-9deeLdQVwFn9pGfxPrHrnGhzMINed","object":"chat.completion.chunk","created":1719238077,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"
test"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9G7iUffCm37bRsnrZcMjcD55dwZcN","object":"chat.completion.chunk","created":1713629938,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"."},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9deeLdQVwFn9pGfxPrHrnGhzMINed","object":"chat.completion.chunk","created":1719238077,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"."},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9G7iUffCm37bRsnrZcMjcD55dwZcN","object":"chat.completion.chunk","created":1713629938,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}]}
data: {"id":"chatcmpl-9deeLdQVwFn9pGfxPrHrnGhzMINed","object":"chat.completion.chunk","created":1719238077,"model":"gpt-4-0613","system_fingerprint":null,"choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}]}
data: [DONE]
Expand All @@ -105,39 +105,33 @@ interactions:
CF-Cache-Status:
- DYNAMIC
CF-RAY:
- 8776740aaa3e73a3-MRS
Cache-Control:
- no-cache, must-revalidate
- 898d497cff180da3-MRS
Connection:
- keep-alive
Content-Type:
- text/event-stream
- text/event-stream; charset=utf-8
Date:
- Sat, 20 Apr 2024 16:18:59 GMT
- Mon, 24 Jun 2024 14:07:57 GMT
Server:
- cloudflare
Set-Cookie:
- __cf_bm=VDKSK7GoYoxisSawjMl9W0b7YZarMfekW_Y69gq5ons-1713629939-1.0.1.1-fH6Sc.9fQ0Kb4MvzvnBRlAmk_cXfYNeNbDd_K6pZeMNxnmMy3qiDlS.olHx3Y7rfDhYg7a3FffrCHr.Xu8j_Uw;
path=/; expires=Sat, 20-Apr-24 16:48:59 GMT; domain=.api.openai.com; HttpOnly;
- __cf_bm=9.DjXDRH6yqveelPQR44ZtyVUYJzgnA0G.nzqFBokzU-1719238077-1.0.1.1-aMCvEWGLFuE6ItsOru0NDaEtwnP4gVQChxLODAqfb5jmClZJa8lKqg.tNgoeMSGnxhnih1YJRhw2gq02fThn1g;
path=/; expires=Mon, 24-Jun-24 14:37:57 GMT; domain=.api.openai.com; HttpOnly;
Secure; SameSite=None
- _cfuvid=GmZ5rJWGxt2nlOYm6_pDkhIo_8V.YkD6O9_B1qloO7g-1713629939085-0.0.1.1-604800000;
- _cfuvid=O9yzYcvRGvChbCerFoeKPvIqX_jwhxHPRVMMxoF4wLY-1719238077679-0.0.1.1-604800000;
path=/; domain=.api.openai.com; HttpOnly; Secure; SameSite=None
Transfer-Encoding:
- chunked
access-control-allow-origin:
- '*'
alt-svc:
- h3=":443"; ma=86400
openai-model:
- gpt-4-0613
openai-organization:
- scale3-1
openai-processing-ms:
- '247'
- '193'
openai-version:
- '2020-10-01'
strict-transport-security:
- max-age=15724800; includeSubDomains
- max-age=31536000; includeSubDomains
x-ratelimit-limit-requests:
- '10000'
x-ratelimit-limit-tokens:
Expand All @@ -151,7 +145,7 @@ interactions:
x-ratelimit-reset-tokens:
- 5ms
x-request-id:
- req_4c6f987df5c44fb9c842d54126d2608d
- ae303b10004d584d9da9cb432917ef64
status:
code: 200
message: OK
Expand Down
Loading

0 comments on commit 5686642

Please sign in to comment.