-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllm
executable file
·329 lines (266 loc) · 9.41 KB
/
llm
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
# Author: Doug Rudolph (https://github.com/11)
# Date: Jan. 5, 2025
# Description: LLM CLI
# Setup:
# You will need create these environment variables for each service you'd like to work with
# - `OPENAI_API_KEY='<your-openai-key>'`
# - `ANTHROPIC_API_KEY='<your-anthropic-key>'`
# - `GEMINI_API_KEY='<your-gemini-key>'`
import os
import sys
import json
from typing import List
from pathlib import Path
from urllib import request, error
from argparse import ArgumentParser, BooleanOptionalAction
#
# UTILITY FUNCTIONS
#
def find_and_replace(prompt: str, files: List[Path] | None) -> str:
"""
Find and replace variables in prompt with data from file,
otherwise append uninjected file text to end of prompt
"""
# Initialize files to empty list if None
files = files or []
# Find and replace text from file into prompt
file_data = [
(idx, file.stem, file.name, read_file(file))
for idx, file in enumerate(files)
]
found_placeholders = [False] * len(file_data)
for (idx, stem, _, content) in file_data:
name_placeholder = f'${stem.upper()}'
name_placeholder2 = f'${stem.lower()}'
index_placeholder = f'${idx+1}'
found_placeholders[idx] = name_placeholder in prompt or name_placeholder2 in prompt or index_placeholder in prompt
prompt = prompt.replace(name_placeholder, content.strip())
prompt = prompt.replace(name_placeholder2, content.strip())
prompt = prompt.replace(index_placeholder, content.strip())
# If no placeholders found in prompt, append file data to end of file
for idx, found in enumerate(found_placeholders):
if found:
continue
name = file_data[idx][2]
data = file_data[idx][3]
prompt += f'\n{name}:\n{data}'
return prompt
def print_inputs(prompt: str, model: str, output: str, tokens: int, entropy: float, role: str, files: List[Path] | None, verbose: bool = False):
params = locals()
params['prompt'] = prompt.split('\n')
print("\nInputs:")
for arg_name, arg_value in params.items():
if arg_name == 'prompt':
print(f' {arg_name}: {"\n ".join(arg_value)}')
else:
print(f' {arg_name}: {arg_value}')
print()
def create_argparser() -> ArgumentParser:
parser = ArgumentParser(prog='llm', description='LLM CLI')
subparsers = parser.add_subparsers(dest='command', required=True)
# env command
env_cmd = subparsers.add_parser('env', help='List found LLM environment variables')
env_cmd.add_argument(
'-V',
'--verbose',
type=bool,
default=False,
action=BooleanOptionalAction,
help='Enable verbose output'
)
# chat command and its arguments
prompt_cmd = subparsers.add_parser('chat', help='Send a prompt to an LLM model')
prompt_cmd.add_argument(
'-o',
'--output',
type=str,
choices=['json', 'text'],
default='text',
help='Output format - defaults to `text`'
)
prompt_cmd.add_argument(
'-t',
'--tokens',
type=int,
default=2048,
help='Max amount of tokens- defaults to `2048`'
)
prompt_cmd.add_argument(
'-e',
'--entropy',
metavar='{0.1...2.0}',
type=float,
choices=[.1, .2, .3, .4, .5, .6, .7, .8, .9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0],
default=0.7,
help='Model\'s temperature - defaults to `0.7`'
)
prompt_cmd.add_argument(
'-r',
'--role',
type=str,
choices=['user', 'system'],
default='user',
help='Model role - defaults to `user`'
)
prompt_cmd.add_argument(
'-V',
'--verbose',
type=bool,
default=False,
action=BooleanOptionalAction,
help='Enable verbose output'
)
prompt_cmd.add_argument(
'-f',
'--files',
type=Path,
nargs='*',
help='Optional file paths to read and append to prompt. Use $1, $2, etc. in prompt to specify file placement - otherwise files are appended in order at the end of the prompt.'
)
prompt_cmd.add_argument(
'-p',
'--prompt',
type=str,
required=True,
help='Input prompt to send to model'
)
prompt_cmd.add_argument(
'-m',
'--model',
type=str,
metavar='',
choices=[
# openai text models
'gpt-4',
'gpt-4o-mini',
'gpt-4o',
# anthropic text models
'claude-3-5-sonnet-latest',
'claude-3-5-haiku-latest',
'claude-3-opus-latest',
# gemini text models
'gemini-1.5-flash',
'gemini-1.5-flash-8b',
'gemini-1.5-pro',
],
default='gpt-4o-mini',
help='Defaults to gpt-4o-mini. OpenAI: gpt-4, gpt-4o-mini, gpt-4o. Anthropic: claude-3-5-sonnet-latest, claude-3-5-haiku-latest, claude-3-opus-latest. Gemini: gemini-1.5-flash, gemini-1.5-flash-8b, gemini-1.5-pro'
)
return parser
def fetch(url: str, method: str = 'POST', headers: dict | None = None, body: dict | None = None):
""" Make a post HTTP request """
if body:
body = json.dumps(body).encode('utf-8')
req = request.Request(
url,
method=method,
headers=headers or {},
data=body
)
try:
with request.urlopen(req) as res:
data = res.read().decode('utf-8')
try:
return json.loads(data)
except json.JSONDecodeError:
return data
except error.HTTPError as err:
return f'HTTP Error: {err.code}, {err.reason}'
except error.URLError as err:
return f'URL Error: {err.reason}'
def read_file(file: Path | None) -> str:
"""Read contents of a file and return as string. Returns empty string if file is None."""
if file is None:
sys.exit(0)
if not file.exists():
print(f'Warning: File not found: {file}', file=sys.stderr)
sys.exit(0)
try:
return file.read_text()
except Exception as e:
print(f'Error reading file {file}: {e}', file=sys.stderr)
sys.exit(0)
#
# LLM REQUEST FUNCTIONS
#
def openai_request(prompt: str, model: str, output: str, tokens: int, entropy: float, role: str, verbose: bool = False):
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY', None)
if OPENAI_API_KEY is None:
print('Could not find `OPENAI_API_KEY` environment variable', file=sys.stderr)
sys.exit(0)
res = fetch(
'https://api.openai.com/v1/chat/completions',
method='POST',
headers={
'Content-Type': 'application/json',
'Authorization': f'Bearer {OPENAI_API_KEY}',
},
body={
'model': model,
'messages': [{ 'role': role, 'content': prompt }],
'temperature': entropy,
'max_completion_tokens': tokens,
'response_format': {
'type': 'json_object' if output == 'json' else output
},
}
)
if verbose:
print(json.dumps(res, sort_keys=True, indent=4))
return
# Print response in format specified in command
msg = res['choices'][0]['message']['content']
if output == 'json':
data = json.loads(msg)
prety_json = json.dumps(data, sort_keys=True, indent=4)
print(prety_json)
else:
print(msg)
#
# CLI COMMAND FUNCTIONS
#
def env(verbose: bool = False):
""" List out `found` and `missing` LLM API environment variables """
services = {
'Anthropic': os.environ.get('ANTHROPIC_API_KEY', None),
'OpenAI': os.environ.get('OPENAI_API_KEY', None),
'Gemini': os.environ.get('GEMINI_API_KEY', None),
}
padding = max(len(service) for service in services)
for service, var in services.items():
if verbose:
status = f'\033[92m{var}\033[0m' if var is not None else '\033[91mmissing\033[0m'
print(f'{service:<{padding}}\t{status}')
else:
status = '\033[92mfound\033[0m' if var is not None else '\033[91mmissing\033[0m'
print(f'{service:<{padding}}\t{status}')
def chat(prompt: str, model: str, output: str, tokens: int, entropy: float, role: str, files: List[Path] | None, verbose: bool = False):
prompt = find_and_replace(prompt, files)
if verbose:
params = locals()
print_inputs(**params)
match model:
case 'gpt-4' | 'gpt-4o-mini' | 'gpt-4o':
params = locals()
openai_request(prompt, model, output, tokens, entropy, role, verbose)
case 'claude-3-5-sonnet-latest' | 'claude-3-5-haiku-latest' |'claude-3-opus-latest':
print('NOT_YET_IMPLEMENTED_ERROR: API call not yet implemented for Anthropic')
case 'gemini-1.5-flash' |'gemini-1.5-flash-8b' | 'gemini-1.5-pro':
print('NOT_YET_IMPLEMENTED_ERROR: API call not yet implemented for Gemini')
if __name__ == '__main__':
try:
parser = create_argparser()
args = parser.parse_args()
match args.command:
case 'env':
params = vars(args)
del params['command']
env(**params)
case 'chat':
params = vars(args)
del params['command']
chat(**params)
except KeyboardInterrupt:
sys.exit(1)