├── docs
├── audio.md
├── chat.md
├── files.md
├── image.md
├── models.md
├── moderations.md
├── assets
│ └── apl385.ttf
├── img
│ ├── SquidError.PNG
│ ├── favicon-32.png
│ └── dyalog-white.svg
├── release-notes.md
├── LICENSE.md
├── css
│ └── main.css
├── quickstart.md
├── index.md
├── userguide.md
└── demos.md
├── .gitignore
├── demos
├── demos.zip
├── Chat.aplf
├── ShowImage.aplf
├── ShowText.aplf
├── Play.aplf
├── Linguist.aplf
├── Image.aplf
├── Translation.aplf
├── Setup.aplf
├── Transcription.aplf
├── Speech.aplf
└── OpenAI.apln
├── README.md
├── apl-package.json
├── LICENSE
├── mkdocs.yml
└── source
└── OpenAI.apln
/docs/audio.md:
--------------------------------------------------------------------------------
1 | Pending publication
--------------------------------------------------------------------------------
/docs/chat.md:
--------------------------------------------------------------------------------
1 | Pending publication
--------------------------------------------------------------------------------
/docs/files.md:
--------------------------------------------------------------------------------
1 | Pending publication
--------------------------------------------------------------------------------
/docs/image.md:
--------------------------------------------------------------------------------
1 | Pending publication
--------------------------------------------------------------------------------
/docs/models.md:
--------------------------------------------------------------------------------
1 | Pending publication
--------------------------------------------------------------------------------
/docs/moderations.md:
--------------------------------------------------------------------------------
1 | Pending publication
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.dlf
2 | *.rng*
3 | site/
4 | demos/demos.zip
--------------------------------------------------------------------------------
/demos/demos.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Dyalog/OpenAI/main/demos/demos.zip
--------------------------------------------------------------------------------
/docs/assets/apl385.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Dyalog/OpenAI/main/docs/assets/apl385.ttf
--------------------------------------------------------------------------------
/docs/img/SquidError.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Dyalog/OpenAI/main/docs/img/SquidError.PNG
--------------------------------------------------------------------------------
/docs/img/favicon-32.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Dyalog/OpenAI/main/docs/img/favicon-32.png
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # OpenAI
2 | Dyalog APL interface to OpenAI API
3 |
4 | Documentation can be found [here](https://dyalog.github.io/OpenAI).
5 |
--------------------------------------------------------------------------------
/docs/release-notes.md:
--------------------------------------------------------------------------------
1 | ## Version 0.4.0
2 | This is the initial release of `OpenAI`. It implements interfaces to the following OpenAI endpoints: audio, chat, files, image, models, and moderations. Additional interfaces to other OpenAI endpoints will be available in future releases.
3 |
4 | A set of demos is available in the demos folder
--------------------------------------------------------------------------------
/apl-package.json:
--------------------------------------------------------------------------------
1 | {
2 | api: "OpenAI",
3 | assets: "",
4 | description: "Utilities to interface with the OpenAI API",
5 | documentation: "https://dyalog.github.io/OpenAI",
6 | files: "",
7 | group: "dyalog",
8 | io: 1,
9 | license: "MIT",
10 | lx: "",
11 | maintainer: "",
12 | minimumAplVersion: "18.0",
13 | ml: 1,
14 | name: "OpenAI",
15 | os_lin: 1,
16 | os_mac: 1,
17 | os_win: 1,
18 | project_url: "https://github.com/Dyalog/OpenAI",
19 | source: "source/OpenAI.apln",
20 | tags: "mac-os,windows,linux,dyalog,pi,llm,openai",
21 | userCommandScript: "",
22 | version: "0.1.0+1",
23 | }
24 |
--------------------------------------------------------------------------------
/demos/Chat.aplf:
--------------------------------------------------------------------------------
1 | Chat;⎕ML;⎕IO;inp;system;c;user;resp;msg
2 | (⎕ML ⎕IO)←1 1
3 |
4 | ⍝ Check that OpenAI is present
5 | :If 9.1≠⎕NC⊂'OpenAI' ⋄ →0⊣⎕←'OpenAI is not loaded, please run Setup' ⋄ :EndIf
6 |
7 | ⍝ Make sure the OpenAI.APIKey is set
8 | :If 0∊⍴OpenAI.APIKey ⋄ →0⊣⎕←'OpenAPI.APIKey has not been set, please run Setup' ⋄ :EndIf
9 |
10 | inp←{inp←⍞⊣⍞←⍵ ⋄ inp↓⍨+/∧\=⌿↑inp ⍵}
11 |
12 | system←inp'Describe the assistant you would like to chat with: '
13 | c←OpenAI.Chat.Completion system
14 |
15 | loop:→0⍴⍨0∊⍴user←inp'Enter user message: '
16 | c.User user
17 | resp←c.Run
18 |
19 | :If 0≠resp.rc ⋄ msg←'** OpenAI Chat request failed due to: ',resp.msg
20 | :ElseIf 200≠resp.HttpStatus ⋄ msg←{0::⍵.HttpStatus ⋄ ⍵.Data.error.message}resp
21 | :Else ⋄ →loop⊣⎕←1⌽(2⍴⎕UCS 13),80 OpenAI.U.Wrap (⊢/c.messages).content
22 | :EndIf
23 | →loop⊣⎕←'** ',msg
24 |
--------------------------------------------------------------------------------
/demos/ShowImage.aplf:
--------------------------------------------------------------------------------
1 | ShowImage args;⎕ML;⎕IO;h
2 | (⎕ML ⎕IO)←1 1
3 | ⍝ Shows text in an HTMLRenderer window
4 | ⍝ args is either the text to Show
5 | ⍝ or the result from Transcription or Translation which is a return code followed by a message
6 | ⍝ If the return code is 0, the message is the text to show
7 | ⍝ If the return code is not 0, the message is an indication of what went wrong
8 | :If 2=|≡args
9 | :If 0≠⊃args
10 | →0⊣⎕←'Unable to create image due to ',⍕args
11 | :Else ⋄ args←2⊃args
12 | :EndIf
13 | :EndIf
14 |
15 | ⍝ now try to open an HTMLRenderer to display the image
16 | :Trap 0
17 | 'h'⎕WC'HTMLRenderer'('Coord' 'Pixel')('Size'(720 700))('HTML'(''))
18 | {}⎕DQ h
19 | :Else
20 | →0⊣⎕←⎕DMX.('A ',EM,' (',Message,') occurred while trying to play the audio file')
21 | :EndTrap
22 |
--------------------------------------------------------------------------------
/demos/ShowText.aplf:
--------------------------------------------------------------------------------
1 | ShowText args;⎕ML;⎕IO;h
2 | (⎕ML ⎕IO)←1 1
3 | ⍝ Shows text in an HTMLRenderer window
4 | ⍝ args is either the text to Show
5 | ⍝ or the result from Transcription or Translation which is a return code followed by a message
6 | ⍝ If the return code is 0, the message is the text to show
7 | ⍝ If the return code is not 0, the message is an indication of what went wrong
8 | :If 2=|≡args
9 | :If 0≠⊃args
10 | →0⊣⎕←'Unable to convert the your text due to ',⍕args
11 | :Else ⋄ args←2⊃args
12 | :EndIf
13 | :EndIf
14 |
15 | ⍝ now try to open an HTMLRenderer to play the audiofile
16 | :Trap 0
17 | 'h'⎕WC'HTMLRenderer'('Coord' 'Prop')('Size'(75 75))('HTML'('
',('\. '⎕R'.'⊢args),'
'))
18 | {}⎕DQ h
19 | :Else
20 | →0⊣⎕←⎕DMX.('A ',EM,' (',Message,') occurred while trying to play the audio file')
21 | :EndTrap
22 |
--------------------------------------------------------------------------------
/demos/Play.aplf:
--------------------------------------------------------------------------------
1 | Play args;⎕ML;⎕IO;data;h;html
2 | (⎕ML ⎕IO)←1 1
3 | ⍝ Plays an audio file
4 | ⍝ args is either the name of a .mp3 audio file to play
5 | ⍝ or the result from Speech which is a return code followed by a message
6 | ⍝ If the return code is 0, the message is the name of a .mp3 audio file
7 | ⍝ If the return code is not 0, the message is an indication of what went wrong
8 | ⍝ auto is optional and indicates whether to automatically play the audio
9 | :If 2=|≡args
10 | :If 0≠⊃args
11 | →0⊣⎕←'Speech was unable to convert the your text due to ',⍕args
12 | :Else ⋄ args←2⊃args
13 | :EndIf
14 | :EndIf
15 |
16 | ⍝ now try to open an HTMLRenderer to play the audiofile
17 | :Trap 0
18 | data←OpenAI.HttpCommand.Base64Encode{(⎕NUNTIE t)⊢⎕NREAD t,83,2↑t←⍵ ⎕NTIE 0}args
19 | html←''
20 | 'h'⎕WC'HTMLRenderer'('Size'(15 25))('HTML'html)
21 | {}⎕DQ h
22 | :Else
23 | →0⊣⎕←⎕DMX.('A ',EM,' (',Message,') occurred while trying to play the audio file')
24 | :EndTrap
25 |
--------------------------------------------------------------------------------
/demos/Linguist.aplf:
--------------------------------------------------------------------------------
1 | Linguist;⎕ML;⎕IO;inp;system;c;user;resp;msg;audio;tmpfile
2 | (⎕ML ⎕IO)←1 1
3 |
4 | ⍝ Check that OpenAI is present
5 | :If 9.1≠⎕NC⊂'OpenAI' ⋄ →0⊣⎕←'OpenAI is not loaded, please run Setup' ⋄ :EndIf
6 |
7 | ⍝ Make sure the OpenAI.APIKey is set
8 | :If 0∊⍴OpenAI.APIKey ⋄ →0⊣⎕←'OpenAPI.APIKey has not been set, please run Setup' ⋄ :EndIf
9 |
10 | inp←{inp←⍞⊣⍞←⍵ ⋄ inp↓⍨+/∧\=⌿↑inp ⍵}
11 | tmpfile←(739⌶0),'/linguist.mp3' ⍝ temp file for audio
12 |
13 | →end⍴⍨0∊⍴system←inp'What output language would you like? '
14 | audio←'y'=⊃⎕C inp'Would you like audio output (y/N)? '
15 | c←OpenAI.Chat.Completion('translate the input into ',system)
16 |
17 | loop:→end⍴⍨0∊⍴user←inp'Text to translate: '
18 | c.User user
19 | resp←c.Run
20 |
21 | :If 0≠resp.rc ⋄ msg←'** OpenAI Chat request failed due to: ',resp.msg
22 | :ElseIf 200≠resp.HttpStatus ⋄ msg←{0::⍵.HttpStatus ⋄ ⍵.Data.error.message}resp
23 | :Else
24 | ⎕←(⎕UCS 13),⍨80 OpenAI.U.Wrap msg←(⊢/c.messages).content
25 | :If audio
26 | Play tmpfile Speech msg
27 | :EndIf
28 | →loop
29 | :EndIf
30 | →loop⊣⎕←'** ',msg
31 | end:1 ⎕NDELETE tmpfile
32 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2024 Dyalog
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/docs/LICENSE.md:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2024 Dyalog
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/demos/Image.aplf:
--------------------------------------------------------------------------------
1 | (rc msg)←Image description;req;resp;text;⎕ML;⎕IO
2 | ⍝ This demonstrates using OpenAI to generate an image file from a description
3 | ⍝ description is text describing to the image to generate
4 | ⍝ rc is the return code and is 0 if the image generation was successful, otherwise it will be non-0
5 | ⍝ msg is either the base64-encoded image data if the generation was successful or a message explaining why the generation failed
6 | (⎕ML ⎕IO)←1 1
7 | rc←¯1
8 |
9 | ⍝ Check that OpenAI is present
10 | :If 9.1≠⎕NC⊂'OpenAI' ⋄ →0⊣text←'OpenAI is not loaded, please run Setup' ⋄ :EndIf
11 |
12 | ⍝ Make sure the OpenAI.APIKey is set
13 | :If 0∊⍴OpenAI.APIKey ⋄ →0⊣text←'OpenAPI.APIKey has not been set, please run Setup' ⋄ :EndIf
14 |
15 | ⍝ create the request
16 | req←OpenAI.Image.Create description
17 | req.size←'512x512'
18 | req.response_format←'b64_json'
19 |
20 | ⍝ run the request, capture the response
21 | resp←req.Run
22 | :If 0≠resp.rc ⋄ (rc msg)←resp.(rc msg)
23 | :ElseIf 200≠resp.HttpStatus ⋄ →0⊣(rc msg)←(resp.HttpStatus)({0::⍵.HttpStatus ⋄ ⍵.Data.error.message}resp)
24 | :Else ⋄ (rc msg)←0 (⊃resp.Data.data.b64_json)
25 | :EndIf
26 |
--------------------------------------------------------------------------------
/demos/Translation.aplf:
--------------------------------------------------------------------------------
1 | (rc text)←Translation audioFile;oref;req;resp;h
2 | ⍝ This demonstrates using OpenAI to translate an audio file to written English
3 | ⍝ audioFile is the path to a previously recorded audio file. .mp3 is preferred
4 | ⍝ rc is the return code and is 0 if the translation was successful, otherwise it will be non-0
5 | ⍝ text is either the translated text if the translation was successful or a message explaining why the translation failed
6 |
7 | rc←¯1
8 |
9 | ⍝ Check that OpenAI is present
10 | :If 9.1≠⎕NC⊂'OpenAI' ⋄ →0⊣text←'OpenAI is not loaded, please run Setup' ⋄ :EndIf
11 |
12 | ⍝ Make sure the OpenAI.APIKey is set
13 | :If 0∊⍴OpenAI.APIKey ⋄ →0⊣text←'OpenAPI.APIKey has not been set, please run Setup' ⋄ :EndIf
14 |
15 | ⍝ Check that the audio file exists
16 | :If ~⎕NEXISTS audioFile ⋄ →0⊣text←'Audio file "',audioFile,'" not found' ⋄ :EndIf
17 |
18 | ⍝ create the request
19 | req←OpenAI.Audio.Translation audioFile
20 |
21 | ⍝ run the request, capture the response
22 | resp←req.Run
23 | :If 0≠resp.rc ⋄ (rc text)←resp.(rc msg)
24 | :ElseIf 200≠resp.HttpStatus ⋄ (rc text)←(resp.HttpStatus(msg,' - ',HttpMessage))
25 | :Else ⋄ (rc text)←0 resp.Data.text
26 | :EndIf
27 |
--------------------------------------------------------------------------------
/demos/Setup.aplf:
--------------------------------------------------------------------------------
1 | Setup;url;rc;msg;⎕IO;⎕ML;prompt;key;resp
2 | (⎕ML ⎕IO)←1
3 | :If 9.1≠⎕NC⊂'OpenAI' ⍝ is OpenAI here?
4 | :Trap 0
5 | {}⎕SE.UCMD'GET ',url←'https://github.com/Dyalog/OpenAI/blob/main/source/OpenAI.apln'
6 | :Else
7 | ⎕←'We encountered a ',⎕DMX.EM,' when attempting to download OpenAI from ',url
8 | ⎕←'Please download OpenAI.apln manually, import it into your APL session, and rerun Setup'
9 | →0
10 | :EndTrap
11 | :EndIf
12 | :If ~OpenAI.Initialized ⍝ has OpenAI been initialized?
13 | :If 0≠⊃(rc msg)←OpenAI.Initialize ⍝ were we able to initialize OpenAI?
14 | →0⊣⎕←'We were unable to initialize OpenAI due to "',msg,'"'
15 | :EndIf
16 | :EndIf
17 | :If 0∊⍴OpenAI.APIKey
18 | key←{inp←⍞⊣⍞←⍵ ⋄ inp↓⍨+/∧\=⌿↑inp ⍵}'Enter your OpenAI API key or the environment variable that contains your API key: '
19 | :If 0∊⍴2 ⎕NQ #'GetEnvironment'key
20 | OpenAI.APIKey←key
21 | :Else
22 | OpenAI.HttpCommand.HeaderSubstitution←'%'
23 | OpenAI.APIKey←'%',key,'%'
24 | :EndIf
25 | :EndIf
26 |
27 | ⍝ try OpenAI to see that everything is set up properly
28 | resp←OpenAI.Models
29 | :If 0≠resp.rc ⋄ msg←resp.msg
30 | :OrIf 200≠resp.HttpStatus ⋄ msg←{0::⍵.HttpMessage ⋄ ⍵.Data.error.message}resp
31 | ⎕←'The OpenAI interface failed due to "',msg,'"'
32 | OpenAI.APIKey/⍨←401≠resp.HttpStatus
33 | :Else
34 | ⎕←'The OpenAI interface is set up and ready for use'
35 | :EndIf
36 |
--------------------------------------------------------------------------------
/demos/Transcription.aplf:
--------------------------------------------------------------------------------
1 | (rc text)←Transcription args;oref;req;resp;audioFile;language;h
2 | ⍝ This demonstrates using OpenAI to transcribe an audio file to its native language text
3 | ⍝ args is audiofile [language]
4 | ⍝ audioFile is the path to a previously recorded audio file. .mp3 is preferred
5 | ⍝ language is the optional ISO 639-1 language code for the file
6 | ⍝ rc is the return code and is 0 if the transcription was successful, otherwise it will be non-0
7 | ⍝ text is either the transcribed text if the transcription was successful or a message explaining why the transcription failed
8 | ⍝ Assumes that the OpenAI interface is either in the same namespace or the parent namespace and has been initialized
9 |
10 | rc←¯1
11 |
12 | ⍝ Check that OpenAI is present
13 | :If 9.1≠⎕NC⊂'OpenAI' ⋄ →0⊣text←'OpenAI is not loaded, please run Setup' ⋄ :EndIf
14 |
15 | ⍝ Make sure the OpenAI.APIKey is set
16 | :If 0∊⍴OpenAI.APIKey ⋄ →0⊣text←'OpenAPI.APIKey has not been set, please run Setup' ⋄ :EndIf
17 |
18 | ⍝ Split args
19 | (audioFile language)←2↑(,⊆args),⊂''
20 |
21 | ⍝ Check that the audio file exists
22 | :If ~⎕NEXISTS audioFile ⋄ →0⊣text←'Audio file "',audioFile,'" not found' ⋄ :EndIf
23 |
24 | ⍝ create the request
25 | req←OpenAI.Audio.Transcription audioFile
26 | req.language←language
27 |
28 | ⍝ run the request, capture the response
29 | resp←req.Run
30 | :If 0≠resp.rc ⋄ (rc text)←resp.(rc msg)
31 | :ElseIf 200≠resp.HttpStatus ⋄ (rc text)←(resp.HttpStatus(msg,' - ',HttpMessage))
32 | :Else ⋄ (rc text)←0 resp.Data.text
33 | :EndIf
34 |
--------------------------------------------------------------------------------
/demos/Speech.aplf:
--------------------------------------------------------------------------------
1 | (rc msg)←audioFile Speech text;file;⎕IO;⎕ML;req;resp;data;h
2 | ⍝ This demonstrates using OpenAI to convert text into an audio file in its native language
3 | ⍝ audioFile is the name of the audio file to be written, If you do not give a file extension, .mp3 will be used
4 | ⍝ text is the text to be converted. If text begins with 'file://', the remainder of text is assumed to be a file name containing the text to be converted.
5 | ⍝ rc is the return code and is 0 if the conversion was successful, otherwise it will be non-0
6 | ⍝ msg is either the fill name of the audio file if the conversion was successful or a message explaining why the conversion failed
7 | ⍝ Assumes that the OpenAI interface is either in the same namespace or the parent namespace and has been initialized
8 | (⎕IO ⎕ML)←1
9 | rc←¯1
10 |
11 | ⍝ Check that OpenAI is present
12 | :If 9.1≠⎕NC⊂'OpenAI' ⋄ →0⊣msg←'OpenAI is not loaded, please run Setup' ⋄ :EndIf
13 |
14 | ⍝ Make sure the OpenAI.APIKey is set
15 | :If 0∊⍴OpenAI.APIKey ⋄ →0⊣msg←'OpenAPI.APIKey has not been set, please run Setup' ⋄ :EndIf
16 |
17 | ⍝ Check if the text is coming from a file
18 | :If 'file://'≡7↑text
19 | :If ⎕NEXISTS file←7↓text ⋄ text←⊃⎕NGET file
20 | :Else ⋄ →0⊣msg←'Input file "',file,' was not found'
21 | :EndIf
22 | :EndIf
23 |
24 | ⍝ create the request
25 | req←OpenAI.Audio.Speech text(audioFile 1)
26 |
27 | ⍝ run the request, capture the response
28 | resp←req.Run
29 | :If 0≠resp.rc ⋄ (rc msg)←resp.(rc msg)
30 | :ElseIf 200≠resp.HttpStatus ⋄ →0⊣(rc text)←(resp.HttpStatus(msg,' - ',HttpMessage))
31 | :Else ⋄ (rc msg)←0 resp.OutFile
32 | :EndIf
33 |
--------------------------------------------------------------------------------
/docs/css/main.css:
--------------------------------------------------------------------------------
1 | @font-face {
2 | font-family: APL;
3 | src: local("APL385 Unicode"), url("../assets/apl385.ttf");
4 | }
5 | .language-APL {
6 | font-family: APL!important;
7 | line-height: 1.2em!important;
8 | }
9 | code {
10 | font-family: APL;
11 | font-size: 1em!important;
12 | padding-left: 0em!important;
13 | padding-right: 0em!important;
14 | white-space: pre;
15 | overflow-x: scroll;
16 | }
17 | .md-logo > img{
18 | width: 7rem!important;
19 | height: 1.2rem!important;
20 | }
21 | @media screen and (max-width: 76.1875em) {
22 | .md-logo > img{
23 | width: 10rem!important;
24 | height: 1.8rem!important;
25 | }
26 | }
27 | td:first-child {
28 | white-space: nowrap;
29 | }
30 | table.scrollable {
31 | display:block;
32 | overflow-x: auto;
33 | white-space: nowrap;
34 | }
35 |
36 | /* Custom colors */
37 | :root {
38 | --md-primary-fg-color: #ED7F00;
39 | --md-primary-fg-color--dark: #563336;
40 | --md-default-bg-color: #F8F8F8;
41 | }
42 | /* Code copy only input (see CONTRIBUTING.md) */
43 | pre + pre > button, pre + hr {
44 | display: none!important;
45 | }
46 | pre > code {
47 | padding-bottom: 1em!important;
48 | }
49 | hr + pre > button {
50 | top: -0.2em!important;
51 | }
52 | pre + pre , hr + pre {
53 | margin-top: -1.8em!important;
54 | }
55 | pre + pre > code, hr + pre > code {
56 | padding-top: 0.2em!important;
57 | }
58 |
59 | /* make admonition styling a la Dyalog */
60 | .md-typeset .admonition,
61 | .md-typeset details {
62 | border-color: #ED7F00 !important;
63 | box-shadow: #ED7F00 !important;
64 | }
65 |
66 | .md-typeset :is(.admonition-title, summary){
67 | background-color: #F3AD5b !important;
68 | }
69 |
70 | .md-typeset .admonition-title:before,
71 | .md-typeset summary:before{
72 | background-color: #F8F8F8 !important;
73 | }
74 |
75 | .md-typeset pre > code {
76 | overflow: scroll !important;
77 | scrollbar-color: black;
78 | scrollbar-width: thin;
79 | }
80 |
81 | .md-copyright {
82 | width: 100%;
83 | }
84 |
85 | .md-typeset dd {
86 | margin-top: 0em;
87 | }
88 |
89 | .left {
90 | float: left;
91 | }
92 |
93 | .right {
94 | float: right;
95 | }
--------------------------------------------------------------------------------
/mkdocs.yml:
--------------------------------------------------------------------------------
1 | site_name: OpenAI
2 | repo_url: https://github.com/dyalog/OpenAI
3 | repo_name: Dyalog/OpenAI
4 | dev_addr: 127.0.0.1:22222
5 | copyright:
6 | nav:
7 | - Overview: 'index.md'
8 | - "Quick Start": 'quickstart.md'
9 | - "Demos": 'demos.md'
10 | - 'User Guide and Reference':
11 | - 'Using OpenAI': 'userguide.md'
12 | - 'Audio Endpoint' : 'audio.md'
13 | - 'Chat Endpoint' : 'chat.md'
14 | - 'Files Endpoint' : 'files.md'
15 | - 'Image Endpoint' : 'image.md'
16 | - 'Models Endpoint' : 'models.md'
17 | - 'Moderations Endpoint' : 'moderations.md'
18 | - About:
19 | - License: 'LICENSE.md' # complete
20 | - 'Release Notes': 'release-notes.md'
21 |
22 | theme:
23 | favicon: 'img/favicon-32.png'
24 | logo: 'img/dyalog-white.svg'
25 | features:
26 | - navigation.sections
27 | - navigation.instant
28 | - content.footnote.tooltips
29 | name: material
30 |
31 | extra:
32 | generator: false
33 | version:
34 | provider: mike
35 |
36 | extra_css:
37 | - css/main.css
38 |
39 | plugins:
40 | - search
41 | - mike
42 | - print-site:
43 | add_to_navigation: true
44 | print_page_title: 'Print'
45 | add_print_site_banner: false
46 | # Table of contents
47 | add_table_of_contents: false
48 | toc_title: 'Table of Contents'
49 | toc_depth: 6
50 | # Content-related
51 | add_full_urls: false
52 | enumerate_headings: false
53 | enumerate_figures: false
54 | add_cover_page: true
55 | cover_page_template: ""
56 | path_to_pdf: ""
57 | include_css: true
58 | enabled: true
59 | exclude:
60 |
61 | markdown_extensions:
62 | - admonition
63 | - abbr
64 | - footnotes
65 | - attr_list
66 | - def_list
67 | - markdown_tables_extended
68 | - pymdownx.details
69 | - pymdownx.superfences
70 | - pymdownx.tasklist:
71 | custom_checkbox: true
72 | - pymdownx.emoji:
73 | emoji_index: !!python/name:materialx.emoji.twemoji
74 | emoji_generator: !!python/name:materialx.emoji.to_svg
75 | - toc:
76 | title: On this page
--------------------------------------------------------------------------------
/docs/quickstart.md:
--------------------------------------------------------------------------------
1 | ## Getting Started
2 | This is an abbreviated introduction to get you started with `OpenAI`. The [OpenAI website](https://platform.openai.com) has considerably more information. `OpenAI` uses `HttpCommand` to communicate with the OpenAI API.
3 |
4 | ### 1. Create an OpenAI account
5 | * Navigate to OpenAI's [quickstart page](https://platform.openai.com/docs/quickstart).
6 | * Click "Sign up" and follow the instructions to create your account
7 |
8 | ### 2. Optionally, create an OpenAI project
9 | * You can create an OpenAI project. If you don't create a project, OpenAI will use a Default project.
10 |
11 | ### 3. Create an OpenAI API project key
12 | You will need a project API key to be able to access the OpenAI API via `OpenAI`. Protect this API key, **do not** publish it on GitHub or other public places.
13 |
14 | * Navigate to OpenAI's [API keys page](https://platform.openai.com/api-keys)
15 | * Click "+ Create a new secret key"
16 | * Click "Create secret key"
17 | * Make sure you copy and save the generated secret key! **This will be the only time it will be displayed.**
18 | * OpenAI recommends that you set an environment variable to hold your API key. This way you will not expose the key in your APL code. If you're using Linux, do: `export OPENAI_API_KEY="your_api_key_here"` If you're using Windows, under PowerShell do: `setx OPENAI_API_KEY "your_api_key_here"`
19 | * If you choose to not use an environment variable, you could save your API key in a text file and then
20 |
21 | ### 4. Obtain `OpenAI`
22 | There are a couple options to obtain `OpenAI`.
23 |
24 | * Use the `]get` user command
25 | ```
26 | ]get github.com/Dyalog/OpenAI/blob/main/source/OpenAI.apln
27 | ```
28 | * Download [`OpenAI.apln`](https://github.com/Dyalog/OpenAI/releases/latest) from GitHub. Remember where you saved the downloaded file and then use the `]import` user command, replacing "downloaded-file-path" with the path name where you saved the downloaded file.
29 | ```
30 | ]import # downloaded-file-path/OpenAI.apln
31 | ```
32 |
33 | ### 5. Configure `OpenAI`
34 | Now that you have `OpenAI` in your workspace, you can configure it. First, run `OpenAI.Initialize`. This will copy the latest release of `HttpCommand` into the `OpenAI` namespace.
35 | ```
36 | OpenAI.Initialize
37 | 0 Initialized
38 | ```
39 | If the result of `OpenAI.Initialize` is not as shown above, the result will contain an error message explaining what failed.
40 |
41 | If you have saved your OpenAI API key as an environment variable, you can use `HttpCommand`'s `HeaderSubstitution` setting to have `HttpCommand` use the environment variable rather than explicitly setting the API key in the configuration.
42 |
43 | ```
44 | OpenAI.HttpCommand.HeaderSubstitution←'%' ⍝ sets '%' as the environment variable indicator
45 | OpenAI.APIKey←'%your-environment-variable-name%' ⍝ note the leading and trailing '%'
46 | ```
47 | If you have not saved your OpenAI API key as an environment variable, you will need to set `OpenAI.APIKey` to the API key's value directly.
48 | ```
49 | OpenAI.APIKey←'your-OpenAI-API-key-here'
50 | ```
51 | ## Next Steps
52 | Having completed the above steps, you are now ready to begin interacting with the OpenAI API. From here you may want to look at:
53 |
54 | * [`OpenAI` demos](./demos.md)
55 | * [Using `OpenAI`](./userguide.md)
--------------------------------------------------------------------------------
/docs/index.md:
--------------------------------------------------------------------------------
1 | `OpenAI` is a Dyalog APL namespace that contains code to interact with the [OpenAI API](https://platform.openai.com/docs/api-reference/introduction). OpenAI develops and maintains several Large Language Models (LLMs). `OpenAI` contains code to interact with OpenAI endpoints. A few things to note:
2 |
3 | * We are going to use the term "OpenAI" a lot in this documentation. When formatted as `OpenAI`, we are referring to the Dyalog namespace which implements an interface to the OpenAI API. When formatted as OpenAI, we are referring to the OpenAI API itself.
4 | * ChatGPT is actively developing OpenAI, adding new LLMs and endpoint definitions. This puts us in a reactive mode to incorporate these new features and interfaces. We anticipate
5 | * Not all OpenAI endpoints are currently implemented in `OpenAI`. This is partly due to the reason stated above. Our goal is to have sufficient endpoint coverage that our users can perform useful tasks with OpenAI from APL.
6 | * There are other LLMs available - `OpenAI` should serve as a good model for how to implement interfaces to them. In particular, `OpenAI` makes heavy use of [`HttpCommand`](https://dyalog.github.io/HttpCommand) and the techniques used should be applicable for interacting with other LLM APIs.
7 | * This documentation presents information on how to use the `OpenAI` namespace to interact with the OpenAI endpoints. It does not attempt to document all of the features and nuances of those endpoints. For that information, please see the [OpenAI API Reference](https://platform.openai.com/docs/overview).
8 | * We encourage feedback, feature requests, and guidance from our users.
9 |
10 | ## Endpoints Currently Implemented
11 |
12 | * [Audio](./userguide.md#audio) - turn audio into text or text into audio
13 | * [Chat](./userguide.md#chat) - have a conversation with an OpenAI model
14 | * [Files](./userguide.md#files) - upload and manage documents that can be used with other features
15 | * [Image](./userguide.md#image) - given a prompt or existing image, generate a new image
16 | * [Models](./userguide.md#models) - list and describe the various models available in the OpenAI API
17 | * [Moderations](./userguide.md#moderations) - Classify text input as potentially harmful
18 |
19 | ## Forthcoming Endpoints
20 | ### Expected in Q4 2024
21 | OpenAI has released, in beta, version 2 of their Assistants and related endpoints. We expect to have completed their development in `OpenAI` in the 4th quarter of 2024.
22 |
23 | * Assistants - Build assistants that can call models and use tools to perform tasks.
24 | * Threads - Create threads that assistants can interact with.
25 | * Messages - Create messages within threads.
26 | * Runs - Represents an execution on a thread.
27 | * Vector Stores - Used to store files for use be the OpenAI's `file_search` tool.
28 | * Vector Store Files - Represent files inside a vector store.
29 | ### Future
30 | Beyond 2024, we expect to add support for the following endpoints.
31 |
32 | * Vector Store File Batches - Represent operations to add multiple files to a vector store.
33 | * Run Steps - Represents the steps (model and tool calls) taken during the run.
34 | * Uploads - Upload large files in multiple parts.
35 | * Embeddings - Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms.
36 |
37 | Other future work may include Projects and Users endpoints depending on our users' needs.
--------------------------------------------------------------------------------
/docs/userguide.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | Whenever you see OpenAI formatted as `OpenAI`, it is referring to the Dyalog OpenAI API interface. `OpenAI` is a Dyalog APL namespace which contains code that implements interfaces to OpenAI API endpoints.
4 |
5 | ## Getting Started
6 | To use the OpenAI API, you will need to:
7 |
8 | * Create an OpenAI account
9 | * Optionally, create an OpenAI project
10 | * Create an OpenAI API key - this will enable you to make calls to the OpenAI API
11 | * Download `OpenAI`
12 | * Configure `OpenAI`
13 |
14 | ### Create an OpenAI account
15 | * Navigate to OpenAI's [quickstart page](https://platform.openai.com/docs/quickstart).
16 | * Click "Sign up" and follow the instructions to create your account
17 |
18 | ### Optionally, create an OpenAI project
19 | * Optionally, create an OpenAI project. If you don't create a project, OpenAI will use a "Default project".
20 | * Create one or more project API key(s). You will need a project API key to be able to access the OpenAI API via `OpenAI`. Protect this API key, **do not** publish it on GitHub or other public places.
21 | * Navigate to OpenAI's [API keys page](https://platform.openai.com/api-keys)
22 | * Click "+ Create a new secret key"
23 | * Click "Create secret key"
24 | * Make sure you copy the generated secret key! **This will be the only time it will be displayed.**
25 | * OpenAI recommends that you set an environment variable to hold your API key. This way you will not expose the key in your APL code. If you're using Linux, do: `export OPENAI_API_KEY="your_api_key_here"` If you're using Windows, under PowerShell do: `setx OPENAI_API_KEY "your_api_key_here"`
26 |
27 | *
28 | ### Obtain `OpenAI`
29 | Download [`OpenAI.apln`](https://github.com/Dyalog/OpenAI/releases/latest) from GitHub.
30 | ### Configure `OpenAI`
31 | You will need to provide the API key you created earlier.
32 |
33 | ```
34 | OpenAI.APIKey←'your-API-key-here'
35 | ```
36 | You may not want to include your API key in your code that initializes `OpenAI`. One technique to avoid this is to store your APIKey in an environment variable and then retrieve its value.
37 | ```
38 | OpenAI.APIKey←2 ⎕NQ # 'GetEnvironment' 'your-APIKey-environment-variable-name'
39 | ```
40 | ## `OpenAI` Initialization
41 | `OpenAI` makes heavy use of `HttpCommand` and requires `HttpCommand` version 5.6 or later. During initialization, `OpenAI` will look for `HttpCommand` in its parent namespace and if it doesn't find `HttpCommand` it will load it from your Dyalog installation and then upgrade to the latest version of HttpCommand. The endpoints implemented in `OpenAI` will initialize `OpenAI` if not already initialized. You can also initialize `OpenAI` by running `OpenAI.Initialize`. In a production environment, `HttpCommand` should be copied and saved into the workspace rather than relying on loading and upgrading.
42 |
43 | ## `OpenAI` Naming Conventions
44 | Each of the endpoints implemented in `OpenAI` has a number of parameters. Parameters beginning with a lower-case letter (a-z) are parameters that OpenAI itself uses. Parameters beginning with an upper-case letter (A-Z) are parameters used by `OpenAI` to make it easier to use in an APL environment.
45 |
46 | ## Endpoints
47 | Most endpoints, when run, will save the last `HttpCommand` response namespace in the `Response` variable for the endpoint. This can be used to access the result of the endpoint's execution or to examine in case the execution failed.
48 |
49 | For example:
50 | ```
51 | s←OpenAI.Audio.Speech 'This is a test'
52 | s.Run
53 | [rc: 0 | msg: | HTTP Status: 401 "Unauthorized" | ≢Data: 1 (namespace)]
54 | s.Show ⍝ show Response.Data
55 | {
56 | "error": {
57 | "code": null,
58 | "message": "You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY), or as the password field (with blank username) if you're accessing the API from your browser and are prompted for a username and password. You can obtain an API key from https://platform.openai.com/account/api-keys.",
59 | "param": null,
60 | "type": "invalid_request_error"
61 | }
62 | }
63 | ```
64 | !!! Important
65 | Remember to set `OpenAI.APIKey` prior to running any endpoints.
66 |
--------------------------------------------------------------------------------
/docs/img/dyalog-white.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
199 |
--------------------------------------------------------------------------------
/docs/demos.md:
--------------------------------------------------------------------------------
1 | You'll need to register and obtain an OpenAI API key as described in the [Quick Start page](./quickstart.md).
2 |
3 | ### Obtaining the demos
4 |
5 | The easiest way to obtain and set up the `OpenAI` demos is to use the `]get` user command.
6 |
7 | ```
8 | )clear
9 | clear ws
10 | ]get https://github.com/Dyalog/OpenAI/archive/refs/heads/main.zip
11 | Working on it…
12 | #.main
13 | )cs #.main.demos
14 | #.main.demos
15 | ```
16 |
17 | If this doesn't work for you, then you can download and unzip [demos.zip](https://github.com/Dyalog/OpenAI/releases/latest) from GitHub.
18 | Using the folder name that you unzipped into, use `]link.import` to import the demos.
19 |
20 | ```
21 | ]link.import # /your-folder-name-here/OpenAI-main/demos
22 | Imported: # - ...
23 | ```
24 |
25 | ### Setting up the demos
26 | Run the `Setup` function to initialize `OpenAI`.
27 |
28 | ```
29 | Setup
30 | Enter your OpenAI API key or the environment variable that contains your API key: your-API-key-here
31 | The OpenAI interface is set up and ready for use
32 | ```
33 |
34 | At the prompt either enter your OpenAI API key or, if you've saved your API key in an environment variable, enter the name of the environment variable.
35 |
36 | Congratulations! You're now ready to run the demos.
37 |
38 | ## Audio Demo Functions
39 |
40 | * `Speech` which generates audio from text input.
41 | * `Play` uses HTMLRenderer to play an audio file.
42 | * `Transcription` which transcribes audio into the input language.
43 | * `Translation` which translates audio in the English text.
44 | * `ShowText` uses HTMLRenderer to display the result of `Transcription` or `Translation`.
45 |
46 | ### `Speech`
47 | |--|--|
48 | | Syntax | `(rc msg)←audioFile Speech text` |
49 | | `audioFile` | The name of the .mp3 audio file to save. You do not have to specify the .mp3 extension. |
50 | | `text` | Either
The actual text to be converted to audio
The name of a file, prefixed with `file://`, containing the text to be converted.
|
51 | | `rc` | Either
`0` indicating the text was successfully converted and written to the file.
non-`0` indicating some error occurred.
|
52 | | `msg` | Either
If `rc` is `0`, the name of the audio file written (including the .mp3 extension)
If `rc` is not `0`, a message indicating what might have gone wrong
|
53 | | Example | `'/tmp/spanish.mp3' Speech 'APL es muy fácil de aprender y divertido de usar'` `0 C:/tmp/spanish.mp3`|
54 | | Notes | You can pass the result of `Speech` as the argument to `Play` to play the audio file. For example: `Play '/tmp/test' Speech 'This is a test'`|
55 |
56 | ### `Play`
57 | |--|--|
58 | | Syntax | `Play args` |
59 | | `args` | Either
`audioFile` is the name of the .mp3 audio file to transcribe.
`language` is an optional 2-character [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes) language code. This will assist OpenAI to determine the spoken language in the audio file.|
66 | | `rc` | Either
`0` indicating the audio file was successfully transcribed.
non-`0` indicating some error occurred.
|
67 | | `msg` | Either
If `rc` is `0`, the transcribed text
If `rc` is not `0`, a message indicating what might have gone wrong
|
68 | | Example | `Transcription '/tmp/spanish.mp3'` `0 APL es muy fácil de aprender y divertido de usar.` |
69 | | Notes | You can pass the result of `Transcription` as the argument to `ShowText` to display the transcribed text in an HTMLRenderer window. For example: `ShowText Transcription '/tmp/spanish.mp3'`|
70 |
71 | ### `Translation`
72 | |--|--|
73 | | Syntax | `(rc msg)←Translation args` |
74 | | `audioFile` | The name of the .mp3 audio file to translate.|
75 | | `rc` | Either
`0` indicating the audio file was successfully translated.
non-`0` indicating some error occurred.
|
76 | | `msg` | Either
If `rc` is `0`, the translated text
If `rc` is not `0`, a message indicating what might have gone wrong
|
77 | | Example | `Translation '/tmp/spanish.mp3'` `0 APL is very easy to learn and fun to use.` |
78 | | Notes | You can pass the result of `Translation` as the argument to `ShowText` to display the translated text in an HTMLRenderer window. For example: `ShowText Translation '/tmp/spanish.mp3'`|
79 |
80 | ### `ShowText`
81 | |--|--|
82 | | Syntax | `ShowText args` |
83 | | `args` | The result from `Transcription` or `Translation`|
84 | | Examples | `ShowText Transcription '/tmp/spanish.mp3'`|
85 |
86 | ## Image Demo Functions
87 |
88 | * Image - generate an image from a text prompt
89 | * ShowImage - display an image using HTMLRenderer
90 |
91 | ### `Image`
92 | |--|--|
93 | | Syntax | `(rc msg)←Image description` |
94 | | `description` | a description of the image to generate. The description can be in most languages.|
95 | | `rc` | Either
`0` indicating an image was successfully generated.
non-`0` indicating some error occurred.
|
96 | | `msg` | Either
If `rc` is `0`, the base64-encoded representation of the .png image data
If `rc` is not `0`, a message indicating what might have gone wrong
|
97 | | Example | `Image 'drei süße Welpen' ⍝ three cute puppies`|
98 | | Notes | You can pass the result of `Image` as the argument to `ShowImage` to display the generated imagein an HTMLRenderer window. For example: `ShowImage Image 'a pretty sunset on the water'`|
99 |
100 | ### `ShowImage`
101 | |--|--|
102 | | Syntax | `ShowImage args` |
103 | | `args` | The result from `Image`|
104 | | Examples | `ShowImage Image 'dos perros' ⍝ two dogs`|
105 |
106 | ## Chat Demo Functions
107 |
108 | * Chat - have a chat with an assistant you describe
109 | * Linguist - translate phrases into a desired language, optionally playing audio of the translation
110 |
111 | ### `Chat`
112 | |--|--|
113 | | Syntax | `Chat` |
114 | | Usage |
Describe the type of assistant you want to chat with; a more detailed description will generally result in more focused response.
Then ask questions, give directions, and so on for the assisant to respond to. The assistant "remembers" the conversation so you can build upon previous inputs and responses.
Pressing "Enter" without entering any text will cause `Chat` to exit.
|
115 |
116 | #### `Chat` Example 1
117 | ```
118 | Chat
119 | Describe the assistant you would like to chat with: poetic
120 | Enter user message: compose a haiku about APL
121 |
122 | Symbols dance with code,
123 | APL's concise power,
124 | Elegance in form.
125 |
126 | Enter user message: translate that to German
127 |
128 | Symbole tanzen mit Code,
129 | APLs präzise Kraft,
130 | Eleganz in Form.
131 |
132 | Enter user message:
133 | ```
134 | #### `Chat` Example 2
135 | ```
136 | Chat
137 | Describe the assistant you would like to chat with: you understand programming languages
138 | Enter user message: translate APL's +.× to python
139 |
140 | In APL, the +.× operator represents the matrix multiplication operation. To
141 | translate this operation to Python using NumPy, you can use the numpy.dot()
142 | function. Here's how you can do it:
143 |
144 | python
145 | import numpy as np
146 |
147 | # Define two matrices
148 | matrix1 = np.array([[1, 2], [3, 4]])
149 | matrix2 = np.array([[5, 6], [7, 8]])
150 |
151 | # Perform matrix multiplication
152 | result = np.dot(matrix1, matrix2)
153 |
154 | print("Result of matrix multiplication:")
155 | print(result)
156 |
157 |
158 | In this Python code snippet, we first import the NumPy library. We define two
159 | matrices matrix1 and matrix2 using NumPy arrays. Then, we use the np.dot()
160 | function to perform matrix multiplication between matrix1 and matrix2. Finally,
161 | we print the result of the matrix multiplication.
162 |
163 | Enter user message:
164 | ```
165 |
166 |
167 | ### `Linguist`
168 | |--|--|
169 | | Syntax | `Linguist` |
170 | | Usage |
Specify the output language that you would like
Specify whether you would like audio output for the responses
Enter some text to translate and press Enter (pressing Enter without any text will exit `Linguist`)