Data Management

Getting Structured Responses from LLMs

Summary

Hey there! In this guide, I'll show you how to use large language models like ChatGPT to produce structured data outputs that you can then directly integrate into your application's database using Xano. This can be incredibly useful when you want to populate your database with sample data or add new records based on AI-generated content.

Let's say you have a `companies` table in your database with fields like `name`, `description`, `employees`, and `location`. You can leverage the power of AI to generate sample data that fits this schema, and then seamlessly add it to your database using Xano's no-code platform.

Step 1: Configure the LLM API Request

First, we'll set up the API request to communicate with the large language model (LLM) of your choice. In this example, we'll use ChatGPT from OpenAI.

  1. Head to the API endpoint in Xano and import the API request structure from the documentation. This will give you a starting point with the correct URL, method, and other required parameters.
  2. Look for the option to specify the response format in the documentation. For ChatGPT, we can set the `response_format` parameter to `json` to receive structured data instead of plain text.
  3. Add the `response_format` parameter to your API request by copying an existing parameter (e.g., `model`) and updating the key to `response_format` and the value to `json`.

Step 2: Provide a Prompt for Structured Data

Next, we'll craft a prompt that instructs the LLM to generate data in the desired format for your database schema.

  1. Copy the schema of your database table (e.g., `companies` table with fields like `name`, `description`, `employees`, and `location`).
  2. Add a section to your prompt that clearly states the task you want the LLM to perform. For example: "Your task is to generate JSON formatted data as per the below request. You are to generate sample data. Tech company follows the following schema: [paste your schema here]. Output a single record in JSON format only."
  3. Make sure to include the instruction to "Output a single record in JSON format only" to ensure the LLM understands the expected output format.
  4. Finish your prompt with a polite request, such as "As always, use your manners, please generate JSON as requested."

Step 3: Add API Authorization

Many LLM providers require authorization to access their APIs. If you're using OpenAI's ChatGPT, you'll need to include your API key in the request headers.

  1. Store your OpenAI API key as an environment variable in Xano's system settings.
  2. In your API request, add an `Authorization` header and reference the environment variable containing your API key.

Step 4: Parse the LLM Response

After making the API request, you should receive a response from the LLM containing the generated structured data. However, this response may be nested within a larger JSON object, so we'll need to extract the relevant part.

  1. Create a new variable to store the LLM's response (e.g., `jsonResponse`).
  2. Use Xano's `subpath` filter to navigate to the specific part of the response that contains the generated data.
  3. Since the extracted data is likely a stringified JSON object, use Xano's `decode` filter to convert it back to a valid JSON object.

Step 5: Add the Data to Your Database

Finally, with the structured data in a valid JSON format, you can easily add it to your database table using Xano's no-code interface.

  1. Create a new record in your database table (e.g., `companies` table).
  2. Use the magic wand feature to automatically map the keys from your parsed JSON object to the corresponding fields in your table.
  3. If needed, adjust any additional fields (e.g., set the `timestamp` field to the current time).
  4. Run the workflow, and voilà! The AI-generated data will be added to your database table.

That's it! You've successfully used a large language model to generate structured data and integrated it into your application's database using Xano's no-code platform. You can repeat this process as needed to populate your database with sample data or add new records based on AI-generated content.

Remember, while some LLMs like ChatGPT provide options to control the response format, others may require you to rely solely on well-crafted prompts to receive structured data outputs. Adjust your approach accordingly for the specific LLM you're using.

Happy coding (or should I say, happy no-coding)!

This transcript was AI generated to allow users to quickly answer technical questions about Xano.

Was this helpful?

I found it helpful

I need more support
Sign up for XanoSign up for Xano

Build without limits on a secure, scalable backend.

Unblock your team’s progress and create a backend that will scale for free.

Start building for free