-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Fix resource leak, replace print with logger, fix O(n²) string concat #3803
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
Open
Copilot
wants to merge
2
commits into
master
Choose a base branch
from
copilot/create-issues-for-fixes
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.
+181
−28
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # ========= Copyright 2023-2026 @ 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-2026 @ CAMEL-AI.org. All Rights Reserved. ========= | ||
| import logging | ||
|
|
||
| from camel.messages.conversion.sharegpt.hermes.hermes_function_formatter import ( | ||
| HermesFunctionFormatter, | ||
| ) | ||
|
|
||
|
|
||
| def test_extract_tool_calls_invalid_json_logs_warning(caplog): | ||
| r"""Test that invalid tool call JSON triggers a warning log | ||
| instead of a print statement.""" | ||
| formatter = HermesFunctionFormatter() | ||
| message = "<tool_call>\n{invalid json}\n</tool_call>" | ||
|
|
||
| with caplog.at_level(logging.WARNING): | ||
| result = formatter.extract_tool_calls(message) | ||
|
|
||
| assert result == [] | ||
| assert "Failed to parse tool call" in caplog.text | ||
|
|
||
|
|
||
| def test_extract_tool_response_invalid_json_logs_warning(caplog): | ||
| r"""Test that invalid tool response JSON triggers a warning log | ||
| instead of a print statement.""" | ||
| formatter = HermesFunctionFormatter() | ||
| message = "<tool_response>\n{invalid json}\n</tool_response>" | ||
|
|
||
| with caplog.at_level(logging.WARNING): | ||
| result = formatter.extract_tool_response(message) | ||
|
|
||
| assert result is None | ||
| assert "Failed to parse tool response" in caplog.text | ||
|
|
||
|
|
||
| def test_extract_tool_calls_valid_json(): | ||
| r"""Test that valid tool calls are extracted correctly.""" | ||
| formatter = HermesFunctionFormatter() | ||
| message = ( | ||
| '<tool_call>\n{"name": "add", "arguments": {"a": 1, "b": 2}}' | ||
| "\n</tool_call>" | ||
| ) | ||
|
|
||
| result = formatter.extract_tool_calls(message) | ||
|
|
||
| assert len(result) == 1 | ||
| assert result[0].name == "add" | ||
| assert result[0].arguments == {"a": 1, "b": 2} |
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,78 @@ | ||
| # ========= Copyright 2023-2026 @ 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-2026 @ CAMEL-AI.org. All Rights Reserved. ========= | ||
| import os | ||
| import tempfile | ||
| from unittest.mock import MagicMock, Mock, patch | ||
|
|
||
| from camel.models import OpenAIAudioModels | ||
|
|
||
|
|
||
| @patch.dict(os.environ, {"OPENAI_API_KEY": "test-key"}) | ||
| @patch("camel.models.openai_audio_models.OpenAI") | ||
| def test_speech_to_text_closes_file_handle(mock_openai_cls): | ||
| r"""Test that speech_to_text properly closes file handles | ||
| using context managers.""" | ||
| mock_client = MagicMock() | ||
| mock_openai_cls.return_value = mock_client | ||
| mock_response = Mock() | ||
| mock_response.text = "transcribed text" | ||
| mock_client.audio.transcriptions.create.return_value = mock_response | ||
|
|
||
| openai_audio = OpenAIAudioModels() | ||
|
|
||
| with tempfile.NamedTemporaryFile( | ||
| suffix=".wav", delete=False | ||
| ) as temp_file: | ||
| temp_file.write(b"Test audio data") | ||
| temp_file_path = temp_file.name | ||
|
|
||
| try: | ||
| result = openai_audio.speech_to_text(temp_file_path) | ||
| assert result == "transcribed text" | ||
| mock_client.audio.transcriptions.create.assert_called_once() | ||
|
|
||
| # Verify the file argument was passed from a context manager | ||
| call_args = mock_client.audio.transcriptions.create.call_args | ||
| assert call_args.kwargs["file"] is not None | ||
| finally: | ||
| os.remove(temp_file_path) | ||
|
|
||
|
|
||
| @patch.dict(os.environ, {"OPENAI_API_KEY": "test-key"}) | ||
| @patch("camel.models.openai_audio_models.OpenAI") | ||
| def test_speech_to_text_translate_closes_file_handle(mock_openai_cls): | ||
| r"""Test that speech_to_text with translation properly closes | ||
| file handles.""" | ||
| mock_client = MagicMock() | ||
| mock_openai_cls.return_value = mock_client | ||
| mock_response = Mock() | ||
| mock_response.text = "translated text" | ||
| mock_client.audio.translations.create.return_value = mock_response | ||
|
|
||
| openai_audio = OpenAIAudioModels() | ||
|
|
||
| with tempfile.NamedTemporaryFile( | ||
| suffix=".wav", delete=False | ||
| ) as temp_file: | ||
| temp_file.write(b"Test audio data") | ||
| temp_file_path = temp_file.name | ||
|
|
||
| try: | ||
| result = openai_audio.speech_to_text( | ||
| temp_file_path, translate_into_english=True | ||
| ) | ||
| assert result == "translated text" | ||
| mock_client.audio.translations.create.assert_called_once() | ||
| finally: | ||
| os.remove(temp_file_path) |
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.
This preserves the original behaviour