|
| 1 | +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import click |
| 16 | +import json |
| 17 | +import requests |
| 18 | +from veadk.utils.logger import get_logger |
| 19 | +from veadk.config import getenv |
| 20 | +from pathlib import Path |
| 21 | + |
| 22 | +logger = get_logger(__name__) |
| 23 | + |
| 24 | + |
| 25 | +@click.command() |
| 26 | +@click.option("--file", required=True, help="JSON file path containing dataset items") |
| 27 | +@click.option("--cozeloop-workspace-id", default=None, help="CozeLoop workspace ID") |
| 28 | +@click.option("--cozeloop-evalset-id", default=None, help="CozeLoop evaluation set ID") |
| 29 | +@click.option( |
| 30 | + "--cozeloop-api-key", |
| 31 | + default=None, |
| 32 | + help="CozeLoop API key (or set COZELOOP_API_KEY env var)", |
| 33 | +) |
| 34 | +def uploadevalset( |
| 35 | + file: str, |
| 36 | + cozeloop_workspace_id: str, |
| 37 | + cozeloop_evalset_id: str, |
| 38 | + cozeloop_api_key: str, |
| 39 | +) -> None: |
| 40 | + """Upload dataset items to CozeLoop evaluation set.""" |
| 41 | + |
| 42 | + if not cozeloop_workspace_id: |
| 43 | + cozeloop_workspace_id = getenv( |
| 44 | + "OBSERVABILITY_OPENTELEMETRY_COZELOOP_SERVICE_NAME" |
| 45 | + ) |
| 46 | + if not cozeloop_evalset_id: |
| 47 | + cozeloop_evalset_id = getenv("OBSERVABILITY_OPENTELEMETRY_COZELOOP_EVALSET_ID") |
| 48 | + if not cozeloop_api_key: |
| 49 | + cozeloop_api_key = getenv("OBSERVABILITY_OPENTELEMETRY_COZELOOP_API_KEY") |
| 50 | + |
| 51 | + # Read JSON file |
| 52 | + file_path = Path(file) |
| 53 | + if not file_path.exists(): |
| 54 | + logger.error(f"File not found: {file}") |
| 55 | + return |
| 56 | + |
| 57 | + logger.info(f"Reading dataset from {file}") |
| 58 | + with open(file_path, "r", encoding="utf-8") as f: |
| 59 | + data = json.load(f) |
| 60 | + |
| 61 | + # Prepare items |
| 62 | + items = [] |
| 63 | + for case in data.get("eval_cases", []): |
| 64 | + conversation = case.get("conversation", []) |
| 65 | + for turn in conversation: |
| 66 | + user_text = ( |
| 67 | + turn.get("user_content", {}).get("parts", [{}])[0].get("text", "") |
| 68 | + ) |
| 69 | + output_text = ( |
| 70 | + turn.get("final_response", {}).get("parts", [{}])[0].get("text", "") |
| 71 | + ) |
| 72 | + |
| 73 | + items.append( |
| 74 | + { |
| 75 | + "turns": [ |
| 76 | + { |
| 77 | + "field_datas": [ |
| 78 | + { |
| 79 | + "name": "input", |
| 80 | + "content": { |
| 81 | + "content_type": "Text", |
| 82 | + "text": user_text, |
| 83 | + }, |
| 84 | + }, |
| 85 | + { |
| 86 | + "name": "output", |
| 87 | + "content": { |
| 88 | + "content_type": "Text", |
| 89 | + "text": output_text, |
| 90 | + }, |
| 91 | + }, |
| 92 | + ] |
| 93 | + } |
| 94 | + ] |
| 95 | + } |
| 96 | + ) |
| 97 | + |
| 98 | + # Upload to CozeLoop |
| 99 | + url = f"https://api.coze.cn/v1/loop/evaluation/evaluation_sets/{cozeloop_evalset_id}/items" |
| 100 | + logger.info( |
| 101 | + f"Uploading {len(items)} items to workspace_id={cozeloop_workspace_id} evalset_id={cozeloop_evalset_id}" |
| 102 | + ) |
| 103 | + |
| 104 | + response = requests.post( |
| 105 | + url=url, |
| 106 | + headers={ |
| 107 | + "Authorization": f"Bearer {cozeloop_api_key}", |
| 108 | + "Content-Type": "application/json", |
| 109 | + "X-TT-ENV": "ppe_eval_openapi", |
| 110 | + "x-use-ppe": "1", |
| 111 | + }, |
| 112 | + json={ |
| 113 | + "workspace_id": cozeloop_workspace_id, |
| 114 | + "is_allow_partial_add": True, |
| 115 | + "is_skip_invalid_items": True, |
| 116 | + "items": items, |
| 117 | + }, |
| 118 | + ) |
| 119 | + |
| 120 | + if response.status_code == 200: |
| 121 | + logger.info( |
| 122 | + f"Successfully uploaded dataset to CozeLoop evalset {cozeloop_evalset_id}" |
| 123 | + ) |
| 124 | + else: |
| 125 | + logger.error(f"Failed to upload dataset: {response.text}") |
0 commit comments