Skip to content

Commit

Permalink
feat: Implement reliability processing for agent task results
Browse files Browse the repository at this point in the history
  • Loading branch information
onuratakan committed Feb 12, 2025
1 parent 6809230 commit 3681c08
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/upsonic/client/level_two/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

from ..tools.tools import Search

from ...reliability_processor import ReliabilityProcessor


class SubTask(ObjectResponse):
description: str
Expand Down Expand Up @@ -195,7 +197,14 @@ def send_agent_request(
with sentry_sdk.start_span(op="deserialize"):
deserialized_result = response_format_deserializer(response_format_str, result)

task._response = deserialized_result["result"]
# Process result through reliability layer
processed_result = ReliabilityProcessor.process_result(
deserialized_result["result"],
agent_configuration.reliability_layer,
task,
llm_model
)
task._response = processed_result

response_format_req = None
if response_format_str == "str":
Expand All @@ -209,7 +218,7 @@ def send_agent_request(

len_of_context = len(task.context) if task.context is not None else 0

return {"result": deserialized_result["result"], "llm_model": llm_model, "response_format": response_format_req, "usage": deserialized_result["usage"], "tool_count": len(tools), "context_count": len_of_context}
return {"result": processed_result, "llm_model": llm_model, "response_format": response_format_req, "usage": deserialized_result["usage"], "tool_count": len(tools), "context_count": len_of_context}

except CallErrorException as e:
last_error = e
Expand Down
66 changes: 66 additions & 0 deletions src/upsonic/reliability_processor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from copy import deepcopy
from typing import Any, Optional, Union, Type
from .client.tasks.tasks import Task

from .client.agent_configuration.agent_configuration import AgentConfiguration

from pydantic import Field

from .client.tasks.task_response import ObjectResponse


class ValidationResult(ObjectResponse):
result: bool
feedback: str = Field(description="Feedback on the result if it is incorrect. Only fill this field if the result is incorrect")


class ReliabilityProcessor:
@staticmethod
def process_result(
result: Any,
reliability_layer: Optional[Any] = None,
task: Optional[Task] = None,
llm_model: Optional[str] = None
) -> Any:
"""
Process the result based on reliability layer settings.
If reliability_layer is None or fields are 0, return original result.
Args:
result: The result to process
reliability_layer: Configuration for reliability checks (can be instance or class)
task: The original task that generated this result
llm_model: The LLM model used to generate the result
"""
if reliability_layer is None:
return result

# Get prevent_hallucination value
prevent_hallucination = getattr(reliability_layer, 'prevent_hallucination', 0)
if isinstance(prevent_hallucination, property):
prevent_hallucination = prevent_hallucination.fget(reliability_layer)

processed_result = result

# Check prevent_hallucination
if prevent_hallucination > 0:

if prevent_hallucination == 10:
validator_agent = AgentConfiguration("Information Validator Agent", model=llm_model, sub_task=False)
copy_task = deepcopy(task)
copy_task._response = result
validator_task = Task("Evaluate the old question and its answer for correctness, consistency, and completeness", context=[copy_task], response_format=ValidationResult)
validator_agent.do(validator_task)

if validator_task.response.result == True:
return result
else:
editor_agent = AgentConfiguration("Information Editor Agent", model=llm_model, sub_task=False)
editor_task = Task("Edit the answer to the question to correct the inaccuracies by the feedback provided", context=[copy_task, validator_task], response_format=task.response_format)
editor_agent.do(editor_task)

return editor_task.response



return processed_result

0 comments on commit 3681c08

Please sign in to comment.