### Format Inputs for Tool Calling in Swift Source: https://github.com/huggingface/swift-transformers/blob/main/README.md This example shows how to use swift-transformers to format inputs for tool calling with language models. It involves defining tool schemas and applying them to chat messages using the AutoTokenizer. This allows for structured interactions where the model can determine which tools to call based on user input. ```swift let tokenizer = try await AutoTokenizer.from(pretrained: "mlx-community/Qwen2.5-7B-Instruct-4bit") let weatherTool = [ "type": "function", "function": [ "name": "get_current_weather", "description": "Get the current weather in a given location", "parameters": [ "type": "object", "properties": ["location": ["type": "string", "description": "City and state"]], "required": ["location"] ] ] ] let tokens = try tokenizer.applyChatTemplate( messages: [["role": "user", "content": "What's the weather in Paris?"]], tools: [weatherTool] ) ``` -------------------------------- ### Generate Text with Transformers CLI Source: https://github.com/huggingface/swift-transformers/blob/main/Examples/Mistral7B/README.md This snippet shows how to use the `transformers-cli` tool to generate text based on a given prompt. It utilizes a pre-trained model, 'StatefulMistral7BInstructInt4.mlpackage', to produce a response of a specified maximum length. The output is a list of recommendations for visiting Paris in August 2024. ```shell ✗ swift run transformers-cli "Best recommendations for a place to visit in Paris in August 2024:" --max-length 128 StatefulMistral7BInstructInt4.mlpackage Best recommendations for a place to visit in Paris in August 2024: 1. Palace of Versailles: This iconic palace is a must-visit. It's a short train ride from Paris and offers a glimpse into the opulence of the French monarchy. 2. Eiffel Tower: No trip to Paris is complete without a visit to the Eiffel Tower. You can take an elevator ride to the top for a stunning view of the city. 3. Louvre Museum: Home to thousands of works of art, including the Mona Lisa and the Winged Victory of Samothrace, the Louvre is a cultural treasure. ``` -------------------------------- ### Run Swift Tests Source: https://github.com/huggingface/swift-transformers/blob/main/README.md Executes the test suite for the Swift Transformers project to ensure code quality and functionality. This command is essential before submitting any changes. ```shell swift test ``` -------------------------------- ### Export Mistral 7B Instruct v0.3 Model Source: https://github.com/huggingface/swift-transformers/blob/main/Examples/Mistral7B/README.md This snippet demonstrates the process of exporting the Mistral 7B Instruct v0.3 model using a Python script. It involves loading PyTorch checkpoints, converting them to MIL operations, and running various MIL pipelines for optimization and compression. The output shows the progress of each stage. ```shell ✗ uv run export.py Loading checkpoint shards: 100%|███████████████████████████| 3/3 [00:12<00:00, 4.11s/it] Converting PyTorch Frontend ==> MIL Ops: 100%|███| 5575/5575 [00:02<00:00, 2440.66 ops/s] Running MIL frontend_pytorch pipeline: 100%|██████████| 5/5 [00:00<00:00, 7.12 passes/s] Running MIL default pipeline: 100%|█████████████████| 79/79 [02:36<00:00, 1.98s/ passes] Running MIL backend_mlprogram pipeline: 100%|███████| 12/12 [00:00<00:00, 22.90 passes/s] Running compression: 100%|███████████████████████████| 296/296 [03:04<00:00, 1.60 ops/s] ... ``` -------------------------------- ### Download Models from Hugging Face Hub in Swift Source: https://github.com/huggingface/swift-transformers/blob/main/README.md This snippet illustrates downloading models from the Hugging Face Hub using the Hub module in swift-transformers. It supports progress reporting and handling unreliable connections. The function returns the URL of the downloaded model directory, downloading specified files like 'config.json' and safetensors. ```swift let repo = Hub.Repo(id: "mlx-community/Qwen2.5-0.5B-Instruct-2bit-mlx") let modelDirectory: URL = try await Hub.snapshot( from: repo, matching: ["config.json", "*.safetensors"], progressHandler: { progress in print("Download progress: (progress.fractionCompleted * 100)%") } ) print("Files downloaded to: (modelDirectory.path)") ``` -------------------------------- ### Format Swift Code Source: https://github.com/huggingface/swift-transformers/blob/main/README.md Applies code formatting recursively to all Swift files in the current directory. This ensures adherence to project-specific coding style guidelines. ```shell swift format -i --recursive . ``` -------------------------------- ### Add swift-transformers Dependency in SwiftPM Source: https://github.com/huggingface/swift-transformers/blob/main/README.md This code demonstrates how to add the swift-transformers library as a dependency to a Swift project using Swift Package Manager (SwiftPM). It shows the necessary entries for both the Package.swift file to declare the package dependency and the target definition to link the 'Transformers' product. ```swift dependencies: [ .package(url: "https://github.com/huggingface/swift-transformers", from: "0.1.17") ] targets: [ .target( name: "YourTargetName", dependencies: [ .product(name: "Transformers", package: "swift-transformers") ] ) ] ``` -------------------------------- ### Tokenize Text and Apply Chat Template in Swift Source: https://github.com/huggingface/swift-transformers/blob/main/README.md This snippet demonstrates how to tokenize text and apply a chat template using the AutoTokenizer from the swift-transformers library. It requires the AutoTokenizer to be initialized with a pretrained model. The output is an encoded sequence of tokens and a decoded string representation of the chat. ```swift let tokenizer = try await AutoTokenizer.from(pretrained: "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B") let messages = [["role": "user", "content": "Describe the Swift programming language."]] let encoded = try tokenizer.applyChatTemplate(messages: messages) let decoded = tokenizer.decode(tokens: encoded) ``` -------------------------------- ### Lint Swift Code Source: https://github.com/huggingface/swift-transformers/blob/main/README.md Performs linting checks on the Swift code recursively within the current directory. This helps maintain code consistency and identify potential issues. ```shell swift format lint --recursive . ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.