instructor icon indicating copy to clipboard operation
instructor copied to clipboard

Return all the fields from model response

Open erastogi opened this issue 10 months ago • 5 comments

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Describe the solution you'd like A clear and concise description of what you want to happen.

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Additional context Add any other context or screenshots about the feature request here.

In current scenario, the following code will only return a response_model type class but I would also like to get other fields returned by anthropic model like usage, id etc.

message = anthropic_client.messages.create(
        model=engine,
        max_tokens=1000,
        temperature=temperature,
        top_p=top_p,
        system=system_prompt,
        messages=user_prompt,
        response_model=response_model
    )

erastogi avatar Apr 10 '24 19:04 erastogi

import openai
import instructor
from pydantic import BaseModel


client = instructor.from_openai(openai.OpenAI())


class User(BaseModel):
    name: str
    age: int


user, completion = client.chat.completions.create_with_completion(
    model="gpt-4-turbo-preview",
    messages=[
        {"role": "user", "content": "Create a user"},
    ],
    response_model=User,
)

jxnl avatar Apr 10 '24 20:04 jxnl

For antrhopic, it throws following error: AttributeError: 'Message' object has no attribute '_raw_response'

erastogi avatar Apr 11 '24 03:04 erastogi

can you include the entire stack trace?

jxnl avatar Apr 12 '24 13:04 jxnl

can you include the entire stack trace?

The same happens to me with the OpenAI instructor-patched client.

instructor==1.1.0 openai==1.14.1

My model is a List[User], hence not directly a Pydantic object, I believe the reason is that. For reproduction:

client.chat.completions.create_with_completion(
            model=model_name,
            messages=[
                {
                    "role": "system",
                    "content": "blahblah",
                },
                {
                    "role": "user",
                    "content": f"# Example output\n{example}\n\n# Input :\n\n----\n\n{text}\n\n----",
                },
            ],
            response_model=List[MyModel],
            response_format={"type": "json_object"},
            tool_choice="auto",
        )

and I get this error.

Traceback (most recent call last):
  File "/home/ubuntu/opt/miniconda3/envs/sm2/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3577, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-1-c37d075e354d>", line 1, in <module>
    out = client.chat.completions.create_with_completion(
  File "/home/ubuntu/opt/miniconda3/envs/sm2/lib/python3.10/site-packages/instructor/client.py", line 141, in create_with_completion
    return model, model._raw_response
AttributeError: 'list' object has no attribute '_raw_response'

What if I try passing a pydantic model like?

class ListMyModel(BaseModel):
    list_models: List[MyModel]

The attribute _raw_response should be there, right?

CarloNicolini avatar Apr 15 '24 13:04 CarloNicolini

Correct!

jxnl avatar Apr 15 '24 19:04 jxnl