Skip to content

Commit 21d0487

Browse files
committed
async generate
1 parent 1f46733 commit 21d0487

File tree

1 file changed

+5
-3
lines changed
  • src/langtrace_python_sdk/instrumentation/ollama

1 file changed

+5
-3
lines changed

src/langtrace_python_sdk/instrumentation/ollama/patch.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ def _set_input_attributes(span, kwargs, attributes):
165165

166166
def _handle_streaming_response(span, response, api):
167167
accumulated_tokens = None
168-
print("APIIII", api)
169168
if api == "chat":
170169
accumulated_tokens = {"message": {"content": "", "role": ""}}
171170
if api == "completion" or api == "generate":
@@ -198,19 +197,22 @@ async def _ahandle_streaming_response(span, response, api):
198197
accumulated_tokens = None
199198
if api == "chat":
200199
accumulated_tokens = {"message": {"content": "", "role": ""}}
201-
if api == "completion":
200+
if api == "completion" or api == "generate":
202201
accumulated_tokens = {"response": ""}
203202

204203
span.add_event(Event.STREAM_START.value)
205204
try:
206205
async for chunk in response:
206+
content = None
207207
if api == "chat":
208+
content = chunk["message"]["content"]
208209
accumulated_tokens["message"]["content"] += chunk["message"]["content"]
209210
accumulated_tokens["message"]["role"] = chunk["message"]["role"]
210211
if api == "generate":
212+
content = chunk["response"]
211213
accumulated_tokens["response"] += chunk["response"]
212214

213-
set_event_completion_chunk(span, chunk)
215+
set_event_completion_chunk(span, content)
214216
_set_response_attributes(span, chunk | accumulated_tokens)
215217
finally:
216218
# Finalize span after processing all chunks

0 commit comments

Comments
 (0)