mr-helper/ai-chat

35 lines
1.1 KiB
Plaintext
Raw Normal View History

2023-02-22 05:55:23 +00:00
#!/usr/bin/env bash
conversation_file=$(mktemp)
echo new conversation: "$conversation_file"
OPENAPI_TOKEN=$(pass openai/api-key)
2023-02-22 05:55:23 +00:00
2023-03-17 00:30:15 +00:00
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."
2023-02-22 05:55:23 +00:00
2023-03-17 00:30:15 +00:00
echo -n "$prompt" | jq -sR '{"role": "system", "content": .}' > "$conversation_file"
2023-02-22 05:55:23 +00:00
2023-03-23 16:12:18 +00:00
bind 'set disable-completion On' 2>/dev/null
2023-03-17 16:45:44 +00:00
printf '\033[48;5;241m\033[38;5;15m'
2023-03-17 00:45:01 +00:00
while read -rep '> ' prompt ; do
2023-03-17 00:30:15 +00:00
[ -n "$prompt" ] || continue
2023-02-22 05:55:23 +00:00
2023-03-17 00:30:15 +00:00
echo -n "$prompt" | jq -sR '{"role": "user", "content": .}' >> "$conversation_file"
2023-02-22 05:55:23 +00:00
2023-03-17 00:30:15 +00:00
messages=$(jq -rsc < "$conversation_file")
2023-02-22 05:55:23 +00:00
2023-03-17 00:30:15 +00:00
out="$(
http \
-A bearer \
--auth "$OPENAPI_TOKEN" \
2023-03-17 00:30:15 +00:00
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"
2023-03-17 16:45:44 +00:00
printf '\033[48;5;235m\033[38;5;15m'
2023-03-17 00:30:15 +00:00
echo "$reply" | jq -rc .content
2023-03-17 16:45:44 +00:00
printf '\033[48;5;241m\033[38;5;15m'
2023-02-22 05:55:23 +00:00
done