From 68938c0808e9336ca076c0748d7866289aa50e82 Mon Sep 17 00:00:00 2001 From: Marc Klingen Date: Fri, 27 Sep 2024 18:56:52 +0200 Subject: [PATCH 1/3] fix --- examples/filters/langfuse_filter_pipeline.py | 32 ++++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/examples/filters/langfuse_filter_pipeline.py b/examples/filters/langfuse_filter_pipeline.py index ada6d888..2bee9b6b 100644 --- a/examples/filters/langfuse_filter_pipeline.py +++ b/examples/filters/langfuse_filter_pipeline.py @@ -9,15 +9,20 @@ """ from typing import List, Optional -from schemas import OpenAIChatMessage import os import uuid -from utils.pipelines.main import get_last_user_message, get_last_assistant_message +from utils.pipelines.main import get_last_assistant_message from pydantic import BaseModel from langfuse import Langfuse from langfuse.api.resources.commons.errors.unauthorized_error import UnauthorizedError +def get_last_assistant_message_obj(messages: List[dict]) -> dict: + for message in reversed(messages): + if message["role"] == "assistant": + return message + return {} + class Pipeline: class Valves(BaseModel): @@ -109,21 +114,28 @@ async def inlet(self, body: dict, user: Optional[dict] = None) -> dict: 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: return body generation = self.chat_generations[body["chat_id"]] assistant_message = get_last_assistant_message(body["messages"]) - # Extract usage information - info = assistant_message.get("info", {}) + + # Extract usage information for models that support it usage = None - if "prompt_tokens" in info and "completion_tokens" in info: - usage = { - "input": info["prompt_tokens"], - "output": info["completion_tokens"], - "unit": "TOKENS", - } + assistant_message_obj = get_last_assistant_message_obj(body["messages"]) + if assistant_message_obj: + info = assistant_message_obj.get("info", {}) + if isinstance(info, dict): + prompt_eval_count = info.get("prompt_eval_count") + eval_count = info.get("eval_count") + if prompt_eval_count is not None and eval_count is not None: + usage = { + "input": prompt_eval_count, + "output": eval_count, + "unit": "TOKENS", + } # Update generation generation.end( From bd4d8249036d5670db83d3a0fdb5d5109cc89c34 Mon Sep 17 00:00:00 2001 From: Marc Klingen Date: Fri, 27 Sep 2024 19:06:22 +0200 Subject: [PATCH 2/3] openai non-stream metrics --- examples/filters/langfuse_filter_pipeline.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/filters/langfuse_filter_pipeline.py b/examples/filters/langfuse_filter_pipeline.py index 2bee9b6b..1fab2012 100644 --- a/examples/filters/langfuse_filter_pipeline.py +++ b/examples/filters/langfuse_filter_pipeline.py @@ -128,12 +128,12 @@ async def outlet(self, body: dict, user: Optional[dict] = None) -> dict: if assistant_message_obj: info = assistant_message_obj.get("info", {}) if isinstance(info, dict): - prompt_eval_count = info.get("prompt_eval_count") - eval_count = info.get("eval_count") - if prompt_eval_count is not None and eval_count is not None: + input_tokens = info.get("prompt_eval_count") or info.get("prompt_tokens") + output_tokens = info.get("eval_count") or info.get("completion_tokens") + if input_tokens is not None and output_tokens is not None: usage = { - "input": prompt_eval_count, - "output": eval_count, + "input": input_tokens, + "output": output_tokens, "unit": "TOKENS", } From 57889980063b98ddc40f701a0310ac0029e56d28 Mon Sep 17 00:00:00 2001 From: Marc Klingen Date: Fri, 27 Sep 2024 19:47:49 +0200 Subject: [PATCH 3/3] bump version --- examples/filters/langfuse_filter_pipeline.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/filters/langfuse_filter_pipeline.py b/examples/filters/langfuse_filter_pipeline.py index 1fab2012..8576ef2f 100644 --- a/examples/filters/langfuse_filter_pipeline.py +++ b/examples/filters/langfuse_filter_pipeline.py @@ -1,8 +1,8 @@ """ title: Langfuse Filter Pipeline author: open-webui -date: 2024-05-30 -version: 1.3 +date: 2024-09-27 +version: 1.4 license: MIT description: A filter pipeline that uses Langfuse. requirements: langfuse