Skip to content

Commit

Permalink
feat: make works with simliar chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 2, 2023
1 parent 5185028 commit f9561ab
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 9 deletions.
9 changes: 7 additions & 2 deletions editor/src/components/editor/action/AiActionExecutor.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Editor } from "@tiptap/core";
import { ChangeForm, OutputForm, PromptAction } from "@/components/editor/defs/custom-action.type";
import { PromptCompiler, actionPosition } from "@/components/editor/action/PromptCompiler";
import { actionPosition, PromptCompiler } from "@/components/editor/action/PromptCompiler";

// @ts-ignore
import { MarkdownParser } from "node_modules/tiptap-markdown/src/parse/MarkdownParser";
import { BuiltinFunctionExecutor } from "@/components/editor/action/BuiltinFunctionExecutor";

export class AiActionExecutor {
private readonly editor: Editor;
Expand Down Expand Up @@ -93,7 +94,11 @@ export class AiActionExecutor {
}

public async execute(action: PromptAction) {
// builtinFunction
console.log("execute action", action)
if (action.builtinFunction) {
return new BuiltinFunctionExecutor(this.editor).execute(action);
}

const actionExecutor = new PromptCompiler(action, this.editor);
actionExecutor.compile();

Expand Down
42 changes: 42 additions & 0 deletions editor/src/components/editor/action/BuiltinFunctionExecutor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Editor } from "@tiptap/core";
import { BuiltInFunc, PromptAction } from "@/components/editor/defs/custom-action.type";

export class BuiltinFunctionExecutor {
private editor: Editor;

constructor(editor: Editor) {
this.editor = editor;
}

execute(action: PromptAction): Promise<string> | undefined {
switch (action.builtinFunction) {
case BuiltInFunc.GRAMMAR_CHECK:
break;
case BuiltInFunc.SIMILAR_CHUNKS:
return this.searchSimilarChunks(action);
case BuiltInFunc.SPELLING_CHECK:
break;
case BuiltInFunc.WIKI_SUMMARY:
break;
case BuiltInFunc.RELATED_CHUNKS:
break;
}
}

private searchSimilarChunks(action: PromptAction) {
return new Promise<string>((resolve, reject) => {
const selection = this.editor.state.selection;
const query = this.editor.state.doc.textBetween(selection.from, selection.to);

const response = fetch(`http://127.0.0.1:8080/api/embedding-document/search?q=${query}`, {
method: "GET",
});

response.then((res) => {
res.json().then((data) => {
resolve(data);
});
});
});
}
}
10 changes: 5 additions & 5 deletions editor/src/components/editor/defs/custom-action.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ export enum ChangeForm {
}

export enum BuiltInFunc {
SIMILAR_CHUNKS = 0,
RELATED_CHUNKS = 1,
GRAMMAR_CHECK = 2,
SPELLING_CHECK = 3,
WIKI_SUMMARY = 4,
SIMILAR_CHUNKS = "SIMILAR_CHUNKS",
RELATED_CHUNKS = "RELATED_CHUNKS",
GRAMMAR_CHECK = "GRAMMAR_CHECK",
SPELLING_CHECK = "SPELLING_CHECK",
WIKI_SUMMARY = "WIKI_SUMMARY",
}

export enum DefinedVariable {
Expand Down
4 changes: 2 additions & 2 deletions local_server/src/document_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};

use crate::app_state::AppState;

#[post("/embedding-document")]
#[post("/api/embedding-document")]
async fn create_embedding_document(
form: web::Form<ReqDocument>,
_data: web::Data<AppState>,
Expand All @@ -17,7 +17,7 @@ async fn create_embedding_document(
.body(response)
}

#[get("/embedding-document/search")]
#[get("/api/embedding-document/search")]
async fn search_embedding_document(
query: web::Query<SearchQuery>,
data: web::Data<AppState>,
Expand Down
1 change: 1 addition & 0 deletions local_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ async fn main() -> std::io::Result<()> {
App::new()
.wrap(Cors::default()
.allowed_origin("https://editor.unitmesh.cc")
.allowed_origin("http://localhost:3000")
.allowed_methods(vec!["GET", "POST"])
.allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT])
.allowed_header(http::header::CONTENT_TYPE)
Expand Down

0 comments on commit f9561ab

Please sign in to comment.