dify/web/service/debug.ts

96 lines
3.1 KiB
TypeScript
Raw Normal View History

2023-06-06 10:52:02 +08:00
import { get, post, ssePost } from './base'
import type { IOnCompleted, IOnData, IOnError, IOnFile, IOnMessageEnd, IOnMessageReplace, IOnThought } from './base'
import type { ChatPromptConfig, CompletionPromptConfig } from '@/models/debug'
import type { ModelModeType } from '@/types/app'
import type { ModelParameterRule } from '@/app/components/header/account-setting/model-provider-page/declarations'
2023-09-20 12:27:06 +08:00
export type AutomaticRes = {
prompt: string
variables: string[]
opening_statement: string
}
export const sendChatMessage = async (appId: string, body: Record<string, any>, { onData, onCompleted, onThought, onFile, onError, getAbortController, onMessageEnd, onMessageReplace }: {
2023-05-15 08:51:32 +08:00
onData: IOnData
onCompleted: IOnCompleted
onFile: IOnFile
onThought: IOnThought
2023-09-09 19:17:12 +08:00
onMessageEnd: IOnMessageEnd
onMessageReplace: IOnMessageReplace
2023-06-06 10:52:02 +08:00
onError: IOnError
2023-05-15 08:51:32 +08:00
getAbortController?: (abortController: AbortController) => void
}) => {
return ssePost(`apps/${appId}/chat-messages`, {
body: {
...body,
2023-06-06 10:52:02 +08:00
response_mode: 'streaming',
},
}, { onData, onCompleted, onThought, onFile, onError, getAbortController, onMessageEnd, onMessageReplace })
2023-05-15 08:51:32 +08:00
}
2023-06-12 16:37:03 +08:00
export const stopChatMessageResponding = async (appId: string, taskId: string) => {
return post(`apps/${appId}/chat-messages/${taskId}/stop`)
}
export const sendCompletionMessage = async (appId: string, body: Record<string, any>, { onData, onCompleted, onError, onMessageReplace }: {
2023-05-15 08:51:32 +08:00
onData: IOnData
onCompleted: IOnCompleted
onError: IOnError
onMessageReplace: IOnMessageReplace
2023-05-15 08:51:32 +08:00
}) => {
return ssePost(`apps/${appId}/completion-messages`, {
body: {
...body,
2023-06-06 10:52:02 +08:00
response_mode: 'streaming',
},
}, { onData, onCompleted, onError, onMessageReplace })
2023-05-15 08:51:32 +08:00
}
export const fetchSuggestedQuestions = (appId: string, messageId: string) => {
return get(`apps/${appId}/chat-messages/${messageId}/suggested-questions`)
}
export const fetchConvesationMessages = (appId: string, conversation_id: string) => {
return get(`apps/${appId}/chat-messages`, {
params: {
2023-06-06 10:52:02 +08:00
conversation_id,
},
})
}
export const generateRule = (body: Record<string, any>) => {
2023-09-20 12:27:06 +08:00
return post<AutomaticRes>('/rule-generate', {
2023-06-06 10:52:02 +08:00
body,
2023-05-15 08:51:32 +08:00
})
}
export const fetchModelParams = (providerName: string, modelId: string) => {
return get(`workspaces/current/model-providers/${providerName}/models/parameter-rules`, {
params: {
model: modelId,
},
}) as Promise<{ data: ModelParameterRule[] }>
}
export const fetchPromptTemplate = ({
appMode,
mode,
modelName,
hasSetDataSet,
}: { appMode: string; mode: ModelModeType; modelName: string; hasSetDataSet: boolean }) => {
return get<Promise<{ chat_prompt_config: ChatPromptConfig; completion_prompt_config: CompletionPromptConfig; stop: [] }>>('/app/prompt-templates', {
params: {
app_mode: appMode,
model_mode: mode,
model_name: modelName,
has_context: hasSetDataSet,
},
})
}
export const fetchTextGenerationMessge = ({
appId,
messageId,
}: { appId: string; messageId: string }) => {
return get<Promise<{ message: [] }>>(`/apps/${appId}/messages/${messageId}`)
}