-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathzsh-llm-suggestions-openai.py
executable file
·67 lines (56 loc) · 1.99 KB
/
zsh-llm-suggestions-openai.py
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
#!/usr/bin/env python3
import sys
import os
MISSING_PREREQUISITES = "zsh-llm-suggestions missing prerequisites:"
def highlight_explanation(explanation):
try:
import pygments
from pygments.lexers import MarkdownLexer
from pygments.formatters import TerminalFormatter
return pygments.highlight(explanation, MarkdownLexer(), TerminalFormatter(style='material'))
except ImportError:
return explanation
def main():
mode = sys.argv[1]
if mode != 'generate' and mode != 'explain':
print("ERROR: something went wrong in zsh-llm-suggestions, please report a bug. Got unknown mode: " + mode)
return
try:
import openai
except ImportError:
print(f'echo "{MISSING_PREREQUISITES} Install OpenAI Python API." && pip3 install openai')
return
api_key = os.environ.get('OPENAI_API_KEY')
if api_key is None:
print(f'echo "{MISSING_PREREQUISITES} OPENAI_API_KEY is not set." && export OPENAI_API_KEY="<copy from https://platform.openai.com/api-keys>"')
return
client = openai.Client(
api_key=api_key,
)
buffer = sys.stdin.read()
system_message="""You are a zsh shell expert, please write a ZSH command that solves my problem.
You should only output the completed command, no need to include any other explanation."""
if mode == 'explain':
system_message="""You are a zsh shell expert, please briefly explain how the given command works. Be as concise as possible. Use Markdown syntax for formatting."""
message=[
{
"role":'system',
"content": system_message,
},
{"role": "user", "content": buffer}
]
response = client.chat.completions.create(
model="gpt-4-1106-preview",
messages = message,
temperature=0.2,
max_tokens=1000,
frequency_penalty=0.0
)
result = response.choices[0].message.content.strip()
if mode == 'generate':
result = result.replace('```zsh', '').replace('```', '').strip()
print(result)
if mode == 'explain':
print(highlight_explanation(result))
if __name__ == '__main__':
main()