-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsummarize_file.sh
62 lines (49 loc) · 1.62 KB
/
summarize_file.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
#summarize_file.sh
#!/usr/bin/env bash
# This script sends the contents of a given file to the OpenAI API and asks for a short summary
# of the file's salient details and functions.
set -e
if [ -z "$OPENAI_API_KEY" ]; then
echo "Error: OPENAI_API_KEY environment variable is not set."
exit 1
fi
if [ -z "$OPENAI_MODEL" ]; then
echo "Error: OPENAI_MODEL environment variable is not set."
exit 1
fi
if [ $# -lt 1 ]; then
echo "Usage: $0 <file_path>"
exit 1
fi
FILE_PATH="$1"
if [ ! -f "$FILE_PATH" ]; then
echo "Error: File '$FILE_PATH' not found."
exit 1
fi
FILE_CONTENT=$(cat "$FILE_PATH")
# Create the prompt asking the model to summarize the file
PROMPT="You are an assistant who reads code files. Given the following file content, create a short summary highlighting the salient details, main functions, and purpose of the code."
# Call the OpenAI API
RESPONSE=$(curl -sS https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d "$(jq -n \
--arg model_var "$OPENAI_MODEL" \
--arg prompt "$PROMPT" \
--arg file_content "$FILE_CONTENT" \
'{
"model": $model_var,
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": $prompt},
{"role": "user", "content": $file_content}
],
"max_tokens": 300,
"temperature": 0.7
}'
)")
# Check if the response contains an error
echo "$RESPONSE" >&2
# Extract the generated summary from the response
SUMMARY=$(echo "$RESPONSE" | jq -r '.choices[0].message.content')
echo "$SUMMARY"