Skip to content

Commit

Permalink
feat: set the length of the historical message (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
chancelyg authored Jun 28, 2023
1 parent e4f87e4 commit 3c2b344
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/providers/openai/handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fetchChatCompletion, fetchImageGeneration } from './api'
import { parseStream } from './parser'
import type { Message } from '@/types/message'
import type { HandlerPayload, Provider } from '@/types/provider'

export const handlePrompt: Provider['handlePrompt'] = async(payload, signal?: AbortSignal) => {
Expand Down Expand Up @@ -35,14 +36,35 @@ export const handleRapidPrompt: Provider['handleRapidPrompt'] = async(prompt, gl
}

const handleChatCompletion = async(payload: HandlerPayload, signal?: AbortSignal) => {
// An array to store the chat messages
const messages: Message[] = []

let maxTokens = payload.globalSettings.maxTokens as number
let messageHistorySize = payload.globalSettings.messageHistorySize as number

// Iterate through the message history
while (messageHistorySize > 0) {
messageHistorySize--
// Get the last message from the payload
const m = payload.messages.pop()
if (m === undefined)
break

if (maxTokens - m.content.length < 0)
break

maxTokens -= m.content.length
messages.unshift(m)
}

const response = await fetchChatCompletion({
apiKey: payload.globalSettings.apiKey as string,
baseUrl: (payload.globalSettings.baseUrl as string).trim().replace(/\/$/, ''),
body: {
messages,
max_tokens: maxTokens,
model: payload.globalSettings.model as string,
messages: payload.messages,
temperature: payload.globalSettings.temperature as number,
max_tokens: payload.globalSettings.maxTokens as number,
top_p: payload.globalSettings.topP as number,
stream: payload.globalSettings.stream as boolean ?? true,
},
Expand Down
10 changes: 10 additions & 0 deletions src/providers/openai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ const providerOpenAI = () => {
default: 2048,
step: 1,
},
{
key: 'messageHistorySize',
name: 'Max History Message Size',
description: 'The number of retained historical messages will be truncated if the length of the message exceeds the MaxToken parameter.',
type: 'slider',
min: 1,
max: 24,
default: 5,
step: 1,
},
{
key: 'temperature',
name: 'Temperature',
Expand Down

0 comments on commit 3c2b344

Please sign in to comment.