Merge pull request #162 from Webifi/main

Fix token limit issue in #161
This commit is contained in:
Niek van der Maas 2023-06-10 21:57:39 +02:00 committed by GitHub
commit 8577ae7f56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 13 deletions

View File

@ -20,7 +20,10 @@
type Request, type Request,
type Message, type Message,
type Chat, type Chat,
type ChatCompletionOpts type ChatCompletionOpts,
type Model
} from './Types.svelte' } from './Types.svelte'
import Prompts from './Prompts.svelte' import Prompts from './Prompts.svelte'
import Messages from './Messages.svelte' import Messages from './Messages.svelte'
@ -358,6 +361,7 @@
// Update token count with actual // Update token count with actual
promptTokenCount = countPromptTokens(messagePayload, model) promptTokenCount = countPromptTokens(messagePayload, model)
const maxAllowed = getModelMaxTokens(chatSettings.model as Model) - (promptTokenCount + 1)
try { try {
const request: Request = { const request: Request = {
@ -369,17 +373,22 @@
if (typeof setting.apiTransform === 'function') { if (typeof setting.apiTransform === 'function') {
value = setting.apiTransform(chatId, setting, value) value = setting.apiTransform(chatId, setting, value)
} }
if (opts.maxTokens) { if (key === 'max_tokens') {
if (key === 'max_tokens') value = opts.maxTokens // only as large as requested if (opts.maxTokens) {
value = opts.maxTokens // only as large as requested
}
if (value > maxAllowed || value < 1) value = null
} }
if (opts.streaming || opts.summaryRequest) { if (key === 'n') {
/* if (opts.streaming || opts.summaryRequest) {
Streaming goes insane with more than one completion. /*
Doesn't seem like there's any way to separate the jumbled mess of deltas for the Streaming goes insane with more than one completion.
different completions. Doesn't seem like there's any way to separate the jumbled mess of deltas for the
Summary should only have one completion different completions.
*/ Summary should only have one completion
if (key === 'n') value = 1 */
value = 1
}
} }
if (value !== null) acc[key] = value if (value !== null) acc[key] = value
return acc return acc
@ -392,7 +401,7 @@
const signal = controller.signal const signal = controller.signal
console.log('apikey', $apiKeyStorage) // console.log('apikey', $apiKeyStorage)
const fetchOptions = { const fetchOptions = {
method: 'POST', method: 'POST',
@ -408,7 +417,7 @@
let errorResponse let errorResponse
try { try {
const errObj = await response.json() const errObj = await response.json()
errorResponse = errObj?.error?.code errorResponse = errObj?.error?.message || errObj?.error?.code
if (!errorResponse && response.choices && response.choices[0]) { if (!errorResponse && response.choices && response.choices[0]) {
errorResponse = response.choices[0]?.message?.content errorResponse = response.choices[0]?.message?.content
} }