Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix LLM validation #5

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 51 additions & 22 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,26 +246,15 @@ export default class TextMoverPlugin extends Plugin {
};
});
}
// sort headings alphabetically
headings.sort((a, b) => a.heading.localeCompare(b.heading))
return headings
}

async sort_headings_via_llm(headings: Heading[], selection = "") {
const heading_str = headings.map(heading => heading.heading).join(", ")
const prompt = "For the text below, suggest top3 classes from one of the following classes(no fluff, no explaination, no numbering just the classes): \n" +
"```\n " +
"classes:" + heading_str + "\n" +
"text: " + selection
// api post call to openai
const chatCompletion = await this.llm_client.chat.completions.create({
messages: [{role: 'user', content: prompt}],
model: this.settings.modelName,
});

// get chosen classes
let chosen_classses: string[] = [];
const llm_response = chatCompletion.choices[0].message.content
process_headings_from_llm_response(llm_response: string | null) {
// alert notice the class
// split classes by ", " or newline
let chosen_classses: string[] = []
if (llm_response == null || llm_response.length == 0) {
chosen_classses = []
} else if (llm_response.includes("\n")) {
Expand All @@ -278,11 +267,43 @@ export default class TextMoverPlugin extends Plugin {
// strip non alphabetic characters
chosen_classses = chosen_classses.map((item: string) => item.trim().replace(/[^a-zA-Z /]/g, '').trim())
console.log(chosen_classses);
//sort heading by chosen_classes
headings.sort((a, b) => {
return -(chosen_classses.indexOf(a.heading) - chosen_classses.indexOf(b.heading));
})
return headings
return chosen_classses
}

async sort_headings_via_llm(headings: Heading[], selection = "", editor: Editor, callback: {
(): void;
(arg0: Heading[], arg1: string, arg2: Editor): void;
}) {
if (!this.settings.openAIapiKey) {
new Notice("OpenAI API key not set in the plugin settings. Returning default headings.")
callback(headings, selection, editor)
return
}
const heading_str = headings.map(heading => heading.heading).join(", ")
const prompt = "For the text below, suggest top3 classes from one of the following classes(no fluff, no explaination, no numbering just the classes): \n" +
"```\n " +
"classes:" + heading_str + "\n" +
"text: " + selection
// api post call to openai
await this.llm_client.chat.completions.create({
messages: [{role: 'user', content: prompt}],
model: this.settings.modelName,
}).then(
(response) => {
const llm_response = response.choices[0].message.content
const chosen_classses = this.process_headings_from_llm_response(llm_response)
headings.sort((a, b) => {
return -(chosen_classses.indexOf(a.heading) - chosen_classses.indexOf(b.heading));
})
// return headings
callback(headings, selection, editor)
},
(error) => {
// console.log("c2")
new Notice("Error: " + error.message)
callback(headings, selection, editor)
}
);
}

async sort_headings_via_bayesian(headings: Heading[], training_instances: object[], selection = "") {
Expand Down Expand Up @@ -446,7 +467,15 @@ export default class TextMoverPlugin extends Plugin {

// sort headings via LLM call
if (this.settings.classifier == "llm") {
headings = await this.sort_headings_via_llm(headings, selection)
await this.sort_headings_via_llm(headings, selection, editor, () => {
const hmodal = new HeadingSuggestionModal(this.app, headings, (result) => {
this.modal_submit_callback(result, editor)
}
);
hmodal.setPlaceholder(selection);
hmodal.open()
})
return
} else if (this.settings.classifier == "nbc") {
const training_instances = this.getTrainingInstancesFromFile(editor, file)
headings = await this.sort_headings_via_bayesian(headings, training_instances, selection)
Expand Down Expand Up @@ -580,7 +609,7 @@ class TextMoverSettingTab extends PluginSettingTab {
.onChange(async (value) => {
this.plugin.settings.openAIapiKey = value;
await this.plugin.saveSettings();
if (this.plugin.settings.classifier != "llm") {
if (this.plugin.settings.classifier == "llm") {
this.plugin.build_api();
}
}),
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "smart-text-mover",
"name": "Smart Text Mover",
"version": "1.0.3",
"version": "1.0.4",
"minAppVersion": "1.6.3",
"description": "Intelligent way to move text in file.",
"author": "Ankush-Chander",
Expand Down