6 Commits
1.2.1 ... 1.3.0

Author SHA1 Message Date
Achiya Elyasaf
eb2a9ae867 Raise version 2023-12-04 13:53:23 +02:00
Achiya Elyasaf
a7b540de70 Merge pull request #7 from bThink-BGU/chat-api
Update OpenAI API
2023-12-04 13:52:21 +02:00
Achiya Elyasaf
116c4898d5 Replace the API to the new OpenAI Chat Completion API.
Closes #6.
2023-12-04 13:51:16 +02:00
Achiya Elyasaf
7c475c9de1 First commit for OpenAI's chat api 2023-12-04 12:50:01 +02:00
Achiya Elyasaf
15319c67ef Added an error message when API key is not set. 2023-12-03 09:55:51 +02:00
Achiya Elyasaf
693f3f2bce Fixed a bug in detecting manual changes of keyboard shortcuts 2023-11-19 15:35:15 +02:00
5 changed files with 61 additions and 30 deletions

View File

@@ -42,5 +42,5 @@
"manifest_version": 3,
"name": "LeafLLM",
"homepage_url": "https://github.com/achiyae/LeafLLM",
"version": "1.2.1"
"version": "1.3.0"
}

View File

@@ -1,7 +1,6 @@
<html>
<head>
<script src="/scripts/jquery.js"></script>
<script src="/scripts/utils.js" type="module"></script>
<script src="/popup/popup.js" type="module"></script>
<link rel="stylesheet" href="/popup/popup.css"/>
</head>

View File

@@ -17,17 +17,32 @@ async function refreshStorage() {
$('#api-token-form .api-token-status').text(chrome.runtime.lastError || !openAIAPIKey ? 'not set' : 'set')
})
const commands = await chrome.commands.getAll();
chrome.storage.local.get(['Improve', 'Complete', 'Ask']).then((settings) => {
Object.values(settings).forEach(setting => {
let command = commands.filter(({ name }) => name === setting.key)[0]
if(command.shortcut !== setting.shortcut) {
setting.shortcut = command.shortcut;
if(setting.status === 'enabled' && setting.shortcut === '') {
setting.status = 'error'
}
chrome.storage.local.set({ [setting.key]: setting });
} else if(setting.status === 'enabled' && setting.shortcut === '') {
setting.status = 'error'
chrome.storage.local.set({ [setting.key]: setting })
}
})
let bindingFailures = Object.values(settings)
.filter(({ status }) => status === 'error')
.map(({ key, shortcut }) => `${shortcut} for ${key}`)
.join(', ')
.map(({ key }) => `${key}`)
.join(', ');
if (bindingFailures.length > 0) {
addErrorMessage(`Could not bind the following shortcuts:\n${bindingFailures}.\nYou can set it manually at <a href="chrome://extensions/shortcuts">chrome://extensions/shortcuts</a>.`)
}
Object.values(settings).forEach(({ key, status, shortcut }) => {
$(`#settings-form input[name='text-${key}']:checkbox`).prop('checked', status === 'enabled')
let shortcut2 = status === 'error' ? 'not set' : shortcut
let shortcut2 = shortcut === '' ? 'not set' : shortcut
$(`#shortcut-${key}`).html(`<span>${shortcut2}</span>`)
})
})
@@ -64,7 +79,7 @@ async function handleAPITokenClear(event) {
.catch((error) => addErrorMessage(`Failed to remove API Token. Error: ${error}`))
}
async function makeHandleSettingChange(key) {
function makeHandleSettingChange(key) {
return async (event) => {
event.preventDefault()
event.stopPropagation()
@@ -72,10 +87,12 @@ async function makeHandleSettingChange(key) {
const value = event.target.checked
const setting = await chrome.storage.local.get(key)
if (setting[key].status !== 'error') {
/* let commandKey = await chrome.commands.getAll()
commandKey = commandKey.filter(({ name }) => name === key)[0]*/
// if (setting[key].status !== 'error') {
setting[key].status = value ? 'enabled' : 'disabled'
await chrome.storage.local.set({ [key]: setting[key] })
}
// }
return refreshStorage()
}
}

View File

@@ -1,6 +1,6 @@
class OpenAIAPI {
static defaultModel = 'text-davinci-003'
static defaultModel = 'gpt-3.5-turbo'
constructor(apiKey) {
this.apiKey = apiKey
@@ -11,6 +11,8 @@ class OpenAIAPI {
const url = `https://api.openai.com/v1/${endpoint}`
if (!data.model) data.model = OpenAIAPI.defaultModel
if (!data.n) data.n = 1
if (!data.temperature) data.temperature = 0.5
const xhr = new XMLHttpRequest()
xhr.open('POST', url, true)
@@ -34,27 +36,38 @@ class OpenAIAPI {
async completeText(text) {
const data = {
max_tokens: 512,
prompt: text,
n: 1,
temperature: 0.5
messages: [
{ role: 'system', content: 'You are an assistant in a Latex editor' },
{ role: 'user', 'content': text }
],
}
return this.query('completions', data)
.then(result => result[0].text)
return this.query('chat/completions', data)
.then(result => result[0]['message'].content)
}
async improveText(text) {
const data = {
model: 'code-davinci-edit-001',
input: text,
instruction:
'Correct any spelling mistakes, grammar mistakes, and improve the overall style of the (latex) text.',
n: 1,
temperature: 0.5
messages: [
{ role: 'system', content: 'You are an assistant in a Latex editor' },
{ role: 'user', 'content': 'Improve the following text:\n'+text }],
}
return this.query('edits', data)
.then(result => result[0].text)
return this.query('chat/completions', data)
.then(result => result[0]['message'].content)
}
async ask(text) {
const data = {
max_tokens: 512,
messages: [
{ role: 'system', content: 'You are an assistant in a Latex editor. Answer questions without introduction/explanations' },
{ role: 'user', 'content': text }
],
}
return this.query('chat/completions', data)
.then(result => result[0]['message'].content)
}
}
@@ -108,7 +121,7 @@ async function askHandler(openAI) {
const selection = window.getSelection()
const selectedText = selection.toString()
if (!selectedText) return
const editedText = (await openAI.completeText('In latex, ' + selectedText)).trimStart()
const editedText = (await openAI.ask(selectedText)).trimStart()
replaceSelectedText(editedText, selection)
}
@@ -181,6 +194,8 @@ chrome.runtime.onMessage.addListener(
setAPIKey(openAIAPIKey)
if (openAI) {
handleCommand(request.command)
} else {
error('OpenAI API key is not set, LeafLLM features are disabled.')
}
})
} else {

View File

@@ -20,13 +20,7 @@ function addListener(commandName) {
})
}
chrome.runtime.onInstalled.addListener((reason) => {
if (reason.reason === chrome.runtime.OnInstalledReason.INSTALL) {
checkCommandShortcuts()
}
})
// Only use this function during the initial install phase. After
// Only use this function during the initial installation phase. After
// installation the user may have intentionally unassigned commands.
// Example for install commands: [{"description":"","name":"_execute_action","shortcut":""},{"description":"Use the selected text to ask GPT. It adds to the beginning of the selected text: 'In Latex, '","name":"Ask","shortcut":""},{"description":"Complete selected text","name":"Complete","shortcut":""},{"description":"Improve selected text","name":"Improve","shortcut":""}]
async function checkCommandShortcuts() {
@@ -45,6 +39,12 @@ async function checkCommandShortcuts() {
})
}
chrome.runtime.onInstalled.addListener((reason) => {
if (reason.reason === chrome.runtime.OnInstalledReason.INSTALL) {
checkCommandShortcuts()
}
})
async function setup() {
addListener('Improve')
addListener('Complete')