Skip to content

Commit

Permalink
feat: basic deno code for using wiki
Browse files Browse the repository at this point in the history
  • Loading branch information
linonetwo committed Nov 20, 2024
1 parent 3686183 commit a5cb577
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 2,602 deletions.
1 change: 1 addition & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DEEPSEEK_API_KEY=
9 changes: 7 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
{
"tasks": {
"dev": "deno run --watch main.ts"
"dev": "deno run --watch --allow-read --allow-net main.ts"
},
"imports": {
"@std/assert": "jsr:@std/assert@1",
"@std/fs": "jsr:@std/fs@^1.0.5",
"@std/path": "jsr:@std/path@^1.0.8"
"@std/path": "jsr:@std/path@^1.0.8",
"tiddlywiki": "npm:tiddlywiki@^5.3.6",
"tw5-typed": "npm:tw5-typed@^0.5.14"
},
"compilerOptions": {
"types": ["npm:tw5-typed"]
}
}
48 changes: 46 additions & 2 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions generateChatML.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { join } from "@std/path";
import "tw5-typed";
import { TiddlyWiki } from "tiddlywiki";

const wikiInstance = TiddlyWiki();
const wikiPath = join(Deno.cwd(), "wiki");
wikiInstance.boot.argv = [wikiPath, "--version"];
wikiInstance.boot.startup({});

export async function generateChatML(tidContent: string): Promise<string> {
const dataPromptTiddlers = wikiInstance.wiki.filterTiddlers(
`[!is[system]tag[Prompt]tag[Data]]`,
)
.map((title) => wikiInstance.wiki.getTiddler(title))
.filter((tiddler) => tiddler !== undefined)
.filter((tiddler) => tiddler.fields.prompt !== undefined);

const chatmlContents = await Promise.all(dataPromptTiddlers.map(async (tiddler) => {
const prompt = tiddler.fields.prompt as string;
const AIOutput = await callLLM(prompt, tidContent);
const chatmlContent = wikiInstance.wiki.renderTiddler("text/plain", tiddler.fields.title, {
variables: {
prompt,
AIOutput,
}
})
return chatmlContent;
}))


// 合并所有消息
return chatmlContents.join("");
}

async function callLLM(
systemPrompt: string,
userPrompt: string,
): Promise<string> {
// 调用 LLM API 的逻辑,提取为可替换的函数
// ...implementation...
return ""; // 返回助手的回复
}
8 changes: 5 additions & 3 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ensureDir } from '@std/fs';
import { join, extname, relative, dirname } from '@std/path';
import { generateChatML } from './generateChatML.ts';

async function readTidFilesAndCreateChatML(folderPath: string) {
const dataFolderPath = join('data', folderPath);
Expand Down Expand Up @@ -33,9 +34,10 @@ async function readTidFilesAndCreateChatML(folderPath: string) {
await processFolder(folderPath);
}

async function generateChatML(tidContent: string): Promise<string> {
// 生成 ChatML 内容的逻辑
return ``;
async function callLLM(systemPrompt: string, userPrompt: string): Promise<string> {
// 调用 LLM API 的逻辑,提取为可替换的函数
// ...implementation...
return ''; // 返回助手的回复
}

if (import.meta.main) {
Expand Down
Loading

0 comments on commit a5cb577

Please sign in to comment.