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(instructor): add middleware and update package #551

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

jxnl
Copy link
Owner

@jxnl jxnl commented Apr 2, 2024

Ellipsis 🚀 This PR description was created by Ellipsis for commit 20bb86d.

Summary:

This PR introduces middleware to the Instructor library, updates the instructor package, provides an example of middleware implementation, and makes minor changes in other files.

Key points:

  • Introduced MessageMiddleware and AsyncMessageMiddleware classes in instructor/messages_middleware.py.
  • Updated instructor/client.py to include a message_middleware list and a with_middleware method.
  • Provided an example of middleware implementation in examples/middleware/run.py.
  • Made minor changes in instructor/__init__.py and tests/llm/test_openai/test_validators.py.

Generated with ❤️ by ellipsis.dev

@jxnl jxnl marked this pull request as ready for review April 2, 2024 00:41
@ellipsis-dev ellipsis-dev bot changed the title ... feat(instructor): add middleware and update package Apr 2, 2024
Copy link

cloudflare-pages bot commented Apr 2, 2024

Deploying instructor with  Cloudflare Pages  Cloudflare Pages

Latest commit: 20bb86d
Status: ✅  Deploy successful!
Preview URL: https://b0c6733c.instructor.pages.dev
Branch Preview URL: https://messages-middleware.instructor.pages.dev

View logs

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 6e50514
  • Looked at 328 lines of code in 5 files
  • Took 2 minutes and 12 seconds to review
More info
  • Skipped 0 files when reviewing.
  • Skipped posting 3 additional comments because they didn't meet confidence threshold of 85%.
1. examples/middleware/readme.md:1:
  • Assessed confidence : 100%
  • Grade: 0%
  • Comment:
    The new md file created in docs is not added to mkdocs.yml. Please add it to ensure the documentation is correctly generated and the new content is accessible.
  • Reasoning:
    The new md file created in docs is not added to mkdocs.yml. This is necessary to ensure the documentation is correctly generated and the new content is accessible.
2. instructor/client.py:64:
  • Assessed confidence : 100%
  • Grade: 30%
  • Comment:
    The with_middleware method does not check the type of the middleware passed to it. This could lead to runtime errors if the middleware does not have a __call__ method. Consider checking the type of the middleware and raising a clear error if it is not a function or an instance of MessageMiddleware.
  • Reasoning:
    The with_middleware method in the client.py file does not check the type of the middleware passed to it. This could lead to runtime errors if the middleware does not have a __call__ method. It would be better to check the type of the middleware and raise a clear error if it is not a function or an instance of MessageMiddleware.
3. instructor/messages_middleware.py:25:
  • Assessed confidence : 100%
  • Grade: 0%
  • Comment:
    The messages_middleware function returns an instance of _Middleware without any arguments. This could be a problem if the middleware function requires arguments. Consider returning a function that creates a new instance of _Middleware with the given arguments when called.
  • Reasoning:
    The messages_middleware function in the messages_middleware.py file returns an instance of _Middleware without any arguments. This could be a problem if the middleware function requires arguments. It would be better to return a function that creates a new instance of _Middleware with the given arguments when called.

Workflow ID: wflow_1m3ZAZRPd66jbqmK


Want Ellipsis to fix these issues? Tag @ellipsis-dev in a comment. We'll respond in a few minutes. Learn more here.

examples/middleware/run.py Show resolved Hide resolved
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.

👍 Looks good to me!

  • Performed an incremental review on 8a28d7e
  • Looked at 90 lines of code in 5 files
  • Took 1 minute and 16 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. examples/middleware/run.py:8:
  • Assessed confidence : 50%
  • Comment:
    There are unnecessary blank lines in your code. Please remove them for better readability. This applies to lines 7, 20, 31, 37, 43, 44.
  • Reasoning:
    The PR introduces middleware to the Instructor library, allowing for the modification of messages sent to the language model before they are processed. The changes include updates to the instructor package and examples demonstrating middleware usage. The changes seem to be well implemented and there are no clear violations of best practices, logical bugs, performance bugs, or security bugs. However, there are some unnecessary blank lines in the code that could be removed for better readability.

Workflow ID: wflow_0w4cJpFUaEZWuvGG


Not what you expected? You can customize the content of the reviews using rules. Learn more here.

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.

  • Performed an incremental review on 20bb86d
  • Looked at 21 lines of code in 1 files
  • Took 1 minute and 15 seconds to review
More info
  • Skipped 0 files when reviewing.
  • Skipped posting 0 additional comments because they didn't meet confidence threshold of 85%.

Workflow ID: wflow_JFCFwVEsbWMR96tt


Want Ellipsis to fix these issues? Tag @ellipsis-dev in a comment. We'll respond in a few minutes. Learn more here.

try:

@instructor.messages_middleware
def dumb_rag(x):
Copy link
Contributor

Choose a reason for hiding this comment

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

The function dumb_rag is defined twice in this file. If the second definition is meant for testing, consider moving it to a dedicated test function or file. If it's not for testing, please remove the duplicate.

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.

  • Performed an incremental review on 4560ae9
  • Looked at 21 lines of code in 1 files
  • Took 2 minutes and 31 seconds to review
More info
  • Skipped 0 files when reviewing.
  • Skipped posting 0 additional comments because they didn't meet confidence threshold of 85%.

Workflow ID: wflow_ZBx4GrCuG3suf89X


Want Ellipsis to fix these issues? Tag @ellipsis-dev in a comment. We'll respond in a few minutes. Learn more here.



def messages_middleware(func: Callable) -> MessageMiddleware:
import inspect
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider moving the import statement for the inspect module to the top of the file. This aligns with Python's best practices for import statements.

@mwildehahn
Copy link
Contributor

mwildehahn commented Apr 17, 2024

Is this still in the works? I'd like to use something like this for caching request / responses from the LLM. I currently have custom code to do this just for openai but wanted to do the same for anthropic. Given the new client refactor, it'd be nice to just be able to do something like:

anthropic = Instructor.from_anthropic(..., middleware=[ResponseCache()])
openai = Instructor.from_openai(..., middleware=[ResponseCache()])

I know there is this for caching: https://jxnl.github.io/instructor/concepts/caching/#2-redis-caching-decorator-for-distributed-systems but in this case, I wanted to log the raw request / responses in a db so I can use them later.

@jxnl
Copy link
Owner Author

jxnl commented Apr 17, 2024

Definitely this is mostly a sketch right now because I don't know if this is something people would actually want

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants