Skip to content
This repository has been archived by the owner on May 19, 2024. It is now read-only.

Commit

Permalink
override gpt models #2
Browse files Browse the repository at this point in the history
  • Loading branch information
pew committed Apr 5, 2024
1 parent 34b30bc commit b442c34
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,20 @@ export default {
}

const chat = await request.json()
const model = chat.model
let { model, stream } = chat
console.log('original model', model)
console.log('original stream', stream)

if (!stream) {
stream = false
}
if (model.includes('gpt-4') || model.includes('gpt-3')) {
model = '@cf/meta/llama-2-7b-chat-fp16'
}

console.log('new model', model)
console.log('new stream', stream)

const messages = [
{
role: 'system',
Expand All @@ -47,10 +60,10 @@ export default {
]
const resp = await ai.run(model, {
messages,
stream: chat.stream || false,
stream: stream,
})

if (!chat.stream) {
if (!stream) {
const output = {
// id: 'chatcmpl-123',
object: 'chat.completion',
Expand Down Expand Up @@ -80,7 +93,7 @@ export default {
for await (const part of resp) {
const text = textDecoder.decode(part)
const lines = text.split('\n')
lines.forEach((element) => {
lines.forEach((element, index) => {
if (element.includes('[DONE]')) {
const data = {
id: 'chatcmpl-123',
Expand All @@ -99,7 +112,17 @@ export default {
object: 'chat.completion.chunk',
created: Date.now(),
model: model,
choices: [{ index: 0, delta: { role: 'assistant', content: json.response }, logprobs: null, finish_reason: null }],
choices: [
{
index: 0,
delta: { role: 'assistant', content: json.response },
logprobs: null,
finish_reason: null,
},
],
}
if (index === 0 || index === 2) {
data.choices[0].delta.content = json.response.trim()
}
writer.write(textEncoder.encode(`data: ${JSON.stringify(data)}\n\n`))
}
Expand Down

0 comments on commit b442c34

Please sign in to comment.