feat: add metering data
This commit is contained in:
parent
f617a05483
commit
da162be01d
@ -8,7 +8,7 @@ import {
|
|||||||
promptForRag,
|
promptForRag,
|
||||||
systemPromptForNonRagOption
|
systemPromptForNonRagOption
|
||||||
} from "~/services/ollama"
|
} from "~/services/ollama"
|
||||||
import { type ChatHistory, type Message } from "~/store/option"
|
import { type ChatHistory, ChatMessage, type Message } from "~/store/option"
|
||||||
import { SystemMessage } from "@langchain/core/messages"
|
import { SystemMessage } from "@langchain/core/messages"
|
||||||
import { useStoreMessageOption } from "~/store/option"
|
import { useStoreMessageOption } from "~/store/option"
|
||||||
import {
|
import {
|
||||||
@ -55,6 +55,8 @@ export const useMessageOption = () => {
|
|||||||
const {
|
const {
|
||||||
history,
|
history,
|
||||||
setHistory,
|
setHistory,
|
||||||
|
chatMessages,
|
||||||
|
setChatMessages,
|
||||||
setStreaming,
|
setStreaming,
|
||||||
streaming,
|
streaming,
|
||||||
setIsFirstMessage,
|
setIsFirstMessage,
|
||||||
@ -112,6 +114,26 @@ export const useMessageOption = () => {
|
|||||||
setWebSearch(true)
|
setWebSearch(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 从最后的结果中解析出 思维链 和 结果
|
||||||
|
const responseResolver = (msg: string) => {
|
||||||
|
const thinkStart = msg.indexOf("<think>")
|
||||||
|
const thinkEnd = msg.indexOf("</think>")
|
||||||
|
let think = ""
|
||||||
|
let content = ""
|
||||||
|
if (thinkStart > -1 && thinkEnd > -1) {
|
||||||
|
think = msg.substring(thinkStart + 7, thinkEnd)
|
||||||
|
content = msg.substring(thinkEnd + 8)
|
||||||
|
} else {
|
||||||
|
content = msg
|
||||||
|
}
|
||||||
|
// 去掉换行符
|
||||||
|
think = think.replace(/\n/g, "")
|
||||||
|
content = content.replace(/\n/g, "")
|
||||||
|
return {
|
||||||
|
think,
|
||||||
|
content
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const searchChatMode = async (
|
const searchChatMode = async (
|
||||||
webSearch: boolean,
|
webSearch: boolean,
|
||||||
@ -166,9 +188,12 @@ export const useMessageOption = () => {
|
|||||||
useMlock:
|
useMlock:
|
||||||
currentChatModelSettings?.useMlock ?? userDefaultModelSettings?.useMlock
|
currentChatModelSettings?.useMlock ?? userDefaultModelSettings?.useMlock
|
||||||
})
|
})
|
||||||
|
|
||||||
let newMessage: Message[] = []
|
let newMessage: Message[] = []
|
||||||
let generateMessageId = generateID()
|
let generateMessageId = generateID()
|
||||||
|
const chatMessage: ChatMessage = {
|
||||||
|
id: generateMessageId,
|
||||||
|
queryContent: message
|
||||||
|
} as ChatMessage
|
||||||
|
|
||||||
if (!isRegenerate) {
|
if (!isRegenerate) {
|
||||||
newMessage = [
|
newMessage = [
|
||||||
@ -285,17 +310,21 @@ export const useMessageOption = () => {
|
|||||||
const response = await ollama.invoke(promptForQuestion)
|
const response = await ollama.invoke(promptForQuestion)
|
||||||
let res = response.content.toString()
|
let res = response.content.toString()
|
||||||
res = removeReasoning(res)
|
res = removeReasoning(res)
|
||||||
keywords = res.replace(/^Keywords:/i, '').split(', ').map(k => k.trim())
|
keywords = res
|
||||||
|
.replace(/^Keywords:/i, "")
|
||||||
|
.split(", ")
|
||||||
|
.map((k) => k.trim())
|
||||||
}
|
}
|
||||||
|
|
||||||
const { prompt, webSources, iodSources } = await getSystemPromptForWeb(
|
const { prompt, webSources, iodSources } = await getSystemPromptForWeb(
|
||||||
query,
|
query,
|
||||||
keywords,
|
keywords,
|
||||||
webSearch,
|
webSearch,
|
||||||
iodSearch,
|
iodSearch
|
||||||
)
|
)
|
||||||
console.log("prompt:\n"+prompt);
|
console.log("prompt:\n" + prompt)
|
||||||
setIsSearchingInternet(false)
|
setIsSearchingInternet(false)
|
||||||
|
chatMessage.prompt = prompt
|
||||||
|
|
||||||
// message = message.trim().replaceAll("\n", " ")
|
// message = message.trim().replaceAll("\n", " ")
|
||||||
|
|
||||||
@ -455,6 +484,14 @@ export const useMessageOption = () => {
|
|||||||
|
|
||||||
setIsProcessing(false)
|
setIsProcessing(false)
|
||||||
setStreaming(false)
|
setStreaming(false)
|
||||||
|
|
||||||
|
chatMessage.relatedDataCount = keywords.length
|
||||||
|
chatMessage.timeTaken = timetaken
|
||||||
|
chatMessage.date = reasoningStartTime
|
||||||
|
const { think, content } = responseResolver(fullText)
|
||||||
|
chatMessage.thinkingChain = think
|
||||||
|
chatMessage.responseContent = content
|
||||||
|
setChatMessages([...chatMessages, chatMessage])
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const errorSave = await saveMessageOnError({
|
const errorSave = await saveMessageOnError({
|
||||||
e,
|
e,
|
||||||
@ -567,7 +604,7 @@ export const useMessageOption = () => {
|
|||||||
currentChatModelSettings?.numThread ??
|
currentChatModelSettings?.numThread ??
|
||||||
userDefaultModelSettings?.numThread,
|
userDefaultModelSettings?.numThread,
|
||||||
useMlock:
|
useMlock:
|
||||||
currentChatModelSettings?.useMlock ?? userDefaultModelSettings?.useMlock
|
currentChatModelSettings?.useMlock ?? userDefaultModelSettings?.useMlock,
|
||||||
})
|
})
|
||||||
|
|
||||||
let newMessage: Message[] = []
|
let newMessage: Message[] = []
|
||||||
|
@ -26,15 +26,43 @@ export type Message = {
|
|||||||
export type ChatHistory = {
|
export type ChatHistory = {
|
||||||
role: "user" | "assistant" | "system"
|
role: "user" | "assistant" | "system"
|
||||||
content: string
|
content: string
|
||||||
image?: string,
|
image?: string
|
||||||
messageType?: string
|
messageType?: string
|
||||||
}[]
|
}[]
|
||||||
|
|
||||||
|
export type ChatMessage = {
|
||||||
|
id: string
|
||||||
|
// 问题
|
||||||
|
queryContent: string
|
||||||
|
// 提示词全文
|
||||||
|
prompt: string
|
||||||
|
// 思维链(只有深度思考时有)
|
||||||
|
thinkingChain?: string
|
||||||
|
// 回答
|
||||||
|
responseContent: string
|
||||||
|
// 关联数据个数
|
||||||
|
relatedDataCount: number
|
||||||
|
// 数联网输入token
|
||||||
|
iodInputToken: string
|
||||||
|
// 数联网输出token
|
||||||
|
iodOutputToken: string
|
||||||
|
// 大模型输入token
|
||||||
|
modelInputToken: string
|
||||||
|
// 大模型输出token
|
||||||
|
modelOutputToken: string
|
||||||
|
// 日期
|
||||||
|
date: Date
|
||||||
|
// 耗时
|
||||||
|
timeTaken: number
|
||||||
|
}
|
||||||
|
|
||||||
type State = {
|
type State = {
|
||||||
messages: Message[]
|
messages: Message[]
|
||||||
setMessages: (messages: Message[]) => void
|
setMessages: (messages: Message[]) => void
|
||||||
history: ChatHistory
|
history: ChatHistory
|
||||||
setHistory: (history: ChatHistory) => void
|
setHistory: (history: ChatHistory) => void
|
||||||
|
chatMessages: ChatMessage[]
|
||||||
|
setChatMessages: (chatMessages: ChatMessage[]) => void
|
||||||
streaming: boolean
|
streaming: boolean
|
||||||
setStreaming: (streaming: boolean) => void
|
setStreaming: (streaming: boolean) => void
|
||||||
isFirstMessage: boolean
|
isFirstMessage: boolean
|
||||||
@ -82,6 +110,8 @@ export const useStoreMessageOption = create<State>((set) => ({
|
|||||||
setMessages: (messages) => set({ messages }),
|
setMessages: (messages) => set({ messages }),
|
||||||
history: [],
|
history: [],
|
||||||
setHistory: (history) => set({ history }),
|
setHistory: (history) => set({ history }),
|
||||||
|
chatMessages: [],
|
||||||
|
setChatMessages: (chatMessages) => set({ chatMessages }),
|
||||||
streaming: false,
|
streaming: false,
|
||||||
setStreaming: (streaming) => set({ streaming }),
|
setStreaming: (streaming) => set({ streaming }),
|
||||||
isFirstMessage: true,
|
isFirstMessage: true,
|
||||||
@ -120,5 +150,5 @@ export const useStoreMessageOption = create<State>((set) => ({
|
|||||||
setTemporaryChat: (temporaryChat) => set({ temporaryChat }),
|
setTemporaryChat: (temporaryChat) => set({ temporaryChat }),
|
||||||
|
|
||||||
useOCR: false,
|
useOCR: false,
|
||||||
setUseOCR: (useOCR) => set({ useOCR }),
|
setUseOCR: (useOCR) => set({ useOCR })
|
||||||
}))
|
}))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user