-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e92c9b
commit a212a74
Showing
13 changed files
with
203 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<script lang="ts" setup> | ||
import type { SessionData } from '@/types' | ||
import { estimateTokens } from '@/utils' | ||
import { ImageCost } from '@/constants' | ||
const props = defineProps<{ | ||
data: SessionData | ||
}>() | ||
const { currentRole } = storeToRefs(useRoleStore()) | ||
const { uuid, isTokenUsage } = storeToRefs(useSettingsStore()) | ||
const { sessionDataList, currentSession } = storeToRefs(useSessionStore()) | ||
/** | ||
* 计算单条消息消耗的token | ||
* @param data 单条消息 | ||
*/ | ||
const calcToken = (data: SessionData) => { | ||
if (!currentRole.value) return '' | ||
if (currentSession.value?.type === 'text') { | ||
// 角色描述字符数 | ||
const roleToken = estimateTokens(currentRole.value!.description) | ||
// 消息内容字符数 | ||
const contentToken = estimateTokens(data.message.content) | ||
// 记忆模式下额外消耗的字符数 | ||
let memoryToken = 0 | ||
if (data.is_memory) { | ||
// 获取sessionDataList中的此条之前的最后5条消息 | ||
const memoryList = sessionDataList.value | ||
.filter((data) => data.id! < data.id!) | ||
.slice(-5) | ||
memoryToken = estimateTokens( | ||
memoryList.map((data) => data.message.content).join('') | ||
) | ||
} | ||
if (data.is_ask) { | ||
return `${roleToken + contentToken + memoryToken}TK${ | ||
data.is_memory ? '*' : '' | ||
}` | ||
} | ||
return `${contentToken}TK` | ||
} | ||
if (currentSession.value?.type === 'image') { | ||
return '0TK' | ||
// const { message, message_type } = props.data | ||
// if (message_type !== 'image') return '0TK' | ||
// // 计算图片的 token * 数量 | ||
// const num = message.content.length | ||
// TODO 通过base64获得图片的大小 | ||
// const size = calcImageSize(message.content[0].b64_json) | ||
// if (!size) return '0TK' | ||
// return `${num * ImageCost[size]}TK` | ||
} | ||
} | ||
</script> | ||
|
||
<template> | ||
<div class="flex flex-col items-center gap-1"> | ||
<Avatar class="w-12!" :value="data.is_ask ? uuid : currentRole?.name" /> | ||
<span class="text-gray text-xs" v-if="isTokenUsage"> | ||
{{ calcToken(data) }} | ||
</span> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.