Find answers from the community

Updated 11 months ago

hello what can I do if i want deterministic output from my engine? it should only return

hello @kapa.ai what can I do if i want deterministic output from my engine? it should only return json in a structure I specify. How can I achieve this?
k
3 comments
Please stand by @prathamesh4585 as I search the knowledge base. I normally take about 20 seconds or so. :wave:
----------------------
To achieve deterministic output from your engine in a specific JSON structure, you can use the JSON Query Engine provided by LlamaIndex. This engine is useful for querying JSON documents that conform to a JSON schema. The JSON schema is used in the context of a prompt to convert a natural language query into a structured JSON Path query, which is then used to retrieve data to answer the given question.

Here is an example of how to use the JSON Query Engine:

Plain Text
from llama_index.indices.service_context import ServiceContext
from llama_index.llms import OpenAI
from llama_index.indices.struct_store import JSONQueryEngine

llm = OpenAI(model="gpt-4")
service_context = ServiceContext.from_defaults(llm=llm)
nl_query_engine = JSONQueryEngine(
    json_value=json_value,
    json_schema=json_schema,
    service_context=service_context,
)
raw_query_engine = JSONQueryEngine(
    json_value=json_value,
    json_schema=json_schema,
    service_context=service_context,
    synthesize_response=False,
)

nl_response = nl_query_engine.query(
    "What comments has Jerry been writing?",
)
raw_response = raw_query_engine.query(
    "What comments has Jerry been writing?",
)

In this example, json_value is the JSON document you want to query, and json_schema is the schema that the JSON document conforms to. The synthesize_response parameter determines whether the engine should synthesize a natural language response (True) or return the raw JSON response (False).

You can also use Pydantic with the Query Engine to ensure your output is structured. You can extract a Pydantic object from a query engine class. Check out the Structured Outputs guide () for more information.
Please note that while JSON mode enforces the format of the output, it does not help with validation against a specified schema. Directly passing in a schema may not generate expected JSON and may require additional careful formatting and prompting.

Sources:
  • JSON Query Engine Example ()
  • Structured Outputs Guide ()
  • Function Calling vs JSON Mode ()
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Add a reply
Sign up and join the conversation on Discord