-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path-
More file actions
186 lines (140 loc) · 4.19 KB
/
Copy path-
File metadata and controls
186 lines (140 loc) · 4.19 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
#!/bin/bash
# Function to determine the shell configuration file
get_shell_rc_file() {
local shell_name
shell_name=$(basename "$SHELL")
case "$shell_name" in
bash)
# Check for .bash_profile first, then .bashrc
if [ -f "$HOME/.bash_profile" ]; then
echo "$HOME/.bash_profile"
elif [ -f "$HOME/.bashrc" ]; then
echo "$HOME/.bashrc"
else
echo "No .bash_profile or .bashrc found for bash"
exit 1
fi
;;
zsh)
echo "$HOME/.zshrc"
;;
ksh)
echo "$HOME/.kshrc"
;;
*)
echo "Unsupported shell: $shell_name"
exit 1
;;
esac
}
binding_line='bind -x "\"\\C-k\": stty -g > /tmp/stty.now; stty sane; shllm; stty $(cat /tmp/stty.now)"'
# Determine the right shell configuration file
rc_file=$(get_shell_rc_file)
echo -e "\n\n"
# Add the binding line to the shell configuration if not already present
if [ -f "$rc_file" ]; then
if ! grep -Fxq "$binding_line" "$rc_file"; then
echo -e "\n# Bind Ctrl+K to a shllm" >> "$rc_file"
echo "$binding_line" >> "$rc_file"
echo "Ctrl+K binding added to $rc_file"
. "$rc_file" > /dev/null 2>&1
else
echo "Binding already present in $rc_file"
fi
else
echo "Shell configuration file $rc_file not found."
fi
filename="/usr/local/bin/shllm"
filename_core="/usr/local/bin/core_shllm"
# Use a Heredoc to write the script content to the file
cat << 'EOF' > "$filename_core"
#!/usr/bin/env python3
import os
import sys
import http.client
import json
def llm(cmd):
current_directory = os.getcwd()
files = [f for f in os.listdir(current_directory)]
api_key = os.getenv('OPENAI_API_KEY')
# Connection to OpenAI's API
conn = http.client.HTTPSConnection("api.openai.com")
# Define the headers, including the authorization and content type
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}',
}
payload = json.dumps({
"model": "gpt-4o-mini",
"messages": [
{
"role": "system",
"content": "you should always only output a single bash one-liner"
}, {
"role": "user",
"content": f'{cmd} - here are the content of the current dir: ${files}',
}
],
})
# Make a POST request to the completions endpoint
conn.request("POST", "/v1/chat/completions", payload, headers)
# Get the response from the server
res = conn.getresponse()
data = res.read()
response_text = data.decode("utf-8")
conn.close()
response_data = json.loads(response_text)
assistant_response = response_data['choices'][0]['message']['content']
assistant_response = assistant_response \
.replace('```bash\n', '') \
.replace('```\n', '') \
.replace('```', '') \
.replace('`', '')
return assistant_response
if len(sys.argv) > 1:
user_cmd = sys.argv[1]
print(llm(user_cmd))
else:
print("No variable passed")
EOF
cat << 'EOF' > "$filename"
execute_command() {
# Prompt the user for input
echo -n "shllm> "
read user_command < /dev/tty
if [[ "$user_command" == "?" || "$user_command" == "help" ]]; then
cat << 'EOHELP'
shllm: integrated LLM in your shell
To run press Ctrl+K or execute shllm cmd.
Examples:
1.
shllm> ffmpeg -i input.mp4 remove the audio and use the original video encoding
ffmpeg -i input.mp4 -c:v copy -an output.mp4
2.
shllm> remove the last line of the README.md file
sed -i '$d' README.md
3.
shllm> delete files that have two l's in their name
rm *l*l*
4.
shllm> htop sorted by most memory used
htop --sort-key=MEMORY
EOHELP
exit
fi
llm_command=$(/usr/local/bin/core_shllm "$user_command")
echo ""
echo "$llm_command"
echo ""
echo "Press any key to run or Ctrl+C to abort"
read key < /dev/tty
echo "$llm_command" >> ~/.bash_history
eval "$llm_command"
history -r
}
# Call the function
execute_command
EOF
# Make the script executable
chmod +x "$filename"
chmod +x "$filename_core"