force json output

#32
by joa23 - opened

Hi,
Any suggestions how to force json output confirm with a provided schema? I tried JsonFormer but it fails since its missing position_ids. Clearly a limitation of JsonFormer.
I appreciate any pointers.

Maybe you can use a instruction format from langchain.

JSON_FORMAT_INSTRUCTIONS = """The output should be formatted as a JSON instance that conforms to the JSON schema below.

As an example, for the schema {{"properties": {{"foo": {{"title": "Foo", "description": "a list of strings", "type": "array", "items": {{"type": "string"}}}}}}, "required": ["foo"]}}
the object {{"foo": ["bar", "baz"]}} is a well-formatted instance of the schema. The object {{"properties": {{"foo": ["bar", "baz"]}}}} is not well-formatted.

Here is the output schema:

{schema}
```"""

class Translation(BaseModel):
    text: str = Field(description="Tranlated text")


instruction_format = JSON_FORMAT_INSTRUCTIONS.format(schema=Translation.model_json_schema())

user_prompt = f"Translate the input text.\n{instruction_format}\nInput text:\n{input_text}\n"


It's just an example.

Sign up or log in to comment