mirror of
https://github.com/open-webui/pipelines
synced 2025-05-12 16:40:45 +00:00
Refactor: Remove unnecessary cache and exclude last assistant message from input to maintain context integrity
This commit is contained in:
parent
ee5255144f
commit
eff7d5bc3a
@ -25,6 +25,12 @@ def get_last_assistant_message_obj(messages: List[dict]) -> dict:
|
|||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
|
||||||
|
def remove_last_assistant_message(messages: List[dict]) -> List[dict]:
|
||||||
|
if messages and messages[-1]["role"] == "assistant":
|
||||||
|
return messages[:-1]
|
||||||
|
return messages
|
||||||
|
|
||||||
|
|
||||||
class Pipeline:
|
class Pipeline:
|
||||||
class Valves(BaseModel):
|
class Valves(BaseModel):
|
||||||
pipelines: List[str] = []
|
pipelines: List[str] = []
|
||||||
@ -45,8 +51,6 @@ class Pipeline:
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
self.langfuse = None
|
self.langfuse = None
|
||||||
self.chat_traces = {}
|
|
||||||
self.chat_generations = {}
|
|
||||||
|
|
||||||
async def on_startup(self):
|
async def on_startup(self):
|
||||||
print(f"on_startup:{__name__}")
|
print(f"on_startup:{__name__}")
|
||||||
@ -85,38 +89,30 @@ class Pipeline:
|
|||||||
print("chat_id not in body")
|
print("chat_id not in body")
|
||||||
return body
|
return body
|
||||||
|
|
||||||
if (
|
user_id = user.get("id") if user else None
|
||||||
body["chat_id"] not in self.chat_generations
|
user_name = user.get("name") if user else None
|
||||||
or body["chat_id"] not in self.chat_traces
|
user_email = user.get("email") if user else None
|
||||||
):
|
|
||||||
user_id = user.get("id") if user else None
|
|
||||||
user_name = user.get("name") if user else None
|
|
||||||
user_email = user.get("email") if user else None
|
|
||||||
|
|
||||||
trace = self.langfuse.trace(
|
input_messages = remove_last_assistant_message(body["messages"])
|
||||||
name=f"filter:{__name__}",
|
trace = self.langfuse.trace(
|
||||||
input=body,
|
name=f"filter:{__name__}",
|
||||||
user_id=user_email,
|
input=input_messages,
|
||||||
metadata={
|
user_id=user_email,
|
||||||
"user_name": user_name,
|
metadata={
|
||||||
"user_id": user_id,
|
"user_name": user_name,
|
||||||
"chat_id": body["chat_id"],
|
"user_id": user_id,
|
||||||
},
|
"chat_id": body["chat_id"],
|
||||||
session_id=body["chat_id"],
|
},
|
||||||
)
|
session_id=body["chat_id"],
|
||||||
|
)
|
||||||
|
|
||||||
generation = trace.generation(
|
generation = trace.generation(
|
||||||
name=body["chat_id"],
|
name=body["chat_id"],
|
||||||
model=body["model"],
|
model=body["model"],
|
||||||
input=body["messages"],
|
input=input_messages,
|
||||||
metadata={"interface": "open-webui"},
|
metadata={"interface": "open-webui"},
|
||||||
)
|
)
|
||||||
|
|
||||||
self.chat_traces[body["chat_id"]] = trace
|
|
||||||
self.chat_generations[body["chat_id"]] = generation
|
|
||||||
|
|
||||||
trace = self.chat_traces[body["chat_id"]]
|
|
||||||
generation = self.chat_generations[body["chat_id"]]
|
|
||||||
assistant_message = get_last_assistant_message(body["messages"])
|
assistant_message = get_last_assistant_message(body["messages"])
|
||||||
|
|
||||||
# Extract usage information for models that support it
|
# Extract usage information for models that support it
|
||||||
@ -146,8 +142,4 @@ class Pipeline:
|
|||||||
usage=usage,
|
usage=usage,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Clean up the chat_generations dictionary
|
|
||||||
del self.chat_traces[body["chat_id"]]
|
|
||||||
del self.chat_generations[body["chat_id"]]
|
|
||||||
|
|
||||||
return body
|
return body
|
||||||
|
Loading…
Reference in New Issue
Block a user