From 31e44eb287b2bcef67a322e81d21cf9984a06139 Mon Sep 17 00:00:00 2001 From: Arjun Suresh Date: Tue, 22 Apr 2025 05:08:22 +0530 Subject: [PATCH 1/7] Cleanups for openai-call --- script/openai-call/customize.py | 65 +++++++++++++++++++++++++++++ script/openai-call/meta.yaml | 35 ++++++++++++++++ script/openai-call/openai_call.py | 69 +++++++++++++++++++++++++++++++ 3 files changed, 169 insertions(+) create mode 100644 script/openai-call/customize.py create mode 100644 script/openai-call/meta.yaml create mode 100644 script/openai-call/openai_call.py diff --git a/script/openai-call/customize.py b/script/openai-call/customize.py new file mode 100644 index 000000000..9bab431fb --- /dev/null +++ b/script/openai-call/customize.py @@ -0,0 +1,65 @@ +from mlc import utils +import os +import subprocess +import json + + +import yaml + +def write_openai_yaml(model, system_prompt, user_prompt, filename='openai-prompt.yaml'): + data = { + 'model': model, + 'messages': [ + { + 'role': 'system', + 'content': system_prompt + }, + { + 'role': 'user', + 'content': user_prompt + } + ], + 'max_tokens': 200, + 'temperature': 0.7 + } + + with open(filename, 'w', encoding='utf-8') as f: + yaml.dump(data, f, sort_keys=False, allow_unicode=True) + + + + +def preprocess(i): + + env = i['env'] + state = i['state'] + + if 'MLC_OPENAI_CONFIG_PATH' not in env or not os.path.exists(env['MLC_OPENAI_CONFIG_PATH']): + if 'user_prompt' in state: + model = env.get('MLC_OPENAI_MODEL', 'gpt-4o') + user_prompt = state['user_prompt'] + system_prompt = state.get('system_prompt', 'You are an AI agent expected to answer questions correctly') + write_openai_yaml(model, system_prompt, user_prompt, 'tmp-openai-prompt.yaml') + env['MLC_OPENAI_CONFIG_PATH'] = 'tmp-openai-prompt.yaml' + + os_info = i['os_info'] + + env['MLC_RUN_CMD'] = f"""{env['MLC_PYTHON_BIN_WITH_PATH']} {os.path.join(env['MLC_TMP_CURRENT_SCRIPT_PATH'], 'openai_call.py')} """ + + return {'return': 0} + + +def postprocess(i): + + env = i['env'] + state = i['state'] + + filename='tmp-openai-results.json' + with open(filename, 'r', encoding='utf-8') as f: + data = json.load(f) + + state['MLC_OPENAI_RESPONSE'] = data['content'] + + os_info = i['os_info'] + + return {'return': 0} diff --git a/script/openai-call/meta.yaml b/script/openai-call/meta.yaml new file mode 100644 index 000000000..580162a35 --- /dev/null +++ b/script/openai-call/meta.yaml @@ -0,0 +1,35 @@ +alias: openai-call +automation_alias: script +automation_uid: 5b4e0237da074764 +category: MLC Script Template +deps: + - tags: get,python3 + names: + - python + - python3 +new_env_keys: [] +new_state_keys: +- MLC_OPENAI_RESPONSE +post_deps: [] +posthook_deps: [] +prehook_deps: [] +tags: +- query +- openai +- openai-call +- call +input_mapping: + api_key: MLC_OPENAI_API_KEY + config_path: MLC_OPENAI_CONFIG_PATH + system_prompt: MLC_OPENAI_SYSTEM_PROMPT + user_prompt: MLC_OPENAI_USER_PROMPT + model: MLC_OPENAI_MODEL +tests: + run_inputs: [] +uid: 8d341b0a14a64d94 +variations: + openai: + group: api_provider + default: true + env: + MLC_OPENAI_API_URL: 'https://api.openai.com/v1/chat/completions' diff --git a/script/openai-call/openai_call.py b/script/openai-call/openai_call.py new file mode 100644 index 000000000..fa8321ef4 --- /dev/null +++ b/script/openai-call/openai_call.py @@ -0,0 +1,69 @@ +import requests +import yaml +import os +import json + +def openai_call(message=None): + try: + api_key = os.environ['MLC_OPENAI_API_KEY'] + url = os.environ['MLC_OPENAI_API_URL'] + config_path = os.environ.get('MLC_OPENAI_CONFIG_PATH') + + # Load config if it exists + if config_path and os.path.exists(config_path): + try: + with open(config_path, 'r') as file: + data = yaml.safe_load(file) + except Exception as e: + return {"error": f"Error reading config file: {str(e)}"} + + if os.environ.get('MLC_OPENAI_CONFIG_MODIFY', '') == 'yes': + try: + data['messages'][1]['content'] = data['messages'][1]['content'].replace("{{ MESSAGE }}", message or "") + except Exception as e: + return {"error": f"Config format issue: {str(e)}"} + else: + system_prompt = os.environ.get('MLC_OPENAI_SYSTEM_PROMPT', 'You are an AI agent expected to correctly answer the asked question') + user_prompt = message or os.environ.get('MLC_OPENAI_USER_PROMPT', '') + data = { + "model": os.environ.get('MLC_OPENAI_MODEL', 'gpt-4.1'), + "messages": [ + {"role": "system", "content": system_prompt}, + {"role": "user", "content": user_prompt} + ] + } + + headers = { + 'Content-Type': 'application/json', + 'Authorization': f'Bearer {api_key}' + } + + + response = requests.post(url, json=data, headers=headers) + response.raise_for_status() + result = response.json() + content = result['choices'][0]['message']['content'] + + with open('tmp-openai-results.json', 'w', encoding='utf-8') as f: + json.dump({'content': content}, f, ensure_ascii=False, indent=2) + + return {"content": content} + + except requests.exceptions.RequestException as e: + return {"error": f"Request error: {str(e)}"} + + except KeyError as e: + return {"error": f"Missing key in response: {str(e)}"} + + except Exception as e: + return {"error": f"Unexpected error: {str(e)}"} + + +def main(): + result = openai_call() + if 'error' in result: + raise Exception(result['error']) + +if __name__ == '__main__': + main() + From a6c5374495602776cf493033556931e3d558b474 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Mon, 21 Apr 2025 23:40:04 +0000 Subject: [PATCH 2/7] [Automated Commit] Format Codebase [skip ci] --- script/openai-call/customize.py | 23 +++++++++++++++-------- script/openai-call/openai_call.py | 26 +++++++++++++++----------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/script/openai-call/customize.py b/script/openai-call/customize.py index 9bab431fb..5963e8ebb 100644 --- a/script/openai-call/customize.py +++ b/script/openai-call/customize.py @@ -6,7 +6,9 @@ import yaml -def write_openai_yaml(model, system_prompt, user_prompt, filename='openai-prompt.yaml'): + +def write_openai_yaml(model, system_prompt, user_prompt, + filename='openai-prompt.yaml'): data = { 'model': model, 'messages': [ @@ -27,19 +29,24 @@ def write_openai_yaml(model, system_prompt, user_prompt, filename='openai-prompt yaml.dump(data, f, sort_keys=False, allow_unicode=True) - - def preprocess(i): env = i['env'] state = i['state'] - if 'MLC_OPENAI_CONFIG_PATH' not in env or not os.path.exists(env['MLC_OPENAI_CONFIG_PATH']): + if 'MLC_OPENAI_CONFIG_PATH' not in env or not os.path.exists( + env['MLC_OPENAI_CONFIG_PATH']): if 'user_prompt' in state: model = env.get('MLC_OPENAI_MODEL', 'gpt-4o') user_prompt = state['user_prompt'] - system_prompt = state.get('system_prompt', 'You are an AI agent expected to answer questions correctly') - write_openai_yaml(model, system_prompt, user_prompt, 'tmp-openai-prompt.yaml') + system_prompt = state.get( + 'system_prompt', + 'You are an AI agent expected to answer questions correctly') + write_openai_yaml( + model, + system_prompt, + user_prompt, + 'tmp-openai-prompt.yaml') env['MLC_OPENAI_CONFIG_PATH'] = 'tmp-openai-prompt.yaml' os_info = i['os_info'] @@ -54,10 +61,10 @@ def postprocess(i): env = i['env'] state = i['state'] - filename='tmp-openai-results.json' + filename = 'tmp-openai-results.json' with open(filename, 'r', encoding='utf-8') as f: data = json.load(f) - + state['MLC_OPENAI_RESPONSE'] = data['content'] os_info = i['os_info'] diff --git a/script/openai-call/openai_call.py b/script/openai-call/openai_call.py index fa8321ef4..13a3ce710 100644 --- a/script/openai-call/openai_call.py +++ b/script/openai-call/openai_call.py @@ -3,6 +3,7 @@ import os import json + def openai_call(message=None): try: api_key = os.environ['MLC_OPENAI_API_KEY'] @@ -19,26 +20,29 @@ def openai_call(message=None): if os.environ.get('MLC_OPENAI_CONFIG_MODIFY', '') == 'yes': try: - data['messages'][1]['content'] = data['messages'][1]['content'].replace("{{ MESSAGE }}", message or "") + data['messages'][1]['content'] = data['messages'][1]['content'].replace( + "{{ MESSAGE }}", message or "") except Exception as e: return {"error": f"Config format issue: {str(e)}"} else: - system_prompt = os.environ.get('MLC_OPENAI_SYSTEM_PROMPT', 'You are an AI agent expected to correctly answer the asked question') - user_prompt = message or os.environ.get('MLC_OPENAI_USER_PROMPT', '') + system_prompt = os.environ.get( + 'MLC_OPENAI_SYSTEM_PROMPT', + 'You are an AI agent expected to correctly answer the asked question') + user_prompt = message or os.environ.get( + 'MLC_OPENAI_USER_PROMPT', '') data = { - "model": os.environ.get('MLC_OPENAI_MODEL', 'gpt-4.1'), - "messages": [ - {"role": "system", "content": system_prompt}, - {"role": "user", "content": user_prompt} - ] - } + "model": os.environ.get('MLC_OPENAI_MODEL', 'gpt-4.1'), + "messages": [ + {"role": "system", "content": system_prompt}, + {"role": "user", "content": user_prompt} + ] + } headers = { 'Content-Type': 'application/json', 'Authorization': f'Bearer {api_key}' } - response = requests.post(url, json=data, headers=headers) response.raise_for_status() result = response.json() @@ -64,6 +68,6 @@ def main(): if 'error' in result: raise Exception(result['error']) + if __name__ == '__main__': main() - From f92df25a2bfed91ffb7d59830e7ad8104d9ece10 Mon Sep 17 00:00:00 2001 From: Arjun Suresh Date: Tue, 22 Apr 2025 05:16:59 +0530 Subject: [PATCH 3/7] Added run files for openai call --- script/openai-call/run.bat | 23 +++++++++++++++++++++++ script/openai-call/run.sh | 17 +++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 script/openai-call/run.bat create mode 100644 script/openai-call/run.sh diff --git a/script/openai-call/run.bat b/script/openai-call/run.bat new file mode 100644 index 000000000..12c8a6224 --- /dev/null +++ b/script/openai-call/run.bat @@ -0,0 +1,23 @@ +@echo off +setlocal enabledelayedexpansion + +:: Function to exit if the last command failed +:exit_if_error +if %ERRORLEVEL% NEQ 0 exit /b %ERRORLEVEL% +exit /b 0 + +:: Function to run a command +:run +echo Running: +echo %1 +echo. + +if /I "%MLC_FAKE_RUN%" NEQ "yes" ( + call %1 + call :exit_if_error +) +exit /b 0 + +:: Add your run commands here... +call :run "%MLC_RUN_CMD%" + diff --git a/script/openai-call/run.sh b/script/openai-call/run.sh new file mode 100644 index 000000000..c4542b8c2 --- /dev/null +++ b/script/openai-call/run.sh @@ -0,0 +1,17 @@ +#!/bin/bash +function exit_if_error() { + test $? -eq 0 || exit $? +} + +function run() { + echo "Running: " + echo "$1" + echo "" + if [[ ${MLC_FAKE_RUN} != 'yes' ]]; then + eval "$1" + exit_if_error + fi +} + +#Add your run commands here... +run "$MLC_RUN_CMD" From 55173e6851e850fc933ef5feb191ffddeae58c26 Mon Sep 17 00:00:00 2001 From: anandhu-eng Date: Tue, 22 Apr 2025 12:29:45 +0530 Subject: [PATCH 4/7] skip authentication when service account credentials are provided --- script/get-dataset-waymo-calibration/meta.yaml | 1 + script/get-rclone-config/customize.py | 2 +- script/get-rclone-config/meta.yaml | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/script/get-dataset-waymo-calibration/meta.yaml b/script/get-dataset-waymo-calibration/meta.yaml index cf9976fa7..c78178cf1 100644 --- a/script/get-dataset-waymo-calibration/meta.yaml +++ b/script/get-dataset-waymo-calibration/meta.yaml @@ -60,6 +60,7 @@ variations: group: run-mode env: MLC_DOWNLOAD_MODE: dry + MLC_BYPASS_RCLONE_AUTH: True dry-run,rclone: env: MLC_DOWNLOAD_EXTRA_OPTIONS: --dry-run \ No newline at end of file diff --git a/script/get-rclone-config/customize.py b/script/get-rclone-config/customize.py index 003d1bf8a..a3df9b1f1 100644 --- a/script/get-rclone-config/customize.py +++ b/script/get-rclone-config/customize.py @@ -19,7 +19,7 @@ def preprocess(i): if env.get('MLC_RCLONE_CONFIG_CMD', '') != '': run_cmds.append(env['MLC_RCLONE_CONFIG_CMD']) - if env.get('MLC_RCLONE_CONNECT_CMD', '') != '': + if env.get('MLC_RCLONE_CONNECT_CMD', '') != '' and not is_true(env.get('MLC_BYPASS_RCLONE_AUTH', '')): run_cmds.append(env['MLC_RCLONE_CONNECT_CMD']) env['MLC_RUN_CMD'] = ' && '.join(run_cmds) diff --git a/script/get-rclone-config/meta.yaml b/script/get-rclone-config/meta.yaml index a7bd8e5b3..8fb2058e2 100644 --- a/script/get-rclone-config/meta.yaml +++ b/script/get-rclone-config/meta.yaml @@ -30,8 +30,8 @@ variations: MLC_RCLONE_CONNECT_CMD: 'rclone config reconnect mlc-llama3-1:' waymo: env: - MLC_RCLONE_CONFIG_CMD: 'rclone config create mlc-waymo drive config_is_local=false scope=<<>> root_folder_id=1xbfnaUurFeXliFFl1i1gj48eRU2NDiH5' - MLC_RCLONE_CONNECT_CMD: 'rclone config reconnect mlc-waymo:' + MLC_RCLONE_CONFIG_CMD: 'rclone config create mlc_waymo drive config_is_local=false scope=<<>> root_folder_id=1xbfnaUurFeXliFFl1i1gj48eRU2NDiH5' + MLC_RCLONE_CONNECT_CMD: 'rclone config reconnect mlc_waymo:' config-name.#: env: MLC_RCLONE_CONFIG_CMD: 'rclone config create # drive config_is_local=false scope=<<>> root_folder_id=<<>>' From 7ba537be92c90265a2342adc1f133fc90efeb4cf Mon Sep 17 00:00:00 2001 From: anandhu-eng Date: Tue, 22 Apr 2025 15:33:09 +0530 Subject: [PATCH 5/7] change the remote name --- script/get-dataset-waymo-calibration/meta.yaml | 2 +- script/get-dataset-waymo/meta.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/script/get-dataset-waymo-calibration/meta.yaml b/script/get-dataset-waymo-calibration/meta.yaml index c78178cf1..96b916764 100644 --- a/script/get-dataset-waymo-calibration/meta.yaml +++ b/script/get-dataset-waymo-calibration/meta.yaml @@ -39,7 +39,7 @@ variations: env: MLC_DOWNLOAD_FINAL_ENV_NAME: MLC_DATASET_WAYMO_CALIBRATION_PATH MLC_EXTRACT_FINAL_ENV_NAME: MLC_DATASET_WAYMO_CALIBRATION_PATH - MLC_DOWNLOAD_URL: mlc-waymo:waymo_preprocessed_dataset/kitti_format/testing + MLC_DOWNLOAD_URL: mlc_waymo:waymo_preprocessed_dataset/kitti_format/testing extra_cache_tags: waymo,dataset force_cache: true names: diff --git a/script/get-dataset-waymo/meta.yaml b/script/get-dataset-waymo/meta.yaml index bfeb56a8c..ef56e4a2e 100644 --- a/script/get-dataset-waymo/meta.yaml +++ b/script/get-dataset-waymo/meta.yaml @@ -36,7 +36,7 @@ variations: env: MLC_DOWNLOAD_FINAL_ENV_NAME: MLC_DATASET_WAYMO_PATH MLC_EXTRACT_FINAL_ENV_NAME: MLC_DATASET_WAYMO_PATH - MLC_DOWNLOAD_URL: mlc-waymo:waymo_preprocessed_dataset/kitti_format + MLC_DOWNLOAD_URL: mlc_waymo:waymo_preprocessed_dataset/kitti_format extra_cache_tags: waymo,dataset force_cache: true names: From c82bca4145a486e2d4372a97df8ff92fc0046c83 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Tue, 22 Apr 2025 10:09:32 +0000 Subject: [PATCH 6/7] [Automated Commit] Format Codebase [skip ci] --- script/get-rclone-config/customize.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/script/get-rclone-config/customize.py b/script/get-rclone-config/customize.py index a3df9b1f1..92ed8f7d2 100644 --- a/script/get-rclone-config/customize.py +++ b/script/get-rclone-config/customize.py @@ -19,7 +19,8 @@ def preprocess(i): if env.get('MLC_RCLONE_CONFIG_CMD', '') != '': run_cmds.append(env['MLC_RCLONE_CONFIG_CMD']) - if env.get('MLC_RCLONE_CONNECT_CMD', '') != '' and not is_true(env.get('MLC_BYPASS_RCLONE_AUTH', '')): + if env.get('MLC_RCLONE_CONNECT_CMD', '') != '' and not is_true( + env.get('MLC_BYPASS_RCLONE_AUTH', '')): run_cmds.append(env['MLC_RCLONE_CONNECT_CMD']) env['MLC_RUN_CMD'] = ' && '.join(run_cmds) From 01d67d8e7e8998611634ce6e1a75a6207064cae5 Mon Sep 17 00:00:00 2001 From: anandhu-eng Date: Tue, 22 Apr 2025 16:00:28 +0530 Subject: [PATCH 7/7] fix import --- script/get-preprocessed-dataset-openorca/customize.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/get-preprocessed-dataset-openorca/customize.py b/script/get-preprocessed-dataset-openorca/customize.py index 18b990e09..daeef32f4 100644 --- a/script/get-preprocessed-dataset-openorca/customize.py +++ b/script/get-preprocessed-dataset-openorca/customize.py @@ -1,5 +1,5 @@ from mlc import utils -from mlc.utils import * +from utils import * import os import shutil