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

Adding Open Router inference Example #226

Merged
merged 10 commits into from
Nov 28, 2023
Merged

Adding Open Router inference Example #226

merged 10 commits into from
Nov 28, 2023

Conversation

PhiBrandon
Copy link
Contributor

@PhiBrandon PhiBrandon commented Nov 26, 2023

Simple example utlizes Openrouter API to access Open source models for inference. Utilizing Json mode within Instructor, we can get Json structured output.

OpenRouter - https://openrouter.ai/

Summary by CodeRabbit

  • New Features

    • Introduced a new user details extraction feature using an open source model.
    • Implemented a user-friendly interface for inputting and receiving user details.
  • Enhancements

    • Improved user interaction by creating specific messages for data retrieval and error handling.
  • Documentation

    • Updated documentation to reflect new features and usage instructions.

Copy link
Contributor

coderabbitai bot commented Nov 26, 2023

Walkthrough

The changes across the board involve the enhancement of an open-source Python codebase, specifically openrouter.py. The updates include the integration of new imports, the creation of a Pydantic model for user details, the initialization of an OpenAI client, and the addition of functions to handle user detail extraction and message creation. The code now iterates through messages to extract and validate user details, with error handling and assertions for robustness.

Changes

File Path Change Summary
.../openrouter.py Introduced imports, a UserDetail Pydantic model, OpenAI client initialization, new functions for user detail extraction and message creation, and iterative message processing with assertions.

🐇✨
In the code where logic threads,
A rabbit hops, patching the spreads.
With models and clients, it deftly weaves,
Crafting details from texts with ease. 🌟📝


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 019114d and 0f9e91c.
Files selected for processing (1)
  • examples/open_source_examples/openrouter.py (1 hunks)
Additional comments: 3
examples/open_source_examples/openrouter.py (3)
  • 15-15:
    Verify that the instructor.patch method is a secure and maintained way to extend the OpenAI client functionality, and ensure that it does not introduce any vulnerabilities or stability issues.

  • 26-26:
    Ensure that the JSON schema generated by UserDetail.model_json_schema() is properly sanitized to prevent any potential injection attacks when used in the system message.

  • 30-30:
    Confirm that the response_format parameter is correctly implemented and supported by the OpenRouter API, as it is not a standard parameter in the OpenAI API.

examples/open_source_examples/openrouter.py Outdated Show resolved Hide resolved
examples/open_source_examples/openrouter.py Outdated Show resolved Hide resolved
examples/open_source_examples/openrouter.py Outdated Show resolved Hide resolved
@jxnl
Copy link
Collaborator

jxnl commented Nov 26, 2023

Take a look at #225 maybe we shuold add some tests or change the conftest to detect open router too

examples/open_source_examples/openrouter.py Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 0f9e91c and ab672c3.
Files selected for processing (1)
  • examples/open_source_examples/openrouter.py (1 hunks)
Additional comments: 1
examples/open_source_examples/openrouter.py (1)
  • 17-17: Consider using environment variables for the base URL to maintain consistency with the API key configuration and to allow for easier configuration changes without modifying the code.

@jxnl
Copy link
Collaborator

jxnl commented Nov 27, 2023

theres no need to make so many functions, they are only ever used once. its easier to edit if everything was just in a continue block of 5 lines of code rather than 5 functions spread around, does that make sense?

look at the other examples

Copy link
Collaborator

@jxnl jxnl left a comment

Choose a reason for hiding this comment

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

make sure you run ruff format . after youre done,
theres no need for so much indirection for code that is ever used once, you dont need a function

examples/open_source_examples/openrouter.py Outdated Show resolved Hide resolved
examples/open_source_examples/openrouter.py Outdated Show resolved Hide resolved
examples/open_source_examples/openrouter.py Outdated Show resolved Hide resolved
examples/open_source_examples/openrouter.py Outdated Show resolved Hide resolved
examples/open_source_examples/openrouter.py Outdated Show resolved Hide resolved
@jxnl
Copy link
Collaborator

jxnl commented Nov 27, 2023

no reason it shouldnt just look like this

https://github.com/jxnl/instructor/blob/main/examples/patching/patching.py

but with client = instructor.patch(OpenAI(...), mode=Mode.JSON)

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between ab672c3 and 695cec8.
Files selected for processing (1)
  • examples/open_source_examples/openrouter.py (1 hunks)
Additional comments: 4
examples/open_source_examples/openrouter.py (4)
  • 23-30: The UserDetail Pydantic model is well-defined and includes descriptions for each field, which is good for maintainability and clarity.

  • 33-40: Verify that the use of instructor.patch and the Maybe class from instructor is appropriate for the OpenAI client initialization and the MaybeUser type definition. This is not a common pattern and could lead to confusion or maintenance issues if not well-documented or if the instructor library is not widely adopted.

  • 43-57: The get_user_details function is implemented correctly and uses the OpenAI client to extract user details. The use of a system message to guide the model's output is a good practice.

  • 60-75: The create_user_message function is well-implemented, with the error parameter used to create a retry message in case of an error. This enhances the robustness of the function.

examples/open_source_examples/openrouter.py Outdated Show resolved Hide resolved
examples/open_source_examples/openrouter.py Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 695cec8 and 1f2b85d.
Files selected for processing (1)
  • examples/open_source_examples/openrouter.py (1 hunks)

examples/open_source_examples/openrouter.py Outdated Show resolved Hide resolved
examples/open_source_examples/openrouter.py Outdated Show resolved Hide resolved
examples/open_source_examples/openrouter.py Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 1f2b85d and b144220.
Files selected for processing (1)
  • examples/open_source_examples/openrouter.py (1 hunks)
Additional comments: 1
examples/open_source_examples/openrouter.py (1)
  • 13-20: Ensure that the environment variables OPENROUTER_API_KEY and OPENROUTER_BASE_URL are properly set in the deployment environment to prevent runtime errors.

Comment on lines 45 to 54
def get_user_details(model: str, messages: list[dict]) -> MaybeUser:
"""
Extract user details using Open soure model.
"""

response = client.chat.completions.create(
response_model=MaybeUser, model=model, messages=messages
)

return response
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider adding error handling for the client.chat.completions.create call in the get_user_details function to manage potential API call failures.

Comment on lines 85 to 92
for message in user_detail_messages:
user_message = create_user_message(message)
user = get_user_details(model, user_message)
# Output the error or the result.
if user.error:
print(user.message)
if user.result:
print(user.result)
Copy link
Contributor

Choose a reason for hiding this comment

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

The error handling in the loop only prints the error message. Consider implementing a retry mechanism or logging the error for better fault tolerance and debugging.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between b144220 and d0e5b6d.
Files selected for processing (1)
  • examples/open_source_examples/openrouter.py (1 hunks)
Additional comments: 1
examples/open_source_examples/openrouter.py (1)
  • 42-42: Verify the implementation of the Maybe class to ensure it works as expected with the UserDetail model.

Comment on lines +56 to +59
if user.error:
print(f"Error: {user.error}")
if user.result:
print(f"Result: {user.result}")
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider using elif for the if user.result: to avoid printing both error and result if both are present.

- if user.result:
+ elif user.result:

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
if user.error:
print(f"Error: {user.error}")
if user.result:
print(f"Result: {user.result}")
if user.error:
print(f"Error: {user.error}")
elif user.result:
print(f"Result: {user.result}")

@jxnl jxnl merged commit fbd65fb into instructor-ai:main Nov 28, 2023
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.

2 participants