Swift Examples - Groq

Service setup

Create a Groq service in the AIProxy dashboard

Follow the integration guide, selecting the Groq icon on the 'Create a New Service' form.

How to generate a non-streaming chat completion using Groq

import AIProxy

/* Uncomment for BYOK use cases */
// let groqService = AIProxy.groqDirectService(
//     unprotectedAPIKey: "your-groq-key"
// )

/* Uncomment for all other production use cases */
// let groqService = AIProxy.groqService(
//     partialKey: "partial-key-from-your-developer-dashboard",
//     serviceURL: "service-url-from-your-developer-dashboard"
// )

do {
    let response = try await groqService.chatCompletionRequest(body: .init(
        messages: [.assistant(content: "hello world")],
        model: "mixtral-8x7b-32768"
    ))
    print(response.choices.first?.message.content ?? "")
}  catch AIProxyError.unsuccessfulRequest(let statusCode, let responseBody) {
    print("Received non-200 status code: \(statusCode) with response body: \(responseBody)")
} catch {
    print(error.localizedDescription)
}

How to generate a streaming chat completion using Groq

import AIProxy

/* Uncomment for BYOK use cases */
// let groqService = AIProxy.groqDirectService(
//     unprotectedAPIKey: "your-groq-key"
// )

/* Uncomment for all other production use cases */
// let groqService = AIProxy.groqService(
//     partialKey: "partial-key-from-your-developer-dashboard",
//     serviceURL: "service-url-from-your-developer-dashboard"
// )

do {
    let stream = try await groqService.streamingChatCompletionRequest(body: .init(
            messages: [.assistant(content: "hello world")],
            model: "mixtral-8x7b-32768"
        )
    )
    for try await chunk in stream {
        print(chunk.choices.first?.delta.content ?? "")
    }
}  catch AIProxyError.unsuccessfulRequest(let statusCode, let responseBody) {
    print("Received \(statusCode) status code with response body: \(responseBody)")
} catch {
    print(error.localizedDescription)
}

How to transcribe audio with Groq

  1. Record an audio file in quicktime and save it as "helloworld.m4a"
  2. Add the audio file to your Xcode project. Make sure it's included in your target: select your audio file in the project tree, type cmd-opt-0 to open the inspect panel, and view Target Membership
  3. Run this snippet:
import AIProxy

/* Uncomment for BYOK use cases */
// let groqService = AIProxy.groqDirectService(
//     unprotectedAPIKey: "your-groq-key"
// )

/* Uncomment for all other production use cases */
// let groqService = AIProxy.groqService(
//     partialKey: "partial-key-from-your-developer-dashboard",
//     serviceURL: "service-url-from-your-developer-dashboard"
// )

do {
    let url = Bundle.main.url(forResource: "helloworld", withExtension: "m4a")!
    let requestBody = GroqTranscriptionRequestBody(
        file: try Data(contentsOf: url),
        model: "whisper-large-v3-turbo",
        responseFormat: "json"
    )
    let response = try await groqService.createTranscriptionRequest(body: requestBody)
    let transcript = response.text ?? "None"
    print("Groq transcribed: \(transcript)")
}  catch AIProxyError.unsuccessfulRequest(let statusCode, let responseBody) {
    print("Received non-200 status code: \(statusCode) with response body: \(responseBody)")
} catch {
    print("Could not get audio transcription from Groq: \(error.localizedDescription)")
}