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

WIP: Feedback fetching with filtering #200

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
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
103 changes: 99 additions & 4 deletions log10/feedback/feedback.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from rich.table import Table
from tqdm import tqdm

from log10._httpx_utils import _try_get
from log10._httpx_utils import _try_get, _try_post_graphql_request
from log10.llm import Log10Config


Expand Down Expand Up @@ -59,7 +59,10 @@ def create(
res = self._post_request(self.feedback_create_url, json_payload)
return res

def list(self, offset: int = 0, limit: int = 50, task_id: str = "") -> httpx.Response:
def list(self, offset: int = 0, limit: int = 50, task_id: str = "", filter: str = "") -> httpx.Response:
if filter:
return self.list_v2(page=round(offset / limit) + 1, limit=limit, task_id=task_id, filter=filter)

base_url = self._log10_config.url
api_url = "/api/v1/feedback"
url = f"{base_url}{api_url}?organization_id={self._log10_config.org_id}&offset={offset}&limit={limit}&task_id={task_id}"
Expand All @@ -75,6 +78,52 @@ def list(self, offset: int = 0, limit: int = 50, task_id: str = "") -> httpx.Res
logger.error(e.response.json()["error"])
raise

def list_v2(self, page: int = 1, limit: int = 50, task_id: str = "", filter: str = "") -> httpx.Response:
query = """
query OrganizationFeedback($id: String!, $filter: String, $taskId: String, $page: Int, $limit: Int) {
organization(id: $id) {
id
feedbackV2(filter: $filter, taskId: $taskId, page: $page, limit: $limit) {
pageInfo{
totalCount
totalCount
currentPage
}
nodes {
id
jsonValues
task {
id
name
}
completions {
id
}
}
}
}
}
"""

variables = {
"id": self._log10_config.org_id,
"taskId": task_id or None,
"filter": filter or None,
"page": page,
"limit": limit,
}

response = _try_post_graphql_request(query, variables)

if response is None:
logger.error("Failed to get feedback")
return None

if response.status_code == 200:
return response.json()
else:
response.raise_for_status()

def get(self, id: str) -> httpx.Response:
base_url = self._log10_config.url
api_url = "/api/v1/feedback"
Expand Down Expand Up @@ -106,10 +155,48 @@ def create_feedback(task_id, values, completion_tags_selector, comment):
click.echo(feedback.json())


def _format_graphql_node(node):
return {
"id": node["id"],
"json_values": node["jsonValues"],
"task_id": node["task"]["id"],
"task_name": node["task"]["name"],
"matched_completion_ids": [c["id"] for c in node["completions"]],
}


def _get_feedback_list_graphql(page, limit, task_id, filter):
feedback_data = []
current_page = page if page else 1
if limit:
limit = int(limit)

try:
while True:
fetch_limit = limit if limit else 50
res = Feedback().list_v2(page=current_page, limit=fetch_limit, task_id=task_id, filter=filter)
new_data = res["data"]["organization"]["feedbackV2"]["nodes"]
feedback_data.extend(new_data)

current_fetched = len(new_data)
current_page += 1

if current_fetched < fetch_limit:
break
except Exception as e:
click.echo(f"Error fetching feedback {e}")
if hasattr(e, "response") and hasattr(e.response, "json") and "error" in e.response.json():
click.echo(e.response.json()["error"])
return []

return list(map(_format_graphql_node, feedback_data))


def _get_feedback_list(offset, limit, task_id):
total_fetched = 0
feedback_data = []
total_feedback = 0

if limit:
limit = int(limit)

Expand Down Expand Up @@ -151,12 +238,20 @@ def _get_feedback_list(offset, limit, task_id):
type=str,
help="The specific Task ID to filter feedback. If not provided, feedback for all tasks will be fetched.",
)
def list_feedback(offset, limit, task_id):
@click.option(
"--filter",
default="",
type=str,
help="The filter applied to the feedback. If not provided, feedback will not be filtered.",
)
def list_feedback(offset, limit, task_id, filter):
"""
List feedback based on the provided criteria. This command allows fetching feedback for a specific task or across all tasks,
with control over the starting point and the number of items to retrieve.
"""
feedback_data = _get_feedback_list(offset, limit, task_id)
feedback_data = (
_get_feedback_list_graphql(1, limit, task_id, filter) if filter else _get_feedback_list(offset, limit, task_id)
)
data_for_table = []
for feedback in feedback_data:
data_for_table.append(
Expand Down