Skip to content

Commit

Permalink
New version 1.1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
ittuann committed May 8, 2023
1 parent f2bdcd3 commit 53ef7aa
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 399 deletions.
66 changes: 40 additions & 26 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var DEFAULT_SETTINGS = {
maxTokens: 16,
defaultPrompt: "",
insertionMode: "end",
displayTokensUsage: true,
showSidebarIcon: true
};
var LightweightChatGPTPlugin = class extends import_obsidian.Plugin {
Expand Down Expand Up @@ -178,18 +179,16 @@ ${this.plugin.settings.defaultPrompt}
responseDividerLine.style.display = "none";
this.outputContainer = contentEl.createEl("div");
this.outputContainer.classList.add("output-container");
const buttonsContainer = contentEl.createEl("div");
buttonsContainer.style.display = "flex";
buttonsContainer.style.marginTop = "1rem";
const copyToClipboardButton = buttonsContainer.createEl("button", {
text: "Copy to clipboard"
}, (el) => {
el.style.backgroundColor = "green";
el.style.color = "white";
});
this.displayTokensUsageContainer = contentEl.createEl("div");
this.displayTokensUsageContainer.classList.add("display-tokens-usage-container");
const buttonsBottomContainer = contentEl.createEl("div");
buttonsBottomContainer.classList.add("buttons-bottom-container");
const copyToClipboardButton = buttonsBottomContainer.createEl("button", { text: "Copy to clipboard" });
copyToClipboardButton.style.marginRight = "1rem";
copyToClipboardButton.style.backgroundColor = "green";
copyToClipboardButton.style.color = "white";
copyToClipboardButton.style.display = "none";
const addToPostButton = buttonsContainer.createEl("button", { text: "Add to current document" });
const addToPostButton = buttonsBottomContainer.createEl("button", { text: "Add to current document" });
addToPostButton.style.marginRight = "1rem";
addToPostButton.style.display = "none";
sendButton.addEventListener("click", async () => {
Expand All @@ -202,7 +201,7 @@ ${this.plugin.settings.defaultPrompt}
return;
}
if (!this.inputTextArea.value) {
new import_obsidian.Notice("Please Enter text");
new import_obsidian.Notice("Please enter text");
return;
}
if (this.isSendingRequest) {
Expand All @@ -214,11 +213,8 @@ ${this.plugin.settings.defaultPrompt}
try {
new import_obsidian.Notice("Sending...");
this.responseAPIText = await this.sendRequestToChatGPT();
if (!this.responseAPIText) {
if (this.responseAPIText && this.responseAPIText.trim() !== "") {
this.outputContainer.empty();
responseDividerLine.style.display = "none";
copyToClipboardButton.style.display = "none";
addToPostButton.style.display = "none";
}
this.outputContainer.createEl("p", { text: this.responseAPIText });
sendButton.textContent = "Send";
Expand All @@ -239,6 +235,14 @@ ${this.plugin.settings.defaultPrompt}
this.appendToCurrentNote(this.inputTextArea.value, this.responseAPIText, this.plugin.settings.insertionMode);
});
}
async displayTokensUsage(promptTokens, completionTokens, totalTokens) {
this.displayTokensUsageContainer.empty();
this.displayTokensUsageContainer.createEl("p", {
text: `Tokens Usage Prompt: ${promptTokens} /
Completion: ${completionTokens} /
Total: ${totalTokens}`
});
}
async sendRequestToChatGPT() {
const maxTokens = parseInt(this.maxTokensInput.value);
try {
Expand All @@ -262,6 +266,12 @@ ${this.plugin.settings.defaultPrompt}
const currentResult = JSON.parse(response);
if (currentResult.choices && currentResult.choices.length > 0) {
const gptResponse = currentResult.choices[0].message.content;
const promptTokens = currentResult.usage.prompt_tokens;
const completionTokens = currentResult.usage.completion_tokens;
const totalTokens = currentResult.usage.total_tokens;
if (this.plugin.settings.displayTokensUsage) {
this.displayTokensUsage(promptTokens, completionTokens, totalTokens);
}
return gptResponse;
} else if (currentResult.error) {
throw new Error(JSON.stringify(currentResult.error));
Expand Down Expand Up @@ -367,6 +377,9 @@ var LightweightChatGPTSettingTab = class extends import_obsidian.PluginSettingTa
containerEl.createEl("h6", { text: "ChatGPT Model setting" });
new import_obsidian.Setting(containerEl).setName("Temperature").setDesc("Enter the temperature value between 0 and 2 (inclusive) for the API response").addText((text) => text.setPlaceholder("Enter temperature").setValue(this.plugin.settings.temperature.toString()).onChange(async (value) => {
let parsedValue = parseFloat(value);
if (isNaN(parsedValue)) {
parsedValue = 1;
}
if (parsedValue < 0) {
parsedValue = 0;
} else if (parsedValue > 2) {
Expand All @@ -381,10 +394,10 @@ var LightweightChatGPTSettingTab = class extends import_obsidian.PluginSettingTa
if (this.plugin.settings.chatGPTModel === "gpt-4") {
parsedMaxValue = 4096;
}
if (parsedValue < 1) {
parsedValue = 1;
} else if (parsedValue > parsedMaxValue) {
if (isNaN(parsedValue) || parsedValue > parsedMaxValue) {
parsedValue = parsedMaxValue;
} else if (parsedValue < 1) {
parsedValue = 1;
}
this.plugin.settings.maxTokens = parsedValue;
await this.plugin.saveSettings();
Expand All @@ -400,6 +413,10 @@ var LightweightChatGPTSettingTab = class extends import_obsidian.PluginSettingTa
this.plugin.settings.insertionMode = value;
await this.plugin.saveSettings();
}));
new import_obsidian.Setting(containerEl).setName("Display Tokens Usage").setDesc("Toggle to display or hide the number of Tokens used per request.").addToggle((toggle) => toggle.setValue(this.plugin.settings.displayTokensUsage).onChange(async (value) => {
this.plugin.settings.displayTokensUsage = value;
await this.plugin.saveSettings();
}));
new import_obsidian.Setting(containerEl).setName("Show Sidebar Icon").setDesc("Toggle to show or hide the sidebar icon").addToggle((toggle) => toggle.setValue(this.plugin.settings.showSidebarIcon).onChange(async (value) => {
this.plugin.settings.showSidebarIcon = value;
await this.plugin.saveSettings();
Expand All @@ -419,20 +436,17 @@ var LightweightChatGPTSettingTab = class extends import_obsidian.PluginSettingTa
const githubAnchor = githubLink.createEl("a", {
cls: "settings-github-link"
});
const githubLogo = githubAnchor.createEl("img", {
cls: "settings-github-logo"
});
githubAnchor.href = "https://github.com/ittuann/obsidian-gpt-liteinquirer-plugin";
githubAnchor.target = "_blank";
githubAnchor.rel = "noopener";
const githubText = githubAnchor.createEl("span", {
text: "View on GitHub"
const githubLogo = githubAnchor.createEl("img", {
cls: "settings-github-logo"
});
githubLogo.src = "https://assets.stickpng.com/images/5847f98fcef1014c0b5e48c0.png";
githubLogo.alt = "GitHub";
githubLogo.style.width = "24px";
githubLogo.style.height = "24px";
githubLogo.style.verticalAlign = "middle";
const githubText = githubAnchor.createEl("span", {
text: "View on GitHub"
});
githubText.style.display = "inline-block";
githubText.style.verticalAlign = "middle";
}
Expand Down
86 changes: 58 additions & 28 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface LightweightChatGPTPluginSettings {
maxTokens: number;
defaultPrompt: string;
insertionMode: string;
displayTokensUsage: boolean;
showSidebarIcon: boolean;
}

Expand All @@ -32,6 +33,7 @@ const DEFAULT_SETTINGS: LightweightChatGPTPluginSettings = {
maxTokens: 16,
defaultPrompt: '',
insertionMode: 'end',
displayTokensUsage: true,
showSidebarIcon: true
}

Expand Down Expand Up @@ -134,6 +136,7 @@ class LightweightChatGPTWindow extends Modal {

private inputTextArea: HTMLTextAreaElement;
private outputContainer: HTMLElement;
private displayTokensUsageContainer: HTMLElement;
private maxTokensInput: HTMLInputElement;

private responseAPIText: string;
Expand Down Expand Up @@ -223,19 +226,19 @@ class LightweightChatGPTWindow extends Modal {
this.outputContainer = contentEl.createEl('div');
this.outputContainer.classList.add('output-container');

// Tokens Container
this.displayTokensUsageContainer = contentEl.createEl('div');
this.displayTokensUsageContainer.classList.add('display-tokens-usage-container');

// Add Other Button
const buttonsContainer = contentEl.createEl('div');
buttonsContainer.style.display = 'flex';
buttonsContainer.style.marginTop = '1rem';
const copyToClipboardButton = buttonsContainer.createEl('button', {
text: 'Copy to clipboard'
}, (el: HTMLButtonElement) => {
el.style.backgroundColor = 'green';
el.style.color = 'white';
});
const buttonsBottomContainer = contentEl.createEl('div');
buttonsBottomContainer.classList.add('buttons-bottom-container');
const copyToClipboardButton = buttonsBottomContainer.createEl('button', { text: 'Copy to clipboard' });
copyToClipboardButton.style.marginRight = '1rem';
copyToClipboardButton.style.backgroundColor = 'green';
copyToClipboardButton.style.color = 'white';
copyToClipboardButton.style.display = 'none';
const addToPostButton = buttonsContainer.createEl('button', { text: 'Add to current document' });
const addToPostButton = buttonsBottomContainer.createEl('button', { text: 'Add to current document' });
addToPostButton.style.marginRight = '1rem';
addToPostButton.style.display = 'none';

Expand All @@ -252,7 +255,7 @@ class LightweightChatGPTWindow extends Modal {
}

if (!this.inputTextArea.value) {
new Notice('Please Enter text');
new Notice('Please enter text');
return;
}

Expand All @@ -269,12 +272,10 @@ class LightweightChatGPTWindow extends Modal {
new Notice('Sending...');
this.responseAPIText = await this.sendRequestToChatGPT();

if (!this.responseAPIText) {
if (this.responseAPIText && this.responseAPIText.trim() !== '') {
this.outputContainer.empty();
responseDividerLine.style.display = 'none';
copyToClipboardButton.style.display = 'none';
addToPostButton.style.display = 'none';
}

this.outputContainer.createEl('p', { text: this.responseAPIText });

sendButton.textContent = 'Send';
Expand All @@ -298,6 +299,16 @@ class LightweightChatGPTWindow extends Modal {
});
}

async displayTokensUsage(promptTokens: number, completionTokens: number, totalTokens: number) {
this.displayTokensUsageContainer.empty();

this.displayTokensUsageContainer.createEl('p', {
text: `Tokens Usage Prompt: ${promptTokens} /
Completion: ${completionTokens} /
Total: ${totalTokens}`
});
}

async sendRequestToChatGPT() {
const maxTokens = parseInt(this.maxTokensInput.value);

Expand All @@ -323,6 +334,15 @@ class LightweightChatGPTWindow extends Modal {
const currentResult = JSON.parse(response);
if (currentResult.choices && currentResult.choices.length > 0) {
const gptResponse = currentResult.choices[0].message.content;

const promptTokens = currentResult.usage.prompt_tokens;
const completionTokens = currentResult.usage.completion_tokens;
const totalTokens = currentResult.usage.total_tokens;

if (this.plugin.settings.displayTokensUsage) {
this.displayTokensUsage(promptTokens, completionTokens, totalTokens);
}

return gptResponse;
} else if (currentResult.error) {
throw new Error(JSON.stringify(currentResult.error));
Expand Down Expand Up @@ -353,7 +373,7 @@ class LightweightChatGPTWindow extends Modal {
new Notice('No text to add');
return;
}

const activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
const activeLeaf = activeView?.leaf;
if (activeView && activeLeaf && activeLeaf.view instanceof MarkdownView) {
Expand Down Expand Up @@ -475,6 +495,9 @@ class LightweightChatGPTSettingTab extends PluginSettingTab {
.setValue(this.plugin.settings.temperature.toString())
.onChange(async (value) => {
let parsedValue = parseFloat(value);
if (isNaN(parsedValue)) {
parsedValue = 1;
}
if (parsedValue < 0) {
parsedValue = 0;
} else if (parsedValue > 2) {
Expand All @@ -498,10 +521,10 @@ class LightweightChatGPTSettingTab extends PluginSettingTab {
if (this.plugin.settings.chatGPTModel === "gpt-4") {
parsedMaxValue = 4096;
}
if (parsedValue < 1) {
parsedValue = 1;
} else if (parsedValue > parsedMaxValue) {
if (isNaN(parsedValue) || parsedValue > parsedMaxValue) {
parsedValue = parsedMaxValue;
} else if (parsedValue < 1) {
parsedValue = 1;
}
this.plugin.settings.maxTokens = parsedValue;
await this.plugin.saveSettings();
Expand Down Expand Up @@ -534,6 +557,16 @@ class LightweightChatGPTSettingTab extends PluginSettingTab {
await this.plugin.saveSettings();
}));

new Setting(containerEl)
.setName('Display Tokens Usage')
.setDesc('Toggle to display or hide the number of Tokens used per request.')
.addToggle(toggle => toggle
.setValue(this.plugin.settings.displayTokensUsage)
.onChange(async (value) => {
this.plugin.settings.displayTokensUsage = value;
await this.plugin.saveSettings();
}));

new Setting(containerEl)
.setName('Show Sidebar Icon')
.setDesc('Toggle to show or hide the sidebar icon')
Expand All @@ -560,22 +593,19 @@ class LightweightChatGPTSettingTab extends PluginSettingTab {
const githubAnchor = githubLink.createEl('a', {
cls: 'settings-github-link',
});
const githubLogo = githubAnchor.createEl('img', {
cls: 'settings-github-logo',
});
githubAnchor.href = 'https://github.com/ittuann/obsidian-gpt-liteinquirer-plugin';
githubAnchor.target = '_blank';
githubAnchor.rel = 'noopener';

const githubLogo = githubAnchor.createEl('img', {
cls: 'settings-github-logo',
});
githubLogo.src = 'https://assets.stickpng.com/images/5847f98fcef1014c0b5e48c0.png';
githubLogo.alt = 'GitHub';

const githubText = githubAnchor.createEl('span', {
text: 'View on GitHub',
});

githubLogo.src = 'https://assets.stickpng.com/images/5847f98fcef1014c0b5e48c0.png';
githubLogo.alt = 'GitHub';
githubLogo.style.width = '24px';
githubLogo.style.height = '24px';
githubLogo.style.verticalAlign = 'middle';
githubText.style.display = 'inline-block';
githubText.style.verticalAlign = 'middle';
}
Expand Down
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"id": "gpt-liteinquirer",
"name": "GPT-LiteInquirer",
"version": "1.0.0",
"version": "1.1.5",
"minAppVersion": "0.15.0",
"description": "Experience OpenAI ChatGPT assistance directly within Obsidian, drafting content without interrupting your creative flow.",
"author": "ittuann",
"authorUrl": "https://github.com/ittuann",
"fundingUrl": "",
"isDesktopOnly": false
}
}
Loading

0 comments on commit 53ef7aa

Please sign in to comment.