-
-
Notifications
You must be signed in to change notification settings - Fork 469
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: Return the messages as well in response for potentially fewer retries. #530
Conversation
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.
❌ Changes requested.
- Reviewed the entire pull request up to fd44008
- Looked at
73
lines of code in2
files - Took 53 seconds to review
More info
- Skipped
0
files when reviewing. - Skipped posting
1
additional comments because they didn't meet confidence threshold of85%
.
1. instructor/utils.py:66
:
- Assessed confidence :
100%
- Grade:
70%
- Comment:
The 'messages' attribute is not part of the CompletionUsage class. This will cause an AttributeError when trying to assign the 'messages' attribute to the 'total_usage' object. Please remove the 'messages' attribute from the 'total_usage' object.
if isinstance(response, ChatCompletion) and response.usage is not None:
total_usage.completion_tokens += response.usage.completion_tokens or 0
total_usage.prompt_tokens += response.usage.prompt_tokens or 0
total_usage.total_tokens += response.usage.total_tokens or 0
- Reasoning:
The 'messages' attribute is not part of the CompletionUsage class. This will cause an AttributeError when trying to assign the 'messages' attribute to the 'total_usage' object. The 'messages' attribute should be removed from the 'total_usage' object.
Workflow ID: wflow_0DDVOPedBmZyL2Hf
Want Ellipsis to fix these issues? Tag @ellipsis-dev
in a comment. We'll respond in a few minutes. Learn more here.
Here's an example. This does not work when messages = [] and max_retries = 1 It does work when:
def chat_message(role: str, content: str) -> dict:
return {"role": role, "content": content}
def user_message(content: str) -> dict:
return chat_message(role="user", content=content)
class People(BaseModel):
people: list[str] = Field(..., examples=["Hamza", "Jason"], min_length=3)
messages = [
{"role": "user", "content": "2 french names"},
{
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": "call_UtoQ9RpuVKmXEmu9DahWA7Mw",
"function": {
"arguments": '{"people":["Jean","Marie"]}',
"name": "People",
},
"type": "function",
}
],
},
{
"role": "tool",
"tool_call_id": "call_UtoQ9RpuVKmXEmu9DahWA7Mw",
"name": "People",
"content": "Validation Error found:\n1 validation error for People\npeople\n List should have at least 3 items after validation, not 2 [type=too_short, input_value=['Jean', 'Marie'], input_type=list]\n For further information visit https://errors.pydantic.dev/2.6/v/too_short\nRecall the function correctly, fix the errors",
},
]
max_retries = 1
prompt = "2 french names"
messages = [user_message(prompt)] + messages
res = ask_oai(
messages=messages, model=MODEL, max_retries=max_retries, response_model=People
)
# People(people=['Jean', 'Marie', 'Pierre']) |
@@ -64,7 +64,12 @@ def retry_sync( | |||
strict: Optional[bool] = None, | |||
mode: Mode = Mode.TOOLS, | |||
) -> T_Model: | |||
total_usage = CompletionUsage(completion_tokens=0, prompt_tokens=0, total_tokens=0) | |||
total_usage = CompletionUsage( |
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.
We can definitely move it somewhere else, but we shouldn't be moving it into the usage.
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.
Cool. What would you suggest? I played around with this today and it feels like a simplified version of DSPy since both approaches give us a better input for future calls. So it's definitely useful.
PR for issue: 522
Summary:
This PR modifies the
retry
functions andupdate_total_usage
to include messages in the response, potentially reducing the number of retries.Key points:
retry_sync
andretry_async
in/instructor/retry.py
to include messages inCompletionUsage
.update_total_usage
in/instructor/utils.py
to add messages tototal_usage
.Generated with ❤️ by ellipsis.dev