Skip to content

Instantly share code, notes, and snippets.

@kofemann
Last active October 9, 2024 13:19
Show Gist options
  • Save kofemann/26a8db491c688f7bb1fd6bbf798d2fdf to your computer and use it in GitHub Desktop.
Save kofemann/26a8db491c688f7bb1fd6bbf798d2fdf to your computer and use it in GitHub Desktop.
Simple screept to ask LLM to explain given source code file
#!/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 an experienced ${language} software developer." \
~/bin/chatgpt \
"Explain provided ${language} file. Identify key algorithms and design patterns.
Generate the output in markdown format." | $MARKDOWN_HIGHLIGHER
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment