Skip to content

introduce chat api to the project supported by langchain's sql-agent #2788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions packages/query/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,30 @@ then run the following command
```sh
NODE_OPTIONS="-r dotenv/config" yarn start:dev -- --name <subquery_name> --playground
```

## LLM Configuration

Suggest to use reasoning models to archive better quality of results.

### Choose one of the following providers:

### Option 1: OpenAI

LLM_PROVIDER=openai
LLM_MODEL=o1 # Optional: defaults to '4o-mini'
OPENAI_API_KEY=your_openai_api_key

### Option 2: Anthropic Claude

LLM_PROVIDER=anthropic
LLM_MODEL=claude-3-7-sonnet-latest # Optional: defaults to 'claude-3-7-sonnet-latest'
ANTHROPIC_API_KEY=your_anthropic_api_key

### Option 3: Custom OpenAI-compatible endpoint

LLM_PROVIDER=openai
LLM_BASE_URL=http://your-llm-endpoint/v1 # Required for custom provider
LLM_MODEL=your-model-name # Required: model name for custom endpoint
OPENAI_API_KEY=your_api_key # Optional: API key for custom endpoint

### Common LLM Settings
6 changes: 6 additions & 0 deletions packages/query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
"@graphile-contrib/pg-simplify-inflector": "^6.1.0",
"@graphile/pg-aggregates": "^0.1.1",
"@graphile/pg-pubsub": "^4.13.0",
"@langchain/anthropic": "^0.3.20",
"@langchain/core": "^0.3.55",
"@langchain/langgraph": "^0.2.71",
"@langchain/openai": "^0.5.10",
"@nestjs/common": "^9.4.0",
"@nestjs/core": "^9.4.0",
"@nestjs/platform-express": "^9.4.0",
Expand All @@ -50,12 +54,14 @@
"graphql": "^15.8.0",
"graphql-query-complexity": "^0.11.0",
"graphql-ws": "^5.16.0",
"langchain": "^0.3.24",
"lodash": "^4.17.21",
"pg": "^8.12.0",
"pg-tsquery": "^8.4.2",
"postgraphile": "^4.13.0",
"postgraphile-plugin-connection-filter": "^2.2.2",
"rxjs": "^7.1.0",
"typeorm": "^0.3.23",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does langchain require typeorm? Could we use sequelize here for consistency?

"ws": "^8.18.0",
"yargs": "^16.2.0"
},
Expand Down
5 changes: 4 additions & 1 deletion packages/query/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import {Module} from '@nestjs/common';
import {ConfigureModule} from './configure/configure.module';
import {GraphqlModule} from './graphql/graphql.module';
import {ChatModule} from './llm/chat.module';

@Module({
imports: [ConfigureModule.register(), GraphqlModule],
// the order is essential, the ChatModule must be before the GraphqlModule so /v1/chat/completions
// can be handled without interference from the GraphqlModule
imports: [ConfigureModule.register(), ChatModule, GraphqlModule],
controllers: [],
})
export class AppModule {}
18 changes: 17 additions & 1 deletion packages/query/src/configure/configure.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {ConnectionOptions} from 'tls';
import {DynamicModule, Global, Module} from '@nestjs/common';
import {getFileContent, CONNECTION_SSL_ERROR_REGEX} from '@subql/common';
import {Pool, PoolConfig} from 'pg';
import {DataSource} from 'typeorm';
import {getLogger} from '../utils/logger';
import {getYargsOption} from '../yargs';
import {Config} from './config';
Expand Down Expand Up @@ -85,6 +86,17 @@ export class ConfigureModule {
pgClient._explainResults = [];
});
}
// todo: support ssl
const dataSource = new DataSource({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we specify this as read only?

type: 'postgres',
host: config.get('DB_HOST_READ'),
port: config.get('DB_PORT'),
username: config.get('DB_USER'),
password: config.get('DB_PASS'),
database: config.get('DB_DATABASE'),
schema: config.get<string>('name'),
});
await dataSource.initialize();
return {
module: ConfigureModule,
providers: [
Expand All @@ -96,8 +108,12 @@ export class ConfigureModule {
provide: Pool,
useValue: pgPool,
},
{
provide: DataSource,
useValue: dataSource,
},
],
exports: [Config, Pool],
exports: [Config, Pool, DataSource],
};
}
}
8 changes: 3 additions & 5 deletions packages/query/src/graphql/graphql.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {playgroundPlugin} from './plugins/PlaygroundPlugin';
import {queryAliasLimit} from './plugins/QueryAliasLimitPlugin';
import {queryComplexityPlugin} from './plugins/QueryComplexityPlugin';
import {queryDepthLimitPlugin} from './plugins/QueryDepthLimitPlugin';
import {ProjectService} from './project.service';

const {argv} = getYargsOption();
const logger = getLogger('graphql-module');
Expand All @@ -45,16 +44,15 @@ class NoInitError extends Error {
}
}
@Module({
providers: [ProjectService],
providers: [],
})
export class GraphqlModule implements OnModuleInit, OnModuleDestroy {
private _apolloServer?: ApolloServer;
private wsCleanup?: ReturnType<typeof useServer>;
constructor(
private readonly httpAdapterHost: HttpAdapterHost,
private readonly config: Config,
private readonly pgPool: Pool,
private readonly projectService: ProjectService
private readonly pgPool: Pool
) {}

private get apolloServer(): ApolloServer {
Expand Down Expand Up @@ -138,7 +136,7 @@ export class GraphqlModule implements OnModuleInit, OnModuleDestroy {
const schemaName = this.config.get<string>('name');
if (!schemaName) throw new Error('Unable to get schema name from config');

const dbSchema = await this.projectService.getProjectSchema(schemaName);
const dbSchema = schemaName;
let options: PostGraphileCoreOptions = {
replaceAllPlugins: plugins,
subscriptions: true,
Expand Down
39 changes: 0 additions & 39 deletions packages/query/src/graphql/project.service.ts

This file was deleted.

Loading