-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexplain-code.sh
66 lines (58 loc) · 1.67 KB
/
explain-code.sh
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
#!/bin/sh
#
# Simple utility to explore unknown code.
#
# Dependencies:
# - access to LLM API
# - chatgpt client https://github.com/kardolus/chatgpt-cli/
# - glow to beautify the output https://github.com/charmbracelet/glow
#
MARKDOWN_HIGHLIGHER=/usr/bin/glow
if [ ! -f $MARKDOWN_HIGHLIGHER ]
then
MARKDOWN_HIGHLIGHER=/usr/bin/tr '[a-z]' '[a-z]'
fi
if [ ! -f $1 ]
then
echo "Usage: explain-code <file-name>"
exit 1
fi
filename=`basename $1`
language=""
case $filename in
*.py)
language="Python"
;;
*.java)
language="Java"
;;
*.js)
language="Javascript"
;;
*.c)
language="C"
;;
*.cpp|*.cc)
language="C++"
;;
*.cs)
language="C#"
;;
*.sh)
language="UNIX-shell"
;;
*.sql)
language="SQL"
;;
esac
(cat $1 | OPENAI_API_KEY=${GH_LLM_KEY} \
OPENAI_URL=https://models.inference.ai.azure.com \
OPENAI_MODEL=gpt-4o-mini\
OPENAI_COMPLETIONS_PATH=/chat/completions \
OPENAI_ROLE="You are a highly-skilled, experienced ${language} software developer.
Your primary goal is to assist in understanding complex software systems." \
~/bin/chatgpt \
"Provide a detailed explanation of the provided ${language} file. Explore the functionality and code flow with code snippets. Identify key algorithms and design patterns.
Elaborate on the usage of well-known frameworks and libraries. Point out code sections that must be explored carefully and provide hints on what to look for.
Generate the output in markdown format."; echo '>NOTE: This is an AI-generated summary that can be **inaccurate of even wrong**.') \
| $MARKDOWN_HIGHLIGHER