mirror of
https://github.com/kennethreitz/instructor.git
synced 2026-06-05 22:50:18 +00:00
added finish reason exception IncompleteOutputException (#279)
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
class IncompleteOutputException(Exception):
|
||||
"""Exception raised when the output from LLM is incomplete due to max tokens limit reached."""
|
||||
|
||||
def __init__(self, message="The output is incomplete due to a max_tokens length limit."):
|
||||
self.message = message
|
||||
super().__init__(self.message)
|
||||
@@ -1,6 +1,7 @@
|
||||
from docstring_parser import parse
|
||||
from functools import wraps
|
||||
from pydantic import BaseModel, create_model
|
||||
from instructor.exceptions import IncompleteOutputException
|
||||
|
||||
import enum
|
||||
|
||||
@@ -121,6 +122,9 @@ class OpenAISchema(BaseModel):
|
||||
Returns:
|
||||
cls (OpenAISchema): An instance of the class
|
||||
"""
|
||||
if completion.choices[0].finish_reason == 'length':
|
||||
raise IncompleteOutputException()
|
||||
|
||||
if stream_multitask:
|
||||
return cls.from_streaming_response(completion, mode)
|
||||
|
||||
@@ -179,6 +183,9 @@ class OpenAISchema(BaseModel):
|
||||
Returns:
|
||||
cls (OpenAISchema): An instance of the class
|
||||
"""
|
||||
if completion.choices[0].finish_reason == 'length':
|
||||
raise IncompleteOutputException()
|
||||
|
||||
if stream_multitask:
|
||||
return await cls.from_streaming_response_async(completion, mode)
|
||||
|
||||
@@ -225,4 +232,4 @@ def openai_schema(cls) -> OpenAISchema:
|
||||
cls.__name__,
|
||||
__base__=(cls, OpenAISchema),
|
||||
)
|
||||
) # type: ignore
|
||||
) # type: ignore
|
||||
Reference in New Issue
Block a user