Skip to content

Commit

Permalink
Merge pull request #387 from marcklingen/fix-output-tracking-in-langf…
Browse files Browse the repository at this point in the history
…use-pipe

fix: include assistant message as trace.output in langfuse
  • Loading branch information
tjbck authored Jan 6, 2025
2 parents 1367d95 + bca4f84 commit db29eb2
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions examples/filters/langfuse_filter_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __init__(self):
}
)
self.langfuse = None
self.chat_traces = {}
self.chat_generations = {}

async def on_startup(self):
Expand Down Expand Up @@ -107,17 +108,18 @@ async def inlet(self, body: dict, user: Optional[dict] = None) -> dict:
metadata={"interface": "open-webui"},
)

self.chat_traces[body["chat_id"]] = trace
self.chat_generations[body["chat_id"]] = generation
print(trace.get_trace_url())

return body

async def outlet(self, body: dict, user: Optional[dict] = None) -> dict:
print(f"outlet:{__name__}")
print(f"Received body: {body}")
if body["chat_id"] not in self.chat_generations:
if body["chat_id"] not in self.chat_generations or body["chat_id"] not in self.chat_traces:
return body

trace = self.chat_traces[body["chat_id"]]
generation = self.chat_generations[body["chat_id"]]
assistant_message = get_last_assistant_message(body["messages"])

Expand All @@ -138,13 +140,17 @@ async def outlet(self, body: dict, user: Optional[dict] = None) -> dict:
}

# Update generation
trace.update(
output=assistant_message,
)
generation.end(
output=assistant_message,
metadata={"interface": "open-webui"},
usage=usage,
)

# Clean up the chat_generations dictionary
del self.chat_traces[body["chat_id"]]
del self.chat_generations[body["chat_id"]]

return body

0 comments on commit db29eb2

Please sign in to comment.