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

[Doc] Add documentation for Structured Outputs #9943

Merged
merged 7 commits into from
Nov 18, 2024

Conversation

ismael-dm
Copy link
Contributor

Adding some documentation on the structured outputs (guided decoding) options that are available, together with two files of examples. Includes both online inference (OpenAI API) and offline inference.

Copy link

github-actions bot commented Nov 2, 2024

👋 Hi! Thank you for contributing to the vLLM project.
Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which starts running only a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of those by going to your fastcheck build on Buildkite UI (linked in the PR checks section) and unblock them. If you do not have permission to unblock, ping simon-mo or khluu to add you in our Buildkite org.

Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging.

To run CI, PR reviewers can do one of these:

  • Add ready label to the PR
  • Enable auto-merge.

🚀

@mergify mergify bot added the documentation Improvements or additions to documentation label Nov 2, 2024
@ismael-dm ismael-dm force-pushed the structured-outputs-docs branch from fd5dfe5 to 734fa12 Compare November 2, 2024 01:12
@ismael-dm ismael-dm force-pushed the structured-outputs-docs branch from 590e800 to 9914dcf Compare November 2, 2024 01:13
@simon-mo
Copy link
Collaborator

simon-mo commented Nov 2, 2024

Nice! Thank you for the PR.

  • Please add a link to the online inference docs from the OpenAI server page
  • Please make sure all the models are instruction tuned variant.

@simon-mo simon-mo self-assigned this Nov 2, 2024
@ismael-dm
Copy link
Contributor Author

@simon-mo sure, I added the link to OpenAI server page and changed the models to instruct variant (an re-tested the examples). But now a couple of automatic tests are failling, any idea why?

@ismael-dm
Copy link
Contributor Author

@simon-mo all the tests are good now and the requested changes were applied

@tcapelle
Copy link

tcapelle commented Nov 15, 2024

Hey, just found your PR as I am struggling with struct output on openai VLLM, do you have any insights on this?

But for some reason, when using a pydantic model I get a bad output and whithout it it works. My code is this:

import openai

MODEL_NAME = "internlm/internlm2_5-7b-chat"

vllm_client = openai.AsyncOpenAI(
    base_url="http://localhost:8000/v1",
    api_key="dummy")

class PersonInfo(BaseModel):
    name: str = Field(description="The name of the person")
    age: int = Field(description="The age of the person")

json_schema = PersonInfo.model_json_schema()

async def instruct_call(prompt: str = "My friend Pedro is 25 years old", **kwargs):
    response = await vllm_client.chat.completions.create(
        model=MODEL_NAME,
        messages=[
            {"role": "system", "content": "You are a helpful assistant that generates JSON objects."},
            {"role": "user", "content": "Get the name and age from the following text: " + prompt}],
        **kwargs
    )
    return response.choices[0].message.content

print("="*80)
print("Structured output asking for JSON")
print(asyncio.run(instruct_call()))
print("="*80)
print("Structured output with JSON schema")
print(asyncio.run(instruct_call(extra_body={"guided_json": json_schema})))

# ================================================================================
# Structured output asking for JSON
#
# {
#   "name": "Pedro",
#   "age": 25
# }
#
# ================================================================================
# Structured output with JSON schema
# {"name":"Weird","age":25}```

@ismael-dm
Copy link
Contributor Author

Hey, just found your PR as I am struggling with struct output on openai VLLM, do you have any insights on this?

But for some reason, when using a pydantic model I get a bad output and whithout it it works. My code is this:

import openai

MODEL_NAME = "internlm/internlm2_5-7b-chat"

vllm_client = openai.AsyncOpenAI(
    base_url="http://localhost:8000/v1",
    api_key="dummy")

class PersonInfo(BaseModel):
    name: str = Field(description="The name of the person")
    age: int = Field(description="The age of the person")

json_schema = PersonInfo.model_json_schema()

async def instruct_call(prompt: str = "My friend Pedro is 25 years old", **kwargs):
    response = await vllm_client.chat.completions.create(
        model=MODEL_NAME,
        messages=[
            {"role": "system", "content": "You are a helpful assistant that generates JSON objects."},
            {"role": "user", "content": "Get the name and age from the following text: " + prompt}],
        **kwargs
    )
    return response.choices[0].message.content

print("="*80)
print("Structured output asking for JSON")
print(asyncio.run(instruct_call()))
print("="*80)
print("Structured output with JSON schema")
print(asyncio.run(instruct_call(extra_body={"guided_json": json_schema})))

# ================================================================================
# Structured output asking for JSON
#
# {
#   "name": "Pedro",
#   "age": 25
# }
#
# ================================================================================
# Structured output with JSON schema
# {"name":"Weird","age":25}```

Not sure tbh. It seems that it´s generating a valid json, but the values are not properly filled. What I´ve seen is that some models perform better when filling out the fields of structured outputs. I´ve had the best results with Qwen models. Also, trying to iterate a little bit over the prompt may help, like giving some examples of the desired output. Are you using the latest vllm version? If any of these things work maybe the best option is to open an issue to discuss further.

@simon-mo simon-mo merged commit 31894a2 into vllm-project:main Nov 18, 2024
31 checks passed
@tcapelle
Copy link

I feel we are missing the openai.beta api example that gives you real structured output. https://x.com/dottxtai/status/1858495878463389877

mikejuliet13 pushed a commit to mikejuliet13/vllm that referenced this pull request Nov 19, 2024
coolkp pushed a commit to coolkp/vllm that referenced this pull request Nov 20, 2024
KuntaiDu pushed a commit to KuntaiDu/vllm that referenced this pull request Nov 20, 2024
mfournioux pushed a commit to mfournioux/vllm that referenced this pull request Nov 20, 2024
rickyyx pushed a commit to rickyyx/vllm that referenced this pull request Nov 20, 2024
tlrmchlsmth pushed a commit to neuralmagic/vllm that referenced this pull request Nov 23, 2024
prashantgupta24 pushed a commit to opendatahub-io/vllm that referenced this pull request Dec 3, 2024
sleepwalker2017 pushed a commit to sleepwalker2017/vllm that referenced this pull request Dec 13, 2024
anko-intel pushed a commit to HabanaAI/vllm-fork that referenced this pull request Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants