Skip to content

Commit

Permalink
修复Gemini适配器、允许临时禁用适配器,跳过空过滤词
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaowFISH committed Jan 5, 2025
1 parent 0701bde commit 6834c12
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
14 changes: 11 additions & 3 deletions src/adapters/config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Schema } from "koishi";

export interface LLM {
Enabled?: boolean;
APIType: "OpenAI" | "Cloudflare" | "Ollama" | "Custom URL" | "Gemini";
BaseURL: string;
UID?: string;
APIKey: string;
AIModel: string;
Ability?: Array<"原生工具调用" | "识图功能" | "结构化输出">;

NUMA?: boolean;
NumCtx?: number;
NumBatch?: number;
Expand All @@ -26,12 +28,13 @@ export interface Config {

export const API: Schema<LLM> = Schema.intersect([
Schema.object({
Enabled: Schema.boolean().default(true).description("是否启用"),
APIType: Schema.union(["OpenAI", "Cloudflare", "Ollama", "Custom URL", "Gemini"])
.default("OpenAI")
.description("API 类型"),
BaseURL: Schema.string()
.default("https://api.openai.com")
.description("API 基础 URL, 设置为\"Custom URL\"需要填写完整的 URL"),
// BaseURL: Schema.string()
// .default("https://api.openai.com")
// .description("API 基础 URL, 设置为\"Custom URL\"需要填写完整的 URL"),
APIKey: Schema.string().required().description("你的 API 令牌"),
AIModel: Schema.string()
.description("模型 ID"),
Expand All @@ -44,16 +47,20 @@ export const API: Schema<LLM> = Schema.intersect([
Schema.union([
Schema.object({
APIType: Schema.const("OpenAI"),
BaseURL: Schema.string().default("https://api.openai.com"),
}),
Schema.object({
APIType: Schema.const("Cloudflare"),
BaseURL: Schema.string().default("https://api.cloudflare.com/client/v4"),
UID: Schema.string().required().description("Cloudflare UID"),
}),
Schema.object({
APIType: Schema.const("Custom URL"),
BaseURL: Schema.string().required().description("自定义 URL"),
}),
Schema.object({
APIType: Schema.const("Ollama"),
BaseURL: Schema.string().default("http://127.0.0.1:11434"),
NUMA: Schema.boolean()
.default(false)
.description("是否使用 NUMA"),
Expand Down Expand Up @@ -100,6 +107,7 @@ export const API: Schema<LLM> = Schema.intersect([
}),
Schema.object({
APIType: Schema.const("Gemini"),
BaseURL: Schema.string().default("https://generativelanguage.googleapis.com"),
}),
]),
]);
Expand Down
8 changes: 8 additions & 0 deletions src/adapters/gemini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ export class GeminiAdapter extends BaseAdapter {
}

async chat(messages: Message[], toolsSchema?: ToolSchema[], debug = false): Promise<Response> {
const system = messages.find((message) => message.role === "system");
if (system) {
messages = messages.filter((message) => message.role !== "system");

}
const requestBody = {
system_instruction: convert(system),
contents: messages.map(convert),
generationConfig: {
stopSequences: this.parameters?.Stop,
Expand Down Expand Up @@ -98,6 +104,8 @@ export class GeminiAdapter extends BaseAdapter {
}

function convert(message: Message): Content {
// @ts-ignore
message.role = message.role == "assistant" ? "model" : message.role;
if (typeof message.content === "string") {
return {
role: message.role,
Expand Down
1 change: 1 addition & 0 deletions src/adapters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class AdapterSwitcher {
) {
this.adapters = [];
for (const adapter of adapterConfig) {
if (!adapter.Enabled) continue;
this.adapters.push(getAdapter(adapter, parameters));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export const Config: Schema<Config> = Schema.object({
.description("立即回复 @ 消息的概率"),
Filter: Schema.array(Schema.string())
.default(["你是", "You are", "吧", "呢"])
.description("过滤的词汇(防止被调皮群友/机器人自己搞傻)"),
.description("过滤的词汇(防止被调皮群友/机器人自己搞傻)可以使用正则表达式"),
}).description("记忆槽位设置"),

API: AdapterConfig,
Expand Down
4 changes: 3 additions & 1 deletion src/utils/toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Element, Session } from "koishi";
import { Mutex } from 'async-mutex';

import { Config } from "../config";
import { isEmpty } from "./string";


export function isChannelAllowed(slotContains: string[], channelId: string): boolean {
Expand Down Expand Up @@ -36,6 +37,7 @@ export function containsFilter(content: string, FilterList: string[]): boolean {
//return re.test(content);

for (const filter of FilterList) {
if (isEmpty(filter)) continue;
let regex = new RegExp(filter, "gi");
if (regex.test(content))
return true;
Expand Down Expand Up @@ -273,7 +275,7 @@ export function downloadFile(url, filePath, debug) {
};

/**
*
*
* @param date
* @returns 2024年12月3日星期二17:34:00
*/
Expand Down

0 comments on commit 6834c12

Please sign in to comment.