├── .gitattributes ├── README.md ├── index.html ├── index.js └── package.json /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Readme 2 | Using GPT-3 in [[logseq]] is actually remarkably easy. The folks over at OpenAI have built a really powerful model with [pretty good documentation](https://beta.openai.com/docs/api-reference/answers) 3 | All that you really need to do is send a post request to their site with the propper api key in the header of the message. 4 | This is all the code the plugin uses: 5 | ``` js 6 | let gpt3Options = { 7 | // "engine": "text-davinci-001", 8 | "prompt": "this is the text that gets sent", 9 | "temperature": 0.7, 10 | "max_tokens": 64, 11 | "top_p": 1, 12 | "frequency_penalty": 0, 13 | "presence_penalty": 0 14 | } 15 | 16 | function main () { 17 | logseq.Editor.registerSlashCommand( 18 | 'GPT-3', 19 | async () => { 20 | const { content, uuid } = await logseq.Editor.getCurrentBlock({includeChildren: true}) 21 | console.log(content) 22 | gpt3Options.prompt = content 23 | 24 | postData('https://api.openai.com/v1/engines/text-davinci-001/completions', gpt3Options) 25 | .then(data => { 26 | logseq.Editor.insertBlock(uuid, data.choices[0].text, { before: false, sibling: true }) 27 | console.log(data); 28 | }); 29 | 30 | } 31 | ) 32 | 33 | 34 | } 35 | 36 | async function postData(url = '', data = {}) { 37 | // Default options are marked with * 38 | const response = await fetch(url, { 39 | method: 'POST', // *GET, POST, PUT, DELETE, etc. 40 | mode: 'cors', // no-cors, *cors, same-origin 41 | cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached 42 | credentials: 'same-origin', // include, *same-origin, omit 43 | headers: { 44 | 'Content-Type': 'application/json', 45 | 'Authorization': 'Bearer PASTE-YOUR-APIKEY-HERE' 46 | // 'Content-Type': 'application/x-www-form-urlencoded', 47 | }, 48 | redirect: 'follow', // manual, *follow, error 49 | 50 | referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url 51 | body: JSON.stringify(data) // body data type must match "Content-Type" header 52 | }); 53 | return response.json(); // parses JSON response into native JavaScript objects 54 | } 55 | 56 | // bootstrap 57 | logseq.ready(main).catch(console.error) 58 | ``` 59 | 60 | All you need to do to use it is: 61 | - [Sign Up for OpenAI](https://beta.openai.com/signup) 62 | - [Copy your API KEY](https://beta.openai.com/account/api-keys) 63 | - Paste it into line 40. Make sure to leave the word 'Bearer' in the string- the line should look like `'Authorization': 'Bearer sk-salkfjasdpfijasfkjavvkblahblahsecretsecretkey'` 64 | - Load the unpacked plugin 65 | - Type some stuff you want to send to gpt-3 66 | - Use the slash command 'GPT-3' 67 | 68 | Things to note are that it only grabs the text of the current block, and the options at the top are somewhat arbitrary. Editing them suit your needs. 69 | There's a lot more that can be done with this model and plugin- feel free to improve on this 70 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | Logseq slash commands 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | let gpt3Options = { 2 | // "engine": "text-davinci-001", 3 | "prompt": "this is the text that gets sent", 4 | "temperature": 0.7, 5 | "max_tokens": 64, 6 | "top_p": 1, 7 | "frequency_penalty": 0, 8 | "presence_penalty": 0 9 | } 10 | 11 | function main () { 12 | logseq.Editor.registerSlashCommand( 13 | 'GPT-3', 14 | async () => { 15 | const { content, uuid } = await logseq.Editor.getCurrentBlock({includeChildren: true}) 16 | gpt3Options.prompt = content 17 | 18 | postData('https://api.openai.com/v1/engines/text-davinci-001/completions', gpt3Options) 19 | .then(data => { 20 | logseq.Editor.insertBlock(uuid, data.choices[0].text, { before: false, sibling: true }) 21 | }); 22 | 23 | } 24 | ) 25 | 26 | 27 | } 28 | 29 | async function postData(url = '', data = {}) { 30 | // Default options are marked with * 31 | const response = await fetch(url, { 32 | method: 'POST', // *GET, POST, PUT, DELETE, etc. 33 | mode: 'cors', // no-cors, *cors, same-origin 34 | cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached 35 | credentials: 'same-origin', // include, *same-origin, omit 36 | headers: { 37 | 'Content-Type': 'application/json', 38 | 'Authorization': 'Bearer PASTE-YOUR-APIKEY-HERE' 39 | // 'Content-Type': 'application/x-www-form-urlencoded', 40 | }, 41 | redirect: 'follow', // manual, *follow, error 42 | 43 | referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url 44 | body: JSON.stringify(data) // body data type must match "Content-Type" header 45 | }); 46 | return response.json(); // parses JSON response into native JavaScript objects 47 | } 48 | 49 | // bootstrap 50 | logseq.ready(main).catch(console.error) 51 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "GPT3", 3 | "version": "0.0.1", 4 | "description": "Generate GPT-3 Responses to Block Trees", 5 | "main": "index.html", 6 | "author": "JOhn", 7 | "license": "MIT", 8 | "logseq": { 9 | "id": "_r5fbt3wdc" 10 | } 11 | } --------------------------------------------------------------------------------