Skip to content

Commit 5dfd196

Browse files
committed
系统变量
Fixes #211
1 parent 287e902 commit 5dfd196

File tree

2 files changed

+39
-30
lines changed

2 files changed

+39
-30
lines changed

code365scripts.openai/Private/Get-PromptContent.ps1

+31-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
function Get-PromptContent($prompt) {
1+
function Get-PromptContent {
2+
param(
3+
[string]$prompt,
4+
[hashtable]$context
5+
)
26

37
# ignore error and continue
48
$ErrorActionPreference = "SilentlyContinue"
5-
69
$type = "userinput"
710
$content = $prompt
811
$lib = ""
@@ -39,9 +42,34 @@ function Get-PromptContent($prompt) {
3942
}
4043
}
4144

45+
46+
47+
# system variable
48+
$systemVariables = @{
49+
"username" = $env:USERNAME
50+
"computername" = $env:COMPUTERNAME
51+
"os" = $env:OS
52+
"osarch" = $env:PROCESSOR_ARCHITECTURE
53+
"currentTimeutc" = [System.DateTime]::UtcNow
54+
"currentTime" = [System.DateTime]::Now
55+
}
56+
57+
if (!$context) {
58+
$context = @{}
59+
}
60+
61+
#merge system variables with context
62+
Merge-Hashtable -table1 $systemVariables -table2 $context
63+
$context = $systemVariables
64+
# if user provide the context, inject the data into the prompt by replace the context key with the context value
65+
if ($context) {
66+
foreach ($key in $context.keys) {
67+
$content = $content -replace "{{$key}}", $context[$key]
68+
}
69+
}
4270
# restore error action preference
4371
$ErrorActionPreference = "Continue"
44-
72+
4573
Write-Output @{
4674
type = $type
4775
content = $content

code365scripts.openai/Public/New-ChatGPTConversation.ps1

+8-27
Original file line numberDiff line numberDiff line change
@@ -159,34 +159,15 @@ function New-ChatGPTConversation {
159159
}
160160

161161
# if prompt is not empty and it is a file, then read the file as the prompt
162-
$parsedprompt = Get-PromptContent($prompt)
162+
$parsedprompt = Get-PromptContent -prompt $prompt -context $context
163163
$prompt = $parsedprompt.content
164-
165-
# if user provide the context, inject the data into the prompt by replace the context key with the context value
166-
if ($context) {
167-
Write-Verbose ($resources.verbose_context_received -f ($context | ConvertTo-Json -Depth 10))
168-
foreach ($key in $context.keys) {
169-
$prompt = $prompt -replace "{{$key}}", $context[$key]
170-
}
171-
Write-Verbose ($resources.verbose_prompt_context_injected -f $prompt)
172-
}
173-
174164
$telemetries.Add("promptType", $parsedprompt.type)
175165
$telemetries.Add("promptLib", $parsedprompt.lib)
176166

177167
# if system is not empty and it is a file, then read the file as the system prompt
178-
$parsedsystem = Get-PromptContent($system)
168+
$parsedsystem = Get-PromptContent -prompt $system -context $context
179169
$system = $parsedsystem.content
180170

181-
# if user provide the context, inject the data into the system prompt by replace the context key with the context value
182-
if ($context) {
183-
Write-Verbose ($resources.verbose_context_received -f ($context | ConvertTo-Json -Depth 10))
184-
foreach ($key in $context.keys) {
185-
$system = $system -replace "{{$key}}", $context[$key]
186-
}
187-
Write-Verbose ($resources.verbose_prompt_context_injected -f $system)
188-
}
189-
190171
$telemetries.Add("systemPromptType", $parsedsystem.type)
191172
$telemetries.Add("systemPromptLib", $parsedsystem.lib)
192173

@@ -245,9 +226,9 @@ function New-ChatGPTConversation {
245226
$body = @{model = "$model"; messages = $messages }
246227

247228
$params = @{
248-
Uri = $endpoint
249-
Method = "POST"
250-
Headers = $header
229+
Uri = $endpoint
230+
Method = "POST"
231+
Headers = $header
251232
}
252233

253234
if ($json) {
@@ -402,9 +383,9 @@ function New-ChatGPTConversation {
402383
# TODO #174 保留消息的个数是不是可以放宽
403384
$body = @{model = "$model"; messages = ($systemPrompt + $messages[-5..-1]); stream = $stream }
404385
$params = @{
405-
Uri = $endpoint
406-
Method = "POST"
407-
Headers = $header
386+
Uri = $endpoint
387+
Method = "POST"
388+
Headers = $header
408389
}
409390

410391
if ($json) {

0 commit comments

Comments
 (0)