56 lines
1.6 KiB
TypeScript
56 lines
1.6 KiB
TypeScript
import { parseVariableNumber } from '@/features/variables/parseVariableNumber'
|
|
import { Connection } from '@planetscale/database'
|
|
import { decrypt } from '@typebot.io/lib/api/encryption'
|
|
import {
|
|
ChatCompletionOpenAIOptions,
|
|
OpenAICredentials,
|
|
} from '@typebot.io/schemas/features/blocks/integrations/openai'
|
|
import { SessionState } from '@typebot.io/schemas/features/chat'
|
|
import type {
|
|
ChatCompletionRequestMessage,
|
|
CreateChatCompletionRequest,
|
|
} from 'openai'
|
|
|
|
export const getChatCompletionStream =
|
|
(conn: Connection) =>
|
|
async (
|
|
state: SessionState,
|
|
options: ChatCompletionOpenAIOptions,
|
|
messages: ChatCompletionRequestMessage[]
|
|
) => {
|
|
if (!options.credentialsId) return
|
|
const credentials = (
|
|
await conn.execute('select data, iv from Credentials where id=?', [
|
|
options.credentialsId,
|
|
])
|
|
).rows.at(0) as { data: string; iv: string } | undefined
|
|
if (!credentials) {
|
|
console.error('Could not find credentials in database')
|
|
return
|
|
}
|
|
const { apiKey } = (await decrypt(
|
|
credentials.data,
|
|
credentials.iv
|
|
)) as OpenAICredentials['data']
|
|
|
|
const temperature = parseVariableNumber(state.typebot.variables)(
|
|
options.advancedSettings?.temperature
|
|
)
|
|
|
|
const res = await fetch('https://api.openai.com/v1/chat/completions', {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
Authorization: `Bearer ${apiKey}`,
|
|
},
|
|
method: 'POST',
|
|
body: JSON.stringify({
|
|
messages,
|
|
model: options.model,
|
|
temperature,
|
|
stream: true,
|
|
} satisfies CreateChatCompletionRequest),
|
|
})
|
|
|
|
return res.body
|
|
}
|