-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡ (openai) Add custom provider and custom models
Closes #532
- Loading branch information
1 parent
436fa25
commit 27a5f4e
Showing
21 changed files
with
684 additions
and
278 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
apps/builder/src/features/blocks/integrations/openai/api/listModels.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import prisma from '@/lib/prisma' | ||
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 } from '@typebot.io/schemas/features/blocks/integrations/openai' | ||
import { IntegrationBlockType, typebotSchema } from '@typebot.io/schemas' | ||
import { isNotEmpty } from '@typebot.io/lib/utils' | ||
|
||
export const listModels = authenticatedProcedure | ||
.meta({ | ||
openapi: { | ||
method: 'GET', | ||
path: '/typebots/{typebotId}/blocks/{blockId}/openai/models', | ||
protect: true, | ||
summary: 'List OpenAI models', | ||
tags: ['OpenAI'], | ||
}, | ||
}) | ||
.input( | ||
z.object({ | ||
typebotId: z.string(), | ||
blockId: z.string(), | ||
credentialsId: z.string(), | ||
workspaceId: z.string(), | ||
}) | ||
) | ||
.output( | ||
z.object({ | ||
models: z.array(z.string()), | ||
}) | ||
) | ||
.query( | ||
async ({ | ||
input: { credentialsId, workspaceId, typebotId, blockId }, | ||
ctx: { user }, | ||
}) => { | ||
const workspace = await prisma.workspace.findFirst({ | ||
where: { id: workspaceId }, | ||
select: { | ||
members: { | ||
select: { | ||
userId: true, | ||
}, | ||
}, | ||
typebots: { | ||
where: { | ||
id: typebotId, | ||
}, | ||
select: { | ||
groups: true, | ||
}, | ||
}, | ||
credentials: { | ||
where: { | ||
id: credentialsId, | ||
}, | ||
select: { | ||
id: true, | ||
data: true, | ||
iv: true, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
if (!workspace || isReadWorkspaceFobidden(workspace, user)) | ||
throw new TRPCError({ | ||
code: 'NOT_FOUND', | ||
message: 'No workspace found', | ||
}) | ||
|
||
const credentials = workspace.credentials.at(0) | ||
|
||
if (!credentials) | ||
throw new TRPCError({ | ||
code: 'NOT_FOUND', | ||
message: 'No credentials found', | ||
}) | ||
|
||
const typebot = workspace.typebots.at(0) | ||
|
||
if (!typebot) | ||
throw new TRPCError({ | ||
code: 'NOT_FOUND', | ||
message: 'Typebot not found', | ||
}) | ||
|
||
const block = typebotSchema._def.schema.shape.groups | ||
.parse(workspace.typebots.at(0)?.groups) | ||
.flatMap((group) => group.blocks) | ||
.find((block) => block.id === blockId) | ||
|
||
if (!block || block.type !== IntegrationBlockType.OPEN_AI) | ||
throw new TRPCError({ | ||
code: 'NOT_FOUND', | ||
message: 'OpenAI block not found', | ||
}) | ||
|
||
const data = (await decrypt( | ||
credentials.data, | ||
credentials.iv | ||
)) as OpenAICredentials['data'] | ||
|
||
const config = new Configuration({ | ||
apiKey: data.apiKey, | ||
basePath: block.options.baseUrl, | ||
baseOptions: { | ||
headers: { | ||
'api-key': data.apiKey, | ||
}, | ||
}, | ||
defaultQueryParams: isNotEmpty(block.options.apiVersion) | ||
? new URLSearchParams({ | ||
'api-version': block.options.apiVersion, | ||
}) | ||
: undefined, | ||
}) | ||
|
||
const openai = new OpenAIApi(config) | ||
|
||
const response = await openai.listModels() | ||
|
||
const modelsData = (await response.json()) as ResponseTypes['listModels'] | ||
|
||
return { | ||
models: modelsData.data | ||
.sort((a, b) => b.created - a.created) | ||
.map((model) => model.id), | ||
} | ||
} | ||
) |
6 changes: 6 additions & 0 deletions
6
apps/builder/src/features/blocks/integrations/openai/api/router.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { router } from '@/helpers/server/trpc' | ||
import { listModels } from './listModels' | ||
|
||
export const openAIRouter = router({ | ||
listModels, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
27a5f4e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
viewer-v2 – ./apps/viewer
bii.bj
1stop.au
wasap.nl
x.cr8.ai
yobot.me
klujo.com
me.cr8.ai
sifuim.co
secretespiao.online
start.belenmotz.com
support.wawplus.com
survey1.digienge.io
surveys.essiell.com
test.botscientis.us
test.getreview.help
test.reventepro.com
typebot.stillio.app
typebot.stillio.com
vg.onewebcenter.com
wa.onewebcenter.com
web.draleticiah.com
whatsdigital.online
wordsandimagery.com
88584434.therpm.club
92109660.therpm.club
app.horadelucrar.com
assistent.m-vogel.de
ativandograna.online
bium.gratirabbit.com
bot.ansuraniphone.my
bot.barrettamario.it
bot.buenanoticia.fun
bot.conhecaojogo.com
bot.cotemeuplano.com
bot.gameincrivel.com
bot.gamesimples.club
bot.grupodojo.com.br
bot.jogoquelucra.com
bot.leadbooster.help
bot.mycompay.reviews
bot.socialcliques.me
cha.onewebcenter.com
chat.gnipharmahq.com
chat.hayurihijab.com
chat.jottagreens.com
chatbee.agfunnel.com
click.sevenoways.com
connect.growthguy.in
detetivepatricia.com
drapamela.gikpro.com
drgisellegarcia.site
forms.bonanza.design
hello.advergreen.com
infomakeracademy.com
kuiz.sistemniaga.com
leoborges-app.online
linspecteuremma.site
malayanboosterhq.com
viewer-v2-typebot-io.vercel.app
mdb.assessoria.rodrigo.progenbr.com
register.thailandmicespecialist.com
mdb.assessoria.desideri.progenbr.com
mdb.assessoria.fernanda.progenbr.com
mdb.assessoria.jbatista.progenbr.com
mdb.assessoria.mauricio.progenbr.com
mdb.evento.autocadastro.progenbr.com
form.shopmercedesbenzsouthorlando.com
mdb.evento.equipeinterna.progenbr.com
bot.studiotecnicoimmobiliaremerelli.it
mdb.assessoria.boaventura.progenbr.com
mdb.assessoria.jtrebesqui.progenbr.com
pesquisa.escolamodacomproposito.com.br
anamnese.clinicaramosodontologia.com.br
gabinete.baleia.formulario.progenbr.com
mdb.assessoria.carreirinha.progenbr.com
chrome-os-inquiry-system.itschromeos.com
mdb.assessoria.paulomarques.progenbr.com
viewer-v2-git-main-typebot-io.vercel.app
main-menu-for-itschromeos.itschromeos.com
mdb.assessoria.qrcode.ademir.progenbr.com
mdb.assessoria.qrcode.arthur.progenbr.com
mdb.assessoria.qrcode.danilo.progenbr.com
mdb.assessoria.qrcode.marcao.progenbr.com
mdb.assessoria.qrcode.marcio.progenbr.com
mdb.assessoria.qrcode.aloisio.progenbr.com
mdb.assessoria.qrcode.girotto.progenbr.com
mdb.assessoria.qrcode.marinho.progenbr.com
mdb.assessoria.qrcode.rodrigo.progenbr.com
mdb.assessoria.carlosalexandre.progenbr.com
mdb.assessoria.qrcode.desideri.progenbr.com
mdb.assessoria.qrcode.fernanda.progenbr.com
mdb.assessoria.qrcode.jbatista.progenbr.com
mdb.assessoria.qrcode.mauricio.progenbr.com
mdb.assessoria.fernanda.regional.progenbr.com
mdb.assessoria.qrcode.boaventura.progenbr.com
mdb.assessoria.qrcode.jtrebesqui.progenbr.com
mdb.assessoria.qrcode.carreirinha.progenbr.com
mdb.assessoria.qrcode.paulomarques.progenbr.com
mdb.assessoria.qrcode.carlosalexandre.progenbr.com
mdb.assessoria.qrcode.fernanda.regional.progenbr.com
27a5f4e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
builder-v2 – ./apps/builder
builder-v2-typebot-io.vercel.app
builder-v2-git-main-typebot-io.vercel.app
app.typebot.io
27a5f4e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
landing-page-v2 – ./apps/landing-page
landing-page-v2-git-main-typebot-io.vercel.app
landing-page-v2-typebot-io.vercel.app
home.typebot.io
www.typebot.io
www.get-typebot.com
get-typebot.com
27a5f4e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
docs – ./apps/docs
docs-typebot-io.vercel.app
docs-git-main-typebot-io.vercel.app
docs.typebot.io