mirror of
https://github.com/langgenius/webapp-conversation.git
synced 2025-12-08 17:32:27 +08:00
feat: to new api
This commit is contained in:
@ -18,7 +18,7 @@ import Loading from '@/app/components/base/loading'
|
|||||||
import { replaceVarWithValues } from '@/utils/prompt'
|
import { replaceVarWithValues } from '@/utils/prompt'
|
||||||
import AppUnavailable from '@/app/components/app-unavailable'
|
import AppUnavailable from '@/app/components/app-unavailable'
|
||||||
import { APP_ID, API_KEY, APP_INFO, isShowPrompt, promptTemplate } from '@/config'
|
import { APP_ID, API_KEY, APP_INFO, isShowPrompt, promptTemplate } from '@/config'
|
||||||
|
import { userInputsFormToPromptVariables } from '@/utils/prompt'
|
||||||
|
|
||||||
const Main: FC = () => {
|
const Main: FC = () => {
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
@ -207,13 +207,13 @@ const Main: FC = () => {
|
|||||||
const isNotNewConversation = conversations.some(item => item.id === _conversationId)
|
const isNotNewConversation = conversations.some(item => item.id === _conversationId)
|
||||||
|
|
||||||
// fetch new conversation info
|
// 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)
|
setLocaleOnClient(APP_INFO.default_language, true)
|
||||||
setNewConversationInfo({
|
setNewConversationInfo({
|
||||||
name: t('app.chat.newChatDefaultName'),
|
name: t('app.chat.newChatDefaultName'),
|
||||||
introduction,
|
introduction,
|
||||||
})
|
})
|
||||||
|
const prompt_variables = userInputsFormToPromptVariables(user_input_form)
|
||||||
setPromptConfig({
|
setPromptConfig({
|
||||||
prompt_template: promptTemplate,
|
prompt_template: promptTemplate,
|
||||||
prompt_variables,
|
prompt_variables,
|
||||||
|
|||||||
@ -31,7 +31,7 @@
|
|||||||
"i18next-resources-to-backend": "^1.1.3",
|
"i18next-resources-to-backend": "^1.1.3",
|
||||||
"immer": "^9.0.19",
|
"immer": "^9.0.19",
|
||||||
"js-cookie": "^3.0.1",
|
"js-cookie": "^3.0.1",
|
||||||
"langgenius-client": "^1.0.0",
|
"langgenius-client": "1.1.1",
|
||||||
"negotiator": "^0.6.3",
|
"negotiator": "^0.6.3",
|
||||||
"next": "13.2.4",
|
"next": "13.2.4",
|
||||||
"react": "18.2.0",
|
"react": "18.2.0",
|
||||||
|
|||||||
25
types/app.ts
25
types/app.ts
@ -6,7 +6,8 @@ export type PromptVariable = {
|
|||||||
type: "string" | "number" | "select",
|
type: "string" | "number" | "select",
|
||||||
default?: string | number,
|
default?: string | number,
|
||||||
options?: string[]
|
options?: string[]
|
||||||
max_length: number
|
max_length?: number
|
||||||
|
required: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export type PromptConfig = {
|
export type PromptConfig = {
|
||||||
@ -14,6 +15,28 @@ export type PromptConfig = {
|
|||||||
prompt_variables: PromptVariable[],
|
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 const MessageRatings = ['like', 'dislike', null] as const
|
||||||
export type MessageRating = typeof MessageRatings[number]
|
export type MessageRating = typeof MessageRatings[number]
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { PromptVariable } from '@/types/app'
|
import { PromptVariable, UserInputFormItem } from '@/types/app'
|
||||||
|
|
||||||
export function replaceVarWithValues(str: string, promptVariables: PromptVariable[], inputs: Record<string, any>) {
|
export function replaceVarWithValues(str: string, promptVariables: PromptVariable[], inputs: Record<string, any>) {
|
||||||
return str.replace(/\{\{([^}]+)\}\}/g, (match, key) => {
|
return str.replace(/\{\{([^}]+)\}\}/g, (match, key) => {
|
||||||
@ -10,3 +10,31 @@ export function replaceVarWithValues(str: string, promptVariables: PromptVariabl
|
|||||||
return valueObj ? `{{${valueObj.key}}}` : match
|
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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user