Skip to content

Commit

Permalink
fix: check chunk has choises before printing
Browse files Browse the repository at this point in the history
  • Loading branch information
k11kirky committed Jan 11, 2025
1 parent 4c41388 commit 43f39cf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
10 changes: 6 additions & 4 deletions llm_observability_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def main_sync():

try:
basic_openai_call(distinct_id, trace_id, properties)
# streaming_openai_call(distinct_id, trace_id, properties)
# non_instrumented_openai_call()
streaming_openai_call(distinct_id, trace_id, properties)
non_instrumented_openai_call()
except Exception as e:
print("Error during OpenAI call:", str(e))

Expand Down Expand Up @@ -106,7 +106,8 @@ def streaming_openai_call(distinct_id, trace_id, properties):
)

for chunk in response:
print(chunk.choices[0].delta.content or "", end="")
if hasattr(chunk, "choices") and chunk.choices and len(chunk.choices) > 0:
print(chunk.choices[0].delta.content or "", end="")

return response

Expand All @@ -127,7 +128,8 @@ async def streaming_async_openai_call(distinct_id, trace_id, properties):
)

async for chunk in response:
print(chunk.choices[0].delta.content or "", end="")
if hasattr(chunk, "choices") and chunk.choices and len(chunk.choices) > 0:
print(chunk.choices[0].delta.content or "", end="")

return response

Expand Down
9 changes: 7 additions & 2 deletions posthog/ai/providers/openai/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,14 @@ def generator():
"total_tokens",
]
}
if chunk.choices[0].delta.content:
accumulated_content.append(chunk.choices[0].delta.content)

if hasattr(chunk, "choices") and chunk.choices and len(chunk.choices) > 0:
content = chunk.choices[0].delta.content
if content:
accumulated_content.append(content)

yield chunk

finally:
end_time = time.time()
latency = end_time - start_time
Expand Down
8 changes: 6 additions & 2 deletions posthog/ai/providers/openai/openai_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,13 @@ async def async_generator():
"total_tokens",
]
}
if chunk.choices[0].delta.content:
accumulated_content.append(chunk.choices[0].delta.content)
if hasattr(chunk, "choices") and chunk.choices and len(chunk.choices) > 0:
content = chunk.choices[0].delta.content
if content:
accumulated_content.append(content)

yield chunk

finally:
end_time = time.time()
latency = end_time - start_time
Expand Down

0 comments on commit 43f39cf

Please sign in to comment.