Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: set the length of the historical message #75

Merged
merged 2 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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