llama-stack-client-swift

Discord

llama-stack-client-swift 将 Llama Stack 的推理和代理 API 带到 iOS。

更新:2025 年 2 月 18 日 llama-stack-client-swift SDK 版本已更新至 0.1.3,与 Llama Stack 0.1.3 配合使用(发行说明)。

特性

iOS 演示

请参阅此处 快速 iOS 演示(视频),该演示使用远程 Llama Stack 服务器进行推理。

对于使用 Llama Stack Agent API 和自定义工具调用功能的更高级演示,请参阅 iOS Calendar Assistant 演示

安装

  1. 点击“Xcode > File > Add Package Dependencies...”。

  2. 在右上角添加此仓库 URL:https://github.com/meta-llama/llama-stack-client-swift,并在 Dependency Rule 中添加 0.1.3,然后单击 Add Package。

  3. 选择并将 llama-stack-client-swift 添加到您的应用程序目标。

  4. 在首次构建时:启用并信任系统提示的 OpenAPIGenerator 扩展。

  5. 尝试远程推理演示的最快方法是使用 Together.ai 的 Llama Stack 发行版,地址为 https://llama-stack.together.ai - 除非您想构建自己的发行版,否则可以跳过第 6 步。

  6. (可选)设置远程 Llama Stack 发行版,假设您拥有 FireworksTogether API 密钥,您可以通过点击链接轻松获取该密钥

conda create -n llama-stack python=3.10
conda activate llama-stack
pip install --no-cache llama-stack==0.1.3 llama-models==0.1.3 llama-stack-client==0.1.3

然后,或者

PYPI_VERSION=0.1.3 llama stack build --template fireworks --image-type conda
export FIREWORKS_API_KEY="<your_fireworks_api_key>"
llama stack run fireworks

PYPI_VERSION=0.1.3 llama stack build --template together --image-type conda
export TOGETHER_API_KEY="<your_together_api_key>"
llama stack run together

llama stack run 的默认端口是 5000,您可以通过在 llama stack run fireworks|together 的末尾添加 --port <your_port> 来指定不同的端口。

将下面的 RemoteInference url 字符串替换为第 6 步中远程 Llama Stack 发行版的主机 IP 和端口

import LlamaStackClient

let inference = RemoteInference(url: URL(string: "https://llama-stack.together.ai")!)
  1. 构建并运行 iOS 演示。

下面是使用 Llama Stack 推理 API 的示例代码片段。 有关完整代码,请参见上面的 iOS 演示。

for await chunk in try await inference.chatCompletion(
    request:
        Components.Schemas.ChatCompletionRequest(
        model_id: "meta-llama/Llama-3.1-8B-Instruct",
        messages: [
            .user(
            Components.Schemas.UserMessage(
                role: .user,
                content:
                    .InterleavedContentItem(
                        .text(Components.Schemas.TextContentItem(
                            _type: .text,
                            text: userInput
                        )
                    )
                )
            )
        )
        ],
        stream: true)
    ) {
        switch (chunk.event.delta) {
        case .text(let s):
            message += s.text
            break
        case .image(let s):
            print("> \(s)")
            break
        case .tool_call(let s):
            print("> \(s)")
            break
        }
    }