Skip to content

Commit

Permalink
refactor: chatdto.done => chatdto.code
Browse files Browse the repository at this point in the history
  • Loading branch information
pond918 committed May 29, 2023
1 parent 47132bc commit 987ff1c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
11 changes: 8 additions & 3 deletions src/bots/chat.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ import { nanoid } from 'nanoid'
export class ChatDto {
/** local msg id */
readonly id?: string
readonly done?: boolean
/** status code. empty means ok; positive means still processing; negative means no more processing */
readonly code?: number

/**
* @param code status code. empty means ok; positive means still processing; negative means no more processing
* @param options
*/
constructor(
public readonly prompt: string | string[],
done = true,
code = 0,
public readonly options: {
/** msg type: true: response, false: request */
resp?: boolean
Expand All @@ -21,6 +26,6 @@ export class ChatDto {
stateless?: boolean
} & Record<string, unknown> = {},
) {
done && ((this.id = nanoid()), (this.done = true))
!code && ((this.id = nanoid()), (this.code = 0))
}
}
10 changes: 5 additions & 5 deletions src/bots/huggingface/GradioBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ export default abstract class GradioBot extends LLMBot {
}

async _sendPrompt(prompt: ChatDto, streamCallback?: (msg: ChatDto) => void): Promise<ChatDto> {
let result: ChatDto = new ChatDto('', false)
let result: ChatDto = new ChatDto('', -1)
for (const key in this._fnIndexes) {
const fn_index = this._fnIndexes[key]
const resp = await this._sendFnIndex(fn_index, prompt, streamCallback)
resp && resp.done && resp.prompt && (result = resp)
resp && !resp.code && resp.prompt && (result = resp)
}
return result
}
Expand Down Expand Up @@ -102,12 +102,12 @@ export default abstract class GradioBot extends LLMBot {
if (event.rank > 0) {
// Waiting in queue
event.rank_eta = Math.floor(event.rank_eta)
streamCallback && streamCallback(new ChatDto('gradio.waiting', false))
streamCallback && streamCallback(new ChatDto('gradio.waiting', 1))
}
} else if (event.msg === 'process_generating') {
// Generating data
if (event.success && event.output.data) {
streamCallback && streamCallback(new ChatDto(this.parseData(fn_index, event.output.data), false))
streamCallback && streamCallback(new ChatDto(this.parseData(fn_index, event.output.data), 1))
} else {
reject(new Error(event.output.error))
}
Expand All @@ -117,7 +117,7 @@ export default abstract class GradioBot extends LLMBot {
const prompt = this.parseData(fn_index, event.output.data)
const resp = new ChatDto(
prompt,
fn_index == this._fnIndexes[this._fnIndexes.length - 1], // Only the last one is done
fn_index == this._fnIndexes[this._fnIndexes.length - 1] ? 0 : -1, // Only the last one is done
)
streamCallback && streamCallback(resp)
resolve(resp)
Expand Down
4 changes: 2 additions & 2 deletions src/storage/chat-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class ChatHistory {
// async append(msg: ChatDto & { prompt: string }) {
async append(msg: ChatDto) {
if (msg.options.compound) throw new Error('chat.history.append.compound.not.allowed')
if (!msg.done) throw new Error('chat.history.append.undone.not.allowed')
if (msg.code) throw new Error('chat.history.append.undone.not.allowed')

let branched = false

Expand Down Expand Up @@ -68,7 +68,7 @@ export class ChatHistory {
if (!(mid = m.options.lastMsgId)) break
}
}
const retMsg = new ChatDto(ret.reverse(), msg.done, { ...msg.options, compound: 1 })
const retMsg = new ChatDto(ret.reverse(), msg.code, { ...msg.options, compound: 1 })
_conversationKey && (retMsg.options._conversationKey = _conversationKey)
return retMsg
}
Expand Down

0 comments on commit 987ff1c

Please sign in to comment.