diff --git a/app/api/utils/common.ts b/app/api/utils/common.ts index 87fadcc..5d8f7c3 100644 --- a/app/api/utils/common.ts +++ b/app/api/utils/common.ts @@ -18,4 +18,4 @@ export const setSession = (sessionId: string) => { return { 'Set-Cookie': `session_id=${sessionId}` } } -export const client = new ChatClient(API_KEY) \ No newline at end of file +export const client = new ChatClient(API_KEY) diff --git a/app/components/index.tsx b/app/components/index.tsx index 3376f7f..aea5bcb 100644 --- a/app/components/index.tsx +++ b/app/components/index.tsx @@ -18,7 +18,7 @@ import Loading from '@/app/components/base/loading' import { replaceVarWithValues } from '@/utils/prompt' import AppUnavailable from '@/app/components/app-unavailable' import { APP_ID, API_KEY, APP_INFO, isShowPrompt, promptTemplate } from '@/config' - +import { userInputsFormToPromptVariables } from '@/utils/prompt' const Main: FC = () => { const { t } = useTranslation() @@ -207,13 +207,13 @@ const Main: FC = () => { const isNotNewConversation = conversations.some(item => item.id === _conversationId) // fetch new conversation info - const { variables: prompt_variables, introduction }: any = appParams - + const { user_input_form, opening_statement: introduction }: any = appParams setLocaleOnClient(APP_INFO.default_language, true) setNewConversationInfo({ name: t('app.chat.newChatDefaultName'), introduction, }) + const prompt_variables = userInputsFormToPromptVariables(user_input_form) setPromptConfig({ prompt_template: promptTemplate, prompt_variables, diff --git a/package.json b/package.json index a6ee5f1..8c683e5 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "i18next-resources-to-backend": "^1.1.3", "immer": "^9.0.19", "js-cookie": "^3.0.1", - "langgenius-client": "^1.0.0", + "langgenius-client": "1.1.1", "negotiator": "^0.6.3", "next": "13.2.4", "react": "18.2.0", @@ -66,4 +66,4 @@ "postcss": "^8.4.21", "tailwindcss": "^3.2.7" } -} \ No newline at end of file +} diff --git a/types/app.ts b/types/app.ts index 5ed1a5f..cc3ea12 100644 --- a/types/app.ts +++ b/types/app.ts @@ -6,7 +6,8 @@ export type PromptVariable = { type: "string" | "number" | "select", default?: string | number, options?: string[] - max_length: number + max_length?: number + required: boolean } export type PromptConfig = { @@ -14,6 +15,28 @@ export type PromptConfig = { prompt_variables: PromptVariable[], } +export type TextTypeFormItem = { + label: string, + variable: string, + required: boolean + max_length: number +} + +export type SelectTypeFormItem = { + label: string, + variable: string, + required: boolean, + options: string[] +} +/** + * User Input Form Item + */ +export type UserInputFormItem = { + 'text-input': TextTypeFormItem +} | { + 'select': SelectTypeFormItem +} + export const MessageRatings = ['like', 'dislike', null] as const export type MessageRating = typeof MessageRatings[number] diff --git a/utils/prompt.ts b/utils/prompt.ts index e2f5d44..2b11efe 100644 --- a/utils/prompt.ts +++ b/utils/prompt.ts @@ -1,4 +1,4 @@ -import { PromptVariable } from '@/types/app' +import { PromptVariable, UserInputFormItem } from '@/types/app' export function replaceVarWithValues(str: string, promptVariables: PromptVariable[], inputs: Record) { return str.replace(/\{\{([^}]+)\}\}/g, (match, key) => { @@ -9,4 +9,32 @@ export function replaceVarWithValues(str: string, promptVariables: PromptVariabl const valueObj: PromptVariable | undefined = promptVariables.find(v => v.key === key) return valueObj ? `{{${valueObj.key}}}` : match }) +} + +export const userInputsFormToPromptVariables = (useInputs: UserInputFormItem[] | null) => { + if (!useInputs) return [] + const promptVariables: PromptVariable[] = [] + useInputs.forEach((item: any) => { + const type = item['text-input'] ? 'string' : 'select' + const content = type === 'string' ? item['text-input'] : item['select'] + if (type === 'string') { + promptVariables.push({ + key: content.variable, + name: content.label, + required: content.required, + type: 'string', + max_length: content.max_length, + options: [], + }) + } else { + promptVariables.push({ + key: content.variable, + name: content.label, + required: content.required, + type: 'select', + options: content.options, + }) + } + }) + return promptVariables } \ No newline at end of file