diff options
| author | Hsieh Chin Fan <pham@topo.tw> | 2023-03-21 15:56:02 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <pham@topo.tw> | 2023-03-21 15:56:02 +0800 |
| commit | 73e036b244965e91d80de5a232afa7cfa6c3469f (patch) | |
| tree | fcacfba3d260817f613a96427da1f805cd54d36c | |
| parent | 122e8c19568fadf1d09b908ffafe537a5bbee047 (diff) | |
Add gpt command
| -rwxr-xr-x | bin/gpt/gpt | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/bin/gpt/gpt b/bin/gpt/gpt new file mode 100755 index 0000000..cdbb997 --- /dev/null +++ b/bin/gpt/gpt | |||
| @@ -0,0 +1,38 @@ | |||
| 1 | #! /bin/bash | ||
| 2 | |||
| 3 | |||
| 4 | # TODO | ||
| 5 | # 1. read arguments about model or system message | ||
| 6 | # 2. Put previous response into tokens | ||
| 7 | |||
| 8 | OPENAI_API_KEY=$(token openai) | ||
| 9 | |||
| 10 | # Available: | ||
| 11 | # v1/models | ||
| 12 | # v1/chat/completions | ||
| 13 | ROUTE=v1/chat/completions | ||
| 14 | |||
| 15 | model=${model:-gpt-4} | ||
| 16 | read content | ||
| 17 | body="$(cat <<EOF | ||
| 18 | { | ||
| 19 | "model": "$model", | ||
| 20 | "messages": [ | ||
| 21 | {"role": "system", "content": "You are a helpful assistant about programming"}, | ||
| 22 | {"role": "user", "content": "$content"} | ||
| 23 | ], | ||
| 24 | "temperature": 0.7 | ||
| 25 | } | ||
| 26 | EOF | ||
| 27 | )" | ||
| 28 | |||
| 29 | echo | ||
| 30 | |||
| 31 | # API call | ||
| 32 | curl https://api.openai.com/$ROUTE \ | ||
| 33 | --silent \ | ||
| 34 | -H "Content-Type: application/json" \ | ||
| 35 | -H "Authorization: Bearer $OPENAI_API_KEY" \ | ||
| 36 | -d "$body" | \ | ||
| 37 | jq . | tee .gpt | \ | ||
| 38 | jq -r .choices[0].message.content | ||