预测输出
预测输出通过利用已知或可预测的内容来优化响应时间。这种方法在保持高质量输出的同时最大限度地减少了延迟。在编辑大量文本、修改代码或生成基于模板的响应等任务中,输出的很大一部分通常是预先确定的。通过使用预测输出预定义这些预期部分,模型可以将更多的计算资源分配给不可预测的元素,从而提高整体效率。
示例:代码修改
预测输出在需要对文本文档或代码文件进行少量修改并重新生成的情况下表现出色。引入的关键参数是 prediction
参数,它允许用户定义预测输出。例如,想象您希望模型更新微调任务中使用的模型。您可以将要修改的代码片段作为用户提示和预测输出包含进去。
- python
- typescript
- curl
import os
from mistralai import Mistral
api_key = os.environ["MISTRAL_API_KEY"]
model = "mistral-large-latest"
client = Mistral(api_key=api_key)
code = """
created_jobs = client.fine_tuning.jobs.create(
model="open-mistral-7b",
training_files=[{"file_id": ultrachat_chunk_train.id, "weight": 1}],
validation_files=[ultrachat_chunk_eval.id],
hyperparameters={
"training_steps": 10,
"learning_rate":0.0001
},
auto_start=False
)
"""
prompt = "Change the model name from open-mistral-7b to open-mistral-nemo. Respond only with code, no explanation, no formatting."
chat_response = client.chat.complete(
model= model,
messages = [
{
"role": "user",
"content": prompt,
},
{
"role": "user",
"content": code
},
],
prediction = {
"type": "content",
"content": code
}
)
print(chat_response.choices[0].message.content)
import { Mistral } from '@mistralai/mistralai';
const apiKey = process.env.MISTRAL_API_KEY;
const client = new Mistral({apiKey: apiKey});
const code = `
created_jobs = client.fine_tuning.jobs.create(
model="open-mistral-7b",
training_files=[{"file_id": ultrachat_chunk_train.id, "weight": 1}],
validation_files=[ultrachat_chunk_eval.id],
hyperparameters={
"training_steps": 10,
"learning_rate":0.0001
},
auto_start=False
)
`.trim();
const prompt = `Change the model name from open-mistral-7b to open-mistral-nemo. Respond only with code, no explanation, no formatting.`;
const chatResponse = await client.chat.complete({
model: "mistral-large-latest",
messages: [
{
role: 'user',
content: prompt
},
{
role: "user",
content: code
},
],
prediction: {
type: "content",
content: code
},
});
console.log('Chat:', chatResponse.choices[0].message.content);
curl --location "https://api.mistral.ai/v1/chat/completions" \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header "Authorization: Bearer $MISTRAL_API_KEY" \
--data '{
"model": "mistral-large-latest",
"messages": [
{"role": "user", "content": "Change the model name from open-mistral-7b to open-mistral-nemo. Respond only with code, no explanation, no formatting."},
{"role": "user", "content": "$CODE"}
],
"prediction": {
"type": "content",
"content": "$CODE"
}
}'
常见问题
哪些模型支持预测输出?
截至目前,codestral-2501
和 mistral-large-2411
支持预测输出。
预测输出如何影响定价?
目前,预测输出不影响定价。
使用预测输出时不支持哪些参数?
使用预测输出时不支持 n
(每个请求返回的完成次数)参数。
预测中某些句子或词语的位置重要吗?
不,句子或词语在预测中的位置不影响其有效性。预测可以出现在生成响应中的任何位置,并且仍然有助于减少 API 的输出延迟。