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 grammar to chat/completions endpoint / Messages API #1858

Open
ggbetz opened this issue May 5, 2024 · 0 comments
Open

Add grammar to chat/completions endpoint / Messages API #1858

ggbetz opened this issue May 5, 2024 · 0 comments

Comments

@ggbetz
Copy link

ggbetz commented May 5, 2024

Feature request

Add grammar as additional parameter to chat/completions.

Motivation

Constrained decoding with grammars is a killer feature for many users. Exposing grammar parameter would allows users to guide chat completions output with regex or json schemes.

The openai client has an extra_body argument, so that we could access TGI endpoints as follows

from openai import OpenAI
from pydantic import BaseModel, conint
from typing import List

class Animals(BaseModel):
    location: str
    activity: str
    animals_seen: conint(ge=1, le=5)  # Constrained integer type
    animals: List[str]

+grammar =  {
+    "type": "json",
+    "value": Animals.schema()
+}

client = OpenAI(
    base_url="http://localhost:3000/v1",
    api_key="_",
)

chat_completion = client.chat.completions.create(
    model="tgi",
    messages=[
        {
            "role": "system",
            "content": "Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.",
        },
        {
            "role": "user",
            "content": "What's the weather like the next 3 days in San Francisco, CA?",
        },
    ],
+   extra_body={"grammar"=grammar},
    max_tokens=500,
)

This exposes grammars in all frameworks based on openai client. E.g., grammars could also be used via bind(extrabody=...) in LangChain OpenAIChatModels hooked on TGI Messages API.

Your contribution

Discuss implementations, test, documentation

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

1 participant