2024-04-28 08:37:45 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -e
|
|
|
|
|
|
|
|
# @meta dotenv
|
|
|
|
# @env DRY_RUN Dry run mode
|
|
|
|
|
|
|
|
# @cmd Test first running
|
|
|
|
# @env AICHAT_CONFIG_DIR=tmp/test-init-config
|
|
|
|
# @arg args~
|
|
|
|
test-init-config() {
|
|
|
|
unset OPENAI_API_KEY
|
|
|
|
mkdir -p "$AICHAT_CONFIG_DIR"
|
|
|
|
config_file="$AICHAT_CONFIG_DIR/config.yaml"
|
|
|
|
if [[ -f "$config_file" ]]; then
|
|
|
|
rm -f "$config_file"
|
|
|
|
fi
|
|
|
|
cargo run -- "$@"
|
|
|
|
}
|
|
|
|
|
2024-04-30 05:15:13 +00:00
|
|
|
# @cmd Test running with AICHAT_PLATFORM environment variable
|
2024-04-30 04:52:58 +00:00
|
|
|
# @env AICHAT_PLATFORM!
|
2024-04-28 08:37:45 +00:00
|
|
|
# @arg args~
|
2024-04-30 04:52:58 +00:00
|
|
|
test-platform-env() {
|
2024-04-28 08:37:45 +00:00
|
|
|
cargo run -- "$@"
|
|
|
|
}
|
|
|
|
|
2024-05-18 11:06:21 +00:00
|
|
|
# @cmd Test function calling
|
|
|
|
# @option --model[?`_choice_model`]
|
|
|
|
# @option --preset[=default|weather|multi-weathers]
|
|
|
|
# @flag -S --no-stream
|
|
|
|
# @arg text~
|
|
|
|
test-function-calling() {
|
|
|
|
args=(--role %functions%)
|
|
|
|
if [[ -n "$argc_model" ]]; then
|
|
|
|
args+=("--model" "$argc_model")
|
|
|
|
fi
|
|
|
|
if [[ -n "$argc_no_stream" ]]; then
|
|
|
|
args+=("-S")
|
|
|
|
fi
|
|
|
|
if [[ -z "$argc_text" ]]; then
|
|
|
|
case "$argc_preset" in
|
|
|
|
multi-weathers)
|
|
|
|
text="what is the weather in London and Pairs?"
|
|
|
|
;;
|
|
|
|
weather|*)
|
|
|
|
text="what is the weather in London?"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
else
|
|
|
|
text="${argc_text[*]}"
|
|
|
|
fi
|
|
|
|
cargo run -- "${args[@]}" "$text"
|
|
|
|
}
|
|
|
|
|
2024-04-28 08:37:45 +00:00
|
|
|
# @cmd Test clients
|
|
|
|
# @arg clients+[`_choice_client`]
|
|
|
|
test-clients() {
|
|
|
|
for c in "${argc_clients[@]}"; do
|
2024-04-28 12:37:46 +00:00
|
|
|
echo "### $c stream"
|
2024-04-28 08:37:45 +00:00
|
|
|
aichat -m "$c" 1 + 2 = ?
|
2024-04-28 12:37:46 +00:00
|
|
|
echo "### $c non-stream"
|
2024-04-28 08:37:45 +00:00
|
|
|
aichat -m "$c" -S 1 + 2 = ?
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# @cmd Test proxy server
|
2024-05-18 11:06:21 +00:00
|
|
|
# @option -m --model[?`_choice_model`]
|
2024-04-28 12:37:46 +00:00
|
|
|
# @flag -S --no-stream
|
2024-04-28 08:37:45 +00:00
|
|
|
# @arg text~
|
|
|
|
test-server() {
|
2024-04-28 12:37:46 +00:00
|
|
|
args=()
|
|
|
|
if [[ -n "$argc_no_stream" ]]; then
|
|
|
|
args+=("-S")
|
|
|
|
fi
|
2024-04-28 22:51:03 +00:00
|
|
|
argc chat-llm "${args[@]}" \
|
2024-04-28 08:37:45 +00:00
|
|
|
--api-base http://localhost:8000/v1 \
|
2024-04-28 22:51:03 +00:00
|
|
|
--model "${argc_model:-default}" \
|
2024-04-28 08:37:45 +00:00
|
|
|
"$@"
|
|
|
|
}
|
|
|
|
|
2024-04-30 05:15:13 +00:00
|
|
|
OPENAI_COMPATIBLE_PLATFORMS=( \
|
2024-04-30 04:52:58 +00:00
|
|
|
openai,gpt-3.5-turbo,https://api.openai.com/v1 \
|
|
|
|
anyscale,meta-llama/Meta-Llama-3-8B-Instruct,https://api.endpoints.anyscale.com/v1 \
|
|
|
|
deepinfra,meta-llama/Meta-Llama-3-8B-Instruct,https://api.deepinfra.com/v1/openai \
|
2024-05-07 08:16:18 +00:00
|
|
|
deepseek,deepseek-chat,https://api.deepseek.com \
|
2024-04-30 04:52:58 +00:00
|
|
|
fireworks,accounts/fireworks/models/llama-v3-8b-instruct,https://api.fireworks.ai/inference/v1 \
|
|
|
|
groq,llama3-8b-8192,https://api.groq.com/openai/v1 \
|
|
|
|
mistral,mistral-small-latest,https://api.mistral.ai/v1 \
|
|
|
|
moonshot,moonshot-v1-8k,https://api.moonshot.cn/v1 \
|
|
|
|
openrouter,meta-llama/llama-3-8b-instruct,https://openrouter.ai/api/v1 \
|
|
|
|
octoai,meta-llama-3-8b-instruct,https://text.octoai.run/v1 \
|
|
|
|
perplexity,llama-3-8b-instruct,https://api.perplexity.ai \
|
|
|
|
together,meta-llama/Llama-3-8b-chat-hf,https://api.together.xyz/v1 \
|
2024-05-07 08:40:18 +00:00
|
|
|
zhipuai,glm-4,https://open.bigmodel.cn/api/paas/v4 \
|
2024-04-30 04:52:58 +00:00
|
|
|
)
|
|
|
|
|
2024-04-30 05:15:13 +00:00
|
|
|
# @cmd Chat with any LLM api
|
2024-04-28 08:37:45 +00:00
|
|
|
# @flag -S --no-stream
|
2024-04-30 05:15:13 +00:00
|
|
|
# @arg platform_model![?`_choice_platform_model`]
|
2024-04-28 08:37:45 +00:00
|
|
|
# @arg text~
|
2024-04-30 04:52:58 +00:00
|
|
|
chat() {
|
2024-04-30 05:15:13 +00:00
|
|
|
if [[ "$argc_platform_model" == *':'* ]]; then
|
|
|
|
model="${argc_platform_model##*:}"
|
|
|
|
argc_platform="${argc_platform_model%:*}"
|
|
|
|
else
|
|
|
|
argc_platform="${argc_platform_model}"
|
|
|
|
fi
|
|
|
|
for platform_config in "${OPENAI_COMPATIBLE_PLATFORMS[@]}"; do
|
|
|
|
if [[ "$argc_platform" == "${platform_config%%,*}" ]]; then
|
|
|
|
api_base="${platform_config##*,}"
|
2024-04-30 04:52:58 +00:00
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
if [[ -n "$api_base" ]]; then
|
|
|
|
env_prefix="$(echo "$argc_platform" | tr '[:lower:]' '[:upper:]')"
|
|
|
|
api_key_env="${env_prefix}_API_KEY"
|
|
|
|
api_key="${!api_key_env}"
|
|
|
|
if [[ -z "$model" ]]; then
|
2024-04-30 05:15:13 +00:00
|
|
|
model="$(echo "$platform_config" | cut -d, -f2)"
|
2024-04-30 04:52:58 +00:00
|
|
|
fi
|
|
|
|
if [[ -z "$model" ]]; then
|
|
|
|
model_env="${env_prefix}_MODEL"
|
|
|
|
model="${!model_env}"
|
|
|
|
fi
|
2024-05-02 09:17:50 +00:00
|
|
|
argc chat-openai-compatible \
|
2024-04-30 04:52:58 +00:00
|
|
|
--api-base "$api_base" \
|
|
|
|
--api-key "$api_key" \
|
|
|
|
--model "$model" \
|
|
|
|
"${argc_text[@]}"
|
|
|
|
else
|
|
|
|
argc chat-$argc_platform "${argc_text[@]}"
|
|
|
|
fi
|
2024-04-28 08:37:45 +00:00
|
|
|
}
|
|
|
|
|
2024-05-02 09:17:50 +00:00
|
|
|
# @cmd List models by openai-compatible api
|
2024-04-30 04:52:58 +00:00
|
|
|
# @arg platform![`_choice_platform`]
|
|
|
|
models() {
|
2024-04-30 05:15:13 +00:00
|
|
|
for platform_config in "${OPENAI_COMPATIBLE_PLATFORMS[@]}"; do
|
|
|
|
if [[ "$argc_platform" == "${platform_config%%,*}" ]]; then
|
|
|
|
api_base="${platform_config##*,}"
|
2024-04-30 04:52:58 +00:00
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
if [[ -n "$api_base" ]]; then
|
|
|
|
env_prefix="$(echo "$argc_platform" | tr '[:lower:]' '[:upper:]')"
|
|
|
|
api_key_env="${env_prefix}_API_KEY"
|
|
|
|
api_key="${!api_key_env}"
|
|
|
|
_openai_models
|
|
|
|
else
|
|
|
|
argc models-$argc_platform
|
|
|
|
fi
|
2024-04-28 08:37:45 +00:00
|
|
|
}
|
|
|
|
|
2024-05-02 09:17:50 +00:00
|
|
|
# @cmd Chat with openai-compatible api
|
2024-04-30 04:52:58 +00:00
|
|
|
# @option --api-base! $$
|
|
|
|
# @option --api-key! $$
|
|
|
|
# @option -m --model! $$
|
2024-04-28 08:37:45 +00:00
|
|
|
# @flag -S --no-stream
|
|
|
|
# @arg text~
|
2024-05-02 09:17:50 +00:00
|
|
|
chat-openai-compatible() {
|
2024-04-28 08:37:45 +00:00
|
|
|
_openai_chat "$@"
|
|
|
|
}
|
|
|
|
|
2024-05-02 09:17:50 +00:00
|
|
|
# @cmd List models by openai-compatible api
|
2024-04-30 04:52:58 +00:00
|
|
|
# @option --api-base! $$
|
|
|
|
# @option --api-key! $$
|
2024-05-02 09:17:50 +00:00
|
|
|
models-openai-compatible() {
|
2024-04-28 08:37:45 +00:00
|
|
|
_openai_models
|
|
|
|
}
|
|
|
|
|
|
|
|
# @cmd Chat with gemini api
|
|
|
|
# @env GEMINI_API_KEY!
|
|
|
|
# @option -m --model=gemini-1.0-pro-latest $GEMINI_MODEL
|
|
|
|
# @flag -S --no-stream
|
|
|
|
# @arg text~
|
2024-04-28 22:51:03 +00:00
|
|
|
chat-gemini() {
|
2024-04-28 08:37:45 +00:00
|
|
|
method="streamGenerateContent"
|
|
|
|
if [[ -n "$argc_no_stream" ]]; then
|
|
|
|
method="generateContent"
|
|
|
|
fi
|
2024-04-30 04:52:58 +00:00
|
|
|
_wrapper curl -i "https://generativelanguage.googleapis.com/v1beta/models/${argc_model}:${method}?key=${GEMINI_API_KEY}" \
|
2024-04-28 08:37:45 +00:00
|
|
|
-i -X POST \
|
|
|
|
-H 'Content-Type: application/json' \
|
2024-05-18 11:06:21 +00:00
|
|
|
-d "$(_build_body gemini "$@")"
|
2024-04-28 08:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# @cmd List gemini models
|
|
|
|
# @env GEMINI_API_KEY!
|
2024-04-28 22:51:03 +00:00
|
|
|
models-gemini() {
|
2024-04-30 04:52:58 +00:00
|
|
|
_wrapper curl "https://generativelanguage.googleapis.com/v1beta/models?key=${GEMINI_API_KEY}" \
|
2024-04-28 08:37:45 +00:00
|
|
|
-H 'Content-Type: application/json' \
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
# @cmd Chat with claude api
|
|
|
|
# @env CLAUDE_API_KEY!
|
|
|
|
# @option -m --model=claude-3-haiku-20240307 $CLAUDE_MODEL
|
|
|
|
# @flag -S --no-stream
|
|
|
|
# @arg text~
|
2024-04-28 22:51:03 +00:00
|
|
|
chat-claude() {
|
2024-04-30 04:52:58 +00:00
|
|
|
_wrapper curl -i https://api.anthropic.com/v1/messages \
|
2024-04-28 08:37:45 +00:00
|
|
|
-X POST \
|
|
|
|
-H 'content-type: application/json' \
|
|
|
|
-H 'anthropic-version: 2023-06-01' \
|
2024-05-18 11:06:21 +00:00
|
|
|
-H 'anthropic-beta: tools-2024-05-16' \
|
2024-04-28 08:37:45 +00:00
|
|
|
-H "x-api-key: $CLAUDE_API_KEY" \
|
2024-05-18 11:06:21 +00:00
|
|
|
-d "$(_build_body claude "$@")"
|
2024-04-28 08:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# @cmd Chat with cohere api
|
|
|
|
# @env COHERE_API_KEY!
|
|
|
|
# @option -m --model=command-r $COHERE_MODEL
|
|
|
|
# @flag -S --no-stream
|
|
|
|
# @arg text~
|
2024-04-28 22:51:03 +00:00
|
|
|
chat-cohere() {
|
2024-04-30 04:52:58 +00:00
|
|
|
_wrapper curl -i https://api.cohere.ai/v1/chat \
|
2024-04-28 08:37:45 +00:00
|
|
|
-X POST \
|
|
|
|
-H 'Content-Type: application/json' \
|
|
|
|
-H "Authorization: Bearer $COHERE_API_KEY" \
|
2024-05-23 11:28:56 +00:00
|
|
|
-d "$(_build_body cohere "$@")"
|
2024-04-28 08:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# @cmd List cohere models
|
|
|
|
# @env COHERE_API_KEY!
|
2024-04-28 22:51:03 +00:00
|
|
|
models-cohere() {
|
2024-04-30 04:52:58 +00:00
|
|
|
_wrapper curl https://api.cohere.ai/v1/models \
|
2024-04-28 08:37:45 +00:00
|
|
|
-H "Authorization: Bearer $COHERE_API_KEY" \
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
# @cmd Chat with ollama api
|
|
|
|
# @option -m --model=codegemma $OLLAMA_MODEL
|
|
|
|
# @flag -S --no-stream
|
|
|
|
# @arg text~
|
2024-04-28 22:51:03 +00:00
|
|
|
chat-ollama() {
|
2024-04-30 04:52:58 +00:00
|
|
|
_wrapper curl -i http://localhost:11434/api/chat \
|
2024-04-28 08:37:45 +00:00
|
|
|
-X POST \
|
|
|
|
-H 'Content-Type: application/json' \
|
2024-05-18 11:06:21 +00:00
|
|
|
-d "$(_build_body ollama "$@")"
|
2024-04-28 08:37:45 +00:00
|
|
|
}
|
|
|
|
|
2024-05-06 00:19:42 +00:00
|
|
|
# @cmd Chat with vertexai api
|
2024-04-28 08:37:45 +00:00
|
|
|
# @env require-tools gcloud
|
|
|
|
# @env VERTEXAI_PROJECT_ID!
|
|
|
|
# @env VERTEXAI_LOCATION!
|
|
|
|
# @option -m --model=gemini-1.0-pro $VERTEXAI_GEMINI_MODEL
|
|
|
|
# @flag -S --no-stream
|
|
|
|
# @arg text~
|
2024-05-06 00:19:42 +00:00
|
|
|
chat-vertexai() {
|
2024-04-28 08:37:45 +00:00
|
|
|
api_key="$(gcloud auth print-access-token)"
|
|
|
|
func="streamGenerateContent"
|
|
|
|
if [[ -n "$argc_no_stream" ]]; then
|
|
|
|
func="generateContent"
|
|
|
|
fi
|
|
|
|
url=https://$VERTEXAI_LOCATION-aiplatform.googleapis.com/v1/projects/$VERTEXAI_PROJECT_ID/locations/$VERTEXAI_LOCATION/publishers/google/models/$argc_model:$func
|
2024-04-30 04:52:58 +00:00
|
|
|
_wrapper curl -i $url \
|
2024-04-28 08:37:45 +00:00
|
|
|
-X POST \
|
|
|
|
-H "Authorization: Bearer $api_key" \
|
|
|
|
-H 'Content-Type: application/json' \
|
2024-05-18 11:06:21 +00:00
|
|
|
-d "$(_build_body vertexai "$@")"
|
2024-04-28 08:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# @cmd Chat with vertexai-claude api
|
|
|
|
# @env require-tools gcloud
|
|
|
|
# @env VERTEXAI_PROJECT_ID!
|
|
|
|
# @env VERTEXAI_LOCATION!
|
|
|
|
# @option -m --model=claude-3-haiku@20240307 $VERTEXAI_CLAUDE_MODEL
|
|
|
|
# @flag -S --no-stream
|
|
|
|
# @arg text~
|
2024-04-28 22:51:03 +00:00
|
|
|
chat-vertexai-claude() {
|
2024-04-28 08:37:45 +00:00
|
|
|
api_key="$(gcloud auth print-access-token)"
|
|
|
|
url=https://$VERTEXAI_LOCATION-aiplatform.googleapis.com/v1/projects/$VERTEXAI_PROJECT_ID/locations/$VERTEXAI_LOCATION/publishers/anthropic/models/$argc_model:streamRawPredict
|
2024-04-30 04:52:58 +00:00
|
|
|
_wrapper curl -i $url \
|
2024-04-28 08:37:45 +00:00
|
|
|
-X POST \
|
|
|
|
-H "Authorization: Bearer $api_key" \
|
|
|
|
-H 'Content-Type: application/json' \
|
2024-05-18 11:06:21 +00:00
|
|
|
-d "$(_build_body vertexai-claude "$@")"
|
2024-04-28 08:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# @cmd Chat with bedrock api
|
|
|
|
# @meta require-tools aws
|
|
|
|
# @option -m --model=mistral.mistral-7b-instruct-v0:2 $BEDROCK_MODEL
|
|
|
|
# @env AWS_REGION=us-east-1
|
2024-04-28 22:51:03 +00:00
|
|
|
chat-bedrock() {
|
2024-04-28 08:37:45 +00:00
|
|
|
file="$(mktemp)"
|
|
|
|
case "$argc_model" in
|
|
|
|
mistral.* | meta.*)
|
|
|
|
body='{"prompt":"'"$*"'"}'
|
|
|
|
;;
|
|
|
|
anthropic.*)
|
2024-05-18 11:06:21 +00:00
|
|
|
body="$(_build_body bedrock-claude "$@")"
|
2024-04-28 08:37:45 +00:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
_die "Invalid model: $argc_model"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
_wrapper aws bedrock-runtime invoke-model \
|
|
|
|
--model-id $argc_model \
|
|
|
|
--region $AWS_REGION \
|
|
|
|
--body "$(echo "$body" | base64)" \
|
|
|
|
"$file"
|
|
|
|
cat "$file"
|
|
|
|
}
|
|
|
|
|
2024-04-29 01:27:11 +00:00
|
|
|
# @cmd Chat with cloudflare api
|
|
|
|
# @env CLOUDFLARE_API_KEY!
|
|
|
|
# @option -m --model=@cf/meta/llama-3-8b-instruct $CLOUDFLARE_MODEL
|
|
|
|
# @flag -S --no-stream
|
|
|
|
# @arg text~
|
|
|
|
chat-cloudflare() {
|
|
|
|
url="https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/run/$argc_model"
|
2024-04-30 04:52:58 +00:00
|
|
|
_wrapper curl -i "$url" \
|
2024-04-29 01:27:11 +00:00
|
|
|
-X POST \
|
|
|
|
-H "Authorization: Bearer $CLOUDFLARE_API_KEY" \
|
2024-05-18 11:06:21 +00:00
|
|
|
-d "$(_build_body cloudflare "$@")"
|
2024-04-29 01:27:11 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 23:07:09 +00:00
|
|
|
# @cmd Chat with replicate api
|
|
|
|
# @env REPLICATE_API_KEY!
|
|
|
|
# @option -m --model=meta/meta-llama-3-8b-instruct $REPLICATE_MODEL
|
|
|
|
# @flag -S --no-stream
|
|
|
|
# @arg text~
|
|
|
|
chat-replicate() {
|
|
|
|
url="https://api.replicate.com/v1/models/$argc_model/predictions"
|
2024-04-30 04:52:58 +00:00
|
|
|
res="$(_wrapper curl -s "$url" \
|
2024-04-29 23:07:09 +00:00
|
|
|
-X POST \
|
|
|
|
-H "Authorization: Bearer $REPLICATE_API_KEY" \
|
|
|
|
-H "Content-Type: application/json" \
|
2024-05-18 11:06:21 +00:00
|
|
|
-d "$(_build_body replicate "$@")" \
|
|
|
|
)"
|
2024-04-29 23:07:09 +00:00
|
|
|
echo "$res"
|
|
|
|
if [[ -n "$argc_no_stream" ]]; then
|
|
|
|
prediction_url="$(echo "$res" | jq -r '.urls.get')"
|
|
|
|
while true; do
|
2024-04-30 04:52:58 +00:00
|
|
|
output="$(_wrapper curl -s -H "Authorization: Bearer $REPLICATE_API_KEY" "$prediction_url")"
|
2024-04-29 23:07:09 +00:00
|
|
|
prediction_status=$(printf "%s" "$output" | jq -r .status)
|
|
|
|
if [ "$prediction_status"=="succeeded" ]; then
|
|
|
|
echo "$output"
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
if [ "$prediction_status"=="failed" ]; then
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
sleep 2
|
|
|
|
done
|
|
|
|
else
|
|
|
|
stream_url="$(echo "$res" | jq -r '.urls.stream')"
|
2024-04-30 04:52:58 +00:00
|
|
|
_wrapper curl -i --no-buffer "$stream_url" \
|
2024-04-29 23:07:09 +00:00
|
|
|
-H "Accept: text/event-stream" \
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-04-28 08:37:45 +00:00
|
|
|
# @cmd Chat with ernie api
|
|
|
|
# @meta require-tools jq
|
|
|
|
# @env ERNIE_API_KEY!
|
|
|
|
# @option -m --model=ernie-tiny-8k $ERNIE_MODEL
|
|
|
|
# @flag -S --no-stream
|
|
|
|
# @arg text~
|
2024-04-28 22:51:03 +00:00
|
|
|
chat-ernie() {
|
2024-04-29 01:27:11 +00:00
|
|
|
auth_url="https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=$ERNIE_API_KEY&client_secret=$ERNIE_SECRET_KEY"
|
|
|
|
ACCESS_TOKEN="$(curl -fsSL "$auth_url" | jq -r '.access_token')"
|
|
|
|
url="https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/$argc_model?access_token=$ACCESS_TOKEN"
|
2024-04-30 04:52:58 +00:00
|
|
|
_wrapper curl -i "$url" \
|
2024-04-28 08:37:45 +00:00
|
|
|
-X POST \
|
2024-05-18 11:06:21 +00:00
|
|
|
-d "$(_build_body ernie "$@")"
|
2024-04-28 08:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# @cmd Chat with qianwen api
|
|
|
|
# @env QIANWEN_API_KEY!
|
|
|
|
# @option -m --model=qwen-turbo $QIANWEN_MODEL
|
|
|
|
# @flag -S --no-stream
|
|
|
|
# @arg text~
|
2024-04-28 22:51:03 +00:00
|
|
|
chat-qianwen() {
|
2024-04-28 08:37:45 +00:00
|
|
|
stream_args="-H X-DashScope-SSE:enable"
|
|
|
|
parameters_args='{"incremental_output": true}'
|
|
|
|
if [[ -n "$argc_no_stream" ]]; then
|
|
|
|
stream_args=""
|
|
|
|
parameters_args='{}'
|
|
|
|
fi
|
2024-04-29 01:27:11 +00:00
|
|
|
url=https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation
|
2024-04-30 04:52:58 +00:00
|
|
|
_wrapper curl -i "$url" \
|
2024-04-28 08:37:45 +00:00
|
|
|
-X POST \
|
|
|
|
-H "Authorization: Bearer $QIANWEN_API_KEY" \
|
|
|
|
-H 'Content-Type: application/json' $stream_args \
|
2024-05-18 11:06:21 +00:00
|
|
|
-d "$(_build_body qianwen "$@")"
|
2024-04-28 08:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_argc_before() {
|
|
|
|
stream="true"
|
|
|
|
if [[ -n "$argc_no_stream" ]]; then
|
|
|
|
stream="false"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
_openai_chat() {
|
|
|
|
api_base="${api_base:-"$argc_api_base"}"
|
|
|
|
api_key="${api_key:-"$argc_api_key"}"
|
|
|
|
_wrapper curl -i $curl_args "$api_base/chat/completions" \
|
|
|
|
-X POST \
|
|
|
|
-H "Content-Type: application/json" \
|
|
|
|
-H "Authorization: Bearer $api_key" \
|
2024-05-18 11:06:21 +00:00
|
|
|
-d "$(_build_body openai "$@")"
|
2024-04-28 08:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_openai_models() {
|
|
|
|
api_base="${api_base:-"$argc_api_base"}"
|
|
|
|
api_key="${api_key:-"$argc_api_key"}"
|
|
|
|
_wrapper curl $curl_args "$api_base/models" \
|
|
|
|
-H "Authorization: Bearer $api_key" \
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-04-30 04:52:58 +00:00
|
|
|
_choice_model() {
|
|
|
|
aichat --list-models
|
|
|
|
}
|
|
|
|
|
2024-04-30 05:15:13 +00:00
|
|
|
_choice_platform_model() {
|
|
|
|
_choice_platform
|
|
|
|
_choice_model
|
|
|
|
}
|
|
|
|
|
2024-04-30 04:52:58 +00:00
|
|
|
_choice_platform() {
|
|
|
|
_choice_client
|
|
|
|
_choice_openai_compatible_platform
|
|
|
|
}
|
|
|
|
|
2024-04-28 08:37:45 +00:00
|
|
|
_choice_client() {
|
2024-04-30 04:52:58 +00:00
|
|
|
printf "%s\n" openai gemini claude cohere ollama azure-openai vertexai bedrock cloudflare replicate ernie qianwen moonshot
|
|
|
|
}
|
|
|
|
|
|
|
|
_choice_openai_compatible_platform() {
|
2024-04-30 05:15:13 +00:00
|
|
|
for platform_config in "${OPENAI_COMPATIBLE_PLATFORMS[@]}"; do
|
|
|
|
echo "${platform_config%%,*}"
|
2024-04-30 04:52:58 +00:00
|
|
|
done
|
2024-04-28 08:37:45 +00:00
|
|
|
}
|
|
|
|
|
2024-05-18 11:06:21 +00:00
|
|
|
_build_body() {
|
|
|
|
kind="$1"
|
|
|
|
if [[ "$#" -eq 1 ]]; then
|
|
|
|
file="${BODY_FILE:-"tmp/body/$1.json"}"
|
|
|
|
if [[ -f "$file" ]]; then
|
|
|
|
cat "$file" | \
|
|
|
|
sed \
|
|
|
|
-e 's/"model": ".*"/"model": "'"$argc_model"'"/' \
|
|
|
|
-e 's/"stream": \(true\|false\)/"stream": '$stream'/' \
|
|
|
|
|
2024-04-28 08:37:45 +00:00
|
|
|
|
2024-05-18 11:06:21 +00:00
|
|
|
fi
|
2024-04-28 08:37:45 +00:00
|
|
|
else
|
2024-05-18 11:06:21 +00:00
|
|
|
shift
|
|
|
|
case "$kind" in
|
|
|
|
openai|ollama)
|
|
|
|
echo '{
|
|
|
|
"model": "'$argc_model'",
|
|
|
|
"messages": [
|
2024-04-28 08:37:45 +00:00
|
|
|
{
|
2024-05-18 11:06:21 +00:00
|
|
|
"role": "user",
|
|
|
|
"content": "'"$*"'"
|
2024-04-28 08:37:45 +00:00
|
|
|
}
|
2024-05-18 11:06:21 +00:00
|
|
|
],
|
|
|
|
"stream": '$stream'
|
2024-05-23 11:28:56 +00:00
|
|
|
}'
|
|
|
|
;;
|
|
|
|
cohere)
|
|
|
|
echo '{
|
|
|
|
"model": "'$argc_model'",
|
|
|
|
"message": "'"$*"'",
|
|
|
|
"stream": '$stream'
|
2024-05-18 11:06:21 +00:00
|
|
|
}'
|
|
|
|
;;
|
|
|
|
claude)
|
|
|
|
echo '{
|
|
|
|
"model": "'$argc_model'",
|
|
|
|
"messages": [
|
|
|
|
{
|
|
|
|
"role": "user",
|
|
|
|
"content": "'"$*"'"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"max_tokens": 4096,
|
|
|
|
"stream": '$stream'
|
|
|
|
}'
|
|
|
|
|
|
|
|
;;
|
|
|
|
vertexai-claude|bedrock-claude)
|
|
|
|
echo '{
|
|
|
|
"anthropic_version": "vertex-2023-10-16",
|
|
|
|
"messages": [
|
|
|
|
{
|
|
|
|
"role": "user",
|
|
|
|
"content": "'"$*"'"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"max_tokens": 4096,
|
|
|
|
"stream": '$stream'
|
|
|
|
}'
|
|
|
|
|
|
|
|
;;
|
|
|
|
gemini|vertexai)
|
|
|
|
echo '{
|
|
|
|
"contents": [{
|
|
|
|
"role": "user",
|
|
|
|
"parts": [
|
|
|
|
{
|
|
|
|
"text": "'"$*"'"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}],
|
|
|
|
"safetySettings":[{"category":"HARM_CATEGORY_HARASSMENT","threshold":"BLOCK_ONLY_HIGH"},{"category":"HARM_CATEGORY_HATE_SPEECH","threshold":"BLOCK_ONLY_HIGH"},{"category":"HARM_CATEGORY_SEXUALLY_EXPLICIT","threshold":"BLOCK_ONLY_HIGH"},{"category":"HARM_CATEGORY_DANGEROUS_CONTENT","threshold":"BLOCK_ONLY_HIGH"}]
|
|
|
|
}'
|
|
|
|
;;
|
|
|
|
ernie|cloudflare)
|
|
|
|
echo '{
|
|
|
|
"messages": [
|
|
|
|
{
|
|
|
|
"role": "user",
|
|
|
|
"content": "'"$*"'"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"stream": '$stream'
|
|
|
|
}'
|
|
|
|
;;
|
|
|
|
replicate)
|
|
|
|
echo '{
|
|
|
|
"stream": '$stream',
|
|
|
|
"input": {
|
|
|
|
"prompt": "'"$*"'"
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
;;
|
|
|
|
qianwen)
|
|
|
|
echo '{
|
|
|
|
"model": "'$argc_model'",
|
|
|
|
"parameters": '"$parameters_args"',
|
|
|
|
"input":{
|
|
|
|
"messages": [
|
|
|
|
{
|
|
|
|
"role": "user",
|
|
|
|
"content": "'"$*"'"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
_die "Unsupported build body for $kind"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2024-04-28 08:37:45 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
_wrapper() {
|
|
|
|
if [[ "$DRY_RUN" == "true" ]] || [[ "$DRY_RUN" == "1" ]]; then
|
|
|
|
echo "$@"
|
|
|
|
else
|
|
|
|
"$@"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
_die() {
|
|
|
|
echo $*
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
# See more details at https://github.com/sigoden/argc
|
|
|
|
eval "$(argc --argc-eval "$0" "$@")"
|