Skip to content

Commit

Permalink
Merge branch 'feat/model-provider-based-on-runtime' into deploy/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
iamjoel committed Jan 2, 2024
2 parents bdd69fd + 0b4a0ee commit 7552314
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 62 deletions.
22 changes: 0 additions & 22 deletions web/app/components/app/configuration/debug/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,6 @@ const Debug: FC<IDebug> = ({
}
},
onMessageEnd: (messageEnd) => {
// TODO
if (messageEnd.metadata?.annotation_reply) {
responseItem.id = messageEnd.id
responseItem.annotation = ({
Expand Down Expand Up @@ -382,27 +381,6 @@ const Debug: FC<IDebug> = ({
onMessageReplace: (messageReplace) => {
responseItem.content = messageReplace.answer
},
onAnnotationReply: (annotationReply) => {
// TODO: temp debug
responseItem.id = annotationReply.id
responseItem.content = annotationReply.answer
responseItem.annotation = ({
id: annotationReply.annotation_id,
authorName: annotationReply.annotation_author_name,
} as AnnotationType)
const newListWithAnswer = produce(
getChatList().filter(item => item.id !== responseItem.id && item.id !== placeholderAnswerId),
(draft) => {
if (!draft.find(item => item.id === questionId))
draft.push({ ...questionItem })

draft.push({
...responseItem,
id: annotationReply.id,
})
})
setChatList(newListWithAnswer)
},
onError() {
setResponsingFalse()
// role back placeholder answer
Expand Down
50 changes: 26 additions & 24 deletions web/app/components/share/chat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import Confirm from '@/app/components/base/confirm'
import type { VisionFile, VisionSettings } from '@/types/app'
import { Resolution, TransferMethod } from '@/types/app'
import { fetchFileUploadConfig } from '@/service/common'
import type { Annotation as AnnotationType } from '@/models/log'

export type IMainProps = {
isInstalledApp?: boolean
Expand Down Expand Up @@ -582,23 +583,40 @@ const Main: FC<IMainProps> = ({
}
setResponsingFalse()
},
onMessageEnd: isInstalledApp
? (messageEnd) => {
if (!isInstalledApp)
return
responseItem.citation = messageEnd.retriever_resources

onMessageEnd: (messageEnd) => {
if (messageEnd.metadata?.annotation_reply) {
responseItem.id = messageEnd.id
responseItem.annotation = ({
id: messageEnd.metadata.annotation_reply.id,
authorName: messageEnd.metadata.annotation_reply.account.name,
} as AnnotationType)
const newListWithAnswer = produce(
getChatList().filter(item => item.id !== responseItem.id && item.id !== placeholderAnswerId),
(draft) => {
if (!draft.find(item => item.id === questionId))
draft.push({ ...questionItem })

draft.push({ ...responseItem })
draft.push({
...responseItem,
})
})
setChatList(newListWithAnswer)
return
}
: undefined,
// not support show citation
// responseItem.citation = messageEnd.retriever_resources
if (!isInstalledApp)
return
const newListWithAnswer = produce(
getChatList().filter(item => item.id !== responseItem.id && item.id !== placeholderAnswerId),
(draft) => {
if (!draft.find(item => item.id === questionId))
draft.push({ ...questionItem })

draft.push({ ...responseItem })
})
setChatList(newListWithAnswer)
},
onMessageReplace: (messageReplace) => {
if (isInstalledApp) {
responseItem.content = messageReplace.answer
Expand All @@ -615,22 +633,6 @@ const Main: FC<IMainProps> = ({
))
}
},
onAnnotationReply: (annotationReply) => {
responseItem.content = annotationReply.answer
const newListWithAnswer = produce(
getChatList().filter(item => item.id !== responseItem.id && item.id !== placeholderAnswerId),
(draft) => {
if (!draft.find(item => item.id === questionId))
draft.push({ ...questionItem })

draft.push({
...responseItem,
id: annotationReply.id,
})
})
setChatList(newListWithAnswer)
tempNewConversationId = annotationReply.conversation_id
},
onError() {
setResponsingFalse()
// role back placeholder answer
Expand Down
12 changes: 4 additions & 8 deletions web/service/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ type IOtherOptions = {
onThought?: IOnThought
onMessageEnd?: IOnMessageEnd
onMessageReplace?: IOnMessageReplace
onAnnotationReply?: IOnAnnotationReply
onError?: IOnError
onCompleted?: IOnCompleted // for stream
getAbortController?: (abortController: AbortController) => void
Expand Down Expand Up @@ -81,7 +80,7 @@ export function format(text: string) {
return res.replaceAll('\n', '<br/>').replaceAll('```', '')
}

const handleStream = (response: Response, onData: IOnData, onCompleted?: IOnCompleted, onThought?: IOnThought, onMessageEnd?: IOnMessageEnd, onMessageReplace?: IOnMessageReplace, onAnnotationReply?: IOnAnnotationReply) => {
const handleStream = (response: Response, onData: IOnData, onCompleted?: IOnCompleted, onThought?: IOnThought, onMessageEnd?: IOnMessageEnd, onMessageReplace?: IOnMessageReplace) => {
if (!response.ok)
throw new Error('Network response was not ok')

Expand Down Expand Up @@ -137,14 +136,12 @@ const handleStream = (response: Response, onData: IOnData, onCompleted?: IOnComp
onThought?.(bufferObj as ThoughtItem)
}
else if (bufferObj.event === 'message_end') {
console.log(bufferObj)
onMessageEnd?.(bufferObj as MessageEnd)
}
else if (bufferObj.event === 'message_replace') {
onMessageReplace?.(bufferObj as MessageReplace)
}
else if (bufferObj.event === 'annotation') {
onAnnotationReply?.(bufferObj as AnnotationReply)
}
}
})
buffer = lines[lines.length - 1]
Expand Down Expand Up @@ -350,7 +347,7 @@ export const upload = (options: any, isPublicAPI?: boolean, url?: string): Promi
})
}

export const ssePost = (url: string, fetchOptions: FetchOptionType, { isPublicAPI = false, onData, onCompleted, onThought, onMessageEnd, onMessageReplace, onAnnotationReply, onError, getAbortController }: IOtherOptions) => {
export const ssePost = (url: string, fetchOptions: FetchOptionType, { isPublicAPI = false, onData, onCompleted, onThought, onMessageEnd, onMessageReplace, onError, getAbortController }: IOtherOptions) => {
const abortController = new AbortController()

const options = Object.assign({}, baseOptions, {
Expand Down Expand Up @@ -382,14 +379,13 @@ export const ssePost = (url: string, fetchOptions: FetchOptionType, { isPublicAP
}
return handleStream(res, (str: string, isFirstMessage: boolean, moreInfo: IOnDataMoreInfo) => {
if (moreInfo.errorMessage) {
// debugger
onError?.(moreInfo.errorMessage, moreInfo.errorCode)
if (moreInfo.errorMessage !== 'AbortError: The user aborted a request.')
Toast.notify({ type: 'error', message: moreInfo.errorMessage })
return
}
onData?.(str, isFirstMessage, moreInfo)
}, onCompleted, onThought, onMessageEnd, onMessageReplace, onAnnotationReply)
}, onCompleted, onThought, onMessageEnd, onMessageReplace)
}).catch((e) => {
if (e.toString() !== 'AbortError: The user aborted a request.')
Toast.notify({ type: 'error', message: e })
Expand Down
7 changes: 3 additions & 4 deletions web/service/debug.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IOnAnnotationReply, IOnCompleted, IOnData, IOnError, IOnMessageEnd, IOnMessageReplace } from './base'
import type { IOnCompleted, IOnData, IOnError, IOnMessageEnd, IOnMessageReplace } from './base'
import { get, post, ssePost } from './base'
import type { ChatPromptConfig, CompletionPromptConfig } from '@/models/debug'
import type { ModelModeType } from '@/types/app'
Expand All @@ -10,12 +10,11 @@ export type AutomaticRes = {
opening_statement: string
}

export const sendChatMessage = async (appId: string, body: Record<string, any>, { onData, onCompleted, onError, getAbortController, onMessageEnd, onMessageReplace, onAnnotationReply }: {
export const sendChatMessage = async (appId: string, body: Record<string, any>, { onData, onCompleted, onError, getAbortController, onMessageEnd, onMessageReplace }: {
onData: IOnData
onCompleted: IOnCompleted
onMessageEnd: IOnMessageEnd
onMessageReplace: IOnMessageReplace
onAnnotationReply: IOnAnnotationReply
onError: IOnError
getAbortController?: (abortController: AbortController) => void
}) => {
Expand All @@ -24,7 +23,7 @@ export const sendChatMessage = async (appId: string, body: Record<string, any>,
...body,
response_mode: 'streaming',
},
}, { onData, onCompleted, onError, getAbortController, onMessageEnd, onMessageReplace, onAnnotationReply })
}, { onData, onCompleted, onError, getAbortController, onMessageEnd, onMessageReplace })
}

export const stopChatMessageResponding = async (appId: string, taskId: string) => {
Expand Down
7 changes: 3 additions & 4 deletions web/service/share.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IOnAnnotationReply, IOnCompleted, IOnData, IOnError, IOnMessageEnd, IOnMessageReplace } from './base'
import type { IOnCompleted, IOnData, IOnError, IOnMessageEnd, IOnMessageReplace } from './base'
import {
del as consoleDel, get as consoleGet, patch as consolePatch, post as consolePost,
delPublic as del, getPublic as get, patchPublic as patch, postPublic as post, ssePost,
Expand All @@ -22,21 +22,20 @@ function getUrl(url: string, isInstalledApp: boolean, installedAppId: string) {
return isInstalledApp ? `installed-apps/${installedAppId}/${url.startsWith('/') ? url.slice(1) : url}` : url
}

export const sendChatMessage = async (body: Record<string, any>, { onData, onCompleted, onError, getAbortController, onMessageEnd, onMessageReplace, onAnnotationReply }: {
export const sendChatMessage = async (body: Record<string, any>, { onData, onCompleted, onError, getAbortController, onMessageEnd, onMessageReplace }: {
onData: IOnData
onCompleted: IOnCompleted
onError: IOnError
onMessageEnd?: IOnMessageEnd
onMessageReplace?: IOnMessageReplace
onAnnotationReply: IOnAnnotationReply
getAbortController?: (abortController: AbortController) => void
}, isInstalledApp: boolean, installedAppId = '') => {
return ssePost(getUrl('chat-messages', isInstalledApp, installedAppId), {
body: {
...body,
response_mode: 'streaming',
},
}, { onData, onCompleted, isPublicAPI: !isInstalledApp, onError, getAbortController, onMessageEnd, onMessageReplace, onAnnotationReply })
}, { onData, onCompleted, isPublicAPI: !isInstalledApp, onError, getAbortController, onMessageEnd, onMessageReplace })
}

export const stopChatMessageResponding = async (appId: string, taskId: string, isInstalledApp: boolean, installedAppId = '') => {
Expand Down

0 comments on commit 7552314

Please sign in to comment.