|
1 | 1 | """
|
2 | 2 | title: Langfuse Filter Pipeline
|
3 | 3 | author: open-webui
|
4 |
| -date: 2024-05-30 |
5 |
| -version: 1.3 |
| 4 | +date: 2024-09-27 |
| 5 | +version: 1.4 |
6 | 6 | license: MIT
|
7 | 7 | description: A filter pipeline that uses Langfuse.
|
8 | 8 | requirements: langfuse
|
9 | 9 | """
|
10 | 10 |
|
11 | 11 | from typing import List, Optional
|
12 |
| -from schemas import OpenAIChatMessage |
13 | 12 | import os
|
14 | 13 | import uuid
|
15 | 14 |
|
16 |
| -from utils.pipelines.main import get_last_user_message, get_last_assistant_message |
| 15 | +from utils.pipelines.main import get_last_assistant_message |
17 | 16 | from pydantic import BaseModel
|
18 | 17 | from langfuse import Langfuse
|
19 | 18 | from langfuse.api.resources.commons.errors.unauthorized_error import UnauthorizedError
|
20 | 19 |
|
| 20 | +def get_last_assistant_message_obj(messages: List[dict]) -> dict: |
| 21 | + for message in reversed(messages): |
| 22 | + if message["role"] == "assistant": |
| 23 | + return message |
| 24 | + return {} |
| 25 | + |
21 | 26 |
|
22 | 27 | class Pipeline:
|
23 | 28 | class Valves(BaseModel):
|
@@ -109,21 +114,28 @@ async def inlet(self, body: dict, user: Optional[dict] = None) -> dict:
|
109 | 114 |
|
110 | 115 | async def outlet(self, body: dict, user: Optional[dict] = None) -> dict:
|
111 | 116 | print(f"outlet:{__name__}")
|
| 117 | + print(f"Received body: {body}") |
112 | 118 | if body["chat_id"] not in self.chat_generations:
|
113 | 119 | return body
|
114 | 120 |
|
115 | 121 | generation = self.chat_generations[body["chat_id"]]
|
116 | 122 | assistant_message = get_last_assistant_message(body["messages"])
|
117 | 123 |
|
118 |
| - # Extract usage information |
119 |
| - info = assistant_message.get("info", {}) |
| 124 | + |
| 125 | + # Extract usage information for models that support it |
120 | 126 | usage = None
|
121 |
| - if "prompt_tokens" in info and "completion_tokens" in info: |
122 |
| - usage = { |
123 |
| - "input": info["prompt_tokens"], |
124 |
| - "output": info["completion_tokens"], |
125 |
| - "unit": "TOKENS", |
126 |
| - } |
| 127 | + assistant_message_obj = get_last_assistant_message_obj(body["messages"]) |
| 128 | + if assistant_message_obj: |
| 129 | + info = assistant_message_obj.get("info", {}) |
| 130 | + if isinstance(info, dict): |
| 131 | + input_tokens = info.get("prompt_eval_count") or info.get("prompt_tokens") |
| 132 | + output_tokens = info.get("eval_count") or info.get("completion_tokens") |
| 133 | + if input_tokens is not None and output_tokens is not None: |
| 134 | + usage = { |
| 135 | + "input": input_tokens, |
| 136 | + "output": output_tokens, |
| 137 | + "unit": "TOKENS", |
| 138 | + } |
127 | 139 |
|
128 | 140 | # Update generation
|
129 | 141 | generation.end(
|
|
0 commit comments