-
Notifications
You must be signed in to change notification settings - Fork 81
/
chatgpt-shell-openrouter.el
170 lines (145 loc) · 6.59 KB
/
chatgpt-shell-openrouter.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
;;; chatgpt-shell-openrouter.el --- OpenRouter-specific logic -*- lexical-binding: t; -*-
;; Author: David J. Rosenbaum <djr7c4@gmail.com>
;; URL: https://github.com/xenodium/chatgpt-shell
;; Package-Requires: ((emacs "28.1") (shell-maker "0.72.1"))
;; This package is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This package is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;; Commentary:
;; Adds OpenRouter specifics for `chatgpt-shell'.
;;; Code:
(declare-function chatgpt-shell-validate-no-system-prompt "chatgpt-shell")
(cl-defun chatgpt-shell-openrouter-make-model (&key label version short-version token-width context-window validate-command other-params)
"Create an OpenRouter model.
Set LABEL, VERSION, SHORT-VERSION, TOKEN-WIDTH, CONTEXT-WINDOW,
VALIDATE-COMMAND and OTHER-PARAMS for `chatgpt-shell-openai-make-model'."
(chatgpt-shell-openai-make-model
:label label
:version version
:short-version short-version
:token-width token-width
:context-window context-window
:other-params other-params
:validate-command #'chatgpt-shell-openrouter--validate-command
:url-base 'chatgpt-shell-openrouter-api-url-base
:path "/v1/chat/completions"
:provider "OpenRouter"
:validate-command validate-command
:key #'chatgpt-shell-openrouter-key
:headers #'chatgpt-shell-openrouter--make-headers
:handler #'chatgpt-shell-openrouter--handle-command
:filter #'chatgpt-shell-openrouter--filter-output))
(defun chatgpt-shell-openrouter-models ()
"Build a list of OpenRouter LLM models."
(list (chatgpt-shell-openrouter-make-model
:version "meta-llama/llama-3.3-70b-instruct"
:short-version "llama-3.3-70b"
:label "Llama"
:token-width 16
;; See https://openrouter.ai/meta-llama/llama-3.3-70b-instruct.
:context-window 131072
;; Multiple quantizations are offered for this model by different
;; providers so we restrict to one for consistency. Note that the sense
;; in which provider is used here means the providers available through
;; OpenRouter. This is different from the meaning of the :provider
;; argument.
;;
;; See https://openrouter.ai/docs/provider-routing#quantization
:other-params '((provider (quantizations . ["bf16"]))))
(chatgpt-shell-openrouter-make-model
:version "qwen/qwq-32b-preview"
:short-version "qwq-32b-preview"
:label "Qwen"
:token-width 16
;; See
:context-window 32768
;; Multiple quantizations are offered for this model by different
;; providers so we restrict to one for consistency. Note that the sense
;; in which provider is used here means the providers available through
;; OpenRouter. This is different from the meaning of the :provider
;; argument.
;;
;; See https://openrouter.ai/qwen/qwq-32b-preview
:other-params '((provider (quantizations . ["bf16"]))))
(chatgpt-shell-openrouter-make-model
:version "openai/o1"
:short-version "o1"
:label "ChatGPT"
:token-width 3
;; See https://openrouter.ai/openai/o1-2024-12-17
:context-window 200000
:validate-command #'chatgpt-shell-validate-no-system-prompt)
(chatgpt-shell-openrouter-make-model
:version "qwen/qwen-2.5-coder-32b-instruct"
:short-version "qwen-2.5-coder-32b"
:label "Qwen"
:token-width 16
;; See
:context-window 32768
;; Multiple quantizations are offered for this model by different
;; providers so we restrict to one for consistency. Note that the sense
;; in which provider is used here means the providers available through
;; OpenRouter. This is different from the meaning of the :provider
;; argument.
;;
;; See https://openrouter.ai/qwen/qwen-2.5-coder-32b-instruct
:other-params '((provider (quantizations . ["bf16"]))))))
(defcustom chatgpt-shell-openrouter-api-url-base "https://openrouter.ai/api"
"OpenRouter API's base URL.
API url = base + path.
If you use OpenRouter through a proxy service, change the URL base."
:type 'string
:safe #'stringp
:group 'chatgpt-shell)
(defcustom chatgpt-shell-openrouter-key nil
"OpenRouter key as a string or a function that loads and returns it."
:type '(choice (function :tag "Function")
(string :tag "String"))
:group 'chatgpt-shell)
(defun chatgpt-shell-openrouter-key ()
"Get the OpenRouter key."
(cond ((stringp chatgpt-shell-openrouter-key)
chatgpt-shell-openrouter-key)
((functionp chatgpt-shell-openrouter-key)
(condition-case _err
(funcall chatgpt-shell-openrouter-key)
(error
"KEY-NOT-FOUND")))
(t
nil)))
(cl-defun chatgpt-shell-openrouter--handle-command (&key model command context shell settings)
"Handle ChatGPT COMMAND (prompt) using ARGS, MODEL, CONTEXT, SHELL, and SETTINGS."
(chatgpt-shell-openai--handle-chatgpt-command
:model model
:command command
:context context
:shell shell
:settings settings
:key #'chatgpt-shell-openrouter-key
:filter #'chatgpt-shell-openrouter--filter-output
:missing-key-msg "Your chatgpt-shell-openrouter-key is missing"))
(defun chatgpt-shell-openrouter--filter-output (raw-response)
"Filter RAW-RESPONSE when processing responses are sent.
This occurs for example with OpenAI's o1 model through OpenRouter."
(unless (string= (string-trim raw-response) ": OPENROUTER PROCESSING")
(chatgpt-shell-openai--filter-output raw-response)))
(defun chatgpt-shell-openrouter--make-headers (&rest args)
"Create the API headers.
ARGS are the same as for `chatgpt-shell-openai--make-headers'."
(apply #'chatgpt-shell-openai--make-headers
:key #'chatgpt-shell-openrouter-key
args))
(defun chatgpt-shell-openrouter--validate-command (_command _model _settings)
"Return error string if command/setup isn't valid."
(unless chatgpt-shell-openrouter-key
"Variable `chatgpt-shell-openrouter-key' needs to be set to your key.
Try M-x set-variable chatgpt-shell-openrouter-key
or
(setq chatgpt-shell-openrouter-key \"my-key\")"))
(provide 'chatgpt-shell-openrouter)
;;; chatgpt-shell-openrouter.el ends here