codestral:代码生成API
的综合指南Codestral是一种尖端的生成模型,在代码生成任务中出色,例如填充中间(FIM)和代码完成。 接受了80多种编程语言的培训,它是使用常见语言和较少语言的开发人员的多功能工具。本教程详细介绍了如何有效利用Codestral API。 有关Codestral的更广泛概述,请参阅我有关“什么是Mistral的Codestral”的文章。
>API端点
codestral提供两个主要的API端点:
codestral.mistral.ai
:非常适合单个用户和小型项目。 目前免费(直到2024年8月1日),它将过渡到订阅型号。
api.mistral.ai
:专为业务需求和大量使用而设计,提供了增加的速率限制和强大的支持。>
codestral.mistral.ai
入门api.mistral.ai
codestral.mistral.ai
>
>注册:>创建一个Mistral AI帐户。
api.mistral.ai
codestral.mistral.ai
身份验证(python):
我们将使用库来为两个端点创建身份验证函数:
了解端点
>填充 - 中间点(FIM)端点:
requests
生成代码以填补
import requests import json api_key = 'INSERT YOUR API KEY HERE' def call_chat_endpoint(data, api_key=api_key): url = "https://codestral.mistral.ai/v1/chat/completions" #Corrected URL headers = { "Authorization": f"Bearer {api_key}", "Content-Type": "application/json", "Accept": "application/json" } response = requests.post(url, headers=headers, data=json.dumps(data)) return response.json() if response.status_code == 200 else f"Error: {response.status_code}, {response.text}" def call_fim_endpoint(data, api_key=api_key): url = "https://codestral.mistral.ai/v1/fim/completions" #Corrected URL headers = { "Authorization": f"Bearer {api_key}", "Content-Type": "application/json", "Accept": "application/json" } response = requests.post(url, headers=headers, data=json.dumps(data)) return response.json() if response.status_code == 200 else f"Error: {response.status_code}, {response.text}"。
url:
prompt
>参数:suffix
https://codestral.mistral.ai/v1/fim/completions
prompt
suffix
stop
使用指示指导代码生成。
https://codestral.mistral.ai/v1/chat/completions
prompt
,temperature
(可选),max_tokens
(可选)>示例:
import requests import json api_key = 'INSERT YOUR API KEY HERE' def call_chat_endpoint(data, api_key=api_key): url = "https://codestral.mistral.ai/v1/chat/completions" #Corrected URL headers = { "Authorization": f"Bearer {api_key}", "Content-Type": "application/json", "Accept": "application/json" } response = requests.post(url, headers=headers, data=json.dumps(data)) return response.json() if response.status_code == 200 else f"Error: {response.status_code}, {response.text}" def call_fim_endpoint(data, api_key=api_key): url = "https://codestral.mistral.ai/v1/fim/completions" #Corrected URL headers = { "Authorization": f"Bearer {api_key}", "Content-Type": "application/json", "Accept": "application/json" } response = requests.post(url, headers=headers, data=json.dumps(data)) return response.json() if response.status_code == 200 else f"Error: {response.status_code}, {response.text}"
高级用法
codestral.mistral.ai
>每个工作空间每秒有200个请求。使用Python的api.mistral.ai
库来实现重试逻辑来处理速率限制。time
prompt
temperature
Integration >
prompt = "def fibonacci(n: int):" suffix = "n = int(input('Enter a number: '))\nprint(fibonacci(n))" data = {"model": "codestral-latest", "prompt": prompt, "suffix": suffix, "temperature": 0} response = call_fim_endpoint(data)
最佳实践
以上是Codestral API教程:开始使用Mistral的API的详细内容。更多信息请关注PHP中文网其他相关文章!