2023-06-06 10:52:02 +08:00
|
|
|
import { get, post, ssePost } from './base'
|
2024-01-23 19:31:56 +08:00
|
|
|
import type { IOnCompleted, IOnData, IOnError, IOnFile, IOnMessageEnd, IOnMessageReplace, IOnThought } from './base'
|
2023-10-12 23:14:28 +08:00
|
|
|
import type { ChatPromptConfig, CompletionPromptConfig } from '@/models/debug'
|
|
|
|
import type { ModelModeType } from '@/types/app'
|
2024-01-02 23:42:00 +08:00
|
|
|
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
|
2024-07-23 19:51:38 +08:00
|
|
|
error?: string
|
2023-09-20 12:27:06 +08:00
|
|
|
}
|
|
|
|
|
2024-01-23 19:31:56 +08:00
|
|
|
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
|
2024-01-23 19:31:56 +08:00
|
|
|
onFile: IOnFile
|
|
|
|
onThought: IOnThought
|
2023-09-09 19:17:12 +08:00
|
|
|
onMessageEnd: IOnMessageEnd
|
2023-11-06 19:36:32 +08:00
|
|
|
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',
|
|
|
|
},
|
2024-01-23 19:31:56 +08:00
|
|
|
}, { 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`)
|
|
|
|
}
|
|
|
|
|
2023-11-06 19:36:32 +08:00
|
|
|
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
|
2023-11-06 19:36:32 +08:00
|
|
|
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',
|
|
|
|
},
|
2023-11-06 19:36:32 +08:00
|
|
|
}, { onData, onCompleted, onError, onMessageReplace })
|
2023-05-15 08:51:32 +08:00
|
|
|
}
|
|
|
|
|
2024-01-25 12:36:55 +08:00
|
|
|
export const fetchSuggestedQuestions = (appId: string, messageId: string, getAbortController?: any) => {
|
|
|
|
return get(
|
|
|
|
`apps/${appId}/chat-messages/${messageId}/suggested-questions`,
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
getAbortController,
|
|
|
|
},
|
|
|
|
)
|
2023-05-15 08:51:32 +08:00
|
|
|
}
|
|
|
|
|
2024-06-24 12:29:14 +08:00
|
|
|
export const fetchConversationMessages = (appId: string, conversation_id: string, getAbortController?: any) => {
|
2023-05-15 08:51:32 +08:00
|
|
|
return get(`apps/${appId}/chat-messages`, {
|
|
|
|
params: {
|
2023-06-06 10:52:02 +08:00
|
|
|
conversation_id,
|
|
|
|
},
|
2024-01-25 12:36:55 +08:00
|
|
|
}, {
|
|
|
|
getAbortController,
|
2023-06-06 10:52:02 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|
2023-08-12 00:57:13 +08:00
|
|
|
|
|
|
|
export const fetchModelParams = (providerName: string, modelId: string) => {
|
|
|
|
return get(`workspaces/current/model-providers/${providerName}/models/parameter-rules`, {
|
|
|
|
params: {
|
2024-01-02 23:42:00 +08:00
|
|
|
model: modelId,
|
2023-08-12 00:57:13 +08:00
|
|
|
},
|
2024-01-02 23:42:00 +08:00
|
|
|
}) as Promise<{ data: ModelParameterRule[] }>
|
2023-08-12 00:57:13 +08:00
|
|
|
}
|
2023-10-12 23:14:28 +08:00
|
|
|
|
|
|
|
export const fetchPromptTemplate = ({
|
|
|
|
appMode,
|
|
|
|
mode,
|
|
|
|
modelName,
|
|
|
|
hasSetDataSet,
|
|
|
|
}: { appMode: string; mode: ModelModeType; modelName: string; hasSetDataSet: boolean }) => {
|
2023-10-13 16:47:22 +08:00
|
|
|
return get<Promise<{ chat_prompt_config: ChatPromptConfig; completion_prompt_config: CompletionPromptConfig; stop: [] }>>('/app/prompt-templates', {
|
2023-10-12 23:14:28 +08:00
|
|
|
params: {
|
|
|
|
app_mode: appMode,
|
|
|
|
model_mode: mode,
|
|
|
|
model_name: modelName,
|
|
|
|
has_context: hasSetDataSet,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export const fetchTextGenerationMessge = ({
|
|
|
|
appId,
|
|
|
|
messageId,
|
|
|
|
}: { appId: string; messageId: string }) => {
|
2024-04-08 18:51:46 +08:00
|
|
|
return get<Promise<any>>(`/apps/${appId}/messages/${messageId}`)
|
2023-10-12 23:14:28 +08:00
|
|
|
}
|