1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
rm -rf ./history.json

read -p "请输入 OpenAI API URL:" OpenAI_API
read -p "请输入 OpenAI API Key:" API_KEY
read -p "请输入模型名称:" MODEL_NAME
[[ -f "./history.json" ]] || echo "{\"model\": \"$MODEL_NAME\",\"messages\": [{\"role\": \"system\", \"content\": \"回答一句话,越简短越好,不需要 Markdown 和换行,要标点符号!\"}],\"stream\": false}" > ./history.json
clear

while true
do
read -p "请输入内容:" input
echo
sed -i '' "s/],/, {\"role\": \"user\", \"content\": \"${input}\"}],/" ./history.json
content=$(curl -sS $OpenAI_API -H "Content-Type: application/json" -H "Authorization: Bearer $API_KEY" -d "$(cat ./history.json)" | grep -oE '"content":"(.*?)"}' | sed -n 's/.*"content":"\([^"]*\)".*/\1/p')
echo "${MODEL_NAME}:${content}"
echo
sed -i '' "s/],/, {\"role\": \"assistant\", \"content\": \"${content}\"}],/" ./history.json
done