fix: cohere stream param does not work (#2907)
This commit is contained in:
parent
aa1f01f4fa
commit
1130979008
@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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 ?? ''
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user