----------------------
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:
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.