forked from ChatGPTNextWeb/NextChat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ChatGPTNextWeb#2 add prompt list
- Loading branch information
Showing
6 changed files
with
206 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,4 +34,6 @@ yarn-error.log* | |
# typescript | ||
*.tsbuildinfo | ||
next-env.d.ts | ||
dev | ||
dev | ||
|
||
public/prompts.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { create } from "zustand"; | ||
import { persist } from "zustand/middleware"; | ||
import JsSearch from "js-search"; | ||
|
||
export interface Prompt { | ||
id?: number; | ||
shortcut: string; | ||
title: string; | ||
content: string; | ||
} | ||
|
||
export interface PromptStore { | ||
latestId: number; | ||
prompts: Map<number, Prompt>; | ||
|
||
add: (prompt: Prompt) => number; | ||
remove: (id: number) => void; | ||
search: (text: string) => Prompt[]; | ||
} | ||
|
||
export const PROMPT_KEY = "prompt-store"; | ||
|
||
export const SearchService = { | ||
ready: false, | ||
progress: 0, // 0 - 1, 1 means ready | ||
engine: new JsSearch.Search("prompts"), | ||
deleted: new Set<number>(), | ||
|
||
async init(prompts: PromptStore["prompts"]) { | ||
this.engine.addIndex("id"); | ||
this.engine.addIndex("shortcut"); | ||
this.engine.addIndex("title"); | ||
|
||
const n = prompts.size; | ||
let count = 0; | ||
for await (const prompt of prompts.values()) { | ||
this.engine.addDocument(prompt); | ||
count += 1; | ||
this.progress = count / n; | ||
} | ||
this.ready = true; | ||
}, | ||
|
||
remove(id: number) { | ||
this.deleted.add(id); | ||
}, | ||
|
||
add(prompt: Prompt) { | ||
this.engine.addDocument(prompt); | ||
}, | ||
|
||
search(text: string) { | ||
const results = this.engine.search(text) as Prompt[]; | ||
return results.filter((v) => !v.id || !this.deleted.has(v.id)); | ||
}, | ||
}; | ||
|
||
export const usePromptStore = create<PromptStore>()( | ||
persist( | ||
(set, get) => ({ | ||
latestId: 0, | ||
prompts: new Map(), | ||
|
||
add(prompt) { | ||
const prompts = get().prompts; | ||
prompt.id = get().latestId + 1; | ||
prompts.set(prompt.id, prompt); | ||
|
||
set(() => ({ | ||
latestId: prompt.id!, | ||
prompts: prompts, | ||
})); | ||
|
||
return prompt.id!; | ||
}, | ||
|
||
remove(id) { | ||
const prompts = get().prompts; | ||
prompts.delete(id); | ||
SearchService.remove(id); | ||
|
||
set(() => ({ | ||
prompts, | ||
})); | ||
}, | ||
|
||
search(text) { | ||
return SearchService.search(text) as Prompt[]; | ||
}, | ||
}), | ||
{ | ||
name: PROMPT_KEY, | ||
version: 1, | ||
} | ||
) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import fetch from "node-fetch"; | ||
import fs from "fs/promises"; | ||
|
||
const CN_URL = | ||
"https://raw.githubusercontent.com/PlexPt/awesome-chatgpt-prompts-zh/main/prompts-zh.json"; | ||
const EN_URL = | ||
"https://raw.githubusercontent.com/f/awesome-chatgpt-prompts/main/prompts.csv"; | ||
const FILE = "./public/prompts.json"; | ||
|
||
async function fetchCN() { | ||
console.log("[Fetch] fetching cn prompts..."); | ||
try { | ||
const raw = await (await fetch(CN_URL)).json(); | ||
return raw.map((v) => [v.act, v.prompt]); | ||
} catch (error) { | ||
console.error("[Fetch] failed to fetch cn prompts", error); | ||
return []; | ||
} | ||
} | ||
|
||
async function fetchEN() { | ||
console.log("[Fetch] fetching en prompts..."); | ||
try { | ||
const raw = await (await fetch(EN_URL)).text(); | ||
return raw | ||
.split("\n") | ||
.slice(1) | ||
.map((v) => v.split('","').map((v) => v.replace('"', ""))); | ||
} catch (error) { | ||
console.error("[Fetch] failed to fetch cn prompts", error); | ||
return []; | ||
} | ||
} | ||
|
||
async function main() { | ||
Promise.all([fetchCN(), fetchEN()]) | ||
.then(([cn, en]) => { | ||
fs.writeFile(FILE, JSON.stringify({ cn, en })); | ||
}) | ||
.catch((e) => { | ||
console.error("[Fetch] failed to fetch prompts"); | ||
fs.writeFile(FILE, JSON.stringify({ cn: [], en: [] })); | ||
}) | ||
.finally(() => { | ||
console.log("[Fetch] saved to " + FILE); | ||
}); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters