2
0

(openai) Add gpt-4 models and temperature setting

Closes #406
This commit is contained in:
Baptiste Arnaud
2023-03-20 17:26:21 +01:00
parent efdcec1c0c
commit 4109a8489c
4 changed files with 61 additions and 5 deletions

View File

@@ -18,6 +18,8 @@ import { saveErrorLog } from '@/features/logs/saveErrorLog'
import { updateVariables } from '@/features/variables/updateVariables'
import { parseVariables } from '@/features/variables/parseVariables'
import { saveSuccessLog } from '@/features/logs/saveSuccessLog'
import { parseVariableNumber } from '@/features/variables/parseVariableNumber'
import { HTTPError } from 'got'
export const createChatCompletionOpenAI = async (
state: SessionState,
@@ -53,11 +55,16 @@ export const createChatCompletionOpenAI = async (
if (variablesTransformedToList.length > 0)
newSessionState = await updateVariables(state)(variablesTransformedToList)
const temperature = parseVariableNumber(newSessionState.typebot.variables)(
options.advancedSettings?.temperature
)
try {
const openai = new OpenAIApi(configuration)
const response = await openai.createChatCompletion({
model: options.model,
messages,
temperature,
})
const messageContent = response.data.choices.at(0)?.message?.content
const totalTokens = response.data.usage?.total_tokens
@@ -100,7 +107,12 @@ export const createChatCompletionOpenAI = async (
newSessionState,
}
} catch (err) {
console.error(err)
if (err instanceof HTTPError) {
console.error(err.response.body)
} else {
console.error(err)
}
const log = {
status: 'error',
description: 'OpenAI block returned error',

View File

@@ -0,0 +1,12 @@
import { Variable } from '@typebot.io/schemas'
import { parseGuessedValueType } from './parseGuessedValueType'
import { parseVariables } from './parseVariables'
export const parseVariableNumber =
(variables: Variable[]) =>
(input: number | `{{${string}}}` | undefined): number | undefined => {
if (typeof input === 'number' || input === undefined) return input
const parsedInput = parseGuessedValueType(parseVariables(variables)(input))
if (typeof parsedInput !== 'number') return undefined
return parsedInput
}