From 31e44eb287b2bcef67a322e81d21cf9984a06139 Mon Sep 17 00:00:00 2001 From: Arjun Suresh Date: Tue, 22 Apr 2025 05:08:22 +0530 Subject: [PATCH 1/2] 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/2] [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() -