Created
November 22, 2023 19:02
-
-
Save ddrscott/66da278a5d439c2e70b556690a41f48b to your computer and use it in GitHub Desktop.
Cloudflare AI inference API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# message can come from script args or environment | |
message=${message:-"${*}"} | |
model=${model:-"@cf/mistral/mistral-7b-instruct-v0.1"} | |
system=${system:-"You are a consice AI assistant. You help the user the best you can. If you don't know something, you admin it and ask clarifying questions. Use markdown as needed."} | |
post_data=$(cat <<JSON | |
{"messages":[{"role":"system","content":"${system}"},{"role":"user","content":"${message}"}],"max_tokens":10240,"stream":true} | |
JSON | |
) | |
curl -X POST -sN \ | |
"https://api.cloudflare.com/client/v4/accounts/${CF_ACCOUNT_ID}/ai/run/${model}" \ | |
-H "Authorization: Bearer ${CF_API_TOKEN}" \ | |
-d "${post_data}" \ | |
| grep --line-buffered '"response"' \ | |
| stdbuf -oL sed 's/data: //' \ | |
| stdbuf -oL jq -r '.response' \ | |
| while IFS= read -r l; do /bin/echo -ne "${l}"; done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requires
jq
.Lets of tricks had to be performed to get streaming to work as expected. Line buffering had to be disabled at each step to get tokens parsed and emitted immediately.