2023-03-09 08:46:36 +01:00
|
|
|
import { z } from 'zod'
|
2023-11-08 15:34:16 +01:00
|
|
|
import {
|
|
|
|
chatCompletionMessageCustomRoles,
|
|
|
|
chatCompletionMessageRoles,
|
|
|
|
chatCompletionResponseValues,
|
|
|
|
openAITasks,
|
|
|
|
} from './constants'
|
|
|
|
import { variableStringSchema } from '../../../utils'
|
|
|
|
import { blockBaseSchema, credentialsBaseSchema } from '../../shared'
|
|
|
|
import { IntegrationBlockType } from '../constants'
|
2023-09-01 16:19:59 +02:00
|
|
|
|
2023-03-09 08:46:36 +01:00
|
|
|
const openAIBaseOptionsSchema = z.object({
|
|
|
|
credentialsId: z.string().optional(),
|
2023-11-08 15:34:16 +01:00
|
|
|
baseUrl: z.string().optional(),
|
2023-09-01 16:19:59 +02:00
|
|
|
apiVersion: z.string().optional(),
|
2023-03-09 08:46:36 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
const initialOptionsSchema = z
|
|
|
|
.object({
|
|
|
|
task: z.undefined(),
|
|
|
|
})
|
|
|
|
.merge(openAIBaseOptionsSchema)
|
|
|
|
|
2023-11-08 15:34:16 +01:00
|
|
|
export const nativeMessageSchema = z.object({
|
2023-03-09 08:46:36 +01:00
|
|
|
id: z.string(),
|
|
|
|
role: z.enum(chatCompletionMessageRoles).optional(),
|
|
|
|
content: z.string().optional(),
|
2023-06-16 16:50:23 +02:00
|
|
|
name: z.string().optional(),
|
2023-03-09 08:46:36 +01:00
|
|
|
})
|
|
|
|
|
2023-11-08 15:34:16 +01:00
|
|
|
const messageSequenceItemSchema = z.object({
|
2023-03-13 16:28:08 +01:00
|
|
|
id: z.string(),
|
2023-11-08 15:34:16 +01:00
|
|
|
role: z.literal('Messages sequence ✨'),
|
2023-03-13 16:28:08 +01:00
|
|
|
content: z
|
|
|
|
.object({
|
|
|
|
assistantMessagesVariableId: z.string().optional(),
|
|
|
|
userMessagesVariableId: z.string().optional(),
|
|
|
|
})
|
|
|
|
.optional(),
|
|
|
|
})
|
|
|
|
|
2023-11-08 15:34:16 +01:00
|
|
|
const dialogueItemSchema = z.object({
|
|
|
|
id: z.string(),
|
|
|
|
role: z.literal('Dialogue'),
|
|
|
|
dialogueVariableId: z.string().optional(),
|
|
|
|
startsBy: z.enum(['user', 'assistant']).optional(),
|
|
|
|
})
|
|
|
|
|
2023-03-09 08:46:36 +01:00
|
|
|
const chatCompletionOptionsSchema = z
|
|
|
|
.object({
|
|
|
|
task: z.literal(openAITasks[0]),
|
2023-11-08 15:34:16 +01:00
|
|
|
model: z.string().optional(),
|
|
|
|
messages: z
|
|
|
|
.array(
|
|
|
|
z.union([
|
|
|
|
nativeMessageSchema,
|
|
|
|
messageSequenceItemSchema,
|
|
|
|
dialogueItemSchema,
|
|
|
|
])
|
|
|
|
)
|
|
|
|
.optional(),
|
2023-03-20 17:26:21 +01:00
|
|
|
advancedSettings: z
|
|
|
|
.object({
|
|
|
|
temperature: z.number().or(variableStringSchema).optional(),
|
|
|
|
})
|
|
|
|
.optional(),
|
2023-11-08 15:34:16 +01:00
|
|
|
responseMapping: z
|
|
|
|
.array(
|
|
|
|
z.object({
|
|
|
|
id: z.string(),
|
|
|
|
valueToExtract: z.preprocess(
|
|
|
|
(val) => (!val ? 'Message content' : val),
|
|
|
|
z.enum(chatCompletionResponseValues)
|
|
|
|
),
|
|
|
|
variableId: z.string().optional(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.optional(),
|
2023-03-09 08:46:36 +01:00
|
|
|
})
|
|
|
|
.merge(openAIBaseOptionsSchema)
|
|
|
|
|
|
|
|
const createImageOptionsSchema = z
|
|
|
|
.object({
|
|
|
|
task: z.literal(openAITasks[1]),
|
|
|
|
prompt: z.string().optional(),
|
|
|
|
advancedOptions: z.object({
|
|
|
|
size: z.enum(['256x256', '512x512', '1024x1024']).optional(),
|
|
|
|
}),
|
|
|
|
responseMapping: z.array(
|
|
|
|
z.object({
|
|
|
|
id: z.string(),
|
|
|
|
valueToExtract: z.enum(['Image URL']),
|
|
|
|
variableId: z.string().optional(),
|
|
|
|
})
|
|
|
|
),
|
|
|
|
})
|
|
|
|
.merge(openAIBaseOptionsSchema)
|
|
|
|
|
|
|
|
export const openAIBlockSchema = blockBaseSchema.merge(
|
|
|
|
z.object({
|
|
|
|
type: z.enum([IntegrationBlockType.OPEN_AI]),
|
2023-11-08 15:34:16 +01:00
|
|
|
options: z
|
|
|
|
.discriminatedUnion('task', [
|
|
|
|
initialOptionsSchema,
|
|
|
|
chatCompletionOptionsSchema,
|
|
|
|
createImageOptionsSchema,
|
|
|
|
])
|
|
|
|
.optional(),
|
2023-03-09 08:46:36 +01:00
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
export const openAICredentialsSchema = z
|
|
|
|
.object({
|
|
|
|
type: z.literal('openai'),
|
|
|
|
data: z.object({
|
|
|
|
apiKey: z.string(),
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
.merge(credentialsBaseSchema)
|
|
|
|
|
|
|
|
export type OpenAICredentials = z.infer<typeof openAICredentialsSchema>
|
|
|
|
export type OpenAIBlock = z.infer<typeof openAIBlockSchema>
|
|
|
|
export type ChatCompletionOpenAIOptions = z.infer<
|
|
|
|
typeof chatCompletionOptionsSchema
|
|
|
|
>
|
|
|
|
export type CreateImageOpenAIOptions = z.infer<typeof createImageOptionsSchema>
|