Skip to content

feat(ai): introduce ai proxy #1260

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions packages/agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@forestadmin/datasource-customizer": "1.61.3",
"@forestadmin/datasource-toolkit": "1.48.0",
"@forestadmin/forestadmin-client": "1.36.8",
"@forestadmin/ai-proxy": "0.0.1",
"@koa/cors": "^5.0.0",
"@koa/router": "^12.0.0",
"@types/koa__router": "^12.0.0",
Expand Down
41 changes: 41 additions & 0 deletions packages/agent/src/routes/ai-proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { ForestAIProxy, createForestAIProxy } from '@forestadmin/ai-proxy';
import { DataSource } from '@forestadmin/datasource-toolkit';
import Router from '@koa/router';
import { Context } from 'koa';

import BaseRoute from './base-route';
import { ForestAdminHttpDriverServices } from '../services';
import { AgentOptionsWithDefaults, HttpCode, RouteType } from '../types';

export default class AIProxy extends BaseRoute {
readonly type = RouteType.PrivateRoute;
private forestProxy: ForestAIProxy;

constructor(services: ForestAdminHttpDriverServices, options: AgentOptionsWithDefaults) {
super(services, options);
this.forestProxy = createForestAIProxy();
}

setupRoutes(router: Router): void {
router.post(`/ai-proxy`, this.proxy.bind(this));
}

private async proxy(context: Context) {
try {
const { openAIApiKey } = this.options.experimental.ai.openAI;

if (context.query.provider === 'openai' && openAIApiKey) {
context.response.body = await this.forestProxy.proxyOpenAI({ openAIApiKey }, context.body);
context.response.status = HttpCode.Ok;
} else {
// TODO add a link to the documentation
context.throw(HttpCode.BadRequest, 'Provider not supported or API key not set');
}
} catch (error) {
console.error(error);
context.throw(HttpCode.InternalServerError, 'Error while proxying AI request');
}

context.response.status = HttpCode.Ok;
}
}
11 changes: 11 additions & 0 deletions packages/agent/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Get from './access/get';
import List from './access/list';
import ListRelated from './access/list-related';
import NativeQueryDatasource from './access/native-query-datasource';
import AIProxy from './ai-proxy';
import BaseRoute from './base-route';
import Capabilities from './capabilities';
import ActionRoute from './modification/action/action';
Expand Down Expand Up @@ -56,6 +57,7 @@ export const RELATED_ROUTES_CTOR = [
DissociateDeleteRelated,
ListRelated,
];
export const AI_ROUTES_CTOR = [AIProxy];
export const RELATED_RELATION_ROUTES_CTOR = [UpdateRelation];
export const CAPABILITIES_ROUTES_CTOR = [Capabilities];
export const NATIVE_QUERY_ROUTES_CTOR = [NativeQueryDatasource];
Expand Down Expand Up @@ -108,6 +110,14 @@ function getCapabilitiesRoutes(
return routes;
}

function getAIRoutes(options: Options, services: Services): BaseRoute[] {
const routes: BaseRoute[] = [];

routes.push(...AI_ROUTES_CTOR.map(Route => new Route(services, options)));

return routes;
}

function getNativeQueryRoutes(
dataSource: DataSource,
options: Options,
Expand Down Expand Up @@ -171,6 +181,7 @@ export default function makeRoutes(
): BaseRoute[] {
const routes = [
...getRootRoutes(options, services),
...getAIRoutes(options, services),
...getCrudRoutes(dataSource, options, services),
...getCapabilitiesRoutes(dataSource, options, services),
...getNativeQueryRoutes(dataSource, options, services),
Expand Down
1 change: 1 addition & 0 deletions packages/agent/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type AgentOptions = {
experimental?: {
webhookCustomActions?: boolean;
updateRecordCustomActions?: boolean;
ai: { openAI?: { openAIApiKey?: string } };
};
maxBodySize?: string;
/**
Expand Down
Empty file added packages/ai-proxy/CHANGELOG.md
Empty file.
Loading
Loading