Mistral Swift 客户端

使用 Mistral Swift 客户端与 Mistral AI API 交互。

用法

命令行演示

$ swift run swift-mistral --api-key "<API_KEY>" --input "Hello, how are you?"

命令行帮助

$ swift run swift-mistral --help

在您自己的代码中

import SwiftMistral

let client = try MistralClient(apiKey: "<API_KEY>")
let chatCompletion = try await client.createChatCompletion(
    model: "<MODEL_ID>", 
    messages: [.init(role: .user, content: "<YOUR_MESSAGE>")]
)
for try await chat in chatCompletion {
    for choice in chat.choices {
        if let delta = choice.delta, let content = delta.content {
            // Do something with the response
        }
    }
}

格式化代码

$ swift package plugin --allow-writing-to-package-directory swiftformat

测试

$ swift test