Add cohere preformatting
This commit is contained in:
@@ -1525,11 +1525,23 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model'
|
|||||||
let lastChatPrompt = ''
|
let lastChatPrompt = ''
|
||||||
let preamble = ''
|
let preamble = ''
|
||||||
|
|
||||||
const lastChat = formated[formated.length-1]
|
let lastChat = formated[formated.length-1]
|
||||||
if(lastChat.role === 'user'){
|
if(lastChat.role === 'user'){
|
||||||
lastChatPrompt = lastChat.content
|
lastChatPrompt = lastChat.content
|
||||||
formated.pop()
|
formated.pop()
|
||||||
}
|
}
|
||||||
|
else{
|
||||||
|
while(lastChat.role !== 'user'){
|
||||||
|
lastChat = formated.pop()
|
||||||
|
if(!lastChat){
|
||||||
|
return {
|
||||||
|
type: 'fail',
|
||||||
|
result: 'Cohere requires a user message to generate a response'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lastChatPrompt = (lastChat.role === 'user' ? '' : `${lastChat.role}: `) + '\n' + lastChat.content + lastChatPrompt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const firstChat = formated[0]
|
const firstChat = formated[0]
|
||||||
if(firstChat.role === 'system'){
|
if(firstChat.role === 'system'){
|
||||||
@@ -1537,29 +1549,36 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model'
|
|||||||
formated.shift()
|
formated.shift()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//reformat chat
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
let body = {
|
let body = {
|
||||||
message: lastChatPrompt,
|
message: lastChatPrompt,
|
||||||
chat_history: formated.map((v) => {
|
chat_history: formated.map((v) => {
|
||||||
if(v.role === 'assistant'){
|
if(v.role === 'assistant'){
|
||||||
return {
|
return {
|
||||||
role: 'CHATBOT',
|
role: 'CHATBOT',
|
||||||
content: v.content
|
message: v.content
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(v.role === 'system'){
|
if(v.role === 'system'){
|
||||||
return {
|
return {
|
||||||
role: 'SYSTEM',
|
role: 'SYSTEM',
|
||||||
content: v.content
|
message: v.content
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(v.role === 'user'){
|
if(v.role === 'user'){
|
||||||
return {
|
return {
|
||||||
role: 'USER',
|
role: 'USER',
|
||||||
content: v.content
|
message: v.content
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
}).filter((v) => v !== null),
|
}).filter((v) => v !== null).filter((v) => {
|
||||||
|
return v.message
|
||||||
|
}),
|
||||||
temperature: temperature,
|
temperature: temperature,
|
||||||
k: db.top_k,
|
k: db.top_k,
|
||||||
p: (db.top_p > 0.99) ? 0.99 : (db.top_p < 0.01) ? 0.01 : db.top_p,
|
p: (db.top_p > 0.99) ? 0.99 : (db.top_p < 0.01) ? 0.01 : db.top_p,
|
||||||
@@ -1568,9 +1587,16 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model'
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(preamble){
|
if(preamble){
|
||||||
|
if(body.chat_history.length > 0){
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
body.preamble = preamble
|
body.preamble = preamble
|
||||||
}
|
}
|
||||||
|
else{
|
||||||
|
body.message = `system: ${preamble}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(body)
|
||||||
|
|
||||||
const res = await globalFetch('https://api.cohere.com/v1/chat', {
|
const res = await globalFetch('https://api.cohere.com/v1/chat', {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
|
|||||||
Reference in New Issue
Block a user