-
Notifications
You must be signed in to change notification settings - Fork 755
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
feat: Implement self-instruct and evolve-instruct synthetic data generation pipeline #720
Open
andrei3131
wants to merge
34
commits into
master
Choose a base branch
from
synth_data_self_instruct
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
24cff2c
add nemotron model
Wendong-Fan d5b307e
fix
Wendong-Fan 737da4b
error handing
Wendong-Fan c3e1b9c
update pytest yml
Wendong-Fan e29d473
Implement self-instruct synthetic data generation pipeline
andrei3131 cd2c68d
adding evolve
Hither1 f2702de
debug evolve
Hither1 25edfc6
seed instruction files
Hither1 30169ca
add ipy notebook
Hither1 ea984a1
Enable configuring spec with list of seed instructions instead of pat…
andrei3131 7d5c58c
local
c99f061
local
6a62295
start jupyter notebook for self-instruct generation
andrei3131 7089b81
nemotron critic
e45b970
local
245377f
Merge branch 'synth_data_self_instruct' of https://github.com/camel-a…
4f3c7bb
Merge branch 'master' into role_play_nemotron_critic
andrei3131 137c45f
Merge branch 'role_play_nemotron_critic' into synth_data_self_instruct
andrei3131 5b0306d
fixed some issues with evolve instruct
a304d39
fixed some issues with evolve instruct
6794527
fixed some issues with evolve instruct
ecd4feb
fixed some issues with evolve instruct
78741e0
start to imple multi-agent system
5fa3358
Add Nvidia Nemtron for synthetic data evaluation
andrei3131 28f9750
Merge branch 'synth_data_self_instruct' of https://github.com/camel-a…
andrei3131 7fd3a5c
clean up
andrei3131 da057e2
deepseek math and coding
96f4cfa
move to subpackage of camel
8240a19
math and coder
9ea9ea3
math and coder
a36da80
add base instruct spec
734534e
fix coder
d98edff
fix coder
fe55e1a
fix according to reviews
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 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 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 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 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,125 @@ | ||
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. =========== | ||
# Licensed under the Apache License, Version 2.0 (the “License”); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an “AS IS” BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. =========== | ||
import os | ||
from typing import Any, Dict, List, Optional, Union | ||
|
||
from openai import OpenAI, Stream | ||
|
||
from camel.configs import OPENAI_API_PARAMS | ||
from camel.messages import OpenAIMessage | ||
from camel.models import BaseModelBackend | ||
from camel.types import ChatCompletion, ChatCompletionChunk, ModelType | ||
from camel.utils import ( | ||
BaseTokenCounter, | ||
OpenAITokenCounter, | ||
model_api_key_required, | ||
) | ||
|
||
|
||
class NvidiaModel(BaseModelBackend): | ||
r"""Nvidia API in a unified BaseModelBackend interface.""" | ||
|
||
# NOTE: Nemotron model doesn't support additional model config like OpenAI. | ||
|
||
def __init__( | ||
self, | ||
model_type: ModelType, | ||
model_config_dict: Dict[str, Any], | ||
api_key: Optional[str] = None, | ||
) -> None: | ||
r"""Constructor for Nvidia backend. | ||
|
||
Args: | ||
model_type (ModelType): Model for which a backend is created. | ||
model_config_dict (Dict[str, Any]): A dictionary that will | ||
be fed into openai.ChatCompletion.create(). | ||
api_key (Optional[str]): The API key for authenticating with the | ||
Nvidia service. (default: :obj:`None`) | ||
""" | ||
super().__init__(model_type, model_config_dict) | ||
url = os.environ.get('NVIDIA_API_BASE_URL', None) | ||
self._api_key = api_key or os.environ.get("NVIDIA_API_KEY") | ||
if not url or not self._api_key: | ||
raise ValueError("The NVIDIA API base url and key should be set.") | ||
self._client = OpenAI( | ||
timeout=60, max_retries=3, base_url=url, api_key=self._api_key | ||
) | ||
self._token_counter: Optional[BaseTokenCounter] = None | ||
|
||
@property | ||
def token_counter(self) -> BaseTokenCounter: | ||
r"""Initialize the token counter for the model backend. | ||
|
||
Returns: | ||
BaseTokenCounter: The token counter following the model's | ||
tokenization style. | ||
""" | ||
if not self._token_counter: | ||
# NOTE: It's a temporary setting for token counter. | ||
self._token_counter = OpenAITokenCounter(ModelType.GPT_3_5_TURBO) | ||
return self._token_counter | ||
|
||
@model_api_key_required | ||
def run( | ||
self, | ||
messages: List[OpenAIMessage], | ||
) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]: | ||
r"""Runs inference of OpenAI chat completion. | ||
|
||
Args: | ||
messages (List[OpenAIMessage]): Message list with the chat history | ||
in OpenAI API format. | ||
|
||
Returns: | ||
Union[ChatCompletion, Stream[ChatCompletionChunk]]: | ||
`ChatCompletion` in the non-stream mode. | ||
""" | ||
print(messages) | ||
# Nemotron model only accept 'user' or 'assistant' as role. | ||
for message in messages: | ||
if message['role'] not in ['user', 'assistant']: | ||
message['role'] = 'assistant' # type: ignore[arg-type] | ||
# user/assistant messages should alternate starting with `user`. | ||
messages[0], messages[1] = messages[1], messages[0] | ||
|
||
response = self._client.chat.completions.create( | ||
messages=messages, | ||
model=self.model_type.value, | ||
) | ||
return response | ||
|
||
def check_model_config(self): | ||
r"""Check whether the model configuration contains any | ||
unexpected arguments to OpenAI API. | ||
|
||
Raises: | ||
ValueError: If the model configuration dictionary contains any | ||
unexpected arguments to OpenAI API. | ||
""" | ||
for param in self.model_config_dict: | ||
if param not in OPENAI_API_PARAMS: | ||
raise ValueError( | ||
f"Unexpected argument `{param}` is " | ||
"input into OpenAI model backend." | ||
) | ||
|
||
@property | ||
def stream(self) -> bool: | ||
r"""Returns whether the model is in stream mode, which sends partial | ||
results each time. | ||
|
||
Returns: | ||
bool: Whether the model is in stream mode. | ||
""" | ||
return self.model_config_dict.get('stream', False) |
This file contains 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,87 @@ | ||
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. =========== | ||
# Licensed under the Apache License, Version 2.0 (the “License”); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an “AS IS” BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. =========== | ||
import os | ||
from typing import List, Optional | ||
|
||
from openai import OpenAI | ||
|
||
from camel.messages import OpenAIMessage | ||
from camel.types import ChatCompletion, ModelType | ||
from camel.utils import ( | ||
BaseTokenCounter, | ||
OpenAITokenCounter, | ||
model_api_key_required, | ||
) | ||
|
||
|
||
class NvidiaModelV2: | ||
r"""Nvidia API in a unified BaseModelBackend interface.""" | ||
|
||
# NOTE: Nemotron model doesn't support additional model config like OpenAI. | ||
|
||
def __init__( | ||
self, | ||
model_type: ModelType, | ||
api_key: Optional[str] = None, | ||
) -> None: | ||
r"""Constructor for Nvidia backend. | ||
|
||
Args: | ||
model_type (ModelType): Model for which a backend is created. | ||
api_key (Optional[str]): The API key for authenticating with the | ||
Nvidia service. (default: :obj:`None`) | ||
""" | ||
self.model_type = model_type | ||
url = os.environ.get('NVIDIA_API_BASE_URL', None) | ||
self._api_key = api_key or os.environ.get("NVIDIA_API_KEY") | ||
if not url or not self._api_key: | ||
raise ValueError( | ||
"NVIDIA_API_BASE_URL and NVIDIA_API_KEY should be set." | ||
) | ||
self._client = OpenAI( | ||
timeout=60, max_retries=3, base_url=url, api_key=self._api_key | ||
) | ||
self._token_counter: Optional[BaseTokenCounter] = None | ||
|
||
@property | ||
def token_counter(self) -> BaseTokenCounter: | ||
r"""Initialize the token counter for the model backend. | ||
|
||
Returns: | ||
BaseTokenCounter: The token counter following the model's | ||
tokenization style. | ||
""" | ||
if not self._token_counter: | ||
# NOTE: It's a temporary setting for token counter. | ||
self._token_counter = OpenAITokenCounter(ModelType.GPT_3_5_TURBO) | ||
return self._token_counter | ||
|
||
@model_api_key_required | ||
def run( | ||
self, | ||
messages: List[OpenAIMessage], | ||
) -> ChatCompletion: | ||
r"""Runs inference of OpenAI chat completion. | ||
|
||
Args: | ||
messages (List[OpenAIMessage]): Message list. | ||
|
||
Returns: | ||
ChatCompletion. | ||
""" | ||
response = self._client.chat.completions.create( | ||
messages=messages, | ||
model=self.model_type.value, | ||
) | ||
return response |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, you can use logging info to instead print func