Providers/FireworksAI.ps1

<#
.SYNOPSIS
    Invokes the Fireworks AI inference API to generate chat responses using specified models.
 
.DESCRIPTION
    The Invoke-FireworksAIProvider function sends requests to the Fireworks AI inference endpoint
    and returns the generated content. It expects an API key to be provided via an environment
    variable. By default it looks for `FireworksAIKey`, then `FIREWORKS_API_KEY`, then `FireworksKey`.
 
.PARAMETER ModelName
    The name of the Fireworks AI model to use (for example: 'accounts/fireworks/models/gpt-oss-120b').
 
.PARAMETER Messages
    An array of hashtables containing the messages to send to the model. Each message should follow
    the chat completion format (for example: @{role='user'; content='Hello'} ).
 
.EXAMPLE
    $Message = New-ChatMessage -Prompt 'Write a PowerShell function to calculate factorial'
    $response = Invoke-FireworksAIProvider -ModelName 'accounts/fireworks/models/gpt-oss-120b' -Messages $Message
 
.NOTES
    - Environment variable used for API key (checked in order): `FireworksAIKey`, `FIREWORKS_API_KEY`, `FireworksKey`.
    - Endpoint: https://api.fireworks.ai/inference/v1/chat/completions
#>

function Invoke-FireworksAIProvider {
    param(
        [Parameter(Mandatory)]
        [string]$ModelName,
        [Parameter(Mandatory)]
        [hashtable[]]$Messages,
        [int]$MaxTokens = 4096,
        [double]$TopP = 1,
        [int]$TopK = 40,
        [double]$PresencePenalty = 0,
        [double]$FrequencyPenalty = 0,
        [double]$Temperature = 0.6
    )

    # Resolve API key from several possible environment variables (assumption: user may set any of these)
    $apiKey = $env:FireworksAIKey
    if (-not $apiKey) { $apiKey = $env:FIREWORKS_API_KEY }
    if (-not $apiKey) { $apiKey = $env:FireworksKey }

    if (-not $apiKey) {
        throw "Fireworks AI API key not found. Please set environment variable 'FireworksAIKey' or 'FIREWORKS_API_KEY' or 'FireworksKey'."
    }

    $headers = @{
        'Accept'        = 'application/json'
        'Content-Type'  = 'application/json'
        'Authorization' = "Bearer $apiKey"
    }

    $body = @{
        model             = $ModelName
        max_tokens        = $MaxTokens
        top_p             = $TopP
        top_k             = $TopK
        presence_penalty  = $PresencePenalty
        frequency_penalty = $FrequencyPenalty
        temperature       = $Temperature
        messages          = $Messages
    }

    $Uri = "https://api.fireworks.ai/inference/v1/chat/completions"

    $params = @{ 
        Uri     = $Uri
        Method  = 'POST'
        Headers = $headers
        Body    = $body | ConvertTo-Json -Depth 10
    }

    try {
        $response = Invoke-RestMethod @params

        # Try common response shapes for chat completions
        if ($null -ne $response.choices -and $response.choices.Count -gt 0) {
            $choice = $response.choices[0]
            if ($null -ne $choice.message -and $null -ne $choice.message.content) {
                return $choice.message.content
            }
            elseif ($null -ne $choice.delta -and $null -ne $choice.delta.content) {
                return $choice.delta.content
            }
            elseif ($null -ne $choice.text) {
                return $choice.text
            }
        }

        # Fallback: return full JSON response as string
        return ($response | ConvertTo-Json -Depth 10)
    }
    catch {
        # Attempt to extract status code and message similarly to other providers
        $statusCode = $null
        try { $statusCode = $_.Exception.Response.StatusCode.value__ } catch { }
        $errorMessage = $null
        try { $errorMessage = $_.ErrorDetails.Message } catch { $errorMessage = $_.Exception.Message }
        Write-Error "Fireworks AI API Error (HTTP $statusCode): $errorMessage"
        return "Error calling Fireworks AI API: $($_.Exception.Message)"
    }
}