From 8bb04a83776bc8bfc735ccfa08cb934141325e8e Mon Sep 17 00:00:00 2001 From: Simon Taggart Date: Sun, 10 Dec 2023 20:48:06 -0800 Subject: [PATCH] feat: updated gtp4 and using official sdk everywhere (#3645) * feat: updated gtp4 and using official sdk everywhere * fix(api): remove duplicated comment in AI endpoint --- packages/paste-website/package.json | 5 +- packages/paste-website/src/pages/api/ai.ts | 45 +++++++----------- .../src/pages/api/discussions-search.ts | 13 ++--- .../src/pages/api/docs-search.ts | 13 ++--- yarn.lock | 47 ++++--------------- 5 files changed, 39 insertions(+), 84 deletions(-) diff --git a/packages/paste-website/package.json b/packages/paste-website/package.json index 5bf063f2e5..3f60f28041 100644 --- a/packages/paste-website/package.json +++ b/packages/paste-website/package.json @@ -138,7 +138,7 @@ "@types/lodash": "^4.14.182", "@types/mdx-js__react": "^1.5.5", "@vercel/og": "^0.5.20", - "ai": "^2.2.13", + "ai": "^2.2.27", "airtable": "^0.11.6", "color": "^3.1.2", "common-tags": "^1.8.2", @@ -160,8 +160,7 @@ "micromark-extension-mdxjs": "^2.0.0", "minimist": "^1.2.8", "next": "^14.0.0", - "openai": "^4.18.0", - "openai-edge": "^1.2.2", + "openai": "^4.20.1", "pretty-format": "^28.1.0", "prism-react-renderer": "^1.3.5", "puppeteer-core": "^19.6.1", diff --git a/packages/paste-website/src/pages/api/ai.ts b/packages/paste-website/src/pages/api/ai.ts index 1afcc2bc86..01651c22bf 100644 --- a/packages/paste-website/src/pages/api/ai.ts +++ b/packages/paste-website/src/pages/api/ai.ts @@ -18,13 +18,7 @@ import { OpenAIStream, StreamingTextResponse } from "ai"; import { codeBlock, oneLine } from "common-tags"; import GPT3Tokenizer from "gpt3-tokenizer"; import type { NextRequest } from "next/server"; -import { - type ChatCompletionRequestMessage, - Configuration, - type CreateEmbeddingResponse, - type CreateModerationResponse, - OpenAIApi, -} from "openai-edge"; +import OpenAI from "openai"; class ApplicationError extends Error { // eslint-disable-next-line @typescript-eslint/no-parameter-properties @@ -39,10 +33,9 @@ const openAiSecret = process.env.OPENAI_API_SECRET; const supabaseUrl = process.env.SUPABASE_URL; const supabaseServiceKey = process.env.SUPABASE_KEY; -const config = new Configuration({ +const openai = new OpenAI({ apiKey: openAiKey, }); -const openai = new OpenAIApi(config); /** * Because we're using an edge function for streaming we can't use winston for logging @@ -102,9 +95,9 @@ export default async function handler(req: NextRequest): Promise res.json()); + const moderationResponse: OpenAI.ModerationCreateResponse = await openai.moderations.create({ + input: sanitizedQuery, + }); // @ts-expect-error this is a bug in the types if (moderationResponse.error) { @@ -126,18 +119,16 @@ export default async function handler(req: NextRequest): Promise