fix: cohere stream param does not work (#2907)

This commit is contained in:
Louis 2024-05-15 17:27:37 +07:00 committed by GitHub
parent aa1f01f4fa
commit 1130979008
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 13 deletions

View File

@ -68,6 +68,10 @@ export function requestInference(
let cachedLines = '' let cachedLines = ''
for (const line of lines) { for (const line of lines) {
try { try {
if (transformResponse) {
content += transformResponse(line)
subscriber.next(content ?? '')
} else {
const toParse = cachedLines + line const toParse = cachedLines + line
if (!line.includes('data: [DONE]')) { if (!line.includes('data: [DONE]')) {
const data = JSON.parse(toParse.replace('data: ', '')) const data = JSON.parse(toParse.replace('data: ', ''))
@ -77,6 +81,7 @@ export function requestInference(
} }
if (content !== '') subscriber.next(content) if (content !== '') subscriber.next(content)
} }
}
} catch { } catch {
cachedLines = line cachedLines = line
} }

View File

@ -26,8 +26,8 @@ enum RoleType {
type CoherePayloadType = { type CoherePayloadType = {
chat_history?: Array<{ role: RoleType; message: string }> chat_history?: Array<{ role: RoleType; message: string }>
message?: string, message?: string
preamble?: string, preamble?: string
} }
/** /**
@ -82,18 +82,24 @@ export default class JanInferenceCohereExtension extends RemoteOAIEngine {
if (payload.messages.length === 0) { if (payload.messages.length === 0) {
return {} return {}
} }
const { messages, ...params } = payload
const convertedData: CoherePayloadType = { const convertedData: CoherePayloadType = {
...params,
chat_history: [], chat_history: [],
message: '', message: '',
} }
payload.messages.forEach((item, index) => { messages.forEach((item, index) => {
// Assign the message of the last item to the `message` property // Assign the message of the last item to the `message` property
if (index === payload.messages.length - 1) { if (index === messages.length - 1) {
convertedData.message = item.content as string convertedData.message = item.content as string
return return
} }
if (item.role === ChatCompletionRole.User) { if (item.role === ChatCompletionRole.User) {
convertedData.chat_history.push({ role: RoleType.user, message: item.content as string }) convertedData.chat_history.push({
role: RoleType.user,
message: item.content as string,
})
} else if (item.role === ChatCompletionRole.Assistant) { } else if (item.role === ChatCompletionRole.Assistant) {
convertedData.chat_history.push({ convertedData.chat_history.push({
role: RoleType.chatbot, role: RoleType.chatbot,
@ -106,5 +112,7 @@ export default class JanInferenceCohereExtension extends RemoteOAIEngine {
return convertedData return convertedData
} }
transformResponse = (data: any) => data.text transformResponse = (data: any) => {
return typeof data === 'object' ? data.text : JSON.parse(data).text ?? ''
}
} }