| 486 | } |
| 487 | |
| 488 | async function createChatCompletion( |
| 489 | nextMessages: Message[], |
| 490 | secretKey: string, |
| 491 | ) { |
| 492 | if (secretKey === 'cat') { |
| 493 | const lastMessage = nextMessages[nextMessages.length - 1] |
| 494 | const lastText = lastMessage.content |
| 495 | const newText = lastText.replace(/\w+/g, (a) => |
| 496 | a[0] === a[0].toLowerCase() ? 'meow' : 'Meow', |
| 497 | ) |
| 498 | return { |
| 499 | data: { |
| 500 | id: 'cat' + String(ObjectID()), |
| 501 | object: 'chat.completion', |
| 502 | created: Math.floor(Date.now() / 1000), |
| 503 | model: 'cat', |
| 504 | usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 }, |
| 505 | choices: [ |
| 506 | { |
| 507 | message: { role: 'assistant', content: newText }, |
| 508 | finish_reason: 'stop', |
| 509 | index: 0, |
| 510 | }, |
| 511 | ], |
| 512 | }, |
| 513 | } |
| 514 | } |
| 515 | const model = (await ikv.get(selectedModal.key)) || selectedModal.defaultValue |
| 516 | const temperatureParam = (await ikv.get(temperature.key)) || temperature.defaultValue |
| 517 | const topPParam = (await ikv.get(top_p.key)) || top_p.defaultValue |
| 518 | return redaxios.post( |
| 519 | 'https://api.openai.com/v1/chat/completions', |
| 520 | { |
| 521 | model: model, |
| 522 | messages: nextMessages, |
| 523 | temperature: temperatureParam, |
| 524 | top_p: topPParam, |
| 525 | }, |
| 526 | { |
| 527 | headers: { |
| 528 | authorization: `Bearer ${secretKey}`, |
| 529 | }, |
| 530 | }, |
| 531 | ) |
| 532 | } |
| 533 | |
| 534 | function renderMeta(response: any) { |
| 535 | const usage = response?.data?.usage |