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

Feature Request: Support for Python Built-in Types as Response Models in Instructor #446

Closed
Fakamoto opened this issue Feb 19, 2024 · 5 comments

Comments

@Fakamoto
Copy link
Contributor

Fakamoto commented Feb 19, 2024

Is your feature request related to a problem? Please describe.
Defining a Pydantic model for responses that could be directly represented by Python built-in types is cumbersome. This issue is evident when dealing with simple boolean queries about the presence of certain conditions in a text, such as detecting emotions.

Describe the solution you'd like
Enable the use of Python built-in types, like bool, as the response_model in Instructor. This enhancement would simplify scenarios where the expected response is a straightforward yes/no or true/false.

Example Code for Desired Feature:

import instructor
from openai import OpenAI

# Patch the OpenAI client
client = instructor.patch(OpenAI())

response = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[
        {
            "role": "user",
            "content": "Does this text contain angry emotions? 'I can't believe this happened! This is absolutely unacceptable and frustrating!'",
        }
    ],
    response_model=bool,  # Expecting a boolean response directly
)

# Validate the boolean response
assert isinstance(response, bool)  # Expected to be True

Current Workaround with Pydantic Model:
Currently, to achieve a boolean response, I need to define a Pydantic model specifically for this purpose, which feels unnecessarily complex for such a simple task.

from pydantic import BaseModel
import instructor
from openai import OpenAI

class EmotionDetectionResponse(BaseModel):
    contains_angry_emotions: bool

# Patch the OpenAI client
client = instructor.patch(OpenAI())

# Using a Pydantic model for a simple boolean response
response = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[
        {
            "role": "user",
            "content": "Does this text contain angry emotions? 'I can't believe this happened! This is absolutely unacceptable and frustrating!'",
        }
    ],
    response_model=EmotionDetectionResponse,
)

# Accessing the boolean through the Pydantic model
assert isinstance(response, EmotionDetectionResponse)
assert response.contains_angry_emotions is True  # Extracting the boolean value

Describe alternatives you've considered
The alternative involves manually parsing the response outside of Instructor, which negates the benefits of streamlined data handling and validation provided by Instructor.

@jxnl
Copy link
Collaborator

jxnl commented Feb 19, 2024

I agree but I'm run into two issues I'd love your opinion on

  1. how do we store the openai response data (tokens etc)

  2. how can you tell it what the prompt means?

We could say for all non basemodel types.

Do you want partial and steaming?

What about

response_model=Annotated[bool, Field(description=...)]

Just to get you some prompting power.

@jxnl
Copy link
Collaborator

jxnl commented Feb 19, 2024

take a look at #447

@jxnl
Copy link
Collaborator

jxnl commented Feb 21, 2024

should be available now!

@jxnl jxnl closed this as completed Feb 21, 2024
@Fakamoto
Copy link
Contributor Author

Sorry about the delay getting back to you, I must say though you executed the implementation of the feature flawlessly. Tried the feature and it works great, even my example code of desired feature works exactly as it is written.

Excellent job!
I think this is a very valuable feature and deserves a detailed explanation in the README.

@jxnl
Copy link
Collaborator

jxnl commented Feb 21, 2024

mind writing something up? can just copy and simplify https://jxnl.github.io/instructor/concepts/types

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

No branches or pull requests

2 participants