debug.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { get, post, ssePost } from './base'
  2. import type { IOnCompleted, IOnData, IOnError, IOnFile, IOnMessageEnd, IOnMessageReplace, IOnThought } from './base'
  3. import type { ChatPromptConfig, CompletionPromptConfig } from '@/models/debug'
  4. import type { ModelModeType } from '@/types/app'
  5. import type { ModelParameterRule } from '@/app/components/header/account-setting/model-provider-page/declarations'
  6. export type AutomaticRes = {
  7. prompt: string
  8. variables: string[]
  9. opening_statement: string
  10. error?: string
  11. }
  12. export type CodeGenRes = {
  13. code: string
  14. language: string[]
  15. error?: string
  16. }
  17. export const sendChatMessage = async (appId: string, body: Record<string, any>, { onData, onCompleted, onThought, onFile, onError, getAbortController, onMessageEnd, onMessageReplace }: {
  18. onData: IOnData
  19. onCompleted: IOnCompleted
  20. onFile: IOnFile
  21. onThought: IOnThought
  22. onMessageEnd: IOnMessageEnd
  23. onMessageReplace: IOnMessageReplace
  24. onError: IOnError
  25. getAbortController?: (abortController: AbortController) => void
  26. }) => {
  27. return ssePost(`apps/${appId}/chat-messages`, {
  28. body: {
  29. ...body,
  30. response_mode: 'streaming',
  31. },
  32. }, { onData, onCompleted, onThought, onFile, onError, getAbortController, onMessageEnd, onMessageReplace })
  33. }
  34. export const stopChatMessageResponding = async (appId: string, taskId: string) => {
  35. return post(`apps/${appId}/chat-messages/${taskId}/stop`)
  36. }
  37. export const sendCompletionMessage = async (appId: string, body: Record<string, any>, { onData, onCompleted, onError, onMessageReplace }: {
  38. onData: IOnData
  39. onCompleted: IOnCompleted
  40. onError: IOnError
  41. onMessageReplace: IOnMessageReplace
  42. }) => {
  43. return ssePost(`apps/${appId}/completion-messages`, {
  44. body: {
  45. ...body,
  46. response_mode: 'streaming',
  47. },
  48. }, { onData, onCompleted, onError, onMessageReplace })
  49. }
  50. export const fetchSuggestedQuestions = (appId: string, messageId: string, getAbortController?: any) => {
  51. return get(
  52. `apps/${appId}/chat-messages/${messageId}/suggested-questions`,
  53. {},
  54. {
  55. getAbortController,
  56. },
  57. )
  58. }
  59. export const fetchConversationMessages = (appId: string, conversation_id: string, getAbortController?: any) => {
  60. return get(`apps/${appId}/chat-messages`, {
  61. params: {
  62. conversation_id,
  63. },
  64. }, {
  65. getAbortController,
  66. })
  67. }
  68. export const generateRule = (body: Record<string, any>) => {
  69. return post<AutomaticRes>('/rule-generate', {
  70. body,
  71. })
  72. }
  73. export const generateRuleCode = (body: Record<string, any>) => {
  74. return post<CodeGenRes>('/rule-code-generate', {
  75. body,
  76. })
  77. }
  78. export const fetchModelParams = (providerName: string, modelId: string) => {
  79. return get(`workspaces/current/model-providers/${providerName}/models/parameter-rules`, {
  80. params: {
  81. model: modelId,
  82. },
  83. }) as Promise<{ data: ModelParameterRule[] }>
  84. }
  85. export const fetchPromptTemplate = ({
  86. appMode,
  87. mode,
  88. modelName,
  89. hasSetDataSet,
  90. }: { appMode: string; mode: ModelModeType; modelName: string; hasSetDataSet: boolean }) => {
  91. return get<Promise<{ chat_prompt_config: ChatPromptConfig; completion_prompt_config: CompletionPromptConfig; stop: [] }>>('/app/prompt-templates', {
  92. params: {
  93. app_mode: appMode,
  94. model_mode: mode,
  95. model_name: modelName,
  96. has_context: hasSetDataSet,
  97. },
  98. })
  99. }
  100. export const fetchTextGenerationMessage = ({
  101. appId,
  102. messageId,
  103. }: { appId: string; messageId: string }) => {
  104. return get<Promise<any>>(`/apps/${appId}/messages/${messageId}`)
  105. }