Commit 06f502a5 authored by Joel's avatar Joel

feat: to new api

parent d728cbb2
......@@ -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)
......@@ -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,
......
......@@ -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
}
......@@ -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]
......
import { PromptVariable } from '@/types/app'
import { PromptVariable, UserInputFormItem } from '@/types/app'
export function replaceVarWithValues(str: string, promptVariables: PromptVariable[], inputs: Record<string, any>) {
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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment