https://github.com/engali94/groq_swift
Swift SDK for the fast Groq API on Apple platforms and Linux
https://github.com/engali94/groq_swift
groq-api
Last synced: 2 months ago
JSON representation
Swift SDK for the fast Groq API on Apple platforms and Linux
- Host: GitHub
- URL: https://github.com/engali94/groq_swift
- Owner: engali94
- License: mit
- Created: 2025-02-24T20:13:39.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-12T00:45:08.000Z (over 1 year ago)
- Last Synced: 2026-04-08T09:23:18.512Z (2 months ago)
- Topics: groq-api
- Language: Swift
- Homepage:
- Size: 1.99 MB
- Stars: 12
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GroqSwift
[](https://github.com/engali94/groq_swift/actions/workflows/ci.yml)
[](https://engali94.github.io/groq_swift/documentation/groqswift/)
> ⚠️ This is a community-maintained library and is not officially supported by Groq.
A Swift SDK for the Groq API, providing a convenient way to interact with Groq's language models in Swift applications. The SDK is designed to work on both Apple platforms and Linux.
## Demo App
Here's a demo chat application built using GroqSwift:

## Features
- ✨ Modern async/await API design
- 🔄 Support for both regular and streaming completions
- 🛡️ Type-safe request and response models
- 🐧 Linux compatibility
- ⚡️ Proper error handling with detailed messages
- 📱 Support for all Apple platforms
## Installation
### Swift Package Manager
Add the following to your `Package.swift` file:
```swift
dependencies: [
.package(url: "https://github.com/engali94/groq-swift.git", from: "0.1.0")
]
```
## Quick Start
Please ensure you have set the `GROQ_API_KEY` environment variable, before running the following code,
please DON'T store API key in code.
```swift
import GroqSwift
let apiKey = ProcessInfo.processInfo.environment["GROQ_API_KEY"] ?? ""
let client = GroqClient(apiKey: apiKey)
let request = ChatCompletionRequest(
model: .mixtral8x7bChat,
messages: [Message(role: .user, content: "What is the capital of France?")],
temperature: 0.7
)
let response = try await client.createChatCompletion(request)
print(response.choices.first?.message.content ?? "")
} catch {
print("Error: \(error)")
}
// Streaming completion
for try await response in await client.createStreamingChatCompletion(request) {
if let content = response.choices.first?.delta.content {
print(content, terminator: "")
}
}
```
## Advanced Usage
### Message Roles
The SDK supports all message roles:
```swift
// System message to set behavior
let systemMessage = Message(role: .system, content: "You are a helpful assistant")
// User message
let userMessage = Message(role: .user, content: "Hello!")
// Assistant message
let assistantMessage = Message(role: .assistant, content: "Hi there!")
```
### Available Models
Use dot syntax to specify models:
```swift
// LLaMA models
let llamaRequest = ChatCompletionRequest(model: .llama70bChat)
let llamaVersatileRequest = ChatCompletionRequest(model: .llama70bVersatile)
// Mixtral models
let mixtralRequest = ChatCompletionRequest(model: .mixtral8x7bChat)
let mixtralVersatileRequest = ChatCompletionRequest(model: .mixtral8x7bVersatile)
// Gemma models
let gemmaRequest = ChatCompletionRequest(model: .gemma7bChat)
let gemmaVersatileRequest = ChatCompletionRequest(model: .gemma7bVersatile)
// DeepSeek models
let deepseekLlamaRequest = ChatCompletionRequest(model: .deepseekR1DistillLlama70b)
let deepseekQwenRequest = ChatCompletionRequest(model: .deepseekR1DistillQwen32b)
```
### Request Parameters
Customize your requests with various parameters:
```swift
let request = ChatCompletionRequest(
model: .mixtral8x7bChat,
messages: messages,
stream: true, // Enable streaming
maxCompletionTokens: 100, // Limit response length
temperature: 0.7, // Control randomness
topP: 0.9, // Nucleus sampling
presencePenalty: 0.5, // Penalize token presence
frequencyPenalty: 0.5, // Penalize token frequency
stop: ["END"], // Stop sequences
user: "user-123" // User identifier
)
```
### Error Handling
The SDK provides detailed error information:
```swift
do {
let response = try await client.createChatCompletion(request)
} catch let error as GroqError {
switch error {
case .invalidRequest(let message):
print("Invalid request: \(message)")
case .authenticationError(let message):
print("Auth error: \(message)")
case .apiError(let statusCode, let message):
print("API error \(statusCode): \(message)")
case .invalidResponse(let message):
print("Invalid response: \(message)")
case .invalidURL:
print("Invalid URL")
}
} catch {
print("Unexpected error: \(error)")
}
```
## Demo Application
Check out the [GroqChatDemo](Examples/GroqChatDemo) directory for a complete SwiftUI chat application that demonstrates the SDK's capabilities.
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## Requirements
- macOS 13.0+
- iOS 16.0+
- watchOS 9.0+
- tvOS 16.0+
- visionOS 1.0+
- Swift 5.9+