Skip to content

Commit

Permalink
[webui] 更新部分组件
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaowFISH committed Jan 7, 2025
1 parent 137fb49 commit 409164f
Show file tree
Hide file tree
Showing 19 changed files with 976 additions and 762 deletions.
13 changes: 0 additions & 13 deletions packages/core/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,7 @@ export interface FailedResponse {
type Response = SuccessResponse | SkipResponse | FailedResponse;

export class Bot {
private memorySize: number;

private summarySize: number; // 上下文达到多少时进行总结
private contextSize: number = 20; // 以对话形式给出的上下文长度
private retainedContextSize: number; // 进行总结时保留的上下文长度,用于保持记忆连贯性
private maxSelfMemoryCharacters: 5000; //

private minTriggerCount: number;
private maxTriggerCount: number;
Expand All @@ -69,10 +64,8 @@ export class Bot {
private template: Template;
private extensions: { [key: string]: Extension & Function } = {};
private toolsSchema: ToolSchema[] = [];
private lastModified: Date = new Date();

private emojiManager: EmojiManager;
private embedder: EmbeddingBase;
readonly verifier: ResponseVerifier;
readonly imageViewer: ImageViewer;

Expand All @@ -89,7 +82,6 @@ export class Bot {
);
if (config.Embedding.Enabled) {
this.emojiManager = new EmojiManager(config.Embedding);
this.embedder = getEmbedding(config.Embedding);
};
if (config.Verifier.Enabled) this.verifier = new ResponseVerifier(config);

Expand All @@ -103,11 +95,6 @@ export class Bot {
}
}

updateConfig(config: Config) {
this.config = config;
this.adapterSwitcher.updateConfig(config.API.APIList, config.Parameters);
}

setSystemPrompt(content: string) {
this.prompt = content;
}
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/extensions/ext_memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { Description, Extension, Name, Param } from "./base";
@Name("addCoreMemory")
@Description("Append to the contents of core memory.")
@Param("content", SchemaNode.String("Content to write to the memory. All unicode (including emojis) are supported."))
@Param("topic", SchemaNode.String("The topic of the memory.", ""))
@Param("keywords", SchemaNode.Array("The keywords of the memory.", ""))
export class AddCoreMemory extends Extension {
async apply(content: string) {
return await this.ctx.memory.addCoreMemory(content);
async apply(content: string, topic?: string, keywords?: string[]) {
return await this.ctx.memory.addCoreMemory(content, topic, keywords);
}
}

Expand Down
12 changes: 1 addition & 11 deletions packages/memory/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ export const EmbeddingConfig: Schema<EmbeddingConfig> = Schema.intersect([
.default(300)
.experimental()
.description("文本分词长度"),

}),
}).description("Embedding API 配置"),
Schema.union([
Schema.object({
APIType: Schema.const("OpenAI"),
Expand Down Expand Up @@ -58,12 +57,3 @@ export const EmbeddingConfig: Schema<EmbeddingConfig> = Schema.intersect([
}),
])
])


export interface Config {
embedding: EmbeddingConfig;
}

export const Config: Schema<Config> = Schema.object({
embedding: EmbeddingConfig,
});
75 changes: 47 additions & 28 deletions packages/memory/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { Context, Schema, Service } from "koishi";
import { ChatMessage } from "koishi-plugin-yesimbot";
import { EmbeddingBase, calculateCosineSimilarity } from "koishi-plugin-yesimbot/embeddings";
import { getEmbedding } from "koishi-plugin-yesimbot/utils";
import { ChatMessage, EmbeddingBase, calculateCosineSimilarity, getEmbedding } from "koishi-plugin-yesimbot";
import { EmbeddingConfig } from "./config";
import { MemoryItem, MemoryType } from "./model";
import { MemoryMetadata, MemoryVectorStore } from "./vectorStore";
import { MEMORY_PROMPT } from "./prompt";
import { MemoryMetadata, MemoryVectorStore } from "./vectorStore";

declare module "koishi" {
interface Context {
Expand All @@ -17,8 +15,6 @@ export const inject = {
required: ["yesimbot", "database"],
};

export { Config } from "./config";

class Memory extends Service {
private vectorStore: MemoryVectorStore;
private embedder: EmbeddingBase;
Expand Down Expand Up @@ -51,13 +47,29 @@ class Memory extends Service {
this.vectorStore.clear();
}

async addCoreMemory(content: string) {
async modifyMemoryById(memoryId: string, content: string, type?: MemoryType, topic?: string, keywords?: string[]) {
const memory = this.vectorStore.get(memoryId);
if (memory) {
const embedding = (content == memory.content) ? memory.embedding : await this.embedder.embed(content);
const metadata: MemoryMetadata = {
content,
type: type || memory.type,
topic: topic || memory.topic,
keywords: keywords || memory.keywords,
createdAt: memory.createdAt,
updatedAt: new Date(),
};
this.vectorStore.update(memoryId, embedding, metadata);
}
}

async addCoreMemory(content: string, topic?: string, keywords?: string[]) {
const embedding = await this.embedder.embed(content);
const metadata: MemoryMetadata = {
content,
topic: "核心记忆",
keywords: [],
type: MemoryType.Core,
topic,
keywords,
createdAt: new Date(),
updatedAt: new Date(),
};
Expand Down Expand Up @@ -124,14 +136,14 @@ class Memory extends Service {
})
}

async searchArchivalMemory(query: string, options: { type?: MemoryType, topic?: string, keywords?: string[]; limit?: number }): Promise<string[]> {
async searchArchivalMemory(query: string, type?: MemoryType, topic?: string, keywords?: string[], limit?: number): Promise<string[]> {
const contextEmbedding = await this.embedder.embed(query);

// 1. 主题与关键词过滤
let filteredMemory = this.vectorStore.filter(item => {
const topicMatch = options.topic ? item.topic === options.topic : true;
const keywordMatch = options.keywords
? options.keywords.some(keyword => item.keywords.includes(keyword))
const topicMatch = topic ? item.topic === topic : true;
const keywordMatch = keywords
? keywords.some(keyword => item.keywords.includes(keyword))
: true;
return topicMatch && keywordMatch;
});
Expand All @@ -145,29 +157,28 @@ class Memory extends Service {
// 3. 排序并限制结果数
const sortedMemory = scoredMemory
.sort((a, b) => b.similarity - a.similarity) // 按相似度降序排序
.slice(0, options.limit || 5); // 限制返回结果数
.slice(0, limit || 5); // 限制返回结果数

return sortedMemory.map(item => item.content);
}


/**
* Searches for conversation messages from a specific user that are semantically similar to a given query.
* 搜索与给定查询语义相似且来自特定用户的对话消息。
*
* This method performs the following steps:
* 1. Embeds the query into a vector representation using the configured embedder.
* 2. Retrieves the most recent chat messages sent by the specified user from the database.
* 3. Computes the cosine similarity between the query embedding and the embedding of each chat message.
* 4. Sorts the messages by similarity in descending order and limits the results to the specified count.
* 5. Re-sorts the limited results by the original send time in ascending order to maintain chronological order.
* 6. Returns the content of the most relevant messages.
* 该方法执行以下步骤:
* 1. 使用配置的嵌入器将查询嵌入为向量表示。
* 2. 从数据库中检索指定用户发送的最新聊天消息。
* 3. 计算查询嵌入与每条聊天消息嵌入之间的余弦相似度。
* 4. 按相似度降序排序消息,并将结果限制为指定的数量。
* 5. 将限制后的结果按原始发送时间升序重新排序,以保持时间顺序。
* 6. 返回最相关消息的内容。
*
* @param query - The search query string to find similar messages.
* @param userId - The ID of the user whose messages are to be searched.
* @param count - The maximum number of messages to return. Defaults to 10.
* @returns A promise that resolves to an array of message contents sorted by relevance and chronological order.
* @param query - 用于查找相似消息的搜索查询字符串。
* @param userId - 要搜索消息的用户ID。
* @param count - 要返回的最大消息数量。默认为10。
* @returns 一个Promise,解析为按相关性和时间顺序排序的消息内容数组。
*/
async searchConversation(query: string, userId: string, count: number = 10): Promise<string[]>{
async searchConversation(query: string, userId: string, count: number = 10): Promise<string[]> {
let embedding = await this.embedder.embed(query);

let chatMessages = await this.ctx.database
Expand Down Expand Up @@ -199,9 +210,17 @@ class Memory extends Service {

namespace Memory {
export interface Config {
memorySize: number; // 记忆容量
summarySize: number; // 上下文达到多少时进行总结
retainedContextSize: number; // 进行总结时保留的上下文长度,用于保持记忆连贯性
maxCoreMemoryCharacters: number; // 最大记忆字符数
embedding: EmbeddingConfig;
}
export const Config: Schema<Config> = Schema.object({
memorySize: Schema.number().default(1000),
summarySize: Schema.number().default(100),
retainedContextSize: Schema.number().default(10),
maxCoreMemoryCharacters: Schema.number().default(5000),
embedding: EmbeddingConfig,
});
}
Expand Down
21 changes: 9 additions & 12 deletions packages/memory/src/vectorStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Context } from "koishi";
import { defineAccessor } from "@satorijs/core";

import { CacheManager } from "koishi-plugin-yesimbot";
import { calculateCosineSimilarity } from "koishi-plugin-yesimbot/embeddings";
import { calculateCosineSimilarity } from "koishi-plugin-yesimbot";
import { MemoryItem, MemoryType } from "./model";

export interface MemoryMetadata {
Expand All @@ -17,13 +17,6 @@ export interface MemoryMetadata {
updatedAt: Date;
}


export interface MemoryVectorStore {
get(id: string): MemoryItem;
delete(id: string): boolean;
clear(): void;
}

export class MemoryVectorStore {
readonly store: CacheManager<MemoryItem>;

Expand All @@ -32,6 +25,14 @@ export class MemoryVectorStore {
this.store = new CacheManager(vectorsFilePath, true);
}

delete(id: string): boolean {
return this.store.delete(id);
}

clear(): void {
this.store.clear();
}

get(id: string): MemoryItem | undefined {
return this.store.get(id);
}
Expand Down Expand Up @@ -136,10 +137,6 @@ export class MemoryVectorStore {
}
}

defineAccessor(MemoryVectorStore.prototype, "get", ["store", "get"]);
defineAccessor(MemoryVectorStore.prototype, "clear", ["store", "clear"]);
defineAccessor(MemoryVectorStore.prototype, "delete", ["store", "delete"]);

/**
* 获取向量的模
* @param vector
Expand Down
50 changes: 0 additions & 50 deletions packages/webui/client/components/MemoryChart.vue

This file was deleted.

Loading

0 comments on commit 409164f

Please sign in to comment.