Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
0b19d78
Add Package.swift in root
danielnugraha Mar 10, 2025
311bb66
Fix sendable issue
danielnugraha Mar 11, 2025
d4245ac
Merge branch 'main' into add-swift-package
Mar 11, 2025
2c92f7b
Add to gitignore
danielnugraha Mar 11, 2025
afb76c2
Merge remote-tracking branch 'refs/remotes/origin/add-swift-package' …
danielnugraha Mar 11, 2025
7d1c3f3
Format
danielnugraha Mar 11, 2025
66f1e85
Format
danielnugraha Mar 11, 2025
7b16105
Add ci checks and tests
danielnugraha Mar 11, 2025
6292a64
use swift 6.0
danielnugraha Mar 11, 2025
a1efd50
Merge branch 'main' into add-swift-ci
Mar 11, 2025
43e373b
Merge main
danielnugraha Mar 11, 2025
ec3d12b
Merge remote-tracking branch 'refs/remotes/origin/add-swift-ci' into …
danielnugraha Mar 11, 2025
675f4af
Add docs generation for Swift
danielnugraha Mar 11, 2025
c94b633
Add sourcedocs
danielnugraha Mar 11, 2025
cfb60af
Use macos
danielnugraha Mar 11, 2025
a3c185f
Add Swift to docs
danielnugraha Mar 11, 2025
2f10d52
Change runner
danielnugraha Mar 11, 2025
f5ef188
Change to xcodebuild test
danielnugraha Mar 11, 2025
3c1e3bd
Merge branch 'main' into add-swift-ci
Mar 11, 2025
42287b0
Use brew
danielnugraha Mar 11, 2025
6f3a4b8
Merge remote-tracking branch 'refs/remotes/origin/add-swift-ci' into …
danielnugraha Mar 11, 2025
717e190
Add check
danielnugraha Mar 11, 2025
25cd9be
Upgrade xcode
danielnugraha Mar 11, 2025
d9d8bea
Upgrade xcode
danielnugraha Mar 11, 2025
2215048
Upgrade xcode
danielnugraha Mar 11, 2025
293f78c
Upgrade xcode
danielnugraha Mar 11, 2025
eb7723b
Add Docs
danielnugraha Mar 12, 2025
8071ea4
Merge branch 'add-swift-ci' into add-swift-docs
danielnugraha Mar 12, 2025
033a578
Merge branch 'main' into add-swift-docs
Mar 12, 2025
d9a1b5a
Merge branch 'main' into add-swift-docs
Mar 12, 2025
1be2779
Merge remote-tracking branch 'origin' into add-swift-docs
danielnugraha Mar 12, 2025
8cc0afd
Merge remote-tracking branch 'refs/remotes/origin/add-swift-docs' int…
danielnugraha Mar 12, 2025
0e5bb30
Separate swift and ts docs job
danielnugraha Mar 12, 2025
a1271d5
Merge branch 'main' into add-swift-docs
Mar 12, 2025
168072e
Fix pandoc
danielnugraha Mar 12, 2025
5ec78f7
Merge branch 'main' into add-swift-docs
Mar 12, 2025
57a1a9c
Fix
danielnugraha Mar 12, 2025
dc8be63
Merge remote-tracking branch 'refs/remotes/origin/add-swift-docs' int…
danielnugraha Mar 12, 2025
3949407
Change ubuntu version
danielnugraha Mar 12, 2025
87c00fe
Merge branch 'main' into add-swift-docs
Mar 12, 2025
8b90838
Fix
danielnugraha Mar 12, 2025
048d245
Merge remote-tracking branch 'refs/remotes/origin/add-swift-docs' int…
danielnugraha Mar 12, 2025
6c73bbe
Fix
danielnugraha Mar 12, 2025
de42afd
Fix
danielnugraha Mar 12, 2025
b3ac856
Merge branch 'main' into add-swift-docs
Mar 12, 2025
a25281b
Fix
danielnugraha Mar 12, 2025
d568412
Merge remote-tracking branch 'refs/remotes/origin/add-swift-docs' int…
danielnugraha Mar 12, 2025
bc8049e
Merge branch 'main' into add-swift-docs
Mar 12, 2025
5350339
Merge branch 'main' into add-swift-docs
Mar 13, 2025
663700c
Merge branch 'main' into add-swift-docs
Mar 13, 2025
f5fc9d1
Merge branch 'main' into add-swift-docs
danieljanes Mar 13, 2025
506f7b4
Merge branch 'main' into add-swift-docs
Mar 13, 2025
ee1bdfa
Merge branch 'main' into add-swift-docs
Mar 13, 2025
ce55dc7
Merge branch 'main' into add-swift-docs
flwrmachine Mar 13, 2025
e62ea66
Merge branch 'main' into add-swift-docs
flwrmachine Mar 13, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 74 additions & 19 deletions .github/workflows/intelligence-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,100 @@ env:
FLWR_TELEMETRY_ENABLED: 0

jobs:
build_and_deploy:
runs-on: macos-14
name: Build and deploy
build_ts_docs:
runs-on: ubuntu-22.04
name: Build TypeScript Docs
steps:
- uses: actions/checkout@v4
- name: Bootstrap
uses: ./.github/actions/bootstrap
- name: Install pandoc
run: brew install pandoc
- name: Install Flower dependencies (mandatory only)
run: python -m poetry install --extras "simulation"
- uses: swift-actions/setup-swift@v2
with:
swift-version: "6.0.0"
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: "16.2"
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '22.14.0'
- name: Install SourceDocs
run: brew install sourcedocs
- name: Install pnpm
run: |
npm install -g pnpm
- name: Install Flower Intelligence
run: |
cd intelligence/ts
pnpm i
- name: Build docs
- name: Build TS docs
run: |
cd intelligence/ts
pnpm build:docs --readme none --name "TypeScript API"
- name: Move TS docs
run: |
echo -e '\n```{toctree}\n:hidden:\n:maxdepth: 2\n:glob:\n\n*/*\n```' >> intelligence/docs/source/ts-api-ref/index.md
- name: Upload TS docs artifact
uses: actions/upload-artifact@v4
with:
name: ts-docs
path: intelligence/docs/source/ts-api-ref/

build_swift_docs:
runs-on: macos-14
name: Build Swift Docs
steps:
- uses: actions/checkout@v4
- name: Bootstrap
uses: ./.github/actions/bootstrap
- uses: swift-actions/setup-swift@v2
with:
swift-version: "6.0.0"
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: "16.2"
- name: Install SourceDocs
run: brew install sourcedocs
- name: Build Swift docs
run: |
sourcedocs generate --module-name FlowerIntelligence -- -scheme flower -destination 'platform=macOS'
mkdir -p intelligence/docs/source/swift-api-ref
cp -r Documentation/Reference/* intelligence/docs/source/swift-api-ref/
mv intelligence/docs/source/swift-api-ref/README.md intelligence/docs/source/swift-api-ref/index.md
sed -i.bak '1 s/^# .*/# Swift API/' intelligence/docs/source/swift-api-ref/index.md
sed -i.bak '/^This file was generated by/d' intelligence/docs/source/swift-api-ref/index.md
rm -f intelligence/docs/source/swift-api-ref/index.md.bak
echo -e '\n```{toctree}\n:hidden:\n:maxdepth: 2\n:glob:\n\n*/*\n```' >> intelligence/docs/source/swift-api-ref/index.md
- name: Upload Swift docs artifact
uses: actions/upload-artifact@v4
with:
name: swift-docs
path: intelligence/docs/source/swift-api-ref/

build_and_deploy:
runs-on: ubuntu-22.04
needs: [build_ts_docs, build_swift_docs]
name: Build and Deploy Docs
steps:
- uses: actions/checkout@v4
- name: Bootstrap
uses: ./.github/actions/bootstrap
- name: Install pandoc
run: sudo apt install pandoc
- name: Install Flower dependencies (mandatory only)
run: python -m poetry install --extras "simulation"
- name: Download TS docs
uses: actions/download-artifact@v4
with:
name: ts-docs
path: intelligence/docs/source/ts-api-ref/
- name: Download Swift docs
uses: actions/download-artifact@v4
with:
name: swift-docs
path: intelligence/docs/source/swift-api-ref/
- name: Build final docs
run: |
./intelligence/dev/build-docs.sh
cd intelligence/docs
make html
- name: Deploy docs
if: ${{ github.ref == 'refs/heads/main' && github.repository == 'adap/flower' && !github.event.pull_request.head.repo.fork }}
env:
AWS_DEFAULT_REGION: ${{ secrets. AWS_DEFAULT_REGION }}
AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets. AWS_SECRET_ACCESS_KEY }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
DOCS_BUCKET: flower.ai
run: |
aws s3 sync --delete --exclude ".*" --exclude "v/*" --cache-control "no-cache" ./intelligence/docs/build/html/ s3://${{ env.DOCS_BUCKET }}/docs/intelligence
192 changes: 190 additions & 2 deletions intelligence/docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Flower Intelligence is a cross-platform inference library that let's user
seamlessly interact with Large-Language Models both locally and remotely in a
secure and private way. The library was created by the ``Flower Labs`` team that also created `Flower: A Friendly Federated AI Framework <https://flower.ai>`_.

We currently only provide a SDK for TypeScript/JavaScript.
We currently provide SDKs for TypeScript/JavaScript and Swift.


Install
Expand All @@ -27,6 +27,17 @@ Install
.. code-block:: bash

npm i "@flwr/flwr"

.. tab-item:: Swift
:sync: swift

.. code-block:: bash

# You can add package dependency to your Xcode project via its UI.
# Please refer to https://developer.apple.com/documentation/xcode/adding-package-dependencies-to-your-app.
#
# To add dependency to your Swift package, you can run the following command:
swift package add-dependency "https://github.com/adap/flower.git"


Hello, Flower Intelligence!
Expand Down Expand Up @@ -77,6 +88,23 @@ Flower Intelligence is built around the Singleton design pattern, meaning you on

await main().then().catch();

.. tab-item:: Swift
:sync: swift

.. code-block:: swift

import FlowerIntelligence

let fi = FlowerIntelligence.instance

let result = await fi.chat("Why is the sky blue?")
switch result {
case .success(let message):
print(message.content)
case .failure(let error):
print(error.localizedDescription)
}


Specify the model
-----------------
Expand Down Expand Up @@ -129,6 +157,26 @@ By specifying a model in the chat options, you can easily switch between differe
}

await main().then().catch();

.. tab-item:: Swift
:sync: swift

.. code-block:: swift

import FlowerIntelligence

let fi = FlowerIntelligence.instance

let options = ChatOptions(model: "meta/llama3.2-1b/instruct-fp16")
let result = await fi.chat("Why is the sky blue?", maybeOptions: options)

switch result {
case .success(let message):
print(message.content)
case .failure(let error):
print(error.localizedDescription)
}


Check for errors
----------------
Expand Down Expand Up @@ -188,6 +236,26 @@ Instead of throwing exceptions that might crash your application, Flower Intelli

await main().then().catch();

.. tab-item:: Swift
:sync: swift

.. code-block:: swift

import FlowerIntelligence

let fi = FlowerIntelligence.instance

let options = ChatOptions(model: "meta/llama3.2-1b/instruct-fp16")
let result = await fi.chat("Why is the sky blue?", maybeOptions: options)

switch result {
case .success(let message):
print(message.content)
case .failure(let error):
print(error.localizedDescription)
}


Stream Responses
----------------

Expand Down Expand Up @@ -251,6 +319,29 @@ The callback function must accept an argument of type :doc:`StreamEvent <ts-api-

await main().then().catch();

.. tab-item:: Swift
:sync: swift

.. code-block:: swift

import FlowerIntelligence

let fi = FlowerIntelligence.instance

let options = ChatOptions(
model: "meta/llama3.2-1b/instruct-fp16",
stream: true,
onStreamEvent: { event in
print(event.chunk)
}
)

let result = await fi.chat("Why is the sky blue?", maybeOptions: options)

if case .failure(let error) = result {
print(error.localizedDescription)
}

Use Roles
---------

Expand Down Expand Up @@ -320,6 +411,29 @@ Instead of simply sending a single string, you can provide an array of :doc:`mes
}

await main().then().catch();

.. tab-item:: Swift
:sync: swift

.. code-block:: swift

import FlowerIntelligence

let fi = FlowerIntelligence.instance

let messages = [
Message(role: "system", content: "You are a helpful assistant."),
Message(role: "user", content: "Why is the sky blue?")
]

let options = ChatOptions(model: "meta/llama3.2-1b/instruct-fp16")
let result = await fi.chat(options: (messages, options))
switch result {
case .success(let message):
print(message.content)
case .failure(let error):
print(error.localizedDescription)
}

Handle history
--------------
Expand Down Expand Up @@ -414,6 +528,50 @@ In this example, the conversation history is maintained in an array that include
}

await main().then().catch();

.. tab-item:: Swift
:sync: swift

.. code-block:: swift

import FlowerIntelligence

let fi = FlowerIntelligence.instance

// Initialize history with a system message
var history: [Message] = [
Message(role: "system", content: "You are a friendly assistant that loves using emojis.")
]

// Function to chat while preserving conversation history
func chatWithHistory(userInput: String) async {
// Append user input to the history
history.append(Message(role: "user", content: userInput))

// Define chat options with streaming
let options = ChatOptions(
model: "meta/llama3.2-1b/instruct-fp16",
stream: true,
onStreamEvent: { event in
print(event.chunk)
}
)

// Perform chat with full history
let result = await fi.chat(options: (history, options))

switch result {
case .success(let response):
// Append assistant's response to history
history.append(response)
print("Assistant's full response:", response.content)
case .failure(let error):
print(error.localizedDescription)
}
}

// Start the conversation
await chatWithHistory("Why is the sky blue?")

Pre-loading the model
---------------------
Expand Down Expand Up @@ -630,6 +788,36 @@ You will also need to provide a valid API key via the ``apiKey`` attribute.
}

await main().then().catch();

.. tab-item:: Swift
:sync: swift

.. code-block:: swift

import FlowerIntelligence

let fi = FlowerIntelligence.instance
fi.remoteHandoff = true
fi.apiKey = "YOUR_API_KEY"

let messages = [
Message(role: "system", content: "You are a helpful assistant."),
Message(role: "user", content: "Why is the sky blue?")
]

let options = ChatOptions(
model: "meta/llama3.2-1b/instruct-fp16",
stream: true,
onStreamEvent: { event in
print(event.chunk)
}
)

let result = await fi.chat(options: (messages, options))

if case .failure(let error) = result {
print(error.localizedDescription)
}

References
----------
Expand All @@ -644,6 +832,7 @@ Information-oriented API reference and other reference material.
ref-models
examples
ts-api-ref/index
swift-api-ref/index

Join the Flower Community
-------------------------
Expand All @@ -655,4 +844,3 @@ The Flower Community is growing quickly - we're a friendly group of researchers,
:shadow:

Join us on Slack