-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathchatgptlistmodels
More file actions
executable file
·171 lines (156 loc) · 4.57 KB
/
chatgptlistmodels
File metadata and controls
executable file
·171 lines (156 loc) · 4.57 KB
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
#!/bin/bash
# Lists models available at the configured API endpoint.
# Supports profile/config file options compatible with the chatgpt script.
set -e
PROFILES_DIR="$HOME/.chatgpt.js/profiles"
DEFAULT_URL="https://api.openai.com/v1/models"
verbose=false
api_url=""
declare -a extra_headers=()
usage() {
cat <<EOF
Usage: $(basename "$0") [options]
Options:
-v Verbose output (raw JSON, also prints unknown config file options)
-api url Use this URL instead of the default models endpoint.
If the URL ends with /chat/completions it is rewritten to /models.
-rh key=value Add a request header (can be given multiple times)
-ocf cfgfile Read options from a config file
-op prof,... Read options from named profile(s) in $PROFILES_DIR/
EOF
}
# Transform a chat completions URL to a models URL
transform_url() {
local u="$1"
if [[ "$u" == */chat/completions ]]; then
echo "${u%/chat/completions}/models"
else
echo "$u"
fi
}
# Parse a config file; sets api_url and appends to extra_headers.
# Skips comment lines (#), -m lines, and ignores unknown options
# (prints them if verbose).
parse_config_file() {
local file="$1"
if [[ ! -f "$file" ]]; then
echo "Error: config file not found: $file" >&2
exit 1
fi
while IFS= read -r line || [[ -n "$line" ]]; do
# skip comments and blank lines
[[ "$line" =~ ^[[:space:]]*# ]] && continue
[[ -z "${line// }" ]] && continue
# tokenize respecting quoted strings
eval "local tokens=($line)" 2>/dev/null || tokens=($line)
local opt="${tokens[0]}"
local arg="${tokens[1]}"
case "$opt" in
-api)
api_url=$(transform_url "$arg")
;;
-rh)
extra_headers+=("$arg")
;;
-m)
: # ignore model option
;;
"")
: # ignore blank token
;;
*)
if $verbose; then
echo "Info: ignoring unknown option in config file '$file': $opt" >&2
fi
;;
esac
done < "$file"
}
# --- Parse command-line arguments ---
while [[ $# -gt 0 ]]; do
case "$1" in
-v|--verbose)
verbose=true
shift
;;
-api)
[[ -z "${2-}" ]] && { echo "Error: -api requires a URL argument" >&2; usage >&2; exit 1; }
api_url=$(transform_url "$2")
shift 2
;;
-rh)
[[ -z "${2-}" ]] && { echo "Error: -rh requires a key=value argument" >&2; usage >&2; exit 1; }
extra_headers+=("$2")
shift 2
;;
-ocf)
[[ -z "${2-}" ]] && { echo "Error: -ocf requires a filename argument" >&2; usage >&2; exit 1; }
parse_config_file "$2"
shift 2
;;
-op)
[[ -z "${2-}" ]] && { echo "Error: -op requires a profile name argument" >&2; usage >&2; exit 1; }
IFS=',' read -ra profiles <<< "$2"
for prof in "${profiles[@]}"; do
prof_file="$PROFILES_DIR/$prof"
if [[ ! -f "$prof_file" ]]; then
echo "Error: profile not found: $prof_file" >&2
exit 1
fi
parse_config_file "$prof_file"
done
shift 2
;;
-*)
echo "Error: unknown option: $1" >&2
usage >&2
exit 1
;;
*)
echo "Error: unexpected argument: $1" >&2
usage >&2
exit 1
;;
esac
done
# --- Determine final URL ---
# OPENAI_API_BASE env var overrides everything (append /models)
if [[ -n "$OPENAI_API_BASE" ]]; then
url="$OPENAI_API_BASE/models"
elif [[ -n "$api_url" ]]; then
url="$api_url"
else
url="$DEFAULT_URL"
fi
# --- Determine API key (only if no Authorization header was provided) ---
has_auth_header=false
for h in "${extra_headers[@]}"; do
if [[ "$h" == Authorization* || "$h" == authorization* ]]; then
has_auth_header=true
break
fi
done
if ! $has_auth_header; then
if [[ -z "$OPENAI_API_KEY" ]]; then
if [[ -f "$HOME/.openai-api-key.txt" ]]; then
OPENAI_API_KEY=$(cat "$HOME/.openai-api-key.txt")
else
echo "Error: no Authorization header set and OPENAI_API_KEY is not set." >&2
echo "Set OPENAI_API_KEY, create $HOME/.openai-api-key.txt, or use -rh 'Authorization=Bearer <key>'" >&2
exit 1
fi
fi
extra_headers+=("Authorization=Bearer $OPENAI_API_KEY")
fi
# --- Build curl header arguments (convert key=value to HTTP header format) ---
declare -a curl_headers=()
for h in "${extra_headers[@]}"; do
# Replace the first '=' with ': ' to form a proper HTTP header
curl_headers+=("-H" "${h%%=*}: ${h#*=}")
done
# --- Execute ---
if $verbose; then
curl -s "$url" "${curl_headers[@]}" | jq . | less
else
curl -s "$url" "${curl_headers[@]}" | jq '.data[] | del(.permission)' | jq .id | sort | less
fi