Text-to-SQL proof of concept (#5788)

Added:
- An "Ask AI" command to the command menu.
- A simple GraphQL resolver that converts the user's question into a
relevant SQL query using an LLM, runs the query, and returns the result.

<img width="428" alt="Screenshot 2024-06-09 at 20 53 09"
src="https://github.com/twentyhq/twenty/assets/171685816/57127f37-d4a6-498d-b253-733ffa0d209f">

No security concerns have been addressed, this is only a
proof-of-concept and not intended to be enabled in production.

All changes are behind a feature flag called `IS_ASK_AI_ENABLED`.

---------

Co-authored-by: Félix Malfait <felix.malfait@gmail.com>
This commit is contained in:
ad-elias
2024-07-04 08:57:26 +02:00
committed by GitHub
parent 25fce27fe3
commit 4c642a0bb8
46 changed files with 1463 additions and 40 deletions

View File

@ -7,6 +7,7 @@ import {
import { EventEmitter2 } from '@nestjs/event-emitter';
import isEmpty from 'lodash.isempty';
import { DataSource } from 'typeorm';
import { IConnection } from 'src/engine/api/graphql/workspace-query-runner/interfaces/connection.interface';
import {
@ -620,15 +621,12 @@ export class WorkspaceQueryRunnerService {
return sanitizedRecord;
}
async execute(
query: string,
async executeSQL(
workspaceDataSource: DataSource,
workspaceId: string,
): Promise<PGGraphQLResult | undefined> {
const workspaceDataSource =
await this.workspaceDataSourceService.connectToWorkspaceDataSource(
workspaceId,
);
sqlQuery: string,
parameters?: any[],
) {
try {
return await workspaceDataSource?.transaction(
async (transactionManager) => {
@ -638,10 +636,7 @@ export class WorkspaceQueryRunnerService {
)};
`);
const results = transactionManager.query<PGGraphQLResult>(
`SELECT graphql.resolve($1);`,
[query],
);
const results = transactionManager.query(sqlQuery, parameters);
return results;
},
@ -655,6 +650,23 @@ export class WorkspaceQueryRunnerService {
}
}
async execute(
query: string,
workspaceId: string,
): Promise<PGGraphQLResult | undefined> {
const workspaceDataSource =
await this.workspaceDataSourceService.connectToWorkspaceDataSource(
workspaceId,
);
return this.executeSQL(
workspaceDataSource,
workspaceId,
`SELECT graphql.resolve($1);`,
[query],
);
}
private async parseResult<Result>(
graphqlResult: PGGraphQLResult | undefined,
objectMetadataItem: ObjectMetadataInterface,