Skip to content

Instantly share code, notes, and snippets.

@AlexanderHolmeset
Created August 22, 2024 11:07
Show Gist options
  • Save AlexanderHolmeset/bba5201893c0146e043db9e6b708ae4b to your computer and use it in GitHub Desktop.
Save AlexanderHolmeset/bba5201893c0146e043db9e6b708ae4b to your computer and use it in GitHub Desktop.
# This function takes a path to a file as a parameter and returns the base64 string of that file.
function Get-Photo {
param (
[Parameter(Mandatory=$true)]
[string]$path
)
[convert]::ToBase64String((get-content $path -Raw -AsByteStream))
}
function Get-AzureOpenAIToken{
<# .SYNOPSIS
Get an azure token for user or managed identity thats required to authenticate to Azure OpenAI with Rest API.
Also construct the header if you are using an Azure OpenAI API key instead of Azure AD authentication.
.PARAMETER ManagedIdentity
Use this parameter if you want to use a managed identity to authenticate to Azure OpenAI.
.PARAMETER User
Use this parameter if you want to use a user to authenticate to Azure OpenAI.
.PARAMETER APIKey
Use this parameter if you want to use an API key to authenticate to Azure OpenAI.
.EXAMPLE
# Manually specify username and password to acquire an authentication token:
Get-AzureOpenAIToken -APIKey "ghgkfhgfgfgkhgh"
Get-AzureOpenAIToken -ManagedIdentity $true
Get-AzureOpenAIToken -User $true
.NOTES
Author: Alexander Holmeset
Twitter: @AlexHolmeset
Website: https://www.alexholmeset.blog
Created: 09-02-2023
Updated:
Version history:
1.0.0 - (09-02-2023) Function created
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$false)]
[string]$APIKey,
[Parameter(Mandatory=$false)]
[string]$ManagedIdentity,
[Parameter(Mandatory=$false)]
[string]$User
)
Process {
$ErrorActionPreference = "Stop"
if (Get-Module -ListAvailable -Name Az.Accounts) {
# Write-Host "You have the Az.Accounts module installed"
}
else {
Write-Host "You need to install the Az.Accounts module";
break
}
If(!$MyHeader){
If($ManagedIdentity -eq $true){
"managed"
try {
Connect-AzAccount -Identity
$MyTokenRequest = Get-AzAccessToken -ResourceUrl "https://cognitiveservices.azure.com"
$MyToken = $MyTokenRequest.token
If(!$MyToken){
Write-Warning "Failed to get API access token!"
Exit 1
}
$Global:MyHeader = @{"Authorization" = "Bearer $MyToken" }
}
catch [System.Exception] {
Write-Warning "Failed to get Access Token, Error message: $($_.Exception.Message)"; break
}
}
If($User -eq $true){
"USER"
try {
Connect-AzAccount
$MyTokenRequest = Get-AzAccessToken -ResourceUrl "https://cognitiveservices.azure.com"
$MyToken = $MyTokenRequest.token
If(!$MyToken){
Write-Warning "Failed to get API access token!"
Exit 1
}
$Global:MyHeader = @{"Authorization" = "Bearer $MyToken" }
}
catch [System.Exception] {
Write-Warning "Failed to get Access Token, Error message: $($_.Exception.Message)"; break
}
}
If($APIkey){
"APIKEY"
$Global:MyHeader = @{"api-key" = $apikey }
}
}
}
}
function Get-Chat {
<# .SYNOPSIS
Get a resposne from the chat endpoint in Azure OpenAI.
.PARAMETER DeploymentName
A deployment name should be provided.
.PARAMETER ResourceName
A Resource name should be provided.
.PARAMETER Prompt
A prompt name should be provided.
.PARAMETER Token
A token name should be provided.
.EXAMPLE
Get-Chat -DeploymentName $DeploymentName -ResourceName $ResourceName -maxtokens 1000 -prompt "What is the meaning of life?" -AssitantInstruction $AssitantInstruction
.NOTES
Author: Alexander Holmeset
Twitter: @AlexHolmeset
Website: https://www.alexholmeset.blog
Created: 09-02-2023
Updated:
Version history:
1.0.0 - (09-02-2023) Function created
#>[CmdletBinding()]
param (
[parameter(Mandatory = $false, HelpMessage = "Your azure openai deployment name")]
[ValidateNotNullOrEmpty()]
[string]$DeploymentName,
[parameter(Mandatory = $false, HelpMessage = "your azure openai resource name")]
[ValidateNotNullOrEmpty()]
[string]$ResourceName,
[parameter(Mandatory = $false, HelpMessage = "Your Azure OpenAI prompt")]
[ValidateNotNullOrEmpty()]
[string]$Prompt,
[parameter(Mandatory = $false, HelpMessage = "Your Azure OpenAI service instructions")]
[ValidateNotNullOrEmpty()]
[string]$AssitantInstruction,
[parameter(Mandatory = $true, HelpMessage = "Use photo?")]
[ValidateNotNullOrEmpty()]
[string]$ReadImage,
[parameter(Mandatory = $true, HelpMessage = "Set number of max tokens")]
[ValidateNotNullOrEmpty()]
[string]$Maxtokens,
[parameter(Mandatory = $true, HelpMessage = "Is image local or online?")]
[ValidateNotNullOrEmpty()]
[string]$ImageLocalOrOnline
)
Process {
$ErrorActionPreference = "Stop"
$APIVersion = "2024-04-01-preview"
# Construct URI
$uri = "https://$ResourceName.openai.azure.com/openai/deployments/$DeploymentName/chat/completions?api-version=$ApiVersion"
$script:conversation = $script:conversation -replace '(?s)(?<=base64,).+?(?="}}]},)',"test"
$body = @"
{
"messages": [
{"role": "user", "content": [{"type": "text", "text": "$Prompt"},{"type": "image_url","image_url": {"url": "data:image/jpeg;base64,$photo"}}]},
{"role": "system", "content": "$assitantInstruction"}
],
"max_tokens": $maxtokens,
"stream": false
}
"@
try {
$Global:Request = invoke-restmethod -Method POST -Uri $uri -ContentType "application/json" -Body $body -Headers $Global:MyHeader
}
catch [System.Exception] {
$_
Write-Warning "Failed to to POST request: $($_.Exception.Message)"; break
}
$script:Foldername = $($Request.choices.message.content)
}
}
#Gets the Azure OpenAI token
Get-AzureOpenAIToken -APIKey "Enter API KEY here"
#Fill in these deployment variables.
$DeploymentName = "Enter model name here"
$ResourceName = "Enter deployment name here"
$AssitantInstruction = "You are a helpfull assistant."
#Gets all image in the current folder in the terminal.
$images = Get-ChildItem
$FolderNameList = @()
#Loops through all images
foreach($image in $images){
#Converts the photo to base64 to be able to send in API request.
$photo = Get-Photo -path $Image.name
#Variable for foldernames you get back from Azure OpenAI
$FolderNameList += ", $foldername, "
#Prompt that is sent to the Azure OpenAI API, containing the current list of foldernames discovered.
$Prompt = "Im sorting photos in folders, can you give this photo a category? Sugest only one category. If there are categories from previous requests, try to find one that match. Reply with one single word. The current categories we have found is: $FolderNameList"
$FoldernameRequest = Get-Chat -DeploymentName $DeploymentName -ResourceName $ResourceName -AssitantInstruction $AssitantInstruction -Prompt $Prompt -ReadImage $true -ImageLocalOrOnline local -maxtokens 4096
$FoldernameCheck = @()
#Check if foldername exist.
$FoldernameCheck = Get-Item $Foldername -ErrorAction SilentlyContinue
If(!$FoldernameCheck){
#If foldername does not exist, create it.
New-Item -ItemType Directory $Foldername
}
#Move picture into correct folder.
Move-Item -path ./$($image.name) -Destination $Foldername
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment