-
Notifications
You must be signed in to change notification settings - Fork 67
Add openai_api model for explorer #523
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 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d9486d2
add openai_api model for explorer
hiyuchang 344540e
add openai_api for aux models and fix typo
hiyuchang 4f7f536
fix typo
hiyuchang 04ac3f6
add unittest
hiyuchang be1aff5
fix config
hiyuchang cd6277f
fix comment
hiyuchang 0027d1c
rename yaml
hiyuchang 7f556e8
fix yaml and test
hiyuchang d28b805
add chat completion
hiyuchang b362116
fix comments
hiyuchang 1237f6f
remove patch
hiyuchang ee5bcc4
update test
hiyuchang c2c7571
fix tests
hiyuchang 670f627
fix comments
hiyuchang 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| mode: bench | ||
| project: Trinity-RFT | ||
| name: external-model-qwen3-max | ||
| checkpoint_root_dir: ${oc.env:TRINITY_CHECKPOINT_ROOT_DIR,./checkpoints} | ||
| model: | ||
| model_path: qwen3-max | ||
| max_model_len: 4096 | ||
| max_prompt_tokens: 2048 | ||
| max_response_tokens: 1024 | ||
| external_model: | ||
| enable: true | ||
| model_name: qwen3-max | ||
| base_url_env: API_BASE_URL | ||
| api_key_env: OPENAI_API_KEY | ||
| max_concurrent_requests: 16 | ||
| buffer: | ||
| batch_size: 16 | ||
| total_epochs: 1 | ||
| explorer_input: | ||
| eval_tasksets: | ||
| - name: gsm8k_eval | ||
| storage_type: file | ||
| path: ${oc.env:TRINITY_TASKSET_PATH,openai/gsm8k} | ||
| subset_name: main | ||
| split: test | ||
| repeat_times: 16 | ||
| format: | ||
| prompt_key: question | ||
| response_key: answer | ||
| rollout_args: | ||
| temperature: 1.0 | ||
| logprobs: 0 | ||
| default_eval_workflow_type: math_workflow | ||
| explorer: | ||
| auxiliary_models: [] | ||
| monitor: | ||
| monitor_type: tensorboard | ||
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 |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import asyncio | ||
| import gc | ||
| import os | ||
| import unittest | ||
|
|
||
| import ray | ||
|
|
||
| from tests.tools import get_model_path, get_template_config | ||
| from trinity.common.config import ExternalModelConfig, InferenceModelConfig | ||
| from trinity.common.models import create_explorer_models | ||
| from trinity.common.models.model import ModelWrapper | ||
| from trinity.common.models.external_model import ExternalModel | ||
|
|
||
|
|
||
| async def prepare_engines(engines, auxiliary_engines): | ||
| prepare_refs = [] | ||
| for engine in engines: | ||
| prepare_refs.append(engine.prepare.remote()) | ||
| for models in auxiliary_engines: | ||
| for engine in models: | ||
| prepare_refs.append(engine.prepare.remote()) | ||
| await asyncio.gather(*prepare_refs) | ||
|
|
||
|
|
||
| class TestExternalModel(unittest.IsolatedAsyncioTestCase): | ||
| @classmethod | ||
| def setUpClass(cls): | ||
| ray.init(ignore_reinit_error=True, namespace="trinity_unittest") | ||
| gc.collect() | ||
|
|
||
| @classmethod | ||
| def tearDownClass(cls): | ||
| ray.shutdown(_exiting_interpreter=True) | ||
|
|
||
| async def asyncSetUp(self): | ||
| model_path = get_model_path() | ||
| # Part 1: bootstrap a local OpenAI-compatible endpoint via vLLM. | ||
| config = get_template_config() | ||
| config.mode = "explore" | ||
| config.model.model_path = model_path | ||
| config.explorer.rollout_model.engine_type = "vllm" | ||
| config.explorer.rollout_model.engine_num = 1 | ||
| config.explorer.rollout_model.tensor_parallel_size = 1 | ||
| config.explorer.rollout_model.enable_openai_api = True | ||
| config.check_and_update() | ||
|
|
||
| self.engines, self.auxiliary_engines = create_explorer_models(config) | ||
| self.vllm_wrapper = ModelWrapper(self.engines[0], enable_history=False) | ||
| await prepare_engines(self.engines, self.auxiliary_engines) | ||
| await self.vllm_wrapper.prepare() | ||
|
|
||
| openai_client = self.vllm_wrapper.get_openai_client() | ||
| self.model_name = openai_client.models.list().data[0].id | ||
|
|
||
| self.base_url_env = "TRINITY_OPENAI_BASE_URL_TEST" | ||
| self.api_key_env = "TRINITY_OPENAI_API_KEY_TEST" | ||
| os.environ[self.base_url_env] = f"{self.vllm_wrapper.api_address}/v1" | ||
| os.environ[self.api_key_env] = "EMPTY" | ||
| self.model_path = model_path | ||
| print( | ||
| f"Model is prepared at {self.vllm_wrapper.api_address}/v1, model_name: {self.model_name}" | ||
| ) | ||
|
|
||
| async def test_external_model(self): | ||
| # Part 2: verify ExternalModel can call the endpoint correctly. | ||
| model = ExternalModel( | ||
| InferenceModelConfig( | ||
| model_path=self.model_path, | ||
| external_model_config=ExternalModelConfig( | ||
| base_url_env=self.base_url_env, | ||
| api_key_env=self.api_key_env, | ||
| model_name=self.model_name, | ||
| ), | ||
| ) | ||
| ) | ||
|
|
||
| generate_exps = await model.generate("Say hello in one sentence.", n=1, max_tokens=16) | ||
| self.assertEqual(len(generate_exps), 1) | ||
| self.assertTrue(len(generate_exps[0].response_text) > 0) | ||
| self.assertEqual(generate_exps[0].reward, 0.0) | ||
| self.assertIn("usage/prompt_tokens", generate_exps[0].metrics) | ||
| self.assertIn("usage/completion_tokens", generate_exps[0].metrics) | ||
| self.assertIn("usage/total_tokens", generate_exps[0].metrics) | ||
| self.assertGreater(generate_exps[0].metrics["usage/total_tokens"], 0.0) | ||
|
|
||
| messages = [ | ||
| {"role": "system", "content": "You are an assistant. Answer the question briefly."}, | ||
| {"role": "user", "content": [{"type": "text", "text": "What is 1+1?"}]}, | ||
| ] | ||
| chat_exps = await model.chat(messages, n=4, max_tokens=32) | ||
| self.assertEqual(len(chat_exps), 4) | ||
| self.assertTrue(len(chat_exps[0].response_text) > 0) | ||
| self.assertEqual(chat_exps[0].reward, 0.0) | ||
| self.assertIn("usage/prompt_tokens", chat_exps[0].metrics) | ||
| self.assertIn("usage/completion_tokens", chat_exps[0].metrics) | ||
| self.assertIn("usage/total_tokens", chat_exps[0].metrics) | ||
| self.assertGreater(chat_exps[0].metrics["usage/total_tokens"], 0.0) |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.