代码生成
Codestral
Codestral 是一款尖端的生成模型,专为代码生成任务(包括填充中间和代码补全)设计和优化。Codestral 在超过 80 种编程语言上进行了训练,使其在常见和不太常见的语言上都能表现良好。
我们目前为 Codestral 端点提供两个域,两者均提供 FIM 和指令路由。
域 | 功能特性 |
---|---|
codestral.mistral.ai | - 基于月度订阅,目前可免费使用 - 需要新密钥,为此需要提供电话号码 |
api.mistral.ai | - 允许使用现有 API 密钥并可付费使用 Codestral - 适合商业用途 |
想知道使用哪个端点?
- 如果您是用户,希望将 Codestral 作为 IDE 插件的一部分进行查询,建议使用 codestral.mistral.ai。
- 如果您正在构建插件或任何直接将这些端点暴露给用户,并期望用户自带 API 密钥的东西,您也应该将目标设置为 codestral.mistral.ai。
- 对于所有其他用例,api.mistral.ai 更适合。
本指南使用 api.mistral.ai 进行演示。
本指南将引导您了解如何使用 Codestral 填充中间端点、指令端点、开源 Codestral 模型以及几个社区集成。
- 填充中间端点
- 指令端点
- 开源 Codestral
- 集成
填充中间端点
通过此功能,用户可以使用 prompt
定义代码的起始点,并使用可选的 suffix
和可选的 stop
定义代码的结束点。Codestral 模型随后将生成介于两者之间的代码,这使其成为需要生成特定代码片段的任务的理想选择。下面是三个示例。
示例 1:填充中间
- python
- curl
import os
from mistralai import Mistral
api_key = os.environ["MISTRAL_API_KEY"]
client = Mistral(api_key=api_key)
model = "codestral-latest"
prompt = "def fibonacci(n: int):"
suffix = "n = int(input('Enter a number: '))\nprint(fibonacci(n))"
response = client.fim.complete(
model=model,
prompt=prompt,
suffix=suffix,
temperature=0,
top_p=1,
)
print(
f"""
{prompt}
{response.choices[0].message.content}
{suffix}
"""
)
curl --location 'https://api.mistral.ai/v1/fim/completions' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header "Authorization: Bearer $MISTRAL_API_KEY" \
--data '{
"model": "codestral-latest",
"prompt": "def f(",
"suffix": "return a + b",
"max_tokens": 64,
"temperature": 0
}'
示例 2:补全
- python
- curl
import os
from mistralai import Mistral
api_key = os.environ["MISTRAL_API_KEY"]
client = Mistral(api_key=api_key)
model = "codestral-latest"
prompt = "def is_odd(n): \n return n % 2 == 1 \ndef test_is_odd():"
response = client.fim.complete(model=model, prompt=prompt, temperature=0, top_p=1)
print(
f"""
{prompt}
{response.choices[0].message.content}
"""
)
curl --location 'https://api.mistral.ai/v1/fim/completions' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header "Authorization: Bearer $MISTRAL_API_KEY" \
--data '{
"model": "codestral-latest",
"prompt": "def is_odd(n): \n return n % 2 == 1 \n def test_is_odd():",
"suffix": "",
"max_tokens": 64,
"temperature": 0
}'
示例 3:停止标记
我们建议为 IDE 自动补全集成添加停止标记,以防止模型过于冗长。
- python
- curl
import os
from mistralai import Mistral
api_key = os.environ["MISTRAL_API_KEY"]
client = Mistral(api_key=api_key)
model = "codestral-latest"
prompt = "def is_odd(n): \n return n % 2 == 1 \ndef test_is_odd():"
suffix = "n = int(input('Enter a number: '))\nprint(fibonacci(n))"
response = client.fim.complete(
model=model, prompt=prompt, suffix=suffix, temperature=0, top_p=1, stop=["\n\n"]
)
print(
f"""
{prompt}
{response.choices[0].message.content}
"""
)
curl --location 'https://api.mistral.ai/v1/fim/completions' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header "Authorization: Bearer $MISTRAL_API_KEY" \
--data '{
"model": "codestral-latest",
"prompt": "def is_odd(n): \n return n % 2 == 1 \n def test_is_odd():",
"suffix": "test_is_odd()",
"stop": ["\n\n"],
"max_tokens": 64,
"temperature": 0
}'
指令端点
我们还提供了相同模型 codestral-latest
的 Codestral 指令端点。唯一的区别是使用的端点不同。
- python
- curl
import os
from mistralai import Mistral
api_key = os.environ["MISTRAL_API_KEY"]
client = Mistral(api_key=api_key)
model = "codestral-latest"
message = [{"role": "user", "content": "Write a function for fibonacci"}]
chat_response = client.chat.complete(
model = model,
messages = message
)
print(chat_response.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": "codestral-latest",
"messages": [{"role": "user", "content": "Write a function for fibonacci"}]
}'
Codestral Mamba
我们还发布了 Codestral Mamba 7B,这是一款 Mamba2 语言模型,专门用于代码生成,带有指令端点。
- python
- curl
import os
from mistralai import Mistral
api_key = os.environ["MISTRAL_API_KEY"]
client = Mistral(api_key=api_key)
model = "codestral-mamba-latest"
message = [
{
"role": "user",
"content": "Write a function for fibonacci"
}
]
chat_response = client.chat.complete(
model=model,
messages=message
)
print(chat_response.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": "codestral-mamba-latest",
"messages": [{"role": "user", "content": "Write a function for fibonacci"}]
}'
开源 Codestral 和 Codestral Mamba
Codestral 在 Mistral AI 非生产 (MNPL) 许可下开源可用,Codestral Mamba 在 Apache 2.0 许可下开源可用。
查看 mistral-inference 的 README,了解如何使用 mistral-inference
运行 Codestral。
与 continue.dev 集成
Continue.dev 支持 Codestral base 用于代码生成,支持 Codestral Instruct 用于聊天。
如何使用 Continue 设置 Codestral
以下是使用 Mistral AI API 将 Codestral 与 Continue 集成的分步指南。
-
按照此处的说明安装 Continue VS Code 或 JetBrains 扩展。请确保您安装的是 v0.8.33 以上版本的 Continue。
-
自动设置
- 点击左侧菜单上的 Continue 扩展图标。选择
Mistral API
作为提供商,选择Codestral
作为模型。 - 点击“获取 API 密钥”以获取 Codestral API 密钥。
- 点击“添加模型”,config.json 将自动填充。

- (备选)手动编辑 config.json
- 点击 Continue 窗口右下角的齿轮图标,打开
~/.continue/config.json
(MacOS) /%userprofile%\.continue\config.json
(Windows) - 在 Mistral AI 的 La Plateforme 此处登录并请求 Codestral API 密钥。
- 要将 Codestral 用作
autocomplete
和chat
的模型,请将下面的[API_KEY]
替换为您的 Mistral API 密钥,并将其添加到您的config.json
文件中。
{
"models": [
{
"title": "Codestral",
"provider": "mistral",
"model": "codestral-latest",
"apiKey": "[API_KEY]"
}
],
"tabAutocompleteModel": {
"title": "Codestral",
"provider": "mistral",
"model": "codestral-latest",
"apiKey": "[API_KEY]"
}
}
如果您遇到任何问题或有任何疑问,请加入我们的 Discord 并在 #help
频道中提问 此处。
与 Tabnine 集成
Tabnine 支持 Codestral Instruct 用于聊天。
如何使用 Tabnine 设置 Codestral
什么是 Tabnine Chat?
Tabnine Chat 是一款以代码为中心的聊天应用程序,可在 IDE 中运行,允许开发者使用自然语言以灵活、自由的形式与 Tabnine 的 AI 模型互动。Tabnine Chat 还支持使用针对特定用例优化的预定义提示的专用快速操作。
入门
要开始使用 Tabnine Chat,首先在您的 IDE(VSCode、JetBrains 或 Eclipse)中启动它。然后,学习如何与 Tabnine Chat 互动,例如如何提问或给出指令。收到回复后,您可以在代码中阅读、审查和应用它。
将 Codestral 选为 Tabnine Chat 应用模型
在 Tabnine Chat 应用中,使用模型选择器选择 Codestral。
与 LangChain 集成
LangChain 支持 Codestral Instruct。以下是您如何在 LangChain 中使用它。
# make sure to install `langchain` and `langchain-mistralai` in your Python environment
import os
from langchain_mistralai import ChatMistralAI
from langchain_core.prompts import ChatPromptTemplate
api_key = os.environ["MISTRAL_API_KEY"]
mistral_model = "codestral-latest"
llm = ChatMistralAI(model=mistral_model, temperature=0, api_key=api_key)
llm.invoke([("user", "Write a function for fibonacci")])
对于使用指令型 Codestral 工具进行自我纠正代码生成的更复杂用例,请查看此notebook和此视频。
与 LlamaIndex 集成
LlamaIndex 支持 Codestral Instruct 和 Fill In Middle (FIM) 端点。以下是您如何在 LlamaIndex 中使用它。
# make sure to install `llama-index` and `llama-index-llms-mistralai` in your Python enviornment
import os
from llama_index.core.llms import ChatMessage
from llama_index.llms.mistralai import MistralAI
api_key = os.environ["MISTRAL_API_KEY"]
mistral_model = "codestral-latest"
messages = [
ChatMessage(role="user", content="Write a function for fibonacci"),
]
MistralAI(api_key=api_key, model=mistral_model).chat(messages)
在此notebook中查看更多关于使用 Instruct 和 Fill In Middle (FIM) 与 LlamaIndex 的详细信息。
与 Jupyter AI 集成
Jupyter AI 将 Codestral 无缝集成到 JupyterLab 中,为用户在 Jupyter 生态系统中提供简化的、增强的 AI 辅助编码体验。此集成提高了生产力并优化了用户与 Jupyter 的整体交互。
要开始在 JupyterLab 中使用 Codestral 和 Jupyter AI,首先在您的 Python 环境中安装所需的软件包。
pip install jupyterlab langchain-mistralai jupyter-ai pandas matplotlib
然后启动 Jupyter Lab。
jupyter lab
之后,您可以选择 Codestral 作为您的首选模型,输入您的 Mistral API 密钥,然后开始使用 Codestral 进行编码!
与 JupyterLite 集成
JupyterLite 是一个旨在将 JupyterLab 环境带到网络浏览器中的项目,允许用户直接在浏览器中运行 Jupyter,无需本地安装。
您可以在浏览器中试用 Codestral 与 JupyterLite:
与 CodeGPT 集成
CodeGPT 是一款强大的无关性扩展,利用大型语言模型 (LLM) 的能力,通过 VSCode 中的 AI 提升您的编程任务。您可以在 CodeGPT 中选择 Codestral 进行代码生成和 Tab 补全。
与 Tabby 集成
Tabby 是一款开源 AI 编码助手。您可以通过 Tabby 使用 Codestral 进行代码补全和聊天。
要在 Tabby 中使用 Codestral,请按如下方式在 ~/.tabby/config.toml
中配置模型。
[model.completion.http]
kind = "mistral/completion"
api_endpoint = "https://api.mistral.ai"
api_key = "secret-api-key"
您可以查看 Tabby 的文档了解更多信息。
与 E2B 集成
E2B 为 AI 生成的代码执行提供了开源的安全沙箱。使用 E2B,开发者可以轻松地为使用 Codestral 的 AI 应用添加代码解释能力。
在以下示例中,AI agent 对上传的 CSV 文件执行数据分析任务,在 E2B 提供的沙箱环境中执行 Codestral 生成的 AI 代码,并返回图表,将其保存为 PNG 文件。
Python 实现 (cookbook)
JS 实现 (cookbook)