⬆️ (openai) Replace openai-edge with openai and upgrade next

This commit is contained in:
Baptiste Arnaud
2023-10-06 14:22:38 +02:00
parent dfcaa0f1d0
commit 225dfed313
15 changed files with 415 additions and 178 deletions

View File

@@ -33,6 +33,7 @@
"@trpc/next": "10.34.0",
"@trpc/react-query": "10.34.0",
"@trpc/server": "10.34.0",
"@typebot.io/bot-engine": "workspace:*",
"@typebot.io/emails": "workspace:*",
"@typebot.io/env": "workspace:*",
"@typebot.io/nextjs": "workspace:*",
@@ -65,13 +66,13 @@
"libphonenumber-js": "1.10.37",
"micro": "10.0.1",
"micro-cors": "0.1.1",
"next": "13.4.3",
"next": "13.5.4",
"next-auth": "4.22.1",
"next-international": "0.9.5",
"nextjs-cors": "^2.1.2",
"nodemailer": "6.9.3",
"nprogress": "0.2.0",
"openai-edge": "1.2.2",
"openai": "^4.11.1",
"papaparse": "5.4.1",
"posthog-js": "^1.77.1",
"posthog-node": "3.1.1",
@@ -89,8 +90,7 @@
"tinycolor2": "1.6.0",
"trpc-openapi": "1.2.0",
"unsplash-js": "^7.0.18",
"use-debounce": "9.0.4",
"@typebot.io/bot-engine": "workspace:*"
"use-debounce": "9.0.4"
},
"devDependencies": {
"@chakra-ui/styled-system": "2.9.1",

View File

@@ -3,13 +3,13 @@ import { authenticatedProcedure } from '@/helpers/server/trpc'
import { TRPCError } from '@trpc/server'
import { z } from 'zod'
import { isReadWorkspaceFobidden } from '@/features/workspace/helpers/isReadWorkspaceFobidden'
import { Configuration, OpenAIApi, ResponseTypes } from 'openai-edge'
import { decrypt } from '@typebot.io/lib/api'
import {
OpenAICredentials,
defaultBaseUrl,
} from '@typebot.io/schemas/features/blocks/integrations/openai'
import { isNotEmpty } from '@typebot.io/lib/utils'
import { OpenAI, ClientOptions } from 'openai'
export const listModels = authenticatedProcedure
.meta({
@@ -79,41 +79,26 @@ export const listModels = authenticatedProcedure
credentials.iv
)) as OpenAICredentials['data']
const config = new Configuration({
const config = {
apiKey: data.apiKey,
basePath: baseUrl,
baseOptions: {
headers: {
'api-key': data.apiKey,
},
baseURL: baseUrl,
defaultHeaders: {
'api-key': data.apiKey,
},
defaultQueryParams: isNotEmpty(apiVersion)
? new URLSearchParams({
defaultQuery: isNotEmpty(apiVersion)
? {
'api-version': apiVersion,
})
}
: undefined,
})
} satisfies ClientOptions
const openai = new OpenAIApi(config)
const openai = new OpenAI(config)
const response = await openai.listModels()
const modelsData = (await response.json()) as
| ResponseTypes['listModels']
| {
error: unknown
}
if ('error' in modelsData)
throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: 'Could not list models',
cause: modelsData.error,
})
const models = await openai.models.list()
return {
models:
modelsData.data
models.data
.sort((a, b) => b.created - a.created)
.map((model) => model.id) ?? [],
}