Spaces:
Sleeping
Sleeping
| #!/usr/bin/python3 | |
| # -*- coding: utf-8 -*- | |
| """ | |
| https://docs.byteplus.com/en/docs/ModelArk/1099455 | |
| model list | |
| https://docs.byteplus.com/en/docs/ModelArk/1330310 | |
| https://docs.byteplus.com/en/docs/ModelArk/Chat | |
| """ | |
| import argparse | |
| from datetime import datetime | |
| import json | |
| import os | |
| from pathlib import Path | |
| import re | |
| import sys | |
| import time | |
| from zoneinfo import ZoneInfo # Python 3.9+ 自带,无需安装 | |
| pwd = os.path.abspath(os.path.dirname(__file__)) | |
| sys.path.append(os.path.join(pwd, "../")) | |
| from openai import OpenAI | |
| from project_settings import environment, project_path | |
| def get_args(): | |
| """ | |
| model list: | |
| https://docs.byteplus.com/en/docs/ModelArk/1330310 | |
| bytedance-seed-1.6 | |
| seed-1-6-250615 | |
| bytedance-seed-1.6-flash | |
| seed-1-6-flash-250615 | |
| deepseek-v3 | |
| deepseek-v3-250324 | |
| """ | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument( | |
| "--model_name", | |
| # default="seed-1-6-250615", | |
| # default="seed-1-6-flash-250615", | |
| # default="deepseek-v3-250324", | |
| default="skylark-pro-250415", | |
| # default="skylark-lite-250215", | |
| type=str | |
| ) | |
| parser.add_argument( | |
| "--eval_dataset_name", | |
| # default="agent-lingoace-zh-80-chat.jsonl", | |
| # default="agent-bingoplus-ph-200-chat.jsonl", | |
| default="agent-cod-zh-70-chat.jsonl", | |
| type=str | |
| ) | |
| parser.add_argument( | |
| "--eval_dataset_dir", | |
| default=(project_path / "data/dataset").as_posix(), | |
| type=str | |
| ) | |
| parser.add_argument( | |
| "--eval_data_dir", | |
| default=(project_path / "data/eval_data").as_posix(), | |
| type=str | |
| ) | |
| parser.add_argument( | |
| "--client", | |
| default="shenzhen_sase", | |
| type=str | |
| ) | |
| parser.add_argument( | |
| "--service", | |
| default="byteplus_api_key", | |
| type=str | |
| ) | |
| parser.add_argument( | |
| "--create_time_str", | |
| # default="null", | |
| default="20250728_113641", | |
| type=str | |
| ) | |
| parser.add_argument( | |
| "--interval", | |
| default=1, | |
| type=int | |
| ) | |
| args = parser.parse_args() | |
| return args | |
| def main(): | |
| args = get_args() | |
| eval_dataset_dir = Path(args.eval_dataset_dir) | |
| eval_dataset_dir.mkdir(parents=True, exist_ok=True) | |
| eval_data_dir = Path(args.eval_data_dir) | |
| eval_data_dir.mkdir(parents=True, exist_ok=True) | |
| if args.create_time_str == "null": | |
| tz = ZoneInfo("Asia/Shanghai") | |
| now = datetime.now(tz) | |
| create_time_str = now.strftime("%Y%m%d_%H%M%S") | |
| # create_time_str = "20250724_090615" | |
| else: | |
| create_time_str = args.create_time_str | |
| eval_dataset = eval_dataset_dir / args.eval_dataset_name | |
| output_file = eval_data_dir / f"byteplus/byteplus/{args.model_name}/{args.client}/{args.service}/{create_time_str}/{args.eval_dataset_name}.raw" | |
| output_file.parent.mkdir(parents=True, exist_ok=True) | |
| api_key = environment.get(args.service, dtype=str) | |
| client = OpenAI( | |
| base_url="https://ark.ap-southeast.bytepluses.com/api/v3/", | |
| # Read your Ark API Key from the environment variable. | |
| api_key=api_key | |
| ) | |
| total = 0 | |
| # finished | |
| finished_idx_set = set() | |
| if os.path.exists(output_file.as_posix()): | |
| with open(output_file.as_posix(), "r", encoding="utf-8") as f: | |
| for row in f: | |
| row = json.loads(row) | |
| idx = row["idx"] | |
| total = row["total"] | |
| finished_idx_set.add(idx) | |
| print(f"finished count: {len(finished_idx_set)}") | |
| with open(eval_dataset.as_posix(), "r", encoding="utf-8") as fin, open(output_file.as_posix(), "a+", encoding="utf-8") as fout: | |
| for row in fin: | |
| row = json.loads(row) | |
| idx = row["idx"] | |
| prompt = row["prompt"] | |
| response = row["response"] | |
| if idx in finished_idx_set: | |
| continue | |
| finished_idx_set.add(idx) | |
| # prompt | |
| splits = prompt[::-1].split("\n\n", maxsplit=1) | |
| conversation = splits[0] | |
| system_prompt = splits[1] | |
| conversation = conversation[::-1].strip() | |
| system_prompt = system_prompt[::-1].strip() | |
| pattern = "^(Client|Assistant): (.*?)(?=\n(?:Client|Assistant):)" | |
| match = re.findall(pattern=pattern, string=conversation, flags=re.I|re.DOTALL|re.MULTILINE) | |
| messages_ = list() | |
| for m in match: | |
| role = m[0].lower() | |
| content = m[1] | |
| if role in ("client", "Client"): | |
| role = "user" | |
| elif role in ("assistant", "Assistant"): | |
| role = "assistant" | |
| else: | |
| raise AssertionError | |
| messages_.append({ | |
| "role": role, | |
| "content": content | |
| }) | |
| messages = [ | |
| {"role": "system", "content": system_prompt}, | |
| *messages_ | |
| ] | |
| # print(json.dumps(messages, ensure_ascii=False, indent=4)) | |
| # exit(0) | |
| try: | |
| time.sleep(args.interval) | |
| print(f"sleep: {args.interval}") | |
| time_begin = time.time() | |
| # https://docs.byteplus.com/en/docs/ModelArk/1449737 | |
| llm_response = client.chat.completions.create( | |
| model=args.model_name, | |
| messages=messages, | |
| stream=False, | |
| max_tokens=4096, | |
| extra_body={ | |
| "thinking": { | |
| "type": "disabled", | |
| # "type": "enabled", | |
| } | |
| } | |
| ) | |
| time_cost = time.time() - time_begin | |
| print(f"time_cost: {time_cost}") | |
| except Exception as e: | |
| print(f"request failed, error type: {type(e)}, error text: {str(e)}") | |
| continue | |
| prediction = llm_response.choices[0].message.content | |
| total += 1 | |
| row_ = { | |
| "idx": idx, | |
| "prompt": prompt, | |
| "response": response, | |
| "prediction": prediction, | |
| "total": total, | |
| "time_cost": time_cost, | |
| } | |
| row_ = json.dumps(row_, ensure_ascii=False) | |
| fout.write(f"{row_}\n") | |
| fout.flush() | |
| return | |
| if __name__ == "__main__": | |
| main() | |