functions/LemonAI-AIChat.ps1

Add-Type -AssemblyName System.Web
function LemonAI-AIChat([string]$prompt,$session_port=11434,$model="llama2",$stream=$false, [int]$streamtype=1) {
    Add-Type -AssemblyName System.Web
    if($prompt){
        $prompt=$prompt.Replace("`n","\n").Replace("`r","\r")
        $prompt_encode=[System.Web.HttpUtility]::HtmlEncode($prompt)
        $apiUrl="http://localhost:$session_port/api/generate"
        $postData="{
    `"model`": `"$model`",
    `"prompt`":`"$prompt_encode`"
    }"

        return (Start-Job -Name "aichat" -ArgumentList $apiUrl, $postData, $stream, $streamtype -ScriptBlock {
            Add-Type -AssemblyName System.Web
            Add-Type -AssemblyName System.Net
            try {
                $apiUrl=$args[0]
                $postData=$args[1]
                $streamdata=[bool]$args[2]
                $streamtype=$args[3]

                $webRequest = [System.Net.HttpWebRequest]::Create($apiUrl)
                $webRequest.Method = "POST"
                $webRequest.ContentType = "application/x-www-form-urlencoded"

                $postDataBytes = [System.Text.Encoding]::UTF8.GetBytes($postData)
    
                $webRequest.ContentLength = $postDataBytes.Length

                $requestStream = $webRequest.GetRequestStream()
                $requestStream.Write($postDataBytes, 0, $postDataBytes.Length)
                $requestStream.Close()

                $webResponse = $webRequest.GetResponse()

                $stream = $webResponse.GetResponseStream()
                $streamReader = New-Object System.IO.StreamReader($stream)

                $streamContent=""
                while (-not $streamReader.EndOfStream) {
                    $line = $streamReader.ReadLine()
                    $json = ConvertFrom-Json $line
                    if($json.done -eq $false){
                        $streamContent+=$json.response
                        if($streamdata){
                            if($streamtype -eq 0){
                                Write-Output $json.response
                            }else {
                                Write-Output $streamContent
                            }
                        }
                    }
                }
                if($streamdata -eq $false){
                    Write-Output $streamContent
                }

                $streamReader.Close()
                $webResponse.Close()
            } catch {
                if($_.Exception.Message -like "*Unable to connect to the remote server*"){
                    Write-Error -Exception "Unable to connect to server" -Message "You must first start a session, or specify the correct session_port. Try using 'LemonAI-StartAISession' then use 'LemonAI-AIChat'." -Category ResourceUnavailable
                }else { Write-Output "Error: $_.Exception.Message" }
            }
        })
    }
    else {
        return $null
    }
}