In any case, you should check the official document, this note may be outdated in the future.
This note is only for working with APIs from AI Services. If you use the AI services as their tools and chat apps, check this note instead.
- Mistral and Claude don’t accept
nullin the request body. OpenAI allows that (not withAzureOpenAI, I tried withstream_options). In OpenAI request, if you inputnull, the default value for that property will be used. However, there will be an error if you putnullin the request body of Mistral.
- Only OpenAI has
userandnparameters.
No services give a very clear discription about the max tokens. You have to read the documentation carefully.
- Claude gives the max output to 4096 tokens to all models.
- Mistral (they write 32k, 8k,…)
- OpenAI sometimes give 4096 tokens for the output.
- AI SDK for Azure provider, it’s a little special.
- Mistral and Claude don’t accept
nullin the request body. OpenAI allows that (not withAzureOpenAI, I tried withstream_options). In OpenAI request, if you inputnull, the default value for that property will be used. However, there will be an error if you putnullin the request body of Mistral.
- Prompt caching (auto enabled) → best practices
There are some remarks (tried with version
1.40.8 and below):- It doesn’t accept
null(when you use REST API) orNone(when using Python SDK) with some specific endpoints (whereas some don’t have this kind of issue). I tried withstream_options. However, the classOpenAI(also from that package) allows that!
- Even if we add
stream_options=Truein the request, there is no usage return as inOpenAIclass!
- Note that, the changes in Azure OpenAI often come after the official OpenAI’s changes. For example, property
max_completion_tokensreplacesmax_tokensbut it isn’t supported yet in Azure OpenAI.
- Although OpenAI says that
max_completion_tokensis a new way of the dedicatedmax_tokensbut they aren’t the same in my tests. For example, if we setmax_tokens=200for modelgpt-4oandmax_completion_tokens=200for modelo1-mini. The latter will stops without content and finish_reson is “length” (it seems it’s too short to show) when I ask “hello”. The former is good to show the answer.
- ⚠️ Be careful: previous messages can be input to the
inputproperty, but it differs from themessagesproperty in older APIs. For example, tools in the message list must be handled differently. Check more here.
- Response format + no stream
- Response format + stream (doc)
- Mistral and Claude don’t accept
nullin the request body. OpenAI allows that (not withAzureOpenAI, I tried withstream_options). In OpenAI request, if you inputnull, the default value for that property will be used. However, there will be an error if you putnullin the request body of Mistral.
- Remark: if you use rest api, i’ts
tool_callsin the returned object where it’stoolCallsif you use sdk. The same for others likefinish_reasonorprompt_tokens.
- Differences with streaming enabled between sdk and rest api:
- Be carefule, if you use Mistral REST API, the returned property is
tool_callsbut if you use Mistral SDK API (@mistralai/mistralai), the returned property istoolCallsinstead. If you usetool_callsin SDK, there will be an error!
- Mistral and Claude don’t accept
nullin the request body. OpenAI allows that (not withAzureOpenAI, I tried withstream_options). In OpenAI request, if you inputnull, the default value for that property will be used. However, there will be an error if you putnullin the request body of Mistral.
max_tokensis required!
- It has a very different format in comparison with OpenAI and Mistral (both in input and output).
- Claude doesn’t have an endpoint for getting the list of models like OpenAI/Mistral.
- If you use REST API, you have to indicate version of the API, if you use SDK, you don’t have to.
- First message must use the “user” role. OpenAI/Mistral don’t restrict this rule.
- There are 2 ways to provide the text
- If you use Python SDK, python v3.9 is required!
- Get the API key in Google AI Studio.
- Sau khi có API key rồi thì check Quickstart.
- List of value for safety configs.
- System instruction (prompt)
- Different from other services, the role in the content message is either “user” or “model” (not “assistant”)
- Different from other services, Gemini doesn’t allow the name of a tool starting with special characters like
-.
- “system_instruction” isn’t enabled for model
gemini-1.0-pro, if you use it, there will be an errorDeveloper instruction is not enabled for models/gemini-1.0-pro". In this case, you can use"role": "model"for the instruction.
- Python SDK example doc:
generation_config
- When using embeddings, note that the model name in the API request must start with
models/ortunedModels/. For instance, while the documentation refers totext-embedding-004, you must usemodels/text-embedding-004in your actual request.
models/text-embedding-004has a fixed output dimensionality of 768 - this is hardcoded into the model's architecture and cannot be changed. Theoutput_dimensionalityparameter in your config is simply ignored by this model.
Điều này có thể dẫn đến lỗi nếu như column chỉ chấp nhận 1 chiều nào đó ko khớp. Cách check dimension:
Most services provide their own SDK to get the streaming response. These SDKs work out of the box. However, there are cases where we cannot use the SDKs (for example, I encountered the error
ERR_REQUIRE_ESM when working with the Mistral SDK). In such cases, it's better to work directly with their REST APIs.Check this code.
Check this code.
Remark: The chunk depends heavily and “strangely” on the
chunk_size. Even if we set chunk_size=None to get the full chunk, the return chunk is shown belowIf we count the
data : found in each chunk, it will be 3, 1, 1, …, 1, 2. I still don’t know why!