feat: chat histories trim by @uniiem/object-trim
This commit is contained in:
@@ -15,6 +15,7 @@
|
|||||||
"@iconify-json/svg-spinners": "^1.1.2",
|
"@iconify-json/svg-spinners": "^1.1.2",
|
||||||
"@iconify-json/tabler": "^1.1.105",
|
"@iconify-json/tabler": "^1.1.105",
|
||||||
"@nuxt/ui": "^2.14.1",
|
"@nuxt/ui": "^2.14.1",
|
||||||
|
"@uniiem/object-trim": "^0.2.0",
|
||||||
"@uniiem/uuid": "^0.2.1",
|
"@uniiem/uuid": "^0.2.1",
|
||||||
"highlight.js": "^11.9.0",
|
"highlight.js": "^11.9.0",
|
||||||
"idb-keyval": "^6.2.1",
|
"idb-keyval": "^6.2.1",
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import type {ChatMessage, ChatMessageId, ChatSessionId} from "~/typings/llm";
|
|||||||
import {useHistory} from "~/composables/useHistory";
|
import {useHistory} from "~/composables/useHistory";
|
||||||
import {uuidv4} from "@uniiem/uuid";
|
import {uuidv4} from "@uniiem/uuid";
|
||||||
import {useLLM} from "~/composables/useLLM";
|
import {useLLM} from "~/composables/useLLM";
|
||||||
import {objectTrimmer} from "~/utils/object-trimmer";
|
import {trimObject} from "@uniiem/object-trim";
|
||||||
|
|
||||||
useHead({
|
useHead({
|
||||||
title: '聊天 | XSH AI'
|
title: '聊天 | XSH AI'
|
||||||
@@ -125,13 +125,9 @@ const handleClickSend = (event: any) => {
|
|||||||
content: '',
|
content: '',
|
||||||
})
|
})
|
||||||
// 请求模型回复
|
// 请求模型回复
|
||||||
const trimmedMessages = objectTrimmer<ChatMessage>(
|
const trimmedMessages = trimObject(getMessages(), 2000, {
|
||||||
getMessages(),
|
keys: ['content']
|
||||||
200,
|
})
|
||||||
{
|
|
||||||
participatingFields: ['content']
|
|
||||||
}
|
|
||||||
)
|
|
||||||
useLLM(trimmedMessages, {
|
useLLM(trimmedMessages, {
|
||||||
modelTag: 'spark3_5'
|
modelTag: 'spark3_5'
|
||||||
}).then(reply => {
|
}).then(reply => {
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
const objectTrimmer = <T>(
|
|
||||||
objectList: T[],
|
|
||||||
length: number,
|
|
||||||
{
|
|
||||||
participatingFields = [],
|
|
||||||
trimFromStart = true
|
|
||||||
}: {
|
|
||||||
participatingFields: (keyof T)[],
|
|
||||||
trimFromStart?: boolean
|
|
||||||
}) => {
|
|
||||||
let participatingLength = 0
|
|
||||||
let i = objectList.length - 1
|
|
||||||
while (i >= 0) {
|
|
||||||
participatingLength = 0
|
|
||||||
for (const field of participatingFields) {
|
|
||||||
participatingLength += JSON.stringify(objectList[i][field]).length
|
|
||||||
if (participatingLength > length) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
i--
|
|
||||||
}
|
|
||||||
return trimFromStart ? objectList.slice(i + 1) : objectList.slice(0, i + 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
export {objectTrimmer}
|
|
||||||
36
yarn.lock
36
yarn.lock
@@ -2400,6 +2400,11 @@
|
|||||||
hookable "^5.5.3"
|
hookable "^5.5.3"
|
||||||
unhead "1.8.10"
|
unhead "1.8.10"
|
||||||
|
|
||||||
|
"@uniiem/object-trim@^0.2.0":
|
||||||
|
version "0.2.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/@uniiem/object-trim/-/object-trim-0.2.0.tgz#c80b07a0ace3d74ef89c12091835e2cf6d4d0825"
|
||||||
|
integrity sha512-xyhVaXuPNit1s9eZKGMGSmlfdG99LrmiPyku9ljlVy5at7Mro3KL3DGOa/GPg4xQaUY7NO3VuPvVU736EqeKYg==
|
||||||
|
|
||||||
"@uniiem/uuid@^0.2.1":
|
"@uniiem/uuid@^0.2.1":
|
||||||
version "0.2.1"
|
version "0.2.1"
|
||||||
resolved "https://registry.yarnpkg.com/@uniiem/uuid/-/uuid-0.2.1.tgz#2ffa44624968cda47ef744770b2b58f5e5e558f8"
|
resolved "https://registry.yarnpkg.com/@uniiem/uuid/-/uuid-0.2.1.tgz#2ffa44624968cda47ef744770b2b58f5e5e558f8"
|
||||||
@@ -7210,16 +7215,7 @@ streamx@^2.15.0:
|
|||||||
optionalDependencies:
|
optionalDependencies:
|
||||||
bare-events "^2.2.0"
|
bare-events "^2.2.0"
|
||||||
|
|
||||||
"string-width-cjs@npm:string-width@^4.2.0":
|
"string-width-cjs@npm:string-width@^4.2.0", "string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
|
||||||
version "4.2.3"
|
|
||||||
resolved "https://registry.npmmirror.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
|
|
||||||
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
|
|
||||||
dependencies:
|
|
||||||
emoji-regex "^8.0.0"
|
|
||||||
is-fullwidth-code-point "^3.0.0"
|
|
||||||
strip-ansi "^6.0.1"
|
|
||||||
|
|
||||||
"string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
|
|
||||||
version "4.2.3"
|
version "4.2.3"
|
||||||
resolved "https://registry.npmmirror.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
|
resolved "https://registry.npmmirror.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
|
||||||
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
|
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
|
||||||
@@ -7302,14 +7298,7 @@ stringify-object@^3.3.0:
|
|||||||
is-obj "^1.0.1"
|
is-obj "^1.0.1"
|
||||||
is-regexp "^1.0.0"
|
is-regexp "^1.0.0"
|
||||||
|
|
||||||
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
|
"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
|
||||||
version "6.0.1"
|
|
||||||
resolved "https://registry.npmmirror.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
|
|
||||||
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
|
|
||||||
dependencies:
|
|
||||||
ansi-regex "^5.0.1"
|
|
||||||
|
|
||||||
strip-ansi@^6.0.0, strip-ansi@^6.0.1:
|
|
||||||
version "6.0.1"
|
version "6.0.1"
|
||||||
resolved "https://registry.npmmirror.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
|
resolved "https://registry.npmmirror.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
|
||||||
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
|
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
|
||||||
@@ -8351,16 +8340,7 @@ workbox-window@7.0.0, workbox-window@^7.0.0:
|
|||||||
"@types/trusted-types" "^2.0.2"
|
"@types/trusted-types" "^2.0.2"
|
||||||
workbox-core "7.0.0"
|
workbox-core "7.0.0"
|
||||||
|
|
||||||
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
|
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
|
||||||
version "7.0.0"
|
|
||||||
resolved "https://registry.npmmirror.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
|
|
||||||
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
|
|
||||||
dependencies:
|
|
||||||
ansi-styles "^4.0.0"
|
|
||||||
string-width "^4.1.0"
|
|
||||||
strip-ansi "^6.0.0"
|
|
||||||
|
|
||||||
wrap-ansi@^7.0.0:
|
|
||||||
version "7.0.0"
|
version "7.0.0"
|
||||||
resolved "https://registry.npmmirror.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
|
resolved "https://registry.npmmirror.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
|
||||||
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
|
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
|
||||||
|
|||||||
Reference in New Issue
Block a user