mirror of
https://github.com/idootop/mi-gpt.git
synced 2025-04-12 16:39:39 +00:00
Merge pull request #160 from yanyao2333/fix-code-block-format
将AI返回内容中的markdown代码块转换为普通json
This commit is contained in:
commit
5d455e2ffe
|
@ -1,5 +1,6 @@
|
|||
import { Memory, Message, Room, User } from "@prisma/client";
|
||||
import { firstOf, lastOf } from "../../../utils/base";
|
||||
import { Logger } from "../../../utils/log";
|
||||
import { MemoryCRUD } from "../../db/memory";
|
||||
import { LongTermMemoryCRUD } from "../../db/memory-long-term";
|
||||
import { ShortTermMemoryCRUD } from "../../db/memory-short-term";
|
||||
|
@ -15,6 +16,7 @@ export class MemoryManager {
|
|||
* owner 为空时,即房间自己的公共记忆
|
||||
*/
|
||||
private owner?: User;
|
||||
private _logger = Logger.create({ tag: "Memory" });
|
||||
|
||||
constructor(room: Room, owner?: User) {
|
||||
this.room = room;
|
||||
|
@ -118,6 +120,7 @@ export class MemoryManager {
|
|||
lastMemory,
|
||||
});
|
||||
if (!newMemory) {
|
||||
this._logger.error("💀 生成短期记忆失败");
|
||||
return false;
|
||||
}
|
||||
const res = await ShortTermMemoryCRUD.addOrUpdate({
|
||||
|
@ -151,6 +154,7 @@ export class MemoryManager {
|
|||
lastMemory,
|
||||
});
|
||||
if (!newMemory) {
|
||||
this._logger.error("💀 生成长期记忆失败");
|
||||
return false;
|
||||
}
|
||||
const res = await LongTermMemoryCRUD.addOrUpdate({
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import { LongTermMemory, ShortTermMemory } from "@prisma/client";
|
||||
import { jsonDecode, lastOf } from "../../../utils/base";
|
||||
import { lastOf } from "../../../utils/base";
|
||||
import { buildPrompt } from "../../../utils/string";
|
||||
import { cleanJsonAndDecode } from "../../../utils/parse";
|
||||
import { openai } from "../../openai";
|
||||
import { MessageContext } from "../conversation";
|
||||
|
||||
|
@ -67,6 +68,6 @@ export class LongTermMemoryAgent {
|
|||
shortTermMemory: lastOf(newMemories)!.text,
|
||||
}),
|
||||
});
|
||||
return jsonDecode(res?.content)?.longTermMemories?.toString();
|
||||
return cleanJsonAndDecode(res?.content)?.longTermMemories?.toString();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { Memory, Message, ShortTermMemory, User } from "@prisma/client";
|
||||
import { jsonDecode } from "../../../utils/base";
|
||||
import { cleanJsonAndDecode } from "../../../utils/parse";
|
||||
import { buildPrompt, formatMsg } from "../../../utils/string";
|
||||
import { openai } from "../../openai";
|
||||
import { MessageContext } from "../conversation";
|
||||
|
@ -78,6 +78,6 @@ export class ShortTermMemoryAgent {
|
|||
.join("\n"),
|
||||
}),
|
||||
});
|
||||
return jsonDecode(res?.content)?.shortTermMemories?.toString();
|
||||
return cleanJsonAndDecode(res?.content)?.shortTermMemories?.toString();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,8 @@ import {
|
|||
getMiIOT,
|
||||
getMiNA,
|
||||
} from "mi-service-lite";
|
||||
import { clamp, jsonEncode, sleep } from "../../utils/base";
|
||||
import { clamp, sleep } from "../../utils/base";
|
||||
import { jsonEncode } from "../../utils/parse";
|
||||
import { Logger } from "../../utils/log";
|
||||
import { StreamResponse } from "./stream";
|
||||
import { kAreYouOK } from "../../utils/string";
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { isEmpty } from "./is";
|
||||
import { jsonEncode } from "./parse"
|
||||
|
||||
export function timestamp() {
|
||||
return new Date().getTime();
|
||||
|
@ -87,24 +88,6 @@ export function toSet<T = any>(datas: T[], byKey?: (e: T) => any) {
|
|||
return Array.from(new Set(datas));
|
||||
}
|
||||
|
||||
export function jsonEncode(obj: any, options?: { prettier?: boolean }) {
|
||||
const { prettier } = options ?? {};
|
||||
try {
|
||||
return prettier ? JSON.stringify(obj, undefined, 4) : JSON.stringify(obj);
|
||||
} catch (error) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
export function jsonDecode(json: string | null | undefined) {
|
||||
if (json == undefined) return undefined;
|
||||
try {
|
||||
return JSON.parse(json!);
|
||||
} catch (error) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
export function withDefault<T = any>(e: any, defaultValue: T): T {
|
||||
return isEmpty(e) ? defaultValue : e;
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import fs from "fs-extra";
|
||||
import path from "path";
|
||||
|
||||
import { jsonDecode, jsonEncode } from "./base";
|
||||
import { jsonDecode, jsonEncode } from "./parse";
|
||||
|
||||
export const kRoot = process.cwd();
|
||||
|
||||
|
|
32
src/utils/parse.ts
Normal file
32
src/utils/parse.ts
Normal file
|
@ -0,0 +1,32 @@
|
|||
/**
|
||||
* 清理掉json字符串前后的其他字符,并解码
|
||||
*/
|
||||
export function cleanJsonAndDecode(input: string | undefined | null) {
|
||||
if (input == undefined) return undefined;
|
||||
const pattern = /(\{[\s\S]*?"\s*:\s*[\s\S]*?})/;
|
||||
const match = input.match(pattern);
|
||||
|
||||
if (!match) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return jsonDecode(match[0]);
|
||||
}
|
||||
|
||||
export function jsonEncode(obj: any, options?: { prettier?: boolean }) {
|
||||
const { prettier } = options ?? {};
|
||||
try {
|
||||
return prettier ? JSON.stringify(obj, undefined, 4) : JSON.stringify(obj);
|
||||
} catch (error) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
export function jsonDecode(json: string | null | undefined) {
|
||||
if (json == undefined) return undefined;
|
||||
try {
|
||||
return JSON.parse(json!);
|
||||
} catch (error) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user