f 性能优化-DeepSwiftSeek 开源软件|用于 DeepSeek LLM 模子的 Swift 客户端|轻量级和高效的 DeepSeek 核心功能通讯 - Powered by qidao123.com技术社区

DeepSwiftSeek 开源软件 |用于 DeepSeek LLM 模子的 Swift 客户端 |轻量级 ...

打印 上一主题 下一主题

主题 2012|帖子 2012|积分 6036

​一、软件介绍

文末提供步伐和源码下载
      DeepSeek Swift SDK 是一个轻量级且高效的基于 Swift 的客户端,用于与 DeepSeek API 进行交互。它支持聊天消息完成、流式处置惩罚、错误处置惩罚以及使用高级参数配置 DeepSeekLLM。

二、Features 特征



  • Supports chat completion requests
    支持聊天完成哀求
  • Supports fill in the middle completion requests
    支持添补中间完成哀求
  • Handles error responses with detailed error descriptions and recovery suggestions.
    使用详细的错误描述和规复建议处置惩罚错误相应。
  • streaming responses both for chat completion and as well fill in the middle responses
    用于聊天完成的流式处置惩罚相应,以及添补中间相应
  • Built-in support for different models and advanced parameters
    内置对不同模子和高级参数的支持
  • User balance fetchin and available LLM models fetching
    用户余额获取和可用LLM模子获取
  • Uses Swift concurrency (async/await) for network calls
    使用 Swift 并发 (async/await) 进行网络调用
三、Installation 安装

To integrate DeepSwiftSeek into your project, you can use Swift Package Manager (SPM):
要集成到 DeepSwiftSeek 您的项目中,您可以使用 Swift Package Manager (SPM):
  
  1. let package = Package(
  2.     dependencies: [
  3.         .package(url: "https://github.com/tornikegomareli/DeepSwiftSeek.git", exact: "0.0.2")
  4.     ]
  5. )
复制代码
   Or add it via Xcode:
或通过 Xcode 添加它:

  • Open your project in Xcode.
    在 Xcode 中打开您的项目。
  • Navigate to File > Swift Packages > Add Package Dependency.
    导航到 File > Swift Packages > Add Package Dependency。
  • Enter the repository URL.
    输入存储库 URL。
  • Choose the latest version and click Next.
    选择最新版本,然后单击 Next(下一步)。
   Usage 用法

    1. Initialize the Client 1. 初始化客户端

   
  1. import DeepSwiftSeek
  2. let configuration = Configuration(apiKey: "YOUR_API_KEY")
  3. let deepSeekClient = DeepSeekClient(configuration: configuration)
复制代码
     2. Sending a Chat Completion Request
2. 发送聊天完成哀求


   
  1. Task {
  2.     do {
  3.         let response = try await deepSeekClient.chatCompletions(
  4.             messages: {
  5.                 ChatMessageRequest(role: .user, content: "Tell me a joke.", name: "User")
  6.             },
  7.             model: .deepSeekChat,
  8.             parameters: .creative
  9.         )
  10.         print(response.choices.first?.message.content ?? "No response")
  11.     } catch {
  12.         print("Error: \(error.localizedDescription)")
  13.     }
  14. }
复制代码
     3. Streaming Chat Completions
3. 流式聊天完成


   
  1. Task {
  2.     do {
  3.         let stream = try await deepSeekClient.chatCompletionStream(
  4.             messages: {
  5.                 ChatMessageRequest(role: .user, content: "Write a poem.", name: "User")
  6.             },
  7.             model: .deepSeekChat,
  8.             parameters: .streaming
  9.         )
  10.         for try await chunk in stream {
  11.             print(chunk) // Prints streamed responses
  12.         }
  13.     } catch {
  14.         print("Streaming error: \(error.localizedDescription)")
  15.     }
  16. }
复制代码
     4. Streaming FIM Completion
4. 流式 FIM 完成


   
  1. Task {
  2.     do {
  3.         let stream = try await deepSeekClient.fimCompletionStream(
  4.             messages: {
  5.                 [
  6.                     ChatMessageRequest(
  7.                       role: .user,
  8.                       content: "function greet() {\n  /* FIM_START */\n  /* FIM_END */\n  return 'Hello world';\n}",
  9.                       name: "User"
  10.                     )
  11.                 ]
  12.             },
  13.             model: .deepSeekReasoner,
  14.             parameters: .streaming
  15.         )
  16.         
  17.         for try await chunk in stream {
  18.             // Each chunk is a streamed part of the fill-in-the-middle response.
  19.             print("FIM Stream Chunk:\n\(chunk)")
  20.         }
  21.     } catch {
  22.         print("FIM Streaming Error: \(error.localizedDescription)")
  23.     }
  24. }
复制代码
     5. Sending FIM Completion Request
5. 发送 FIM 完成哀求


   
  1. Task {
  2.     do {
  3.         let response = try await deepSeekClient.fimCompletions(
  4.             messages: {
  5.                 [
  6.                     ChatMessageRequest(
  7.                       role: .user,
  8.                       content: "function greet() {\n  // FIM_START\n  // FIM_END\n  return 'Hello world';\n}",
  9.                       name: "User"
  10.                     )
  11.                 ]
  12.             },
  13.             model: .deepSeekReasoner,
  14.             parameters: .creative
  15.         )
  16.         if let content = response.choices.first?.message.content {
  17.             print("FIM Completion:\n\(content)")
  18.         }
  19.     } catch {
  20.         print("FIM Error: \(error.localizedDescription)")
  21.     }
  22. }
复制代码
     6. Getting List of Models
6. 获取模子列表


   
  1. Task {
  2.     do {
  3.         let response = try await deepSeekClient.listModels()
  4.     } catch {
  5.         print("ListModels Error: \(error.localizedDescription)")
  6.     }
  7. }
复制代码
     7. Getting Balance of the user
7. 获取用户的余额


   
  1. Task {
  2.     do {
  3.         let response = try await deepSeekClient.fetchUserBalance()
  4.     } catch {
  5.         print("UserBalance Error: \(error.localizedDescription)")
  6.     }
  7. }
复制代码
     8. Handling Errors 8. 处置惩罚错误

  The SDK provides detailed error handling:
SDK 提供了详细的错误处置惩罚:
  
  1. catch let error as DeepSeekError {
  2.     print("DeepSeek API Error: \(error.localizedDescription)")
  3.     print("Recovery Suggestion: \(error.recoverySuggestion ?? "None")")
  4. } catch {
  5.     print("Unexpected error: \(error)")
  6. }
复制代码
     四、Models 模子

  DeepSeek SDK supports multiple models:
DeepSeek SDK 支持多种模子:
  
  1. public enum DeepSeekModel: String {
  2.     case deepSeekChat = "deepseek-chat"
  3.     case deepSeekReasoner = "deepseek-reasoner"
  4. }
复制代码
     Available Parameters 可用参数

  You can configure chat completion parameters:
您可以配置聊天完成参数:
  
  1. let parameters = ChatParameters(
  2.     frequencyPenalty: 0.5,
  3.     maxTokens: 512,
  4.     presencePenalty: 0.5,
  5.     temperature: 0.7,
  6.     topP: 0.9
  7. )
复制代码
     Predefined Parameter Sets
预定义参数集


  Mode 模式Temperature 温度Max Tokens 最大令牌数Top P 前 PCreative 创造性0.920480.9Focused 会合0.320480.3Streaming 流0.740960.9Code Generation 代码天生0.220480.95Concise 简明0.52560.5   Creating Custom Predefined Parameters
创建自定义预定义参数


  If you need specific configurations, you can define your own parameter presets:
假如您须要特定配置,您可以定义本身的参数预设:
  
  1. extension ChatParameters {
  2.     static let myCustomPreset = ChatParameters(
  3.         frequencyPenalty: 0.4,
  4.         maxTokens: 1024,
  5.         presencePenalty: 0.6,
  6.         temperature: 0.8,
  7.         topP: 0.85
  8.     )
  9. }
复制代码
   Then use it in your requests:
然后在您的哀求中使用它:
  
  1. let parameters = ChatParameters.myCustomPreset
复制代码
   This approach allows you to maintain reusable configurations tailored to different needs.
此方法允许您维护针对不同需求量身定制的可重用配置。
   Error Handling 错误处置惩罚

  DeepSeek SDK has built-in error handling for various API failures:
DeepSeek SDK 内置了针对各种 API 故障的错误处置惩罚功能:
Error Type 错误范例Description 描述invalidFormatInvalid request body format.
哀求正文格式无效。authenticationFailedIncorrect API key. API 密钥不准确。insufficientBalanceNo balance remaining. 没有余额。rateLimitReachedToo many requests sent.
发送的哀求过多。serverOverloadedHigh traffic on server.
服务器上的高流量。encodingErrorFailed to encode request body.
无法对哀求正文进行编码。   TODOs 都

  

  •  Improve documentation with more examples
    通过更多示例改进文档
  •  SwiftUI full demo based on chat, history and reasoning
    基于聊天、历史记录和推理的 SwiftUI 完备演示
  •  Reasoning model + OpenAI SDK
    推理模子 + OpenAI SDK

五、软件下载

迅雷云盘
本文信息来源于GitHub作者地址:GitHub - tornikegomareli/DeepSwiftSeek: DeepSwiftSeek

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

南飓风

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表