Extract JSON from Text
Extract JSON Values from JSON Data
The json_extract()
function extracts values from the JSON data passed as its argument.
To show how it works, we use the home rentals example. The model returns the predicted value and the explanation of the prediction in the form of JSON data.
On execution, we get:
Now, if we want to see the confidence
value only, we can use the json_extract()
function as below.
On execution, we get:
Extract JSON from Text Data
In this example, we use the OpenAI model to extract data in a predefined JSON format from the input text data.
Default Model
When you create an OpenAI model in MindsDB, it uses the gpt-3.5-turbo
model by default. But you can use the gpt-4
model as well by passing it to the model-name
parameter.
Let’s create an OpenAI model.
Before creating an OpenAI model, please create an engine, providing your OpenAI API key:
We pass three parameters as follows:
- The
engine
parameter ensures we use the OpenAI engine. - The
json_struct
parameter stores a predefined JSON structure used for the output. - The
prompt_template
parameter contains the instruction passed to the model that may include variables such as{{sentence}}
.
Now we can query the model, passing the input text stored in the sentence
column.
On execution, we get:
Was this page helpful?