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

feat: Return the messages as well in response for potentially fewer retries. #530

Closed
wants to merge 1 commit into from

Conversation

HamzaFarhan
Copy link

@HamzaFarhan HamzaFarhan commented Mar 24, 2024

PR for issue: 522


Ellipsis 🚀 This PR description was created by Ellipsis for commit fd44008.

Summary:

This PR modifies the retry functions and update_total_usage to include messages in the response, potentially reducing the number of retries.

Key points:

  • Modified retry_sync and retry_async in /instructor/retry.py to include messages in CompletionUsage.
  • Updated update_total_usage in /instructor/utils.py to add messages to total_usage.
  • Aimed at reducing retries by including messages in the response.

Generated with ❤️ by ellipsis.dev

Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a 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 in 2 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 of 85%.
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.

instructor/retry.py Show resolved Hide resolved
instructor/retry.py Show resolved Hide resolved
@HamzaFarhan
Copy link
Author

HamzaFarhan commented Mar 24, 2024

Here's an example.

This does not work when messages = [] and max_retries = 1

It does work when:

  • messages = [] but max_retries = 2
  • messages is this list below and max_retries = 1
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(
Copy link
Owner

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.

Copy link
Author

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.

@jxnl jxnl closed this Apr 3, 2024
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.

None yet

2 participants