-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellm.py
More file actions
285 lines (222 loc) · 8.8 KB
/
shellm.py
File metadata and controls
285 lines (222 loc) · 8.8 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
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
#!/usr/bin/env python3
"""
shellm - Natural language to shell command CLI tool
"""
import argparse
import sys
import shutil
from pathlib import Path
import yaml
from platformdirs import user_config_dir
import requests
import json
DEFAULT_BASE_URL = "https://api.openai.com/v1"
DEFAULT_MODEL = "gpt-3.5-turbo"
def get_config_path():
"""Get path to config file."""
config_dir = user_config_dir("shellm")
return Path(config_dir) / "config.yaml"
def get_prompts_path():
"""Get path to prompts file."""
config_dir = user_config_dir("shellm")
return Path(config_dir) / "prompts.yaml"
def copy_default_prompts():
"""Copy default prompts.yaml from project directory to config directory."""
prompts_path = get_prompts_path()
prompts_path.parent.mkdir(parents=True, exist_ok=True)
# Copy default prompts.yaml from project directory
default_prompts_path = Path(__file__).parent / "prompts.yaml"
if default_prompts_path.exists():
shutil.copy2(default_prompts_path, prompts_path)
# Load and return the copied prompts
with open(prompts_path, 'r') as f:
return yaml.safe_load(f)
# Fallback if source file doesn't exist
return {'system_prompt': 'You are a helpful assistant that converts natural language to shell commands.'}
def load_prompts():
"""Load prompts from config directory."""
prompts_path = get_prompts_path()
if not prompts_path.exists():
return copy_default_prompts()
try:
with open(prompts_path, 'r') as f:
return yaml.safe_load(f)
except Exception as e:
print(f"Error loading prompts: {e}", file=sys.stderr)
return copy_default_prompts()
def create_default_config():
"""Create config file by prompting user for each field."""
config_path = get_config_path()
config_path.parent.mkdir(parents=True, exist_ok=True)
print("Setting up shellm configuration...")
print(f"Config will be saved to: {config_path}")
print()
# Prompt for base URL with default
base_url = input(f"Enter API base URL [{DEFAULT_BASE_URL}]: ").strip()
if not base_url:
base_url = DEFAULT_BASE_URL
# Prompt for API key
api_key = input("Enter your API key: ").strip()
# Prompt for model with default
model = input(f"Enter model name [{DEFAULT_MODEL}]: ").strip()
if not model:
model = DEFAULT_MODEL
config = {
'api': {
'base_url': base_url,
'key': api_key,
'model': model
},
'network': {
'proxy': None,
'ca_cert_path': None,
'ssl_verify': True
}
}
with open(config_path, 'w') as f:
yaml.dump(config, f, default_flow_style=False)
print(f"\nConfiguration saved to: {config_path}")
return config
def load_config():
"""Load configuration from file."""
config_path = get_config_path()
if not config_path.exists():
return create_default_config()
try:
with open(config_path, 'r') as f:
config = yaml.safe_load(f)
return config
except Exception as e:
print(f"Error loading config: {e}", file=sys.stderr)
return create_default_config()
def get_client():
"""Initialize client from config."""
config = load_config()
if 'api' not in config:
print("Error: Invalid config file - missing 'api' section", file=sys.stderr)
print(f"Config file: {get_config_path()}", file=sys.stderr)
print("Delete the config file to recreate it", file=sys.stderr)
sys.exit(1)
api_config = config['api']
api_required_fields = ['base_url', 'key', 'model']
for field in api_required_fields:
if field not in api_config:
print(f"Error: Missing required field '{field}' in config", file=sys.stderr)
print(f"Config file: {get_config_path()}", file=sys.stderr)
print("Delete the config file to recreate it", file=sys.stderr)
sys.exit(1)
if not api_config[field]:
print(f"Error: Empty value for required field '{field}' in config", file=sys.stderr)
print(f"Config file: {get_config_path()}", file=sys.stderr)
sys.exit(1)
base_url = api_config['base_url']
api_key = api_config['key']
model = api_config['model']
# Load system prompt from prompts file
prompts_config = load_prompts()
system_prompt = prompts_config.get('system_prompt', 'You are a helpful assistant that converts natural language to shell commands.')
description_prompt = prompts_config.get('description_prompt', 'Analyze the safety of this shell command.')
# Load optional network configuration
network_config = config.get('network', {})
proxy = network_config.get('proxy')
ca_cert_path = network_config.get('ca_cert_path')
ssl_verify = network_config.get('ssl_verify', True)
return api_key, base_url, model, system_prompt, description_prompt, proxy, ca_cert_path, ssl_verify
def generate_shell_command(api_key, base_url, model, system_prompt, description, proxy=None, ca_cert_path=None, ssl_verify=True):
"""Generate shell command from natural language description."""
try:
url = f"{base_url}/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
data = {
"model": model,
"messages": [
{"role": "system", "content": system_prompt},
{"role": "user", "content": description}
],
"max_tokens": 200,
"temperature": 0.1
}
# Build request kwargs with optional network configurations
request_kwargs = {}
if proxy:
request_kwargs['proxies'] = {'http': proxy, 'https': proxy}
if ca_cert_path:
request_kwargs['verify'] = ca_cert_path
elif not ssl_verify:
request_kwargs['verify'] = False
response = requests.post(url, headers=headers, json=data, **request_kwargs)
response.raise_for_status()
result = response.json()
command = result["choices"][0]["message"]["content"].strip()
return command
except Exception as e:
print(f"Error calling API: {e}", file=sys.stderr)
sys.exit(1)
def describe_shell_command(api_key, base_url, model, description_prompt, command, proxy=None, ca_cert_path=None, ssl_verify=True):
"""Describe the generated shell command."""
try:
url = f"{base_url}/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
data = {
"model": model,
"messages": [
{"role": "system", "content": description_prompt},
{"role": "user", "content": f"Analyze this shell command: {command}"}
],
"max_tokens": 150,
"temperature": 0.1
}
# Build request kwargs with optional network configurations
request_kwargs = {}
if proxy:
request_kwargs['proxies'] = {'http': proxy, 'https': proxy}
if ca_cert_path:
request_kwargs['verify'] = ca_cert_path
elif not ssl_verify:
request_kwargs['verify'] = False
response = requests.post(url, headers=headers, json=data, **request_kwargs)
response.raise_for_status()
result = response.json()
assessment = result["choices"][0]["message"]["content"].strip()
return assessment
except Exception as e:
print(f"Error assessing command: {e}", file=sys.stderr)
return "UNKNOWN: Unable to assess command"
def main():
"""Main CLI entry point."""
parser = argparse.ArgumentParser(
description="Convert natural language to shell commands",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""Examples:
shellm "list all python files"
shellm "find files larger than 100MB"
shellm "show disk usage for current directory"
"""
)
parser.add_argument(
'description',
help='Natural language description of what you want to do'
)
parser.add_argument(
'-v', '--version',
action='version',
version='shellm 0.1.0'
)
args = parser.parse_args()
# Initialize client
api_key, base_url, model, system_prompt, description_prompt, proxy, ca_cert_path, ssl_verify = get_client()
# Generate command
command = generate_shell_command(api_key, base_url, model, system_prompt, args.description, proxy, ca_cert_path, ssl_verify)
# Output the command
print(command)
# Describe the shell command
description = describe_shell_command(api_key, base_url, model, description_prompt, command, proxy, ca_cert_path, ssl_verify)
print(description)
if __name__ == '__main__':
main()