Chat recipes
Read a reply as it arrives, look up an order, or get data your app can use.
Set up your example
Get a Meikai API token and set MEIKAI_AI_KEY in your terminal. Use List models to find a text model your token can access. Model capabilities shows which features we tested.
Set MEIKAI_AI_MODEL to the model ID you chose. For example, if your list includes Llama 3.3 70B, run this command.
export MEIKAI_AI_MODEL="meta-llama/Llama-3.3-70B-Instruct"Choose a language below. Copy its setup into a file, add one recipe from this page, then run the file.
# Save as recipe.sh and run with bash recipe.sh. Requires curl and jq.
set -euo pipefail
chat() {
jq --arg model "$MEIKAI_AI_MODEL" '. + {model: $model, max_tokens: 2048}' |
curl --silent --show-error --fail-with-body --no-buffer \
https://ai.meikai.net/v1/chat/completions \
-H "Authorization: Bearer $MEIKAI_AI_KEY" \
-H 'Content-Type: application/json' --data-binary @-
}Stream text and token usage
Streaming sends the reply in pieces. To also get a count of the tokens the model read and produced, turn on stream_options.include_usage. Read usage even when choices is empty. The counts can arrive in their own event.
curl shows each event as a data: line. Read new text from choices[0].delta.content. For this recipe, a finish_reason of stop means the answer is complete. The stream ends with data: [DONE].
The Python and TypeScript examples print the text as it arrives, then print the counts if the model sent them. If the stream fails, treat any text you already received as a partial reply.
chat <<'JSON'
{
"messages": [{"role": "user", "content": "Why does bread rise?"}],
"stream": true,
"stream_options": {"include_usage": true}
}
JSONRun one function call
A tool call lets a model ask your app to run a function. In this example, the user asks whether order A123 has shipped. We tested both requests with meta-llama/Llama-3.3-70B-Instruct.
- Send the question and describe the function the model can request.
- Check the function name and arguments. Look up the order in the sample data.
- Send the model its original tool call and your result. Copy the call’s ID into
tool_call_idso the model can match the result to its request.
The final reply should tell you that order A123 has shipped.
request='{
"messages": [{"role": "user", "content": "Has order A123 shipped?"}],
"tools": [{"type": "function", "function": {
"name": "get_order_status",
"description": "Look up the status of an order by ID.",
"parameters": {"type": "object", "properties": {"order_id": {"type": "string"}},
"required": ["order_id"], "additionalProperties": false}
}}],
"tool_choice": {"type": "function", "function": {"name": "get_order_status"}}
}'
message=$(chat <<< "$request" | jq -ec '.choices[0].message')
call=$(jq -ec '.tool_calls | select(length == 1) | .[0]
| select(.type == "function" and .function.name == "get_order_status")
| select(.id | type == "string" and length > 0)' <<< "$message")
order_id=$(jq -er '.function.arguments | fromjson | select(keys == ["order_id"])
| .order_id | select(type == "string" and test("\\S"))' <<< "$call")
status=unknown
if [[ "$order_id" == A123 ]]; then status=shipped; fi
result=$(jq -nc --arg id "$order_id" --arg status "$status" '{order_id: $id, status: $status}')
jq --argjson message "$message" --argjson call "$call" --arg result "$result" '
.messages += [$message, {role: "tool", tool_call_id: $call.id, content: $result}]
| .tool_choice = "none"' <<< "$request" |
chat | jq -er '.choices[0] | select(.finish_reason == "stop")
| .message.content | select(type == "string" and length > 0)'Return JSON that follows a schema
A JSON schema describes which fields and values your app expects. This example asks the model to label a review. The reply must contain one field, sentiment, with a value of positive, neutral, or negative.
Choose a model that passed the schema check. Set response_format.type to json_schema and enable strict. Check that the model finished and returned an answer before you read the label.
reply=$(chat <<'JSON'
{
"messages": [{"role": "user", "content": "Classify this review: The package arrived early."}],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "review_sentiment",
"strict": true,
"schema": {
"type": "object", "required": ["sentiment"], "additionalProperties": false,
"properties": {"sentiment": {"type": "string", "enum": ["positive", "neutral", "negative"]}}
}
}
}
}
JSON
)
jq -er '
.choices[0] | select(.finish_reason == "stop" and (.message.refusal | not))
| .message.content | fromjson
| select(keys == ["sentiment"]) | .sentiment
| select(. == "positive" or . == "neutral" or . == "negative")
' <<< "$reply"See Chat completions for the full list of request fields.