2023-12-13 10:22:02 +01:00
|
|
|
import { option, createAction } from '@typebot.io/forge'
|
|
|
|
import OpenAI, { ClientOptions } from 'openai'
|
2024-01-29 14:45:44 +01:00
|
|
|
import { defaultOpenAIOptions, maxToolCalls } from '../constants'
|
2024-01-19 08:05:38 +01:00
|
|
|
import { OpenAIStream, ToolCallPayload } from 'ai'
|
2023-12-13 10:22:02 +01:00
|
|
|
import { parseChatCompletionMessages } from '../helpers/parseChatCompletionMessages'
|
|
|
|
import { isDefined } from '@typebot.io/lib'
|
|
|
|
import { auth } from '../auth'
|
|
|
|
import { baseOptions } from '../baseOptions'
|
2024-01-29 14:45:44 +01:00
|
|
|
import {
|
|
|
|
ChatCompletionMessage,
|
|
|
|
ChatCompletionTool,
|
|
|
|
} from 'openai/resources/chat/completions'
|
2024-01-19 08:05:38 +01:00
|
|
|
import { parseToolParameters } from '../helpers/parseToolParameters'
|
2024-01-22 09:22:28 +01:00
|
|
|
import { executeFunction } from '@typebot.io/variables/executeFunction'
|
2023-12-13 10:22:02 +01:00
|
|
|
|
|
|
|
const nativeMessageContentSchema = {
|
2024-01-03 16:29:41 +01:00
|
|
|
content: option.string.layout({
|
|
|
|
inputType: 'textarea',
|
|
|
|
placeholder: 'Content',
|
|
|
|
}),
|
2023-12-13 10:22:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2024-01-29 14:45:44 +01:00
|
|
|
const parameterBase = {
|
2024-01-19 08:05:38 +01:00
|
|
|
name: option.string.layout({
|
|
|
|
label: 'Name',
|
|
|
|
placeholder: 'myVariable',
|
|
|
|
withVariableButton: false,
|
|
|
|
}),
|
|
|
|
description: option.string.layout({
|
|
|
|
label: 'Description',
|
|
|
|
withVariableButton: false,
|
|
|
|
}),
|
|
|
|
required: option.boolean.layout({
|
|
|
|
label: 'Is required?',
|
|
|
|
}),
|
2024-01-29 14:45:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export const toolParametersSchema = option
|
|
|
|
.array(
|
|
|
|
option.discriminatedUnion('type', [
|
|
|
|
option
|
|
|
|
.object({
|
|
|
|
type: option.literal('string'),
|
|
|
|
})
|
|
|
|
.extend(parameterBase),
|
|
|
|
option
|
|
|
|
.object({
|
|
|
|
type: option.literal('number'),
|
|
|
|
})
|
|
|
|
.extend(parameterBase),
|
|
|
|
option
|
|
|
|
.object({
|
|
|
|
type: option.literal('boolean'),
|
|
|
|
})
|
|
|
|
.extend(parameterBase),
|
|
|
|
option
|
|
|
|
.object({
|
|
|
|
type: option.literal('enum'),
|
|
|
|
values: option
|
|
|
|
.array(option.string)
|
|
|
|
.layout({ itemLabel: 'possible value' }),
|
|
|
|
})
|
|
|
|
.extend(parameterBase),
|
|
|
|
])
|
|
|
|
)
|
|
|
|
.layout({
|
|
|
|
accordion: 'Parameters',
|
|
|
|
itemLabel: 'parameter',
|
|
|
|
})
|
2024-01-19 08:05:38 +01:00
|
|
|
|
|
|
|
const functionToolItemSchema = option.object({
|
|
|
|
type: option.literal('function'),
|
|
|
|
name: option.string.layout({
|
|
|
|
label: 'Name',
|
|
|
|
placeholder: 'myFunctionName',
|
|
|
|
withVariableButton: false,
|
|
|
|
}),
|
|
|
|
description: option.string.layout({
|
|
|
|
label: 'Description',
|
|
|
|
placeholder: 'A brief description of what this function does.',
|
|
|
|
withVariableButton: false,
|
|
|
|
}),
|
2024-01-29 14:45:44 +01:00
|
|
|
parameters: toolParametersSchema,
|
2024-01-19 08:05:38 +01:00
|
|
|
code: option.string.layout({
|
|
|
|
inputType: 'code',
|
|
|
|
label: 'Code',
|
|
|
|
lang: 'javascript',
|
|
|
|
moreInfoTooltip:
|
|
|
|
'A javascript code snippet that can use the defined parameters. It should return a value.',
|
|
|
|
withVariableButton: false,
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
|
2023-12-13 10:22:02 +01:00
|
|
|
const dialogueMessageItemSchema = option.object({
|
|
|
|
role: option.literal('Dialogue'),
|
|
|
|
dialogueVariableId: option.string.layout({
|
2024-01-03 16:29:41 +01:00
|
|
|
inputType: 'variableDropdown',
|
2023-12-13 10:22:02 +01:00
|
|
|
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',
|
|
|
|
defaultValue: defaultOpenAIOptions.model,
|
|
|
|
fetcher: 'fetchModels',
|
|
|
|
}),
|
|
|
|
messages: option
|
|
|
|
.array(
|
|
|
|
option.discriminatedUnion('role', [
|
|
|
|
systemMessageItemSchema,
|
|
|
|
userMessageItemSchema,
|
|
|
|
assistantMessageItemSchema,
|
|
|
|
dialogueMessageItemSchema,
|
|
|
|
])
|
|
|
|
)
|
|
|
|
.layout({ accordion: 'Messages', itemLabel: 'message', isOrdered: true }),
|
2024-01-19 08:05:38 +01:00
|
|
|
tools: option
|
|
|
|
.array(option.discriminatedUnion('type', [functionToolItemSchema]))
|
|
|
|
.layout({ accordion: 'Tools', itemLabel: 'tool' }),
|
2023-12-13 10:22:02 +01:00
|
|
|
temperature: option.number.layout({
|
|
|
|
accordion: 'Advanced settings',
|
|
|
|
label: 'Temperature',
|
|
|
|
direction: 'row',
|
|
|
|
defaultValue: defaultOpenAIOptions.temperature,
|
|
|
|
}),
|
|
|
|
responseMapping: option
|
2024-02-14 08:59:05 +01:00
|
|
|
.saveResponseArray(['Message content', 'Total tokens'] as const)
|
2023-12-13 10:22:02 +01:00
|
|
|
.layout({
|
|
|
|
accordion: 'Save response',
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
|
|
|
|
export const createChatCompletion = createAction({
|
|
|
|
name: 'Create chat completion',
|
|
|
|
auth,
|
|
|
|
baseOptions,
|
2024-01-03 16:29:41 +01:00
|
|
|
options,
|
2023-12-13 10:22:02 +01:00
|
|
|
getSetVariableIds: (options) =>
|
|
|
|
options.responseMapping?.map((res) => res.variableId).filter(isDefined) ??
|
|
|
|
[],
|
|
|
|
fetchers: [
|
|
|
|
{
|
|
|
|
id: 'fetchModels',
|
|
|
|
dependencies: ['baseUrl', 'apiVersion'],
|
|
|
|
fetch: async ({ credentials, options }) => {
|
|
|
|
const baseUrl = options?.baseUrl ?? defaultOpenAIOptions.baseUrl
|
|
|
|
const config = {
|
|
|
|
apiKey: credentials.apiKey,
|
|
|
|
baseURL: baseUrl ?? defaultOpenAIOptions.baseUrl,
|
|
|
|
defaultHeaders: {
|
|
|
|
'api-key': credentials.apiKey,
|
|
|
|
},
|
|
|
|
defaultQuery: options?.apiVersion
|
|
|
|
? {
|
|
|
|
'api-version': options.apiVersion,
|
|
|
|
}
|
|
|
|
: undefined,
|
|
|
|
} satisfies ClientOptions
|
|
|
|
|
|
|
|
const openai = new OpenAI(config)
|
|
|
|
|
|
|
|
const models = await openai.models.list()
|
|
|
|
|
|
|
|
return (
|
|
|
|
models.data
|
|
|
|
.filter((model) => model.id.includes('gpt'))
|
|
|
|
.sort((a, b) => b.created - a.created)
|
|
|
|
.map((model) => model.id) ?? []
|
|
|
|
)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
run: {
|
|
|
|
server: async ({ credentials: { apiKey }, options, variables }) => {
|
|
|
|
const config = {
|
|
|
|
apiKey,
|
|
|
|
baseURL: options.baseUrl,
|
|
|
|
defaultHeaders: {
|
|
|
|
'api-key': apiKey,
|
|
|
|
},
|
|
|
|
defaultQuery: options.apiVersion
|
|
|
|
? {
|
|
|
|
'api-version': options.apiVersion,
|
|
|
|
}
|
|
|
|
: undefined,
|
|
|
|
} satisfies ClientOptions
|
|
|
|
|
|
|
|
const openai = new OpenAI(config)
|
|
|
|
|
2024-01-19 08:05:38 +01:00
|
|
|
const tools = options.tools
|
2024-01-29 14:45:44 +01:00
|
|
|
?.filter((t) => t.name && t.parameters)
|
|
|
|
.map((t) => ({
|
|
|
|
type: 'function',
|
|
|
|
function: {
|
|
|
|
name: t.name as string,
|
|
|
|
description: t.description,
|
|
|
|
parameters: parseToolParameters(t.parameters!),
|
|
|
|
},
|
|
|
|
})) satisfies ChatCompletionTool[] | undefined
|
2024-01-19 08:05:38 +01:00
|
|
|
|
|
|
|
const messages = parseChatCompletionMessages({ options, variables })
|
|
|
|
|
2024-01-29 14:45:44 +01:00
|
|
|
const body = {
|
2023-12-13 10:22:02 +01:00
|
|
|
model: options.model ?? defaultOpenAIOptions.model,
|
|
|
|
temperature: options.temperature
|
|
|
|
? Number(options.temperature)
|
|
|
|
: undefined,
|
2024-01-19 08:05:38 +01:00
|
|
|
messages,
|
2024-02-02 11:03:17 +01:00
|
|
|
tools: (tools?.length ?? 0) > 0 ? tools : undefined,
|
2024-01-29 14:45:44 +01:00
|
|
|
}
|
2023-12-13 10:22:02 +01:00
|
|
|
|
2024-01-29 14:45:44 +01:00
|
|
|
let totalTokens = 0
|
|
|
|
let message: ChatCompletionMessage
|
|
|
|
|
|
|
|
for (let i = 0; i < maxToolCalls; i++) {
|
|
|
|
const response = await openai.chat.completions.create(body)
|
|
|
|
|
|
|
|
message = response.choices[0].message
|
|
|
|
totalTokens += response.usage?.total_tokens || 0
|
|
|
|
|
|
|
|
if (!message.tool_calls) break
|
2024-01-19 08:05:38 +01:00
|
|
|
|
|
|
|
messages.push(message)
|
|
|
|
|
|
|
|
for (const toolCall of message.tool_calls) {
|
|
|
|
const name = toolCall.function?.name
|
|
|
|
if (!name) continue
|
|
|
|
const toolDefinition = options.tools?.find((t) => t.name === name)
|
2024-01-29 14:45:44 +01:00
|
|
|
if (!toolDefinition?.code || !toolDefinition.parameters) {
|
|
|
|
messages.push({
|
|
|
|
tool_call_id: toolCall.id,
|
|
|
|
role: 'tool',
|
|
|
|
content: 'Function not found',
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
2024-01-22 15:53:47 -03:00
|
|
|
const toolParams = Object.fromEntries(
|
|
|
|
toolDefinition.parameters.map(({ name }) => [name, null])
|
|
|
|
)
|
2024-01-22 09:22:28 +01:00
|
|
|
const toolArgs = toolCall.function?.arguments
|
|
|
|
? JSON.parse(toolCall.function?.arguments)
|
|
|
|
: undefined
|
|
|
|
if (!toolArgs) continue
|
|
|
|
const { output, newVariables } = await executeFunction({
|
|
|
|
variables: variables.list(),
|
2024-01-22 15:53:47 -03:00
|
|
|
args: { ...toolParams, ...toolArgs },
|
2024-01-22 09:22:28 +01:00
|
|
|
body: toolDefinition.code,
|
|
|
|
})
|
|
|
|
newVariables?.forEach((v) => variables.set(v.id, v.value))
|
|
|
|
|
2024-01-19 08:05:38 +01:00
|
|
|
messages.push({
|
|
|
|
tool_call_id: toolCall.id,
|
|
|
|
role: 'tool',
|
2024-01-22 09:22:28 +01:00
|
|
|
content: output,
|
2024-01-19 08:05:38 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-13 10:22:02 +01:00
|
|
|
options.responseMapping?.forEach((mapping) => {
|
|
|
|
if (!mapping.variableId) return
|
|
|
|
if (!mapping.item || mapping.item === 'Message content')
|
2024-01-19 08:05:38 +01:00
|
|
|
variables.set(mapping.variableId, message.content)
|
2023-12-13 10:22:02 +01:00
|
|
|
if (mapping.item === 'Total tokens')
|
2024-01-19 08:05:38 +01:00
|
|
|
variables.set(mapping.variableId, totalTokens)
|
2023-12-13 10:22:02 +01:00
|
|
|
})
|
|
|
|
},
|
|
|
|
stream: {
|
|
|
|
getStreamVariableId: (options) =>
|
2023-12-19 14:24:36 +01:00
|
|
|
options.responseMapping?.find(
|
|
|
|
(res) => res.item === 'Message content' || !res.item
|
|
|
|
)?.variableId,
|
2023-12-13 10:22:02 +01:00
|
|
|
run: async ({ credentials: { apiKey }, options, variables }) => {
|
|
|
|
const config = {
|
|
|
|
apiKey,
|
|
|
|
baseURL: options.baseUrl,
|
|
|
|
defaultHeaders: {
|
|
|
|
'api-key': apiKey,
|
|
|
|
},
|
|
|
|
defaultQuery: options.apiVersion
|
|
|
|
? {
|
|
|
|
'api-version': options.apiVersion,
|
|
|
|
}
|
|
|
|
: undefined,
|
|
|
|
} satisfies ClientOptions
|
|
|
|
|
|
|
|
const openai = new OpenAI(config)
|
|
|
|
|
2024-01-19 08:05:38 +01:00
|
|
|
const tools = options.tools
|
2024-01-29 14:45:44 +01:00
|
|
|
?.filter((t) => t.name && t.parameters)
|
|
|
|
.map((t) => ({
|
|
|
|
type: 'function',
|
|
|
|
function: {
|
|
|
|
name: t.name as string,
|
|
|
|
description: t.description,
|
|
|
|
parameters: parseToolParameters(t.parameters!),
|
|
|
|
},
|
|
|
|
})) satisfies ChatCompletionTool[] | undefined
|
2024-01-19 08:05:38 +01:00
|
|
|
|
|
|
|
const messages = parseChatCompletionMessages({ options, variables })
|
|
|
|
|
2023-12-13 10:22:02 +01:00
|
|
|
const response = await openai.chat.completions.create({
|
|
|
|
model: options.model ?? defaultOpenAIOptions.model,
|
|
|
|
temperature: options.temperature
|
|
|
|
? Number(options.temperature)
|
|
|
|
: undefined,
|
|
|
|
stream: true,
|
2024-01-19 08:05:38 +01:00
|
|
|
messages,
|
2024-02-02 11:03:17 +01:00
|
|
|
tools: (tools?.length ?? 0) > 0 ? tools : undefined,
|
2023-12-13 10:22:02 +01:00
|
|
|
})
|
|
|
|
|
2024-01-19 08:05:38 +01:00
|
|
|
return OpenAIStream(response, {
|
|
|
|
experimental_onToolCall: async (
|
|
|
|
call: ToolCallPayload,
|
|
|
|
appendToolCallMessage
|
|
|
|
) => {
|
|
|
|
for (const toolCall of call.tools) {
|
|
|
|
const name = toolCall.func?.name
|
|
|
|
if (!name) continue
|
|
|
|
const toolDefinition = options.tools?.find((t) => t.name === name)
|
2024-01-29 14:45:44 +01:00
|
|
|
if (!toolDefinition?.code || !toolDefinition.parameters) {
|
|
|
|
messages.push({
|
|
|
|
tool_call_id: toolCall.id,
|
|
|
|
role: 'tool',
|
|
|
|
content: 'Function not found',
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
2024-01-22 09:22:28 +01:00
|
|
|
|
|
|
|
const { output } = await executeFunction({
|
|
|
|
variables: variables.list(),
|
|
|
|
args:
|
|
|
|
typeof toolCall.func.arguments === 'string'
|
|
|
|
? JSON.parse(toolCall.func.arguments)
|
|
|
|
: toolCall.func.arguments,
|
|
|
|
body: toolDefinition.code,
|
|
|
|
})
|
|
|
|
|
|
|
|
// TO-DO: enable once we're out of edge runtime.
|
|
|
|
// newVariables?.forEach((v) => variables.set(v.id, v.value))
|
2024-01-19 08:05:38 +01:00
|
|
|
|
|
|
|
const newMessages = appendToolCallMessage({
|
|
|
|
tool_call_id: toolCall.id,
|
|
|
|
function_name: toolCall.func.name,
|
2024-01-22 09:22:28 +01:00
|
|
|
tool_call_result: output,
|
2024-01-19 08:05:38 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
return openai.chat.completions.create({
|
|
|
|
messages: [
|
|
|
|
...messages,
|
|
|
|
...newMessages,
|
|
|
|
] as OpenAI.Chat.Completions.ChatCompletionMessageParam[],
|
|
|
|
model: options.model ?? defaultOpenAIOptions.model,
|
|
|
|
stream: true,
|
|
|
|
tools,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
2023-12-13 10:22:02 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|