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

Added example for using unions of models, useful for agents #299

Merged
merged 4 commits into from
Dec 23, 2023

Conversation

zby
Copy link
Contributor

@zby zby commented Dec 22, 2023

This is the example from #296

I searched for Union in the sources - and it seems that there are no examples of this approach.

Summary by CodeRabbit

  • New Features
    • Introduced a new tool selection functionality with options for search, lookup, and finish actions.
    • Enhanced integration with external APIs using response models for better action processing.

Copy link
Contributor

coderabbitai bot commented Dec 22, 2023

Walkthrough

The latest update introduces a suite of Pydantic models that streamline the process of selecting and executing actions within a Python application. These models enable the application to search, lookup, and complete tasks, with an overarching TakeAction class orchestrating these functionalities. Integration with an external API is enhanced by the use of a response_model.

Changes

File Path Change Summary
examples/.../let_the_llm_choose_a_tool.py
examples/union/run.py
Added Search, Lookup, Finish class definitions; TakeAction class for action processing; client variable for API interaction.
docs/concepts/union.md Shifted content to explain using Union types for dynamically choosing actions.

🐇✨
In the code where logic weaves,
A rabbit hopped, rolled up its sleeves.
With classes and models, it took a stance,
To help the code find its dance. 🕺💻
🐇✨

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on X ?


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • You can directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
  • You can tag CodeRabbit on specific lines of code or entire files in the PR by tagging @coderabbitai in a comment. Examples:
  • You can tag @coderabbitai in a PR comment and ask questions about the PR and the codebase. Use quoted replies to pass the context for follow-up questions. Examples:
    • @coderabbitai render interesting statistics about this repository as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai generate unit tests for the src/utils.ts file.

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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

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 64ed6b4 and 2c48865.
Files selected for processing (1)
  • examples/let_the_llm_choose_a_tool.py (1 hunks)
Additional comments: 4
examples/let_the_llm_choose_a_tool.py (4)
  • 6-12: The Search class is well-defined with a clear docstring and a process method. The use of the Field function for the query attribute provides additional metadata which can be useful for documentation or schema generation.

  • 14-20: The Lookup class is consistent with the Search class in structure and implementation, which is good for maintainability and readability.

  • 22-28: The Finish class maintains the consistency of design seen in the Search and Lookup classes, which is beneficial for a unified approach to action classes.

  • 30-36: The TakeAction class correctly uses a union type for the action attribute, allowing for polymorphic behavior. The process method is concise and delegates the call to the contained action's process method, which is a clean implementation of the strategy pattern.

Comment on lines 39 to 49
# Enables `response_model`
client = instructor.patch(OpenAI())
action = client.chat.completions.create(
model="gpt-3.5-turbo",
response_model=TakeAction,
messages=[
{"role": "user", "content": "Please choose one action"},
]
)
assert isinstance(action, TakeAction)
print(action.process())
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 around the API call and the type assertion to gracefully handle any exceptions that may occur, ensuring the robustness of the example.

+ try:
  client = instructor.patch(OpenAI())
  action = client.chat.completions.create(
      model="gpt-3.5-turbo",
      response_model=TakeAction,
      messages=[
          {"role": "user", "content": "Please choose one action"},
      ]
  )
  assert isinstance(action, TakeAction)
  print(action.process())
+ except (SomeSpecificException, AssertionError) as e:
+     print(f"An error occurred: {e}")

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
# Enables `response_model`
client = instructor.patch(OpenAI())
action = client.chat.completions.create(
model="gpt-3.5-turbo",
response_model=TakeAction,
messages=[
{"role": "user", "content": "Please choose one action"},
]
)
assert isinstance(action, TakeAction)
print(action.process())
try:
client = instructor.patch(OpenAI())
action = client.chat.completions.create(
model="gpt-3.5-turbo",
response_model=TakeAction,
messages=[
{"role": "user", "content": "Please choose one action"},
]
)
assert isinstance(action, TakeAction)
print(action.process())
except (SomeSpecificException, AssertionError) as e:
print(f"An error occurred: {e}")

@jxnl
Copy link
Collaborator

jxnl commented Dec 22, 2023

do you mind:

  1. move to examples/union/run.py
  2. add some docs to https://jxnl.github.io/instructor/concepts/union/

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 2c48865 and 471b8a9.
Files selected for processing (2)
  • docs/concepts/union.md (1 hunks)
  • examples/union/run.py (1 hunks)
Files skipped from review due to trivial changes (1)
  • docs/concepts/union.md
Additional comments: 5
examples/union/run.py (5)
  • 1-5: Ensure that all imported modules are used within the file to avoid unnecessary imports that can lead to confusion and performance issues.

  • 6-12: The Search class is well-defined with a clear docstring and a process method. The use of Pydantic's Field for additional metadata is good practice.

  • 14-20: The Lookup class follows the same pattern as the Search class, with appropriate use of Pydantic's Field and a clear process method.

  • 22-28: The Finish class is consistent with the other action classes in structure and implementation, ensuring uniformity across the different action types.

  • 30-36: The TakeAction class correctly uses a Union type for the action field, allowing for dynamic action processing. This aligns with the PR's objective to demonstrate the use of unions.

Comment on lines +38 to +51
try:
# Enables `response_model`
client = instructor.patch(OpenAI())
action = client.chat.completions.create(
model="gpt-3.5-turbo",
response_model=TakeAction,
messages=[
{"role": "user", "content": "Please choose one action"},
]
)
assert isinstance(action, TakeAction), "The action is not TakeAction"
print(action.process())
except Exception as e:
print(f"An error occurred: {e}")
Copy link
Contributor

@coderabbitai coderabbitai bot Dec 23, 2023

Choose a reason for hiding this comment

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

The main execution block includes error handling and an assertion to ensure the correct type is returned. However, consider adding more specific exception handling to provide clearer error messages for different failure scenarios.

try:
    # Existing code...
except SpecificException as e:
    print(f"A specific error occurred: {e}")
except Exception as e:
    print(f"An error occurred: {e}")

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
try:
# Enables `response_model`
client = instructor.patch(OpenAI())
action = client.chat.completions.create(
model="gpt-3.5-turbo",
response_model=TakeAction,
messages=[
{"role": "user", "content": "Please choose one action"},
]
)
assert isinstance(action, TakeAction), "The action is not TakeAction"
print(action.process())
except Exception as e:
print(f"An error occurred: {e}")
try:
# Enables `response_model`
client = instructor.patch(OpenAI())
action = client.chat.completions.create(
model="gpt-3.5-turbo",
response_model=TakeAction,
messages=[
{"role": "user", "content": "Please choose one action"},
]
)
assert isinstance(action, TakeAction), "The action is not TakeAction"
print(action.process())
except SpecificException as e:
print(f"A specific error occurred: {e}")
except Exception as e:
print(f"An error occurred: {e}")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I want the example to be executable.

Copy link
Contributor

Choose a reason for hiding this comment

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

💡 NOTE
Deep insights is a beta feature.

@zby, it seems that the instructor and openai modules are not part of the codebase and might be external dependencies. To ensure the example is executable, you would need to have these packages installed in your environment. If they are already installed and you're encountering issues, could you please provide more details on the errors you're facing? This will help me assist you further in making the example executable.

Analysis chain
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Check if the 'instructor' module exists in the codebase
fd 'instructor.py'

# Check if the 'openai' module exists in the codebase
fd 'openai.py'

# Check if the 'OpenAI' class is defined within the 'openai' module
ast-grep --lang python --pattern $'class OpenAI {
  $$$
}'

Length of output: 538



Script:

#!/bin/bash
# Correcting the ast-grep pattern to search for the 'OpenAI' class definition
ast-grep --lang python --pattern $'class OpenAI { $$$ }'

Length of output: 500

@zby
Copy link
Contributor Author

zby commented Dec 23, 2023

I would rename the concept to Agent - because this is what people are looking for.

@jxnl jxnl merged commit b101a85 into instructor-ai:main Dec 23, 2023
3 checks passed
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