@ -16,6 +16,7 @@ import {
|
||||
} from '@chakra-ui/react'
|
||||
import { TextLink } from '@/components/TextLink'
|
||||
import { ChatCompletionResponseItem } from './ChatCompletionResponseItem'
|
||||
import { NumberInput } from '@/components/inputs'
|
||||
|
||||
const apiReferenceUrl =
|
||||
'https://platform.openai.com/docs/api-reference/chat/create'
|
||||
@ -43,6 +44,18 @@ export const OpenAIChatCompletionSettings = ({
|
||||
})
|
||||
}
|
||||
|
||||
const updateTemperature = (
|
||||
temperature: number | `{{${string}}}` | undefined
|
||||
) => {
|
||||
onOptionsChange({
|
||||
...options,
|
||||
advancedSettings: {
|
||||
...options.advancedSettings,
|
||||
temperature,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const updateResponseMapping = (
|
||||
responseMapping: typeof options.responseMapping
|
||||
) => {
|
||||
@ -61,6 +74,11 @@ export const OpenAIChatCompletionSettings = ({
|
||||
</TextLink>{' '}
|
||||
to better understand the available options.
|
||||
</Text>
|
||||
<DropdownList
|
||||
currentItem={options.model}
|
||||
items={chatCompletionModels}
|
||||
onItemSelect={updateModel}
|
||||
/>
|
||||
<Accordion allowToggle allowMultiple>
|
||||
<AccordionItem>
|
||||
<AccordionButton>
|
||||
@ -88,10 +106,14 @@ export const OpenAIChatCompletionSettings = ({
|
||||
<AccordionIcon />
|
||||
</AccordionButton>
|
||||
<AccordionPanel>
|
||||
<DropdownList
|
||||
currentItem={options.model}
|
||||
items={chatCompletionModels}
|
||||
onItemSelect={updateModel}
|
||||
<NumberInput
|
||||
label="Temperature"
|
||||
placeholder="1"
|
||||
max={2}
|
||||
min={0}
|
||||
step={0.1}
|
||||
defaultValue={options.advancedSettings?.temperature}
|
||||
onValueChange={updateTemperature}
|
||||
/>
|
||||
</AccordionPanel>
|
||||
</AccordionItem>
|
||||
|
@ -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',
|
||||
|
12
apps/viewer/src/features/variables/parseVariableNumber.ts
Normal file
12
apps/viewer/src/features/variables/parseVariableNumber.ts
Normal 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
|
||||
}
|
@ -1,10 +1,15 @@
|
||||
import { z } from 'zod'
|
||||
import { variableStringSchema } from '../../utils'
|
||||
import { blockBaseSchema, credentialsBaseSchema } from '../baseSchemas'
|
||||
import { IntegrationBlockType } from './enums'
|
||||
|
||||
export const openAITasks = ['Create chat completion', 'Create image'] as const
|
||||
|
||||
export const chatCompletionModels = [
|
||||
'gpt-4',
|
||||
'gpt-4-0314',
|
||||
'gpt-4-32k',
|
||||
'gpt-4-32k-0314',
|
||||
'gpt-3.5-turbo',
|
||||
'gpt-3.5-turbo-0301',
|
||||
] as const
|
||||
@ -58,6 +63,11 @@ const chatCompletionOptionsSchema = z
|
||||
messages: z.array(
|
||||
z.union([chatCompletionMessageSchema, chatCompletionCustomMessageSchema])
|
||||
),
|
||||
advancedSettings: z
|
||||
.object({
|
||||
temperature: z.number().or(variableStringSchema).optional(),
|
||||
})
|
||||
.optional(),
|
||||
responseMapping: z.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
|
Reference in New Issue
Block a user