Skip to main content

Query data sources with AI

Discover insights about your data and generate charts and graphs in real time using natural, conversational language.

You can interact with the data in a database connector using Squid's Query with AI feature. The user provides a prompt, which can be written in natural language. Query with AI then replies with a natural language explanation, along with the query it executed to get the result.

With Query with AI, everyone can find key data points, generate charts and graphs, and get answers to questions about data without needing to write queries or graphing functions.

Use cases

You can use Squid AI to get human-readable explanations of your data. Here are some example prompts:

  • How many games has team A won against team B?
  • Create a line graph of sales in 2022, with x representing the month and y representing the total value of sales in dollars.
  • What is the most common question asked by our customers?

Query with AI is a powerful tool in the Squid platform and can be used to simplify complex questions and queries. The following guide provides an overview of the key concepts with accompanying code snippets. See an example on our website.

Configure your database connector

Query with AI runs queries on a database connector. In order for the AI to understand your data, it first looks at your database schema, which lists your collections and fields. Squid AI also uses descriptions of those collections and fields to help it determine what query to make.

There are two options for providing collection and field descriptions:

  • Manually enter descriptions in your own words.
  • Let Squid AI generate schema descriptions for you.

Manually enter descriptions

Follow these steps to view your database schema and provide collection and field descriptions:

  1. In the Squid Console, click the Connectors tab and find the database you want to query.

If you haven't yet connected the database you want to use with Squid AI, then add the connector now. To learn more about configuring database connector, view the database connectors documentation. Once your database connector is added, you can continue to the next step. Alternatively, you can use Squid's built-in database.

  1. Click the ellipsis (…) button on your database connector, and select Schema from the dropdown menu.

  2. To update the schema based on data that already exists in the database, click Rediscover schema. Alternatively, you can edit your schema directly in the console.

  3. Add descriptions to collections by clicking the pencil button located to the right of the collection name. Add descriptions to fields by clicking the ellipsis (…) on the field and selecting Edit field from the dropdown menu.

  4. After rediscovering the schema or editing descriptions, click Save schema to publish the updates.

Use AI-generated descriptions

Squid AI can generate collection and field descriptions on your behalf. This functionality queries a small set of data from a collection and passes the data along with your schema to OpenAI.

Note

The LLMs used by Squid are will never use your data for model training. If you do not want any database data to be passed to an LLM, then do not use AI-generated descriptions. The Query with AI feature does not send any of your database data, so you can still use the feature with your own manually-generated descriptions.

Follow these steps to view your database schema and create AI-generated collection and field descriptions:

  1. In the Squid Console, click the Connectors tab and find the database you want to query.

If you haven't yet connected the database you want to use with Squid AI, then add the connector now. To learn more about configuring database connector, view the database connectors documentation. Once your database connector is added, you can continue to the next step. Alternatively, you can use Squid's built-in database.

  1. Click the ellipsis (…) button on your database connector, and select Schema from the dropdown menu.

  2. To update the schema based on data that already exists in the database, click Rediscover schema. Alternatively, you can edit your schema directly in the console.

  3. Click the Generate Descriptions with AI button to generate descriptions for all collections and fields in your schema. This could take a few moments, depending on the size of your database schema. You can manually review and edit the collection and fields descriptions once they have been generated.

  4. After rediscovering the schema or editing descriptions, click Save schema to publish the updates.

Test Squid AI for Your Data

Once your schema is configured and descriptions are added, you can query your data with Squid AI in the console. In the schema view of your database connector, click the Query with AI button. You can ask the provided AI agent details about your data and test the responses.

Query with AI in the console

Note

Query with AI might take a few seconds to a few minutes to respond depending on the complexity of the question and query generated. Squid AI handles the coding, simplifying the process for you.

Add Squid AI to the backend

You can use an Query with AI in your Squid backend to ask questions about your data programmatically.

The following example executes an AI query in an executable using the executeAiQuery method:

Backend code
export class SquidAiDataService extends SquidService {
@executable()
async askQuestion(question: string): Promise<string> {
const aiResponse = await this.squid
.ai()
.executeAiQuery('DATABASE_CONNECTOR_ID', question);

// Log results to view in Squid Console logs
console.log(
`Question: ${question}
Query: ${aiResponse.executedQuery ?? 'No query executed'}
Explanation: ${aiResponse.explanation ?? 'No explanation'}`
);
// Send the answer to the client
return aiResponse.answer;
}
}

Next Steps

Want to try out Query with AI for your data? For a complete code sample and instructions, check out our tutorial to see how you can get up and running in a matter of minutes!