2828
2929(require 'openai )
3030
31- (defcustom openai-completon-model " text-davinci-003"
32- " ID of the model to use.
33-
34- You can use the List models API to see all of your available models."
35- :type 'string
36- :group 'openai )
37-
38- (defcustom openai-completon-suffix nil
39- " The suffix that comes after a completion of inserted text."
40- :type 'string
41- :group 'openai )
42-
43- (defcustom openai-completon-max-tokens 4000
44- " The maximum number of tokens to generate in the completion.
45-
46- The token count of your prompt plus max_tokens cannot exceed the model's context
47- length. Most models have a context length of 2048 tokens (except for the newest
48- models, which support 4096)."
49- :type 'integer
50- :group 'openai )
51-
52- (defcustom openai-completon-temperature 1.0
53- " What sampling temperature to use.
54-
55- Higher values means the model will take more risks. Try 0.9 for more creative
56- applications, and 0 (argmax sampling) for ones with a well-defined answer."
57- :type 'number
58- :group 'openai )
59-
60- (defcustom openai-completon-top-p 1.0
61- " An alternative to sampling with temperature, called nucleus sampling, where
62- the model considers the results of the tokens with top_p probability mass.
63- So 0.1 means only the tokens comprising the top 10% probability mass are
64- considered.
65-
66- We generally recommend altering this or `temperature' but not both."
67- :type 'number
68- :group 'openai )
69-
70- (defcustom openai-completon-n 1
71- " How many completions to generate for each prompt."
72- :type 'integer
73- :group 'openai )
74-
75- (defcustom openai-completon-stream nil
76- " Whether to stream back partial progress.
77-
78- If set, tokens will be sent as data-only server-sent events as they become
79- available, with the stream terminated by a data: [DONE] message."
80- :type 'boolean
81- :group 'openai )
82-
83- (defcustom openai-completon-logprobs nil
84- " Include the log probabilities on the logprobs most likely tokens, as well the
85- chosen tokens. For example, if logprobs is 5, the API will return a list of the
86- 5 most likely tokens. The API will always return the logprob of the sampled
87- token, so there may be up to logprobs+1 elements in the response.
88-
89- The maximum value for logprobs is 5."
90- :type 'integer
91- :group 'openai )
92-
93- (defcustom openai-completon-echo nil
94- " Echo back the prompt in addition to the completion."
95- :type 'boolean
96- :group 'openai )
97-
98- (defcustom openai-completon-stop nil
99- " Up to 4 sequences where the API will stop generating further tokens.
100- The returned text will not contain the stop sequence."
101- :type 'string
102- :group 'openai )
103-
104- (defcustom openai-completon-presence-penalty 0
105- " Number between -2.0 and 2.0. Positive values penalize new tokens based on
106- whether they appear in the text so far, increasing the model's likelihood to
107- talk about new topics."
108- :type 'number
109- :group 'openai )
110-
111- (defcustom openai-completon-frequency-penalty 0
112- " Number between -2.0 and 2.0.
113-
114- Positive values penalize new tokens based on their existing frequency in the
115- text so far, decreasing the model's likelihood to repeat the same line verbatim."
116- :type 'number
117- :group 'openai )
118-
119- (defcustom openai-completon-best-of 1
120- " Generates best_of completions server-side and returns the \" best\" (the one
121- with the highest log probability per token). Results cannot be streamed.
122-
123- When used with `n' , `best_of' controls the number of candidate completions and
124- `n' specifies how many to return – `best_of' must be greater than `n' ."
125- :type 'integer
126- :group 'openai )
127-
128- (defcustom openai-completon-logit-bias nil
129- " Modify the likelihood of specified tokens appearing in the completion."
130- :type 'list
131- :group 'openai )
132-
13331; ;
13432; ;; API
13533
13634;;;### autoload
137- (defun openai-completion (query callback )
138- " Query OpenAI with QUERY.
139-
140- Argument CALLBACK is a function received one argument which is the JSON data."
35+ (cl-defun openai-completion ( prompt callback
36+ &key
37+ (key openai-key)
38+ (model " text-davinci-003" )
39+ suffix
40+ max-tokens
41+ temperature
42+ top-p
43+ n
44+ stream
45+ logprobs
46+ echo
47+ stop
48+ presence-penalty
49+ frequency-penalty
50+ best-of
51+ logit-bias
52+ (user openai-user))
53+ " Send completion request.
54+
55+ Arguments PROMPT and CALLBACK are required for this type of request. PROMPT is
56+ either the question or instruction to OpenAI. CALLBACK is the execuation after
57+ request is made.
58+
59+ Arguments KEY and USER are global options; however, you can overwrite the value
60+ by passing it in.
61+
62+ The rest of the arugments are optional, please see OpenAI API reference page
63+ for more information. Arguments here refer to MODEL, SUFFIX, MAX-TOKENS,
64+ TEMPERATURE, TOP-P, N, STREAM, LOGPROBS, ECHO, STOP, PRESENCE-PENALTY,
65+ FREQUENCY-PENALTY, BEST-OF, and LOGIT-BIAS."
14166 (openai-request " https://api.openai.com/v1/completions"
14267 :type " POST"
14368 :headers `((" Content-Type" . " application/json" )
144- (" Authorization" . ,(concat " Bearer " openai-key)))
145- :data (json-encode
146- `((" model" . , openai-completon-model )
147- (" prompt" . , query )
148- (" suffix" . , openai-completon-suffix )
149- (" max_tokens" . , openai-completon-max-tokens )
150- (" temperature" . , openai-completon-temperature )
151- (" top_p" . , openai-completon-top-p )
152- (" n" . , openai-completon-n )
153- ; ;("stream" . ,(if openai-completon-stream "true" "false"))
154- (" logprobs" . , openai-completon-logprobs )
155- ; ;("echo" . ,(if openai-completon-echo "true" "false"))
156- ; ;("stop" . ,openai-completon-stop)
157- ; ;("presence_penalty" . ,openai-completon-presence-penalty)
158- ; ;("frequency_penalty" . ,openai-completon-frequency-penalty)
159- ; ;("best_of" . ,openai-completon-best-of)
160- ; ;("logit_bias" . ,(if (listp openai-completon-logit-bias)
161- ; ; (json-encode openai-completon-logit-bias)
162- ; ; openai-completon-logit-bias))
163- (" user" . , openai-user )))
69+ (" Authorization" . ,(concat " Bearer " key)))
70+ :data (openai--json-encode
71+ `((" model" . , model )
72+ (" prompt" . , prompt )
73+ (" suffix" . , suffix )
74+ (" max_tokens" . , max-tokens )
75+ (" temperature" . , temperature )
76+ (" top-p" . , top-p )
77+ (" n" . , n )
78+ (" stream" . , stream )
79+ (" logprobs" . , logprobs )
80+ (" echo" . , echo )
81+ (" stop" . , stop )
82+ (" presence_penalty" . , presence-penalty )
83+ (" frequency_penalty" . , frequency-penalty )
84+ (" best_of" . , best-of )
85+ (" logit_bias" . , logit-bias )
86+ (" user" . , user )))
16487 :parser 'json-read
16588 :success (cl-function
16689 (lambda (&key data &allow-other-keys )
@@ -169,6 +92,16 @@ Argument CALLBACK is a function received one argument which is the JSON data."
16992; ;
17093; ;; Application
17194
95+ (defcustom openai-completion-max-tokens 4000
96+ " The maximum number of tokens to generate in the completion."
97+ :type 'integer
98+ :group 'openai )
99+
100+ (defcustom openai-completion-temperature 1.0
101+ " What sampling temperature to use."
102+ :type 'number
103+ :group 'openai )
104+
172105;;;### autoload
173106(defun openai-completion-select-insert (start end )
174107 " Send the region to OpenAI and insert the result to the next paragraph.
@@ -197,7 +130,9 @@ START and END are selected region boundaries."
197130 (fill-region original-point (point ))
198131 ; ; Highlight the region!
199132 (call-interactively #'set-mark-command )
200- (goto-char (1+ original-point))))))))
133+ (goto-char (1+ original-point)))))
134+ :max-tokens openai-completion-max-tokens
135+ :temperature openai-completion-temperature)))
201136
202137;;;### autoload
203138(defun openai-completion-buffer-insert ()
0 commit comments