fix: 在提问时携带图片会卡住软件 #108
This commit is contained in:
parent
f543a9ff80
commit
1601fc6d81
@ -65,7 +65,7 @@ const PopupContainer: React.FC<Props> = ({ text, textareaProps, modalProps, reso
|
|||||||
centered>
|
centered>
|
||||||
<TextArea
|
<TextArea
|
||||||
ref={textareaRef}
|
ref={textareaRef}
|
||||||
rows={4}
|
rows={2}
|
||||||
autoFocus
|
autoFocus
|
||||||
{...textareaProps}
|
{...textareaProps}
|
||||||
value={textValue}
|
value={textValue}
|
||||||
|
|||||||
@ -19,11 +19,6 @@ async function getFileContent(file: FileType) {
|
|||||||
|
|
||||||
const fileId = file.id + file.ext
|
const fileId = file.id + file.ext
|
||||||
|
|
||||||
if (file.type === FileTypes.IMAGE) {
|
|
||||||
const data = await window.api.file.base64Image(fileId)
|
|
||||||
return data.data
|
|
||||||
}
|
|
||||||
|
|
||||||
if (file.type === FileTypes.TEXT) {
|
if (file.type === FileTypes.TEXT) {
|
||||||
return await window.api.file.read(fileId)
|
return await window.api.file.read(fileId)
|
||||||
}
|
}
|
||||||
@ -60,18 +55,31 @@ export function estimateTextTokens(text: string) {
|
|||||||
return usedTokens - 7
|
return usedTokens - 7
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function estimateImageTokens(file: FileType) {
|
||||||
|
return Math.floor(file.size / 100)
|
||||||
|
}
|
||||||
|
|
||||||
export async function estimateMessageUsage(message: Message): Promise<CompletionUsage> {
|
export async function estimateMessageUsage(message: Message): Promise<CompletionUsage> {
|
||||||
const { usedTokens, promptUsedTokens, completionUsedTokens } = new GPTTokens({
|
const { usedTokens, promptUsedTokens, completionUsedTokens } = new GPTTokens({
|
||||||
model: 'gpt-4o',
|
model: 'gpt-4o',
|
||||||
messages: await getMessageParam(message)
|
messages: await getMessageParam(message)
|
||||||
})
|
})
|
||||||
|
|
||||||
const hasImage = message.files?.some((f) => f.type === FileTypes.IMAGE)
|
let imageTokens = 0
|
||||||
|
|
||||||
|
if (message.files) {
|
||||||
|
const images = message.files.filter((f) => f.type === FileTypes.IMAGE)
|
||||||
|
if (images.length > 0) {
|
||||||
|
for (const image of images) {
|
||||||
|
imageTokens = estimateImageTokens(image) + imageTokens
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
prompt_tokens: promptUsedTokens,
|
prompt_tokens: promptUsedTokens,
|
||||||
completion_tokens: completionUsedTokens,
|
completion_tokens: completionUsedTokens,
|
||||||
total_tokens: hasImage ? Math.floor(usedTokens / 80) : usedTokens - 7
|
total_tokens: usedTokens + imageTokens - 7
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user