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

Add pydantic support in response_format #2647

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

Conversation

lhoestq
Copy link
Member

@lhoestq lhoestq commented Oct 31, 2024

  • support passing a Pydantic schema in response_format in InferenceClient.chat_completion
class MyExampleResponseFormat(BaseModel):
    foo: str
    bar: str

response = client.chat_completion(messages=messages, response_format=MyExampleResponseFormat)
  • added response.choices[0].message.parsed (and response.choices[0].message.refusal if the generation failed as in the openai client)
message = response.choices[0].message
if message.parsed:
    print(message.parsed)
else:
    print(message.refusal)

close #2646

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@Wauplin
Copy link
Contributor

Wauplin commented Oct 31, 2024

As discussed, we'll have to see what we can do for this. Especially because ChatCompletionOutputMessage is an auto-generated class (also the stream version ChatCompletionStreamOutputMessage would have to be updated). And do the same for InferenceClient.text_generation(..., grammar=...).

@lhoestq
Copy link
Member Author

lhoestq commented Oct 31, 2024

Ok, note that this is mostly useful for the non-streaming case imo

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.

InferenceClient: allow passing a pydantic model as response_format
3 participants