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

[Ellipsis] Add MD_YAML Mode to Patch Function #688

Closed
wants to merge 2 commits into from

Conversation

ellipsis-dev[bot]
Copy link
Contributor

@ellipsis-dev ellipsis-dev bot commented May 20, 2024

This change addresses review comments left by @jxnl on PR #674: Add markdown yaml input mode to optimize input tokens

⚠️ We couldn't build/test your project to verify our changes. Add a Dockerfile to significantly improve code quality.

Summary:

This PR adds a new MD_YAML mode to the patch function to support parsing YAML inputs within markdown and converting them to JSON.

Key points:

  • Introduces new MD_YAML mode in patch function
  • Allows parsing of YAML within markdown to JSON
  • Example usage provided in the documentation

You can configure Ellipsis to address comments with a direct commit or a side PR, see docs.


Something look wrong?: If this Pull Request doesn't address the comments left on the above pull request, create a new PR review with more details. For more information, check the documentation.

Generated with ❤️ by ellipsis.dev

Copy link

cloudflare-pages bot commented May 20, 2024

Deploying instructor with  Cloudflare Pages  Cloudflare Pages

Latest commit: f61fd78
Status: ✅  Deploy successful!
Preview URL: https://bdb1aad4.instructor.pages.dev
Branch Preview URL: https://ellipsis-wflow-akw3ynscskj2i.instructor.pages.dev

View logs

Copy link

sweep-ai bot commented May 20, 2024

Sweep: PR Review

instructor/client.py

The changes add support for the MD_YAML mode for the ANYSCALE, TOGETHER, and OPENAI providers.


instructor/function_calls.py

The changes introduce support for parsing and validating YAML formatted responses by adding a new import, modifying the from_response method to handle a new mode, and adding a new parse_yaml method to the OpenAISchema class.


instructor/mode.py

A new mode MD_YAML was added to the Mode enum class.


instructor/process_response.py

The changes introduce support for handling YAML formatted responses by adding the yaml module import and modifying the handle_response_model function to handle a new mode, Mode.MD_YAML.

Sweep Found These Issues

  • The new conditional block for Mode.MD_YAML does not handle the case where new_kwargs["messages"] is empty, which could lead to an IndexError.
  • message = dedent(
    f"""
    As a genius expert, your task is to understand the content and provide
    the parsed objects in yaml that match the following yaml_schema:\n
    {yaml.dump(response_model.model_json_schema(), indent=2)}
    Make sure to return an instance of the YAML, not the schema itself
    """
    )
    new_kwargs["messages"].append(
    {
    "role": "user",
    "content": "Return the correct YAML response within a ```yaml codeblock. not the YAML_SCHEMA",
    },
    )
    # check that the first message is a system message
    # if it is not, add a system message to the beginning
    if new_kwargs["messages"][0]["role"] != "system":
    new_kwargs["messages"].insert(
    0,
    {
    "role": "system",
    "content": message,
    },
    )
    # if it is, system append the schema to the end
    else:
    new_kwargs["messages"][0]["content"] += f"\n\n{message}"

    View Diff


instructor/retry.py

A new elif condition was added to handle the Mode.MD_YAML mode in the reask_messages function, providing specific instructions for correcting YAML responses.

Sweep Found These Issues

  • The new elif condition for Mode.MD_YAML does not include a return statement, which could lead to unintended fall-through behavior.
  • elif mode == Mode.MD_YAML:
    yield {
    "role": "user",
    "content": f"Correct your YAML ONLY RESPONSE, based on the following errors:\n{exception}",
    }

    View Diff


instructor/utils.py

The changes introduce the yaml module import and add a new function extract_json_from_yaml_codeblock to handle and convert YAML code blocks into JSON format.

Sweep Found These Issues

  • The function extract_json_from_yaml_codeblock does not handle potential exceptions that could be raised by yaml.safe_load, such as yaml.YAMLError, which could lead to unhandled exceptions if the YAML content is malformed.
  • def extract_json_from_yaml_codeblock(content: str) -> str:
    yaml_start = content.find("```yaml")
    if yaml_start != -1:
    yaml_end = content.find("```", yaml_start + 7)
    if yaml_end != -1:
    yaml_string = yaml.safe_load(content[yaml_start + 7 : yaml_end].strip())
    return json.dumps(yaml_string)
    return ""

    View Diff


patch.md

The changes add documentation for a new mode MD_YAML that allows the system to handle YAML inputs within markdown and convert them into JSON for further processing.

Sweep Found These Issues

  • The documentation does not specify how the system handles errors or invalid YAML inputs, which could lead to confusion or misuse.
  • instructor/patch.md

    Lines 1 to 24 in f61fd78

    ## YAML in Patching
    The system now supports a new mode, `MD_YAML`, to handle YAML inputs within markdown. This mode allows the system to parse YAML inputs and convert them into JSON for further processing.
    To use this mode, you need to specify it when calling the `patch` function, like so:
    ```python
    client = instructor.patch(client, mode=instructor.Mode.MD_YAML)
    ```
    You can then provide YAML inputs within markdown. Here's an example:
    ```yaml
    Order Details:
    Customer: Jason
    Items:
    Name: Apple, Price: 0.50
    Name: Bread, Price: 2.00
    Name: Milk, Price: 1.50
    ```
    The system will parse this input and convert it into a JSON object for further processing.

    View Diff


tests/llm/test_openai/test_modes.py

A new test function test_yaml was added to validate the Order model using a YAML-formatted string as input.


```python
client = instructor.patch(client, mode=instructor.Mode.MD_YAML)
```

Copy link
Owner

Choose a reason for hiding this comment

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

@ellipsis-dev delete everything below this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jxnl, I have addressed your comments in commit fb51ac6


You can configure Ellipsis to address comments with a direct commit or a side PR, see docs.

@jxnl jxnl closed this May 20, 2024
ellipsis-dev bot added a commit that referenced this pull request May 20, 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