Swollama

swollama-logo-small

Documentation License

一个全面的、面向协议的 Swift 客户端,用于 Ollama API。 这个包提供了一种类型安全的方式来与 Ollama 的机器学习模型进行交互,支持所有 API 端点,并具有原生 Swift 并发性。

目录

特性

要求

安装

Swift Package Manager

Package.swift 中将 Swollama 添加到你的 Swift 包依赖项中

dependencies: [
    .package(url: "https://github.com/marcusziade/Swollama.git", from: "1.0.0")
]

或者通过 Xcode 添加

  1. File > Add Package Dependencies (文件 > 添加包依赖项)
  2. 输入仓库 URL:https://github.com/marcusziade/Swollama.git

快速开始

import Swollama

// Initialize client
let client = OllamaClient()

// List available models
let models = try await client.listModels()
for model in models {
    print(model.name)
}

// Start a chat
guard let model = OllamaModelName.parse("llama3.2") else {
    throw CLIError.invalidArgument("Invalid model name format")
}
let responses = try await client.chat(
    messages: [
        ChatMessage(role: .user, content: "Hello! How are you?")
    ],
    model: model
)
for try await response in responses {
    print(response.message.content, terminator: "")
}

CLI 用法

流式传输聊天回复

swollama chat llama3.2

CleanShot 2024-10-27 at 15 12 39

使用特定参数生成文本

swollama generate codellama

拉取新的模型

swollama pull llama3.2

CleanShot 2024-10-27 at 15 19 34

列出所有可用的模型

swollama list

CleanShot 2024-10-27 at 15 24 28@2x

显示模型信息

swollama show llama3.2

复制模型

swollama copy llama3.2 my-llama3.2

删除模型

swollama delete my-llama3.2

列出正在运行的模型

swollama ps

文档

有关完整的 API 文档、用法示例和最佳实践,请访问文档

示例

聊天完成

let client = OllamaClient()
let responses = try await client.chat(
    messages: [
        .init(role: .system, content: "You are a helpful assistant"),
        .init(role: .user, content: "Write a haiku about Swift")
    ],
    model: .init("llama3.2")!
)

for try await response in responses {
    print(response.message.content)
}

生成文本

let client = OllamaClient()
let responses = try await client.generate(
    prompt: "Explain quantum computing",
    model: .init("llama3.2")!
)

for try await response in responses {
    print(response.content)
}

贡献

欢迎贡献! 请随时提交 Pull Request。 对于重大更改,请先打开一个 issue 来讨论您想要更改的内容。

联系方式

如果您有任何问题、反馈或遇到问题,请在 GitHub 仓库上打开一个 issue