mr-helper/ai-chat
2023-03-16 17:30:15 -07:00

29 lines
931 B
Bash
Executable File

#!/usr/bin/env bash
conversation_file=$(mktemp)
echo new conversation: "$conversation_file"
prompt="You are Mr.Helper. You are an assistant that lives in a floating terminal above my mac. I can summon you with the press of a button! Feel free to use ansii escape codes to add colors or other formatting to your responses."
echo -n "$prompt" | jq -sR '{"role": "system", "content": .}' > "$conversation_file"
while read -rp '> ' prompt ; do
[ -n "$prompt" ] || continue
echo -n "$prompt" | jq -sR '{"role": "user", "content": .}' >> "$conversation_file"
messages=$(jq -rsc < "$conversation_file")
out="$(
http \
-A bearer \
--auth "$(pass openai/api-key)" \
https://api.openai.com/v1/chat/completions \
model=gpt-3.5-turbo \
messages:="$messages"
)"
reply=$(echo "$out" | jq -rc .choices[0].message)
echo "$reply" >> "$conversation_file"
echo "$reply" | jq -rc .content
done