2024-03-05 10:56:54 +01:00
|
|
|
import { createAction } from '@typebot.io/forge'
|
|
|
|
import { auth } from '../auth'
|
|
|
|
import { parseChatCompletionOptions } from '@typebot.io/openai-block/shared/parseChatCompletionOptions'
|
|
|
|
import { getChatCompletionSetVarIds } from '@typebot.io/openai-block/shared/getChatCompletionSetVarIds'
|
|
|
|
import { getChatCompletionStreamVarId } from '@typebot.io/openai-block/shared/getChatCompletionStreamVarId'
|
|
|
|
import { runChatCompletion } from '@typebot.io/openai-block/shared/runChatCompletion'
|
|
|
|
import { runChatCompletionStream } from '@typebot.io/openai-block/shared/runChatCompletionStream'
|
|
|
|
import { defaultOpenRouterOptions } from '../constants'
|
2024-04-05 09:01:16 +02:00
|
|
|
import ky from 'ky'
|
2024-03-05 10:56:54 +01:00
|
|
|
import { ModelsResponse } from '../types'
|
|
|
|
|
|
|
|
export const createChatCompletion = createAction({
|
|
|
|
name: 'Create chat completion',
|
|
|
|
auth,
|
2024-03-05 15:46:28 +01:00
|
|
|
turnableInto: [
|
|
|
|
{
|
2024-03-18 16:09:19 +01:00
|
|
|
blockId: 'openai',
|
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: 'mistral' },
|
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-03-05 10:56:54 +01:00
|
|
|
options: parseChatCompletionOptions({
|
|
|
|
modelFetchId: 'fetchModels',
|
|
|
|
}),
|
|
|
|
getSetVariableIds: getChatCompletionSetVarIds,
|
|
|
|
fetchers: [
|
|
|
|
{
|
|
|
|
id: 'fetchModels',
|
|
|
|
dependencies: [],
|
|
|
|
fetch: async () => {
|
2024-04-05 09:01:16 +02:00
|
|
|
const response = await ky
|
2024-03-05 10:56:54 +01:00
|
|
|
.get(defaultOpenRouterOptions.baseUrl + '/models')
|
|
|
|
.json<ModelsResponse>()
|
|
|
|
|
|
|
|
return response.data.map((model) => ({
|
|
|
|
value: model.id,
|
|
|
|
label: model.name,
|
|
|
|
}))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
run: {
|
|
|
|
server: (params) =>
|
|
|
|
runChatCompletion({
|
|
|
|
...params,
|
|
|
|
config: { baseUrl: defaultOpenRouterOptions.baseUrl },
|
|
|
|
}),
|
|
|
|
stream: {
|
|
|
|
getStreamVariableId: getChatCompletionStreamVarId,
|
|
|
|
run: (params) =>
|
|
|
|
runChatCompletionStream({
|
|
|
|
...params,
|
|
|
|
config: { baseUrl: defaultOpenRouterOptions.baseUrl },
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|