AI/OpenAI.psm1
|
# OpenAI Integration Module for MiMo CLI # Provides integration with OpenAI API for code understanding function Invoke-OpenAICompletion { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [string]$Prompt, [string]$Model = "gpt-4", [int]$MaxTokens = 1000, [double]$Temperature = 0.7, [string]$ApiKey = $env:OPENAI_API_KEY ) if (-not $ApiKey) { Write-Error "OpenAI API key not found. Set OPENAI_API_KEY environment variable." return $null } $url = "https://api.openai.com/v1/chat/completions" $headers = @{ "Authorization" = "Bearer $ApiKey" "Content-Type" = "application/json" } $body = @{ model = $Model messages = @( @{ role = "user" content = $Prompt } ) max_tokens = $MaxTokens temperature = $Temperature } | ConvertTo-Json -Depth 10 try { $response = Invoke-RestMethod -Uri $url -Method Post -Headers $headers -Body $body return $response.choices[0].message.content } catch { Write-Error "Failed to call OpenAI API: $_" return $null } } function Get-OpenAIModels { [CmdletBinding()] param( [string]$ApiKey = $env:OPENAI_API_KEY ) if (-not $ApiKey) { Write-Error "OpenAI API key not found." return $null } $url = "https://api.openai.com/v1/models" $headers = @{ "Authorization" = "Bearer $ApiKey" } try { $response = Invoke-RestMethod -Uri $url -Method Get -Headers $headers return $response.data | Select-Object id, created, owned_by } catch { Write-Error "Failed to fetch OpenAI models: $_" return $null } } # Export functions Export-ModuleMember -Function Invoke-OpenAICompletion, Get-OpenAIModels |