首頁 > 科技週邊 > 人工智慧 > Codestral API教程:開始使用Mistral的API

Codestral API教程:開始使用Mistral的API

尊渡假赌尊渡假赌尊渡假赌
發布: 2025-03-06 10:10:11
原創
648 人瀏覽過

codestral:代碼生成API

的綜合指南

Codestral是一種尖端的生成模型,在代碼生成任務中出色,例如填充中間(FIM)和代碼完成。 接受了80多種編程語言的培訓,它是使用常見語言和較少語言的開發人員的多功能工具。本教程詳細介紹瞭如何有效利用Codestral API。 有關Codestral的更廣泛概述,請參閱我有關“什麼是Mistral的Codestral”的文章。

>

API端點

codestral提供兩個主要的API端點:

  • codestral.mistral.ai:非常適合單個用戶和小型項目。 目前免費(直到2024年8月1日),它將過渡到訂閱型號。
  • api.mistral.ai:專為業務需求和大量使用而設計,提供了增加的速率限制和強大的支持。 Mistral推薦用於IDE插件或面向用戶的工具,允許用戶管理自己的API鍵。 由於其較高的速率限制和可擴展性,因此首選
  • >。 本教程側重於

> codestral.mistral.ai入門api.mistral.ai codestral.mistral.ai

獲得API鍵:

>

>註冊:>創建一個Mistral AI帳戶。

    獲取您的API鍵:
  1. ,導航到API鍵選項卡並生成新鍵。 對於
  2. >,請轉到Codestral選項卡(通常標記為“新”),請完成註冊(注意:通常需要電話號碼),一旦批准,請訪問您的密鑰。 >
  3. api.mistral.ai codestral.mistral.ai

Codestral API Tutorial: Getting Started With Mistral’s API 身份驗證(python):

我們將使用Codestral API Tutorial: Getting Started With Mistral’s API 庫來為兩個端點創建身份驗證函數:>

了解端點

>填充 - 中間點(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
  • >示例:
  • promptsuffix stop
指令端點:

使用指示指導代碼生成。

  • url: https://codestral.mistral.ai/v1/chat/completions
  • >參數:prompttemperature(可選),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 API Tutorial: Getting Started With Mistral’s API

高級用法

  • 速率限制: 的限制為每分鐘30個請求,每天2000個請求; codestral.mistral.ai>每個工作空間每秒有200個請求。使用Python的api.mistral.ai庫來實現重試邏輯來處理速率限制。 > 使用適當的錯誤代碼處理常見錯誤(401,429,500),time
  • 錯誤處理:
  • 處理常見錯誤(401,429,500)。 重試邏輯對瞬態錯誤是有益的。
  • >自定義輸出:
  • 調整參數,例如>和>以微調生成的代碼。 > prompt temperature Integration
codestral通過continy.dev等插件與IDE(VS代碼,Jetbrains)集成。 您還可以創建自定義腳本。 這是生成測試功能的示例:

>

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 Tutorial: Getting Started With Mistral’s API

清除提示:
    使用精確和明確的提示以獲得最佳結果。 >
  • 迭代精緻:>實驗並完善您的提示以生成更好的代碼。
  • 負責使用:在法律上和法律上使用API​​,避免了惡意代碼生成。 >
  • 結論 本指南提供了對Codestral API的實用介紹。 實驗並將其集成到您的工作流程中,以增強您的開發過程。 有關Mistral的更多信息,請探討Mistral 7B教程和使用Mistral大型模型的指南。

以上是Codestral API教程:開始使用Mistral的API的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板