2
0

⬆️ (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

@@ -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) ?? [],
}