-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSMatrixLLM.ps1
400 lines (343 loc) · 15.5 KB
/
PSMatrixLLM.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
function Invoke-LLMChatCompletion {
param (
[string]$Provider,
[string]$SystemPrompt,
[string]$UserPrompt,
[double]$Temperature,
[double]$TopP,
[int]$MaxTokens,
[bool]$Stream,
[string]$LogFolder,
[string]$DeploymentChat,
[string]$ollamaModel
)
try {
# Check if verbose prompts are enabled and display them
if ($GlobalState.VerbosePrompt) {
Write-Host "System Prompt: $SystemPrompt" -ForegroundColor DarkMagenta
Write-Host "User Prompt: $UserPrompt" -ForegroundColor DarkMagenta
}
# Switch between different LLM providers based on the provider parameter
switch ($Provider) {
"ollama" {
# Verbose message for invoking Ollama model
Write-Verbose "Invoking Ollama model completion function."
# Invoke the Ollama model completion function
$response = Invoke-AIPSTeamOllamaCompletion -SystemPrompt $SystemPrompt -UserPrompt $UserPrompt -Temperature $Temperature -TopP $TopP -ollamaModel $ollamamodel -Stream $Stream
return $response
}
"LMStudio" {
# Verbose message for invoking LMStudio model
Write-Verbose "Invoking LMStudio chat completion function."
# Handle streaming for LMStudio provider
# Invoke the LMStudio chat completion function
$response = Invoke-AIPSTeamLMStudioChatCompletion -SystemPrompt $SystemPrompt -UserPrompt $UserPrompt -Temperature $Temperature -TopP $TopP -Stream $Stream -ApiKey $script:lmstudioApiKey -endpoint $script:lmstudioApiBase -Model $script:LMStudioModel
return $response
}
"OpenAI" {
# Verbose message for unsupported LLM provider
Write-Verbose "Unsupported LLM provider: $Provider."
# Throw an exception for unsupported LLM provider
throw "-- Unsupported LLM provider: $Provider. This provider is not implemented yet."
}
"AzureOpenAI" {
# Verbose message for invoking Azure OpenAI model
Write-Verbose "Invoking Azure OpenAI chat completion function."
# Invoke the Azure OpenAI chat completion function
$response = Invoke-AIPSTeamAzureOpenAIChatCompletion -SystemPrompt $SystemPrompt -UserPrompt $UserPrompt -Temperature $Temperature -TopP $TopP -Stream $Stream -LogFolder $LogFolder -Deployment $DeploymentChat -MaxTokens $MaxTokens
return $response
}
default {
# Verbose message for unknown LLM provider
Write-Verbose "Unknown LLM provider: $Provider."
# Throw an exception for unknown LLM provider
throw "!! Unknown LLM provider: $Provider"
}
}
}
catch {
# Log the error and rethrow it with additional context
$functionName = $MyInvocation.MyCommand.Name
$errorMessage = "Error in ${functionName}: $_"
Write-Error $errorMessage
Update-ErrorHandling -ErrorRecord $_ -ErrorContext "$functionName function" -LogFilePath (Join-Path $LogFolder "ERROR.txt")
throw $_
}
}
function Invoke-AIPSTeamAzureOpenAIChatCompletion {
param (
[string]$SystemPrompt,
[string]$UserPrompt,
[double]$Temperature,
[double]$TopP,
[int]$MaxTokens,
[bool]$Stream,
[string]$LogFolder,
[string]$Deployment
)
try {
# Log the input parameters for debugging purposes
Write-Verbose "SystemPrompt: $SystemPrompt"
Write-Verbose "UserPrompt: $UserPrompt"
Write-Verbose "Temperature: $Temperature"
Write-Verbose "TopP: $TopP"
Write-Verbose "MaxTokens: $MaxTokens"
Write-Verbose "Stream: $Stream"
Write-Verbose "LogFolder: $LogFolder"
Write-Verbose "Deployment: $Deployment"
# Notify the start of the Azure OpenAI process
Write-Host "++ Initiating Azure OpenAI process for deployment: $Deployment..."
if ($Stream) {
Write-Host "++ Streaming mode enabled." -ForegroundColor Blue
}
# Invoke the Azure OpenAI chat completion function
$response = PSAOAI\Invoke-PSAOAIChatCompletion -SystemPrompt $SystemPrompt -usermessage $UserPrompt -Temperature $Temperature -TopP $TopP -LogFolder $LogFolder -Deployment $Deployment -User "AIPSTeam" -Stream $Stream -simpleresponse -OneTimeUserPrompt
if ($Stream) {
Write-Host "++ Streaming completed." -ForegroundColor Blue
}
Write-Host "++ Azure OpenAI process initiated successfully for deployment: $Deployment."
# Check if the response is null or empty
if ([string]::IsNullOrEmpty($response)) {
$errorMessage = "The response from Azure OpenAI API is null or empty."
Write-Error $errorMessage
throw $errorMessage
}
# Log the successful response
Write-Verbose "Azure OpenAI API response received successfully."
return $response
}
catch {
# Log the error and rethrow it with additional context
$functionName = $MyInvocation.MyCommand.Name
$errorMessage = "Error in ${functionName}: $_"
Write-Error $errorMessage
Update-ErrorHandling -ErrorRecord $_ -ErrorContext "$functionName function" -LogFilePath (Join-Path $LogFolder "ERROR.txt")
throw $_
}
}
function Invoke-AIPSTeamOllamaCompletion {
param (
[string]$SystemPrompt,
[string]$UserPrompt,
[double]$Temperature,
[double]$TopP,
[string]$ollamaModel,
[bool]$Stream
)
# Define options for the Ollama API call
$ollamaOptions = [pscustomobject]@{
temperature = $Temperature
top_p = $TopP
}
# Construct the JSON payload for the Ollama API request
$ollamaJson = [pscustomobject]@{
model = $ollamaModel
prompt = $SystemPrompt + "`n" + $UserPrompt
options = $ollamaOptions
stream = $Stream
} | ConvertTo-Json
# Ensure the Ollama endpoint ends with a '/'
if (-not $script:ollamaEndpoint.EndsWith('/')) {
$script:ollamaEndpoint += '/'
}
Write-Verbose "Constructed JSON payload for Ollama API: $ollamaJson"
# Define the URL for the Ollama API endpoint
$url = "$($script:ollamaEndpoint)api/generate"
Write-Verbose "Ollama API endpoint URL: $url"
# Notify the user that the Ollama model is processing
Write-Host "++ Ollama model ($ollamaModel) is processing your request..."
# Check if streaming is enabled and handle accordingly
if ($Stream) {
# Initialize HttpClientHandler with specific configurations for streaming
$httpClientHandler = [System.Net.Http.HttpClientHandler]::new()
$httpClientHandler.AllowAutoRedirect = $false
$httpClientHandler.UseCookies = $false
$httpClientHandler.AutomaticDecompression = [System.Net.DecompressionMethods]::GZip -bor [System.Net.DecompressionMethods]::Deflate
# Create HttpClient using the handler
$httpClient = [System.Net.Http.HttpClient]::new($httpClientHandler)
# Prepare the content of the HTTP request
$content = [System.Net.Http.StringContent]::new($ollamaJson, [System.Text.Encoding]::UTF8, "application/json")
# Create and configure the HTTP request message
$request = New-Object System.Net.Http.HttpRequestMessage ([System.Net.Http.HttpMethod]::Post, $url)
$request.Content = $content
# Send the HTTP request and read the headers of the response
$response = $httpClient.SendAsync($request, [System.Net.Http.HttpCompletionOption]::ResponseHeadersRead).Result
# Stream the response using StreamReader
$reader = [System.IO.StreamReader]::new($response.Content.ReadAsStreamAsync().Result)
# Initialize variable to accumulate the response text
$completeText = ""
Write-Host "++ Streaming" -ForegroundColor Blue
# Read and process each line of the response stream
while ($null -ne ($line = $reader.ReadLine()) -or (-not $reader.EndOfStream)) {
try {
$line = ($line | ConvertFrom-Json)
}
catch {
Write-Error "Error parsing JSON: $_"
}
if (-not $line.done) {
$delta = $line.response
$completeText += $delta
Write-Host $delta -NoNewline -ForegroundColor White
}
}
Write-Host ""
$completeText += "`n"
# Output the complete streamed text
if ($VerbosePreference -eq "Continue") {
Write-Host "++ Streaming completed. Full text: $completeText" -ForegroundColor DarkBlue
}
else {
Write-Host "++ Streaming completed." -ForegroundColor Blue
}
# Clean up resources
$reader.Close()
$httpClient.Dispose()
$response = $completeText
}
else {
# Send a non-streaming HTTP POST request and parse the response
$response = Invoke-WebRequest -Method POST -Body $ollamaJson -Uri $url -UseBasicParsing
$response = $response.Content | ConvertFrom-Json | Select-Object -ExpandProperty response
}
# Log the interaction details
$logEntry = @{
Timestamp = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
SystemPrompt = $SystemPrompt
UserPrompt = $UserPrompt
Response = $response
} | ConvertTo-Json
[void]($this.Log.Add($logEntry))
$this.AddLogEntry("SystemPrompt:`n$SystemPrompt")
$this.AddLogEntry("UserPrompt:`n$UserPrompt")
$this.AddLogEntry("Response:`n$response")
Write-Host "++ Ollama model ($ollamaModel) has successfully processed your request."
return $response.Trim('"')
}
function Invoke-AIPSTeamLMStudioChatCompletion {
param (
[string]$SystemPrompt,
[string]$UserPrompt,
[double]$Temperature,
[double]$TopP,
[string]$Model,
[string]$ApiKey,
[string]$endpoint,
[int]$timeoutSec = 240,
[bool]$Stream
)
$response = ""
# Define headers for the HTTP request
$headers = @{
"Content-Type" = "application/json"
"Authorization" = "Bearer $ApiKey"
}
# Construct the JSON body for the request
$bodyJSON = [ordered]@{
'model' = $Model
'messages' = @(
[ordered]@{
'role' = 'system'
'content' = $SystemPrompt
},
[ordered]@{
'role' = 'user'
'content' = $UserPrompt
}
)
'temperature' = $Temperature
'top_p' = $TopP
'stream' = $Stream
'max_tokens' = $GlobalState.maxtokens
} | ConvertTo-Json
Write-Verbose "Request Body JSON: $bodyJSON"
# Inform the user that the request is being processed
$InfoText = "++ LM Studio" + $(if ($Model) { " model ($Model)" } else { "" }) + " is processing your request..."
Write-Host $InfoText
$url = "$($endpoint)chat/completions"
# Check if streaming is enabled and handle accordingly
if ($Stream) {
# Create an instance of HttpClientHandler and disable buffering
$httpClientHandler = [System.Net.Http.HttpClientHandler]::new()
$httpClientHandler.AllowAutoRedirect = $false
$httpClientHandler.UseCookies = $false
$httpClientHandler.AutomaticDecompression = [System.Net.DecompressionMethods]::GZip -bor [System.Net.DecompressionMethods]::Deflate
# Create an instance of HttpClient
$httpClient = [System.Net.Http.HttpClient]::new($httpClientHandler)
# Set the required headers
$httpClient.DefaultRequestHeaders.Add("api-key", $ApiKey)
# Set the timeout for the HttpClient
$httpClient.Timeout = New-TimeSpan -Seconds $timeoutSec
# Create the HttpContent object with the request body
$content = [System.Net.Http.StringContent]::new($bodyJSON, [System.Text.Encoding]::UTF8, "application/json")
$request = New-Object System.Net.Http.HttpRequestMessage ([System.Net.Http.HttpMethod]::Post, $url)
$request.Content = $content
# Send the HTTP POST request asynchronously with HttpCompletionOption.ResponseHeadersRead
$response = $httpClient.SendAsync($request, [System.Net.Http.HttpCompletionOption]::ResponseHeadersRead).Result
# Ensure the request was successful
if (-not $response.IsSuccessStatusCode) {
Write-Host "-- Response was not successful: $($response.StatusCode) - $($response.ReasonPhrase)"
return
}
# Get the response stream
$stream_ = $response.Content.ReadAsStreamAsync().Result
$reader = [System.IO.StreamReader]::new($stream_)
Write-Host "++ Streaming." -ForegroundColor Blue
# Initialize the completeText variable
$completeText = ""
while ($null -ne ($line = $reader.ReadLine()) -or (-not $reader.EndOfStream)) {
# Check if the line starts with "data: " and is not "data: [DONE]"
if ($line.StartsWith("data: ") -and $line -ne "data: [DONE]") {
# Extract the JSON part from the line
$jsonPart = $line.Substring(6)
if ($completeText.EndsWith('+')) {
$completeText = $completeText.Substring(0, $completeText.Length - 1)
}
try {
# Parse the JSON part
$parsedJson = $jsonPart | ConvertFrom-Json
# Extract the text and append it to the complete text - Chat Completion
$delta = $parsedJson.choices[0].delta.content
$completeText += $delta
Write-Host $delta -NoNewline -ForegroundColor White
}
catch {
Write-Error "Error parsing JSON: $_"
}
}
}
Write-Host ""
$completeText += "`n"
if ($VerbosePreference -eq "Continue") {
Write-Verbose "Streaming completed. Full text: $completeText"
}
else {
Write-Host "++ Streaming completed." -ForegroundColor Blue
}
# Clean up
$reader.Close()
$httpClient.Dispose()
$response = $completeText
}
else {
# Send a non-streaming HTTP POST request and parse the response
$response = Invoke-RestMethod -Uri "$($endpoint)chat/completions" -Headers $headers -Method POST -Body $bodyJSON -TimeoutSec $timeoutSec
$response = $($response.Choices[0].message.content).trim()
}
# Log the prompt and response to the log file
$logEntry = @{
Timestamp = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
SystemPrompt = $SystemPrompt
UserPrompt = $UserPrompt
Response = $response
} | ConvertTo-Json
[void]($this.Log.Add($logEntry))
# Log the summary
[void]($this.AddLogEntry("SystemPrompt:`n$SystemPrompt"))
[void]($this.AddLogEntry("UserPrompt:`n$UserPrompt"))
[void]($this.AddLogEntry("Response:`n$Response"))
$InfoText = "++ LM Studio" + $(if ($Model) { " model ($Model)" } else { "" }) + " has successfully processed your request."
Write-Host $InfoText
return $response
}
import-module PSAOAI