Skip to content

Commit

Permalink
new files
Browse files Browse the repository at this point in the history
  • Loading branch information
voytas75 committed Aug 4, 2024
1 parent 5a820cd commit 73997b6
Show file tree
Hide file tree
Showing 2 changed files with 485 additions and 0 deletions.
85 changes: 85 additions & 0 deletions Invoke-GptPowerShell/Invoke-GptPowerShell.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Import PS LLM
. .\PSMatrixLLM.ps1

function Invoke-GptPowerShell {
param (
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[string]$UserInput,

[int]$MaxTokens = $null,

[double]$Temperature = 0.3,

[double]$TopP = 0.7,
[switch]$ExecuteCode,
[bool]$Stream = $false,
[string]$LogFolder = [System.IO.Path]::GetTempPath(),
[int]$MaxRetries = 3,
[string]$DeploymentChat = "",
[string]$ollamaModel = "",
[string]$Provider = "AzureOpenAI"
)

# Initialize retry count and success flag
$retryCount = 0
$success = $false

# Define the system prompt
$systemPrompt = "You are a PowerShell code generator. Respond only with executable PowerShell code, no code block, no explanations."

# Loop until success or max retries reached
while (-not $success -and $retryCount -lt $MaxRetries) {
try {
# Invoke the LLM chat completion function
Write-Verbose "Invoking LLM chat completion with provider: $Provider"
$code = Invoke-LLMChatCompletion -Provider $Provider -SystemPrompt $systemPrompt -UserPrompt $UserInput -Temperature $Temperature -TopP $TopP -MaxTokens $MaxTokens -Stream $Stream -LogFolder $LogFolder -DeploymentChat $DeploymentChat -ollamaModel $ollamaModel

Write-Host "Generated PowerShell Code (Attempt $($retryCount + 1)):"
Write-Host $code
Write-Host ""

if ($ExecuteCode) {
Write-Host "Executing code..."
try {
# Execute the generated code
$result = Invoke-Expression $code
$success = $true
Write-Host "Code executed successfully."
Write-Host "Result:"
return $result
}
catch {
# Handle execution errors
Write-Host "Error occurred during execution:"
Write-Host $_.Exception.Message
$retryCount++
if ($retryCount -lt $MaxRetries) {
Write-Host "Retrying..."
$UserInput += "`nThe previous attempt resulted in the following error: $($_.Exception.Message). Please fix the code and try again."
}
else {
Write-Host "Max retries reached. Unable to execute code successfully."
}
}
}
else {
Write-Host "Generated code (not executed):"
Write-Host $code
Write-Host "To execute this code, run the command again with the -ExecuteCode switch."
break
}
}
catch {
# Handle errors from LLM chat completion
Write-Host "Error occurred during LLM chat completion:"
Write-Host $_.Exception.Message
$retryCount++
if ($retryCount -lt $MaxRetries) {
Write-Host "Retrying..."
}
else {
Write-Host "Max retries reached. Unable to generate code successfully."
}
}
}
}
Loading

0 comments on commit 73997b6

Please sign in to comment.