DeepSeekSwift
🚨 由于当前服务器资源受限,DeepSeek 暂时停止 API 服务充值,以防止对用户操作产生任何潜在影响。现有余额仍可用于调用。
DeepSeek Swift SDK 是一个轻量级且高效的 Swift 客户端,用于与 DeepSeek API 交互。它为聊天消息补全、流式传输、错误处理以及使用高级参数配置 DeepSeek LLM 提供了支持。
要将 DeepSwiftSeek
集成到你的项目中,你可以使用 Swift Package Manager (SPM)
let package = Package(
dependencies: [
.package(url: "https://github.com/tornikegomareli/DeepSwiftSeek.git", exact: "0.0.2")
]
)
或通过 Xcode 添加
import DeepSwiftSeek
let configuration = Configuration(apiKey: "YOUR_API_KEY")
let deepSeekClient = DeepSeekClient(configuration: configuration)
Task {
do {
let response = try await deepSeekClient.chatCompletions(
messages: {
ChatMessageRequest(role: .user, content: "Tell me a joke.", name: "User")
},
model: .deepSeekChat,
parameters: .creative
)
print(response.choices.first?.message.content ?? "No response")
} catch {
print("Error: \(error.localizedDescription)")
}
}
Task {
do {
let stream = try await deepSeekClient.chatCompletionStream(
messages: {
ChatMessageRequest(role: .user, content: "Write a poem.", name: "User")
},
model: .deepSeekChat,
parameters: .streaming
)
for try await chunk in stream {
print(chunk) // Prints streamed responses
}
} catch {
print("Streaming error: \(error.localizedDescription)")
}
}
Task {
do {
let stream = try await deepSeekClient.fimCompletionStream(
messages: {
[
ChatMessageRequest(
role: .user,
content: "function greet() {\n /* FIM_START */\n /* FIM_END */\n return 'Hello world';\n}",
name: "User"
)
]
},
model: .deepSeekReasoner,
parameters: .streaming
)
for try await chunk in stream {
// Each chunk is a streamed part of the fill-in-the-middle response.
print("FIM Stream Chunk:\n\(chunk)")
}
} catch {
print("FIM Streaming Error: \(error.localizedDescription)")
}
}
Task {
do {
let response = try await deepSeekClient.fimCompletions(
messages: {
[
ChatMessageRequest(
role: .user,
content: "function greet() {\n // FIM_START\n // FIM_END\n return 'Hello world';\n}",
name: "User"
)
]
},
model: .deepSeekReasoner,
parameters: .creative
)
if let content = response.choices.first?.message.content {
print("FIM Completion:\n\(content)")
}
} catch {
print("FIM Error: \(error.localizedDescription)")
}
}
Task {
do {
let response = try await deepSeekClient.listModels()
} catch {
print("ListModels Error: \(error.localizedDescription)")
}
}
Task {
do {
let response = try await deepSeekClient.fetchUserBalance()
} catch {
print("UserBalance Error: \(error.localizedDescription)")
}
}
SDK 提供了详细的错误处理
catch let error as DeepSeekError {
print("DeepSeek API Error: \(error.localizedDescription)")
print("Recovery Suggestion: \(error.recoverySuggestion ?? "None")")
} catch {
print("Unexpected error: \(error)")
}
DeepSeek SDK 支持多种模型
public enum DeepSeekModel: String {
case deepSeekChat = "deepseek-chat"
case deepSeekReasoner = "deepseek-reasoner"
}
你可以配置聊天补全参数
let parameters = ChatParameters(
frequencyPenalty: 0.5,
maxTokens: 512,
presencePenalty: 0.5,
temperature: 0.7,
topP: 0.9
)
模式 | 温度 | 最大 Tokens | Top P |
---|---|---|---|
创造性 | 0.9 | 2048 | 0.9 |
专注性 | 0.3 | 2048 | 0.3 |
流式传输 | 0.7 | 4096 | 0.9 |
代码生成 | 0.2 | 2048 | 0.95 |
简洁性 | 0.5 | 256 | 0.5 |
如果你需要特定的配置,你可以定义自己的参数预设
extension ChatParameters {
static let myCustomPreset = ChatParameters(
frequencyPenalty: 0.4,
maxTokens: 1024,
presencePenalty: 0.6,
temperature: 0.8,
topP: 0.85
)
}
然后在你的请求中使用它
let parameters = ChatParameters.myCustomPreset
这种方法允许你维护可重用的配置,以适应不同的需求。
DeepSeek SDK 内置了针对各种 API 故障的错误处理
错误类型 | 描述 |
---|---|
invalidFormat |
无效的请求体格式。 |
authenticationFailed |
API 密钥不正确。 |
insufficientBalance |
余额不足。 |
rateLimitReached |
发送的请求过多。 |
serverOverloaded |
服务器流量过高。 |
encodingError |
编码请求体失败。 |
本项目根据 MIT 许可证提供。
此 SDK 与 DeepSeek 无关,是与他们的 API 交互的独立实现。