-
Notifications
You must be signed in to change notification settings - Fork 63
feat: parsing the tracing file into evaluation cases #44
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
de9f4ed
feat: parsing the tracing file into evaluation cases
BhAem d2cb215
feat: parsing the tracing file into evaluation cases
BhAem 15e3af5
fix: shorten tracing samples in the test
BhAem e82f8c0
fix: manage evalset and tracing file uniformly in one function
BhAem 268b903
fix: modify the function name
BhAem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| # limitations under the License. | ||
|
|
||
|
|
||
| import json | ||
| import time | ||
| import uuid | ||
| from abc import abstractmethod | ||
|
|
@@ -24,6 +25,8 @@ | |
| from google.genai import types | ||
| from pydantic import BaseModel | ||
|
|
||
| from veadk.utils.misc import formatted_timestamp | ||
|
|
||
|
|
||
| class InvocationTestData(BaseModel): | ||
| invocation_id: str = "" | ||
|
|
@@ -79,15 +82,120 @@ def __init__( | |
| self.result_list: list[EvalResultData] = [] | ||
| self.agent_information_list: list[dict] = [] | ||
|
|
||
| def load_eval_set(self, eval_set_file: str) -> list[EvalSet]: | ||
| def load_eval_set(self, eval_set_file: str) -> EvalSet: | ||
| from .eval_set_file_loader import load_eval_set_from_file | ||
|
|
||
| return load_eval_set_from_file(eval_set_file) | ||
|
|
||
| def generate_eval_data(self, eval_set_file_path: str): | ||
| eval_case_data_list: list[EvalCaseData] = [] | ||
|
|
||
| eval_cases = self.load_eval_set(eval_set_file_path).eval_cases | ||
| self.generate_invocation_data(eval_cases, eval_case_data_list) | ||
|
|
||
| def load_tracing_set(self, tracing_set_file_path: str) -> EvalSet: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. def load_eval_set_from_tracing(...) |
||
| with open(tracing_set_file_path, "r") as f: | ||
| tracing_data = json.load(f) | ||
|
|
||
| # Group spans by trace_id | ||
| trace_groups = {} | ||
| for span in tracing_data: | ||
| trace_id = span["trace_id"] | ||
| if trace_id not in trace_groups: | ||
| trace_groups[trace_id] = [] | ||
| trace_groups[trace_id].append(span) | ||
|
|
||
| # Convert to evalset format | ||
| eval_cases, conversation = [], [] | ||
| app_name, user_id = "", "" | ||
| creation_timestamp = 0 | ||
| for trace_id, spans in trace_groups.items(): | ||
| tool_uses = [] | ||
|
|
||
| # Extract tool_uses from spans with name starting with "execute_tool" | ||
| for span in spans: | ||
| if span["name"].startswith("execute_tool"): | ||
| tool_uses.append( | ||
| { | ||
| "id": span["attributes"].get("gen_ai.tool.call.id", None), | ||
| "args": json.loads( | ||
| span["attributes"].get( | ||
| "gcp.vertex.agent.tool_call_args", "{}" | ||
| ) | ||
| ), | ||
| "name": span["attributes"].get("gen_ai.tool.name", None), | ||
| } | ||
| ) | ||
|
|
||
| # Extract conversation data from spans with name starting with "invocation" | ||
| for span in spans: | ||
| if span["name"].startswith("invocation"): | ||
| # Parse input.value and output.value as JSON | ||
| input_value = json.loads( | ||
| span["attributes"].get("input.value", "{}") | ||
| ) | ||
| output_value = json.loads( | ||
| span["attributes"].get("output.value", "{}") | ||
| ) | ||
|
|
||
| user_content = json.loads(input_value.get("new_message", {})) | ||
| final_response = json.loads(json.dumps(user_content)) | ||
| final_response["parts"][0]["text"] = ( | ||
| output_value.get("content", {}) | ||
| .get("parts", [{}])[0] | ||
| .get("text", None) | ||
| ) | ||
| final_response["role"] = None | ||
| conversation.append( | ||
| { | ||
| "invocation_id": output_value.get( | ||
| "invocation_id", str(uuid.uuid4()) | ||
| ), | ||
| "user_content": user_content, | ||
| "final_response": final_response, | ||
| "intermediate_data": { | ||
| "tool_uses": tool_uses, | ||
| "intermediate_responses": [], | ||
| }, | ||
| "creation_timestamp": span["start_time"] / 1e9, | ||
| } | ||
| ) | ||
| user_id = input_value.get("user_id", None) | ||
| app_name = ( | ||
| span["name"].replace("invocation", "").strip().strip("[]") | ||
| ) | ||
| creation_timestamp = span["start_time"] / 1e9 | ||
|
|
||
| eval_cases.append( | ||
| { | ||
| "eval_id": f"veadk_eval_{formatted_timestamp()}", | ||
| "conversation": conversation, | ||
| "session_input": { | ||
| "app_name": app_name, | ||
| "user_id": user_id, | ||
| "state": {}, | ||
| }, | ||
| "creation_timestamp": creation_timestamp, | ||
| } | ||
| ) | ||
|
|
||
| evalset = EvalSet( | ||
| eval_set_id="default", | ||
| name="default", | ||
| description=None, | ||
| eval_cases=eval_cases, | ||
| creation_timestamp=creation_timestamp, | ||
| ) | ||
|
|
||
| return evalset | ||
|
|
||
| def generate_eval_data_from_tracing(self, tracing_set_file_path: str): | ||
| eval_case_data_list: list[EvalCaseData] = [] | ||
| eval_cases = self.load_tracing_set(tracing_set_file_path).eval_cases | ||
| self.generate_invocation_data(eval_cases, eval_case_data_list) | ||
|
|
||
| def generate_invocation_data( | ||
| self, eval_cases: list[EvalSet], eval_case_data_list: list[EvalCaseData] | ||
| ): | ||
| for eval_case in eval_cases: | ||
| eval_case_data = EvalCaseData(invocations=[]) | ||
| self.agent_information_list.append( | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def test_tracing_file_to_evalset()