Skip to content

Commit 9a9f63e

Browse files
authored
fix: allow the use of older gpt models in the api (#2001)
* fix: allow the use of older gpt models in the api * fix: add script to run migration for test env
1 parent 5c73dc5 commit 9a9f63e

4 files changed

Lines changed: 50 additions & 4 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"openapi:generate": "node dist/scripts/generateOpenAPI.js",
4242
"openapi:lint": "vacuum lint ./openapi.json",
4343
"openapi:report": "vacuum html-report ./openapi.json vacuum-report.html",
44+
"migration:apply-test": "DB_CONNECTION=postgres://test:test@db:5432/test yarn migration:run",
4445
"migration:generate": "yarn drizzle-kit generate",
4546
"migration:run": "yarn drizzle-kit migrate",
4647
"migration:check": "yarn drizzle-kit generate --name=migrationtest --out=./src/drizzle --schema=./src/drizzle/schema.ts --dialect=postgresql && [ -f '$(find ./src/drizzle | grep migrationtest)' ] && exit 1 || exit 0"

src/services/item/plugins/app/chatBot/chatBot.controller.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import build, {
1212
import { seedFromJson } from '../../../../../../test/mocks/seed';
1313
import { db } from '../../../../../drizzle/db';
1414
import { assertIsDefined } from '../../../../../utils/assertions';
15-
import { APP_ITEMS_PREFIX } from '../../../../../utils/config';
15+
import { APP_ITEMS_PREFIX, OPENAI_GPT_VERSION } from '../../../../../utils/config';
1616
import { OpenAILengthError, OpenAIUnknownStopError } from '../../../../../utils/errors';
1717
import { getAccessToken } from '../test/fixtures';
1818
import { FinishReason } from './chatBot.types';
@@ -304,6 +304,36 @@ describe('Chat Bot Tests', () => {
304304
expect(response.statusCode).toEqual(StatusCodes.BAD_REQUEST);
305305
});
306306

307+
it('OK if using accepted version but default to newer model', async () => {
308+
const { apps } = await seedFromJson({ apps: [{}] });
309+
const {
310+
items: [item],
311+
actor,
312+
} = await seedFromJson({
313+
items: [
314+
{
315+
type: ItemType.APP,
316+
memberships: [{ account: 'actor' }],
317+
},
318+
],
319+
});
320+
const chosenApp = apps[0];
321+
322+
assertIsDefined(actor);
323+
mockAuthenticate(actor);
324+
const token = await getAccessToken(app, item, chosenApp);
325+
const response = await app.inject({
326+
method: HttpMethod.Post,
327+
url: `${APP_ITEMS_PREFIX}/${item.id}/${CHAT_PATH}?gptVersion=gpt-4`,
328+
headers: {
329+
Authorization: `Bearer ${token}`,
330+
},
331+
payload: DOCKER_MOCKED_BODY,
332+
});
333+
expect(response.statusCode).toEqual(StatusCodes.OK);
334+
expect(response.json().model).toBe(OPENAI_GPT_VERSION);
335+
});
336+
307337
it('Bad request if post chat with invalid temperature', async () => {
308338
const { apps } = await seedFromJson({ apps: [{}] });
309339
const {

src/services/item/plugins/app/chatBot/chatBot.controller.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox';
22

3+
import { GPTVersion, GPTVersionType } from '@graasp/sdk';
4+
35
import { resolveDependency } from '../../../../../di/utils';
46
import { db } from '../../../../../drizzle/db';
57
import { asDefined } from '../../../../../utils/assertions';
@@ -10,6 +12,15 @@ import { AuthorizedItemService } from '../../../../authorizedItem.service';
1012
import { create } from './chatBot.schemas';
1113
import { ChatBotService } from './chatBot.service';
1214

15+
const validateGPTVersion = (gptVersionInput: string | undefined): GPTVersionType => {
16+
let gptVersion = gptVersionInput;
17+
// convert removed versions to the default
18+
if (!gptVersion || !(Object.values(GPTVersion) as string[]).includes(gptVersion)) {
19+
gptVersion = OPENAI_GPT_VERSION;
20+
}
21+
return gptVersion as GPTVersionType;
22+
};
23+
1324
const chatBotPlugin: FastifyPluginAsyncTypebox = async (fastify) => {
1425
const authorizedItemService = resolveDependency(AuthorizedItemService);
1526
const chatBotService = resolveDependency(ChatBotService);
@@ -27,9 +38,10 @@ const chatBotPlugin: FastifyPluginAsyncTypebox = async (fastify) => {
2738
await authorizedItemService.getItemById(db, { accountId: member.id, itemId });
2839
throw new InvalidJWTItem(jwtItemId ?? '<EMPTY>', itemId);
2940
}
30-
// default to 3.5 turbo / or the version specified in the env variable
41+
// validate the GPTVersion so that we can still support unsupported versions that will default to
42+
// the standard value
43+
const gptVersion = validateGPTVersion(query.gptVersion);
3144
// as it is the cheapest model while still allowing a larger context window than gpt4
32-
const gptVersion = query.gptVersion ?? OPENAI_GPT_VERSION;
3345
const temperature = query.temperature ?? OPENAI_DEFAULT_TEMPERATURE;
3446

3547
const message = await chatBotService.post(

src/services/item/plugins/app/chatBot/chatBot.schemas.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ export const create = {
3434
}),
3535
querystring: customType.StrictObject({
3636
gptVersion: Type.Optional(
37-
Type.Enum(GPTVersion, { description: 'Model to use', default: OPENAI_GPT_VERSION }),
37+
Type.Union([
38+
Type.Literal('gpt-4'),
39+
Type.Enum(GPTVersion, { description: 'Model to use', default: OPENAI_GPT_VERSION }),
40+
]),
3841
),
3942
temperature: Type.Optional(
4043
Type.Number({ maximum: OPENAI_MAX_TEMPERATURE, minimum: OPENAI_MIN_TEMPERATURE }),

0 commit comments

Comments
 (0)