

GitHub - emacs-openai/openai: Elisp library for the OpenAI API
source link: https://github.com/emacs-openai/openai
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

OpenAI.el
Elisp library for the OpenAI API
The OpenAI Elisp library provides convenient access to the OpenAI API from applications written in the Elips language.
P.S. This package is expected to be used as a library, so there are only a few interactable commands you can use, and those are mostly examples.
Documentation
Table of Contents
Usage
You will need to set up your API key before you can use this library.
(setq openai-key "[YOUR API KEY]")
For requests that need your user identifier,
(setq openai-user "[YOUR USER UID]")
Tip
The two variables
openai-key
andopenai-user
are the default values for sending requests! However, you can still overwrite the value by passing the keywords:key
and:user
!
The simplest example
Here is the simplest example that teaches you how to use this library. This is
a function with a query
and a callback function.
(openai-completion "How are you?"
(lambda (data)
(message "%s" data)))
Sending Request
All arguments are exposed in the argument list, so you can send any request in any way you want.
For example, the request function openai-completion
accepts argument
max-tokens
. By seeing OpenAI's references page:
max_tokens
integer Optional Defaults to 16The maximum number of tokens to generate in the completion.
The token count of your prompt plus
max_tokens
cannot exceed the model's context length. Most models have a context length of 2048 tokens (except for the newest models, which support 4096).
(openai-completion ...
...
:max-tokens 4069) ; max out tokens!
API functions
The API functions are followed by this pattern:
[PACKAGE NAME]-[API TYPE]-[REQUEST NAME]
For example:
(openai-file-list ...)
openai
- is the package namefile
- is the api type, see OpenAI API referencelist
- is the request name
Parameters
The function's parameters are followed in this order:
- required - variables are required for this type of request
callback
- execution after the request is made- optional - other variables that are not required, but will affect the final output
(openai-completion "How are you?" ; required
(lambda (data) ; callback
...)
:max-tokens 4069) ; optional
Setting Model
Every type of request has a default model
, and we hope this benefits the users
to not worry about what model to use for their request! However, if you want to
use other models, you can use the keyword :model
to replace them!
(openai-completion ...
...
:model "text-davinci-003") ; replace the default model
Debugging
While playing through this library, you might see this error quite often.
400 - Bad request. Please check error message and your parameters
Try set the variable openai--show-log
to t
, it will show more error messages.
Example projects
References
Contribute
If you would like to contribute to this project, you may either clone and make pull requests to this repository. Or you can clone the project and establish your own branch of this tool. Any methods are welcome!
Recommend
-
146
Table Component for elisp ctable.el is a table component for emacs lisp. Emacs lisp programs can display a nice table view from an abstract data model. The many emacs programs have the code for displaying table views, such as...
-
40
Readme.org Göktuğ’s Emacs Lisp Bits Introduction This repository is a swarm for all the Elisp stuff I published. Most files will have licen...
-
11
Read Me This library was created to fascilitate quickly building web pages, though it also includes tools for working with parsed xml Code Generation with esxml.el This library provides to formats for xml code generat...
-
11
Elisp API Demos Showing an Elisp demo of mapcar in C-h f mapcar: Usage To inject elisp demos into *Help*, such as C-h f (M-x describe-function), use...
-
8
Dash Docs What's it This package provides an elisp interface to query and show documenation using Dash docsets. It doesn't require Dash app. Requirements s...
-
15
emacs-tree-sitter This is an Emacs Lisp binding for tree-sitter, an incremental parsing library. It requires Emacs 25.1 or above, built with dynamic module support. It...
-
10
Parsebib (c) 2014-2021 Joost Kremers Parsebib is an Elisp library for reading bibliographic database files. It supports both BibTeX / biblatex (.bib) files and CSL-JSON (.json
-
11
Posts About The posts about “emacs/elisp” that I publish on the subreddit r/emacs in an org document. Install The posts are in the file
-
7
-
8
The Compat Elisp Library: A Package Maintainer's Best Friend Mar 16, 2023 |
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK