### Quick Start: Create Chat Completion Source: https://github.com/engali94/groq_swift/blob/main/README.md Initialize the Groq client and make a basic chat completion request. Ensure the GROQ_API_KEY environment variable is set. ```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)") } ``` -------------------------------- ### Install GroqSwift with Swift Package Manager Source: https://github.com/engali94/groq_swift/blob/main/README.md Add the GroqSwift SDK to your project's dependencies using Swift Package Manager. ```swift dependencies: [ .package(url: "https://github.com/engali94/groq-swift.git", from: "0.1.0") ] ``` -------------------------------- ### Quick Start: Streaming Chat Completion Source: https://github.com/engali94/groq_swift/blob/main/README.md Initiate a streaming chat completion request and process the response chunks as they arrive. ```swift // Streaming completion for try await response in await client.createStreamingChatCompletion(request) { if let content = response.choices.first?.delta.content { print(content, terminator: "") } } ``` -------------------------------- ### Specifying Available Models in GroqSwift Source: https://github.com/engali94/groq_swift/blob/main/README.md Examples of how to specify different Groq models using dot syntax for LLaMA, Mixtral, Gemma, and DeepSeek 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) ``` -------------------------------- ### Creating a GroqClient Instance Source: https://github.com/engali94/groq_swift/blob/main/Sources/GroqSwift/Documentation.docc/Extensions/GroqClient.md Initializes a new GroqClient with an API key, optional host, and an optional URLSession. ```APIDOC ## init(apiKey:host:session:) ### Description Initializes a new `GroqClient` instance. ### Parameters - **apiKey** (String) - Required - The API key for authentication. - **host** (URLConvertible) - Optional - The base URL for the Groq API. Defaults to the official Groq API endpoint. - **session** (URLSession) - Optional - The URLSession to use for network requests. Defaults to `URLSession.shared`. ``` -------------------------------- ### Create and Use GroqClient for Chat Completions Source: https://github.com/engali94/groq_swift/blob/main/Sources/GroqSwift/Documentation.docc/Extensions/GroqClient.md Instantiate the GroqClient with an API key and use it to create chat completions. Handles both regular and streaming responses. ```swift let client = GroqClient(apiKey: "your-api-key") let request = ChatCompletionRequest( model: .mixtral8x7bChat, messages: [Message(role: .user, content: "Hello!")], temperature: 0.7 ) // Regular completion let response = try await client.createChatCompletion(request) print(response.choices.first?.message.content ?? "") // Streaming completion for try await response in await client.createStreamingChatCompletion(request) { if let content = response.choices.first?.delta.content { print(content, terminator: "") } } ``` -------------------------------- ### Creating Messages Source: https://github.com/engali94/groq_swift/blob/main/Sources/GroqSwift/Documentation.docc/Extensions/Message.md Demonstrates how to create different types of messages using the Message initializer. ```APIDOC ## Creating Messages ### Initializer - ``init(role:content:)`` ### Example ```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!") ``` ``` -------------------------------- ### createChatCompletion(_:) Source: https://github.com/engali94/groq_swift/blob/main/Sources/GroqSwift/Documentation.docc/Extensions/GroqClient.md Creates a chat completion request to the Groq API. ```APIDOC ## createChatCompletion(_:) ### Description Sends a request to the Groq API to generate a chat completion based on the provided `ChatCompletionRequest`. ### Parameters - **request** (ChatCompletionRequest) - Required - The parameters for the chat completion request. ### Response #### Success Response (200) - **ChatCompletionResponse** - The response object containing the generated chat completion. ``` -------------------------------- ### createStreamingChatCompletion(_:) Source: https://github.com/engali94/groq_swift/blob/main/Sources/GroqSwift/Documentation.docc/Extensions/GroqClient.md Creates a streaming chat completion request to the Groq API. ```APIDOC ## createStreamingChatCompletion(_:) ### Description Sends a request to the Groq API to generate a streaming chat completion. This method returns a stream of `ChatCompletionResponse` objects as they are generated. ### Parameters - **request** (ChatCompletionRequest) - Required - The parameters for the chat completion request. ### Response #### Success Response (Stream) - **AsyncThrowingStream** - A stream of `ChatCompletionResponse` objects representing the incremental updates to the chat completion. ``` -------------------------------- ### Creating GroqSwift Messages Source: https://github.com/engali94/groq_swift/blob/main/Sources/GroqSwift/Documentation.docc/Extensions/Message.md Instantiate Message objects for system, user, and assistant roles to build a conversation history. ```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!") ``` -------------------------------- ### Create a Chat Completion Request Source: https://github.com/engali94/groq_swift/blob/main/Sources/GroqSwift/Documentation.docc/Extensions/ChatCompletionRequest.md Instantiate a `ChatCompletionRequest` to configure parameters for generating a chat completion. This includes specifying the model, messages, streaming behavior, and various generation controls like temperature and stop sequences. ```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 ) ``` -------------------------------- ### GroqError Cases Source: https://github.com/engali94/groq_swift/blob/main/Sources/GroqSwift/Documentation.docc/Extensions/GroqError.md This snippet demonstrates how to handle different `GroqError` cases using a switch statement. ```APIDOC ## GroqError Cases ### Description This snippet demonstrates how to handle different `GroqError` cases using a switch statement. ### Example ```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)") } ``` ``` -------------------------------- ### Creating a Chat Completion Request Source: https://github.com/engali94/groq_swift/blob/main/Sources/GroqSwift/Documentation.docc/Extensions/ChatCompletionRequest.md Initializes a ChatCompletionRequest with specified parameters for model, messages, streaming, and generation controls. ```APIDOC ## Initializing ChatCompletionRequest ### Description Initializes a `ChatCompletionRequest` object with various parameters to configure a chat completion request. ### Method Signature ``init(model:messages:stream:streamOptions:maxCompletionTokens:temperature:topP:n:stop:presencePenalty:frequencyPenalty:seed:responseFormat:serviceTier:user:tools:toolChoice:)`` ### Parameters - **model** (Model) - The Groq model to use for the chat completion. - **messages** ([ChatMessage]) - An array of messages representing the conversation history. - **stream** (Bool) - Whether to stream the response. - **streamOptions** (StreamOptions?) - Options for streaming the response. - **maxCompletionTokens** (Int?) - The maximum number of tokens to generate in the completion. - **temperature** (Double?) - Controls the randomness of the generated text. Higher values mean more randomness. - **topP** (Double?) - Nucleus sampling parameter. The model considers only the tokens comprising the top P probability mass. - **n** (Int?) - The number of chat completion choices to generate for each input message. - **stop** ([String]?) - A list of strings that will cause the completion to stop generating further tokens. - **presencePenalty** (Double?) - Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics. - **frequencyPenalty** (Double?) - Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim. - **seed** (Int?) - A random seed to use for generation. - **responseFormat** (ResponseFormat?) - Specifies the format of the response. - **serviceTier** (ServiceTier?) - The service tier to use for the request. - **user** (String?) - A unique identifier representing your end-user. - **tools** ([Tool]?) - A list of tools the model may call. - **toolChoice** (ToolChoice?) - Controls which tool the model may use. ### Example ```swift let messages: [ChatMessage] = [ .init(role: .system, content: "You are a helpful assistant."), .init(role: .user, content: "What is the weather like today?") ] let request = ChatCompletionRequest( model: .mixtral8x7bChat, messages: messages, stream: true, maxCompletionTokens: 100, temperature: 0.7, topP: 0.9, stop: ["END"], user: "user-123" ) ``` ``` -------------------------------- ### Handling GroqError in Swift Source: https://github.com/engali94/groq_swift/blob/main/Sources/GroqSwift/Documentation.docc/Extensions/GroqError.md Catch and handle specific GroqError cases, such as invalid requests, authentication issues, API errors, or invalid responses, by casting the caught error to GroqError and using a switch statement. ```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)") } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.