2024-01-25 16:35:16 +01:00
|
|
|
import { option, createAction } from '@typebot.io/forge'
|
|
|
|
import { isDefined } from '@typebot.io/lib'
|
|
|
|
import { auth } from '../auth'
|
2024-05-21 16:08:35 +02:00
|
|
|
import { createMistral } from '@ai-sdk/mistral'
|
2024-08-22 15:31:05 +02:00
|
|
|
import { generateText } from 'ai'
|
2024-06-18 12:13:00 +02:00
|
|
|
import { fetchModels } from '../helpers/fetchModels'
|
2024-07-15 14:32:42 +02:00
|
|
|
import { toolsSchema } from '@typebot.io/ai/schemas'
|
|
|
|
import { parseTools } from '@typebot.io/ai/parseTools'
|
|
|
|
import { maxToolRoundtrips } from '../constants'
|
|
|
|
import { parseChatCompletionMessages } from '@typebot.io/ai/parseChatCompletionMessages'
|
|
|
|
import { runChatCompletionStream } from '../helpers/runChatCompletionStream'
|
2024-01-25 16:35:16 +01:00
|
|
|
|
|
|
|
const nativeMessageContentSchema = {
|
|
|
|
content: option.string.layout({
|
|
|
|
inputType: 'textarea',
|
|
|
|
placeholder: 'Content',
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
const systemMessageItemSchema = option
|
|
|
|
.object({
|
|
|
|
role: option.literal('system'),
|
|
|
|
})
|
|
|
|
.extend(nativeMessageContentSchema)
|
|
|
|
|
|
|
|
const userMessageItemSchema = option
|
|
|
|
.object({
|
|
|
|
role: option.literal('user'),
|
|
|
|
})
|
|
|
|
.extend(nativeMessageContentSchema)
|
|
|
|
|
|
|
|
const assistantMessageItemSchema = option
|
|
|
|
.object({
|
|
|
|
role: option.literal('assistant'),
|
|
|
|
})
|
|
|
|
.extend(nativeMessageContentSchema)
|
|
|
|
|
|
|
|
const dialogueMessageItemSchema = option.object({
|
|
|
|
role: option.literal('Dialogue'),
|
|
|
|
dialogueVariableId: option.string.layout({
|
|
|
|
inputType: 'variableDropdown',
|
|
|
|
placeholder: 'Dialogue variable',
|
|
|
|
}),
|
|
|
|
startsBy: option.enum(['user', 'assistant']).layout({
|
|
|
|
label: 'starts by',
|
|
|
|
direction: 'row',
|
|
|
|
defaultValue: 'user',
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
|
|
|
|
export const options = option.object({
|
|
|
|
model: option.string.layout({
|
|
|
|
placeholder: 'Select a model',
|
|
|
|
fetcher: 'fetchModels',
|
|
|
|
}),
|
|
|
|
messages: option
|
|
|
|
.array(
|
|
|
|
option.discriminatedUnion('role', [
|
|
|
|
systemMessageItemSchema,
|
|
|
|
userMessageItemSchema,
|
|
|
|
assistantMessageItemSchema,
|
|
|
|
dialogueMessageItemSchema,
|
|
|
|
])
|
|
|
|
)
|
|
|
|
.layout({ accordion: 'Messages', itemLabel: 'message', isOrdered: true }),
|
2024-07-15 14:32:42 +02:00
|
|
|
tools: toolsSchema,
|
2024-01-25 16:35:16 +01:00
|
|
|
responseMapping: option.saveResponseArray(['Message content']).layout({
|
|
|
|
accordion: 'Save response',
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
|
|
|
|
export const createChatCompletion = createAction({
|
|
|
|
name: 'Create chat completion',
|
|
|
|
auth,
|
|
|
|
options,
|
2024-03-05 15:46:28 +01:00
|
|
|
turnableInto: [
|
|
|
|
{
|
2024-03-18 16:09:19 +01:00
|
|
|
blockId: 'openai',
|
2024-07-15 14:32:42 +02:00
|
|
|
transform: (opts) => ({
|
|
|
|
...opts,
|
|
|
|
model: undefined,
|
|
|
|
}),
|
2024-03-05 15:46:28 +01:00
|
|
|
},
|
2024-08-22 15:31:05 +02:00
|
|
|
{
|
|
|
|
blockId: 'groq',
|
|
|
|
transform: (opts) => ({
|
|
|
|
...opts,
|
|
|
|
model: undefined,
|
|
|
|
}),
|
|
|
|
},
|
2024-03-05 15:46:28 +01:00
|
|
|
{
|
2024-03-18 16:09:19 +01:00
|
|
|
blockId: 'together-ai',
|
2024-03-05 15:46:28 +01:00
|
|
|
},
|
2024-03-18 16:09:19 +01:00
|
|
|
{ blockId: 'open-router' },
|
2024-03-15 14:17:06 +01:00
|
|
|
{
|
2024-03-18 16:09:19 +01:00
|
|
|
blockId: 'anthropic',
|
2024-03-15 14:17:06 +01:00
|
|
|
transform: (options) => ({
|
|
|
|
...options,
|
2024-04-24 16:11:06 +02:00
|
|
|
model: undefined,
|
2024-03-15 14:17:06 +01:00
|
|
|
action: 'Create Chat Message',
|
2024-03-15 15:13:54 +01:00
|
|
|
responseMapping: options.responseMapping?.map((res: any) =>
|
|
|
|
res.item === 'Message content'
|
|
|
|
? { ...res, item: 'Message Content' }
|
|
|
|
: res
|
|
|
|
),
|
2024-03-15 14:17:06 +01:00
|
|
|
}),
|
|
|
|
},
|
2024-03-05 15:46:28 +01:00
|
|
|
],
|
2024-01-25 16:35:16 +01:00
|
|
|
getSetVariableIds: (options) =>
|
|
|
|
options.responseMapping?.map((res) => res.variableId).filter(isDefined) ??
|
|
|
|
[],
|
|
|
|
fetchers: [
|
|
|
|
{
|
|
|
|
id: 'fetchModels',
|
|
|
|
dependencies: [],
|
2024-06-18 12:13:00 +02:00
|
|
|
fetch: fetchModels,
|
2024-01-25 16:35:16 +01:00
|
|
|
},
|
|
|
|
],
|
|
|
|
run: {
|
|
|
|
server: async ({ credentials: { apiKey }, options, variables, logs }) => {
|
|
|
|
if (!options.model) return logs.add('No model selected')
|
|
|
|
|
2024-05-21 16:08:35 +02:00
|
|
|
const model = createMistral({
|
|
|
|
apiKey,
|
|
|
|
})(options.model)
|
|
|
|
|
|
|
|
const { text } = await generateText({
|
|
|
|
model,
|
2024-07-15 14:32:42 +02:00
|
|
|
messages: await parseChatCompletionMessages({
|
|
|
|
messages: options.messages,
|
|
|
|
variables,
|
|
|
|
isVisionEnabled: false,
|
|
|
|
shouldDownloadImages: false,
|
|
|
|
}),
|
|
|
|
tools: parseTools({ tools: options.tools, variables }),
|
|
|
|
maxToolRoundtrips: maxToolRoundtrips,
|
2024-01-25 16:35:16 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
options.responseMapping?.forEach((mapping) => {
|
|
|
|
if (!mapping.variableId) return
|
|
|
|
if (!mapping.item || mapping.item === 'Message content')
|
2024-05-21 16:08:35 +02:00
|
|
|
variables.set(mapping.variableId, text)
|
2024-01-25 16:35:16 +01:00
|
|
|
})
|
|
|
|
},
|
|
|
|
stream: {
|
|
|
|
getStreamVariableId: (options) =>
|
|
|
|
options.responseMapping?.find(
|
|
|
|
(res) => res.item === 'Message content' || !res.item
|
|
|
|
)?.variableId,
|
2024-07-15 14:32:42 +02:00
|
|
|
run: async ({ credentials: { apiKey }, options, variables }) =>
|
|
|
|
runChatCompletionStream({
|
|
|
|
credentials: { apiKey },
|
|
|
|
options,
|
|
|
|
variables,
|
|
|
|
}),
|
2024-01-25 16:35:16 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|