Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added code to change loop input to jinja input #171

Merged
merged 2 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions athina/helpers/step_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import json

class StepHelper:

@staticmethod
def prepare_input_data(data):
return {
key: json.dumps(value) if isinstance(value, (list, dict)) else value
for key, value in data.items()
}
16 changes: 14 additions & 2 deletions athina/steps/loop.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import asyncio
import json
from typing import Dict, List, Any, Optional
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Remove unused Any import to comply with linting rules.

Pipeline logs indicate that Any is not used in this file. Removing it will resolve the F401 lint error.

Apply this diff to remove the unused import:

-from typing import Dict, List, Any, Optional
+from typing import Dict, List, Optional
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
from typing import Dict, List, Any, Optional
from typing import Dict, List, Optional
🧰 Tools
🪛 Ruff (0.8.2)

3-3: typing.Any imported but unused

Remove unused import: typing.Any

(F401)

from athina.steps.base import Step
from concurrent.futures import ThreadPoolExecutor

from jinja2 import Environment
from athina.helpers.jinja_helper import PreserveUndefined
from athina.helpers.step_helper import StepHelper

class Loop(Step):
loop_type: str
Expand Down Expand Up @@ -50,7 +53,16 @@ async def _execute_loop(self, inputs: Dict) -> Dict:
results = []

if self.loop_type == "map":
items = inputs.get(self.loop_input, [])
env = Environment(
variable_start_string="{{",
variable_end_string="}}",
undefined=PreserveUndefined,
)

loop_input_template = env.from_string(self.loop_input)
prepared_input_data = StepHelper.prepare_input_data(inputs)
loop_input = loop_input_template.render(**prepared_input_data)
items = json.loads(loop_input, strict=False) if loop_input else None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix indentation to resolve pipeline failure and handle JSON parsing errors gracefully.

  1. The pipeline complains that the closing bracket on line 60 is incorrectly indented.
  2. Consider wrapping json.loads in a try/except block to provide user-friendly error messages if rendering produces invalid JSON.

Proposed fix:

             if self.loop_type == "map":
-                env = Environment(
-                    variable_start_string="{{",
-                    variable_end_string="}}",
-                    undefined=PreserveUndefined,
-                    )
+                env = Environment(
+                    variable_start_string="{{",
+                    variable_end_string="}}",
+                    undefined=PreserveUndefined,
+                )

                 loop_input_template = env.from_string(self.loop_input)
                 prepared_input_data = StepHelper.prepare_input_data(inputs)
                 loop_input = loop_input_template.render(**prepared_input_data)
-                items = json.loads(loop_input, strict=False) if loop_input else None
+                try:
+                    items = json.loads(loop_input, strict=False) if loop_input else []
+                except json.JSONDecodeError:
+                    return {
+                        "status": "error",
+                        "data": "Invalid JSON format in loop_input",
+                        "metadata": {}
+                    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if self.loop_type == "map":
items = inputs.get(self.loop_input, [])
env = Environment(
variable_start_string="{{",
variable_end_string="}}",
undefined=PreserveUndefined,
)
loop_input_template = env.from_string(self.loop_input)
prepared_input_data = StepHelper.prepare_input_data(inputs)
loop_input = loop_input_template.render(**prepared_input_data)
items = json.loads(loop_input, strict=False) if loop_input else None
if self.loop_type == "map":
env = Environment(
variable_start_string="{{",
variable_end_string="}}",
undefined=PreserveUndefined,
)
loop_input_template = env.from_string(self.loop_input)
prepared_input_data = StepHelper.prepare_input_data(inputs)
loop_input = loop_input_template.render(**prepared_input_data)
try:
items = json.loads(loop_input, strict=False) if loop_input else []
except json.JSONDecodeError:
return {
"status": "error",
"data": "Invalid JSON format in loop_input",
"metadata": {}
}
🧰 Tools
🪛 GitHub Actions: Python Linter

[error] 60-60: closing bracket does not match indentation of opening bracket's line

if not isinstance(items, list):
return {"status": "error", "data": "Input not of type list", "metadata": {}}

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "athina"
version = "1.7.13"
version = "1.7.14"
description = "Python SDK to configure and run evaluations for your LLM-based application"
authors = ["Shiv Sakhuja <[email protected]>", "Akshat Gupta <[email protected]>", "Vivek Aditya <[email protected]>", "Akhil Bisht <[email protected]>"]
readme = "README.md"
Expand Down
Loading