Skip to content
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

added finish reason exception IncompleteOutputException #279

Merged
merged 2 commits into from
Dec 15, 2023

Conversation

ionflow
Copy link
Contributor

@ionflow ionflow commented Dec 15, 2023

Based off this issue.

Needed an exception raised when the finish_reason for the completion was due to exceeding max_tokens ("length") versus when the finish_reason is stop.

Maybe better if this exception was called FinishReasonException? Or is IncompleteOutputException okay?

Also, do we want this exceptions file located somewhere else?

Summary by CodeRabbit

  • New Features

    • Introduced a new custom exception to handle incomplete outputs due to maximum token limits.
  • Bug Fixes

    • Implemented checks to raise the new IncompleteOutputException when output is incomplete in synchronous and asynchronous response handling functions.
  • Tests

    • Added new tests to verify the correct triggering of IncompleteOutputException under specified conditions.

Copy link
Contributor

coderabbitai bot commented Dec 15, 2023

Walkthrough

A new custom exception IncompleteOutputException has been introduced to handle scenarios where output from a large language model (LLM) is cut off due to reaching the maximum token limit. The exception is used in two functions, from_response and from_response_async, to raise an alert when incomplete output is detected. Additionally, the test suite has been updated to include new tests that ensure the exception works as intended in both synchronous and asynchronous contexts.

Changes

File Path Change Summary
instructor/exceptions.py Added IncompleteOutputException class.
.../function_calls.py Imported IncompleteOutputException; updated from_response and from_response_async to raise the new exception.
tests/test_function_calls.py Added Mode and ChatCompletion imports, mock_completion fixture, and tests for the new exception handling.

🐇✨
To code so bold, with logic tight,
A rabbit crafted, through day and night.
When tokens reach their peak and cease,
This clever hare says, "Handle with ease!" 🌟🚀

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on X ?


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • You can reply to a review comment made by CodeRabbit.
  • You can tag CodeRabbit on specific lines of code or files in the PR by tagging @coderabbitai in a comment.
  • You can tag @coderabbitai in a PR comment and ask one-off questions about the PR and the codebase. Use quoted replies to pass the context for follow-up questions.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 4df4647 and 334501e.
Files selected for processing (3)
  • instructor/exceptions.py (1 hunks)
  • instructor/function_calls.py (4 hunks)
  • tests/test_function_calls.py (2 hunks)
Additional comments: 6
instructor/exceptions.py (1)
  • 1-6: The implementation of IncompleteOutputException is correct and follows Python's standard practices for custom exceptions.
instructor/function_calls.py (3)
  • 4-4: The import of IncompleteOutputException is correctly added to support the new exception handling logic.

  • 125-126: The addition of the conditional check to raise IncompleteOutputException when the finish_reason is 'length' correctly implements the desired functionality described in the PR.

  • 235-235: The # type: ignore comment has been added without context. Please provide an explanation for this change, as it is not mentioned in the PR objectives or the AI-generated summaries.

tests/test_function_calls.py (2)
  • 16-47: The mock_completion fixture is well-implemented using indirect parameterization to test different scenarios. This is a good use of pytest's features to create flexible and reusable test setups.

  • 86-104: The new tests for checking the behavior of IncompleteOutputException are correctly implemented, using both synchronous and asynchronous versions of the from_response method. The use of pytest.mark.parametrize and pytest.mark.asyncio is appropriate for the intended test scenarios.

@@ -121,6 +122,9 @@ def from_response(
Returns:
cls (OpenAISchema): An instance of the class
"""
if completion.choices[0].finish_reason == 'length':
raise IncompleteOutputException()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indentation for the raise IncompleteOutputException() line is inconsistent with the surrounding code. It should be aligned with the rest of the code block for readability and to adhere to PEP 8 standards.

-          raise IncompleteOutputException()
+        raise IncompleteOutputException()

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
raise IncompleteOutputException()
raise IncompleteOutputException()

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 334501e and 6d373c0.
Files selected for processing (1)
  • instructor/function_calls.py (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • instructor/function_calls.py

@jxnl jxnl merged commit c80dd23 into instructor-ai:main Dec 15, 2023
2 of 3 checks passed
@ionflow ionflow deleted the feature/finish-reason-error branch December 16, 2023 04:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants