Skip to content

Commit 10d8aef

Browse files
committed
add assistant support
1 parent 4cccebd commit 10d8aef

File tree

1 file changed

+297
-0
lines changed

1 file changed

+297
-0
lines changed
Lines changed: 297 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
1+
function Get-AssistantResources {
2+
<#
3+
.SYNOPSIS
4+
List all the available OpenAI assistants.
5+
.DESCRIPTION
6+
List all the available OpenAI assistants.
7+
.PARAMETER apiKey
8+
The OpenAI API key.
9+
.PARAMETER endpoint
10+
The OpenAI API endpoint.
11+
#>
12+
[CmdletBinding()]
13+
param(
14+
[string]$apiKey = $env:OPENAI_API_KEY,
15+
[string]$endpoint = $env:OPENAI_API_ENDPOINT,
16+
[ValidateSet("assistants", "files", "vector_stores")]
17+
[string]$kind = "assistants"
18+
)
19+
20+
if (-not $apiKey) {
21+
Write-Error "API Key is required. Please provide the API key using -apiKey parameter or set the OPENAI_API_KEY environment variable."
22+
return
23+
}
24+
25+
if (-not $endpoint) {
26+
$endpoint = "https://api.openai.com/v1/$kind"
27+
}
28+
29+
$headers = @{
30+
"Content-Type" = "application/json"
31+
"OpenAI-Beta" = "assistants=v2"
32+
}
33+
34+
if ($endpoint -match "azure") {
35+
$headers.Add("api-key", $apiKey)
36+
$endpoint = $endpoint + "openai/${kind}?api-version=2024-05-01-preview"
37+
}
38+
else {
39+
$headers.Add("Authorization", "Bearer $apiKey")
40+
}
41+
42+
43+
44+
Invoke-RestMethod -Uri $endpoint -Headers $headers -Method Get | Select-Object -ExpandProperty data
45+
}
46+
47+
function Remove-AssistantResources {
48+
<#
49+
.SYNOPSIS
50+
Remove an OpenAI assistant.
51+
.DESCRIPTION
52+
Remove an OpenAI assistant.
53+
.PARAMETER assistantId
54+
The ID of the assistant to remove.
55+
.PARAMETER apiKey
56+
The OpenAI API key.
57+
.PARAMETER endpoint
58+
The OpenAI API endpoint.
59+
#>
60+
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = "High")]
61+
param(
62+
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
63+
[string[]]$id,
64+
[string]$apiKey = $env:OPENAI_API_KEY,
65+
[string]$endpoint = $env:OPENAI_API_ENDPOINT,
66+
[ValidateSet("assistants", "files", "vector_stores")]
67+
[string]$kind = "assistants"
68+
)
69+
70+
BEGIN {
71+
if (-not $apiKey) {
72+
Write-Error "API Key is required. Please provide the API key using -apiKey parameter or set the OPENAI_API_KEY environment variable."
73+
return
74+
}
75+
if (-not $endpoint) {
76+
$endpoint = "https://api.openai.com/v1/${kind}/{0}"
77+
}
78+
79+
$headers = @{
80+
"Content-Type" = "application/json"
81+
"OpenAI-Beta" = "assistants=v2"
82+
}
83+
84+
if ($endpoint -match "azure") {
85+
$headers.Add("api-key", $apiKey)
86+
$endpoint = $endpoint + "openai/${kind}/{0}?api-version=2024-05-01-preview"
87+
}
88+
else {
89+
$headers.Add("Authorization", "Bearer $apiKey")
90+
}
91+
}
92+
93+
PROCESS {
94+
if ($PSCmdlet.ShouldProcess("$id")) {
95+
$url = $endpoint -f $id
96+
Invoke-RestMethod -Uri $url -Headers $headers -Method Delete
97+
}
98+
}
99+
}
100+
101+
102+
function New-Assistant {
103+
<#
104+
.SYNOPSIS
105+
Create a new OpenAI assistant.
106+
.DESCRIPTION
107+
Create a new OpenAI assistant.
108+
.PARAMETER name
109+
The name of the assistant.
110+
.PARAMETER apiKey
111+
The OpenAI API key.
112+
.PARAMETER endpoint
113+
The OpenAI API endpoint.
114+
.PARAMETER instructions
115+
The instructions for the assistant.
116+
.PARAMETER model
117+
The model to use for the assistant.
118+
.PARAMETER config
119+
The configuration for the assistant.You can refer to https://platform.openai.com/docs/api-reference/assistants/modifyAssistant to learn more about the optional parameters.
120+
.PARAMETER vector_store_ids
121+
The vector store IDs to use for the assistant.
122+
#>
123+
[CmdletBinding()]
124+
param(
125+
[Parameter(Mandatory = $true)]
126+
[string]$name,
127+
[string]$apiKey = $env:OPENAI_API_KEY,
128+
[string]$endpoint = $env:OPENAI_API_ENDPOINT,
129+
[string]$instructions = "Please help me with the following:",
130+
[string]$model = $env:OPENAI_API_MODEL,
131+
[hashtable]$config,
132+
[string[]]$vector_store_ids
133+
)
134+
135+
if (-not $apiKey) {
136+
Write-Error "API Key is required. Please provide the API key using -apiKey parameter or set the OPENAI_API_KEY environment variable."
137+
return
138+
}
139+
140+
if (-not $model) {
141+
Write-Error "Model is required. Please provide the model using -model parameter or set the OPENAI_API_MODEL environment variable."
142+
return
143+
}
144+
145+
if (-not $endpoint) {
146+
$endpoint = "https://api.openai.com/v1/assistants"
147+
}
148+
149+
$headers = @{
150+
"Content-Type" = "application/json"
151+
"OpenAI-Beta" = "assistants=v2"
152+
}
153+
154+
if ($endpoint -match "azure") {
155+
$headers.Add("api-key", $apiKey)
156+
$endpoint = $endpoint + "openai/assistants?api-version=2024-05-01-preview"
157+
}
158+
else {
159+
$headers.Add("Authorization", "Bearer $apiKey")
160+
}
161+
162+
$body = @{
163+
"name" = $name
164+
"instructions" = $instructions
165+
"model" = $model
166+
}
167+
168+
if ($vector_store_ids -and $vector_store_ids.Count -gt 0) {
169+
$body.Add("tool_resources", @{
170+
"file_search" = @{
171+
"vector_store_ids" = @($vector_store_ids)
172+
}
173+
})
174+
175+
$body.Add("tools", @{
176+
"type" = "file_search"
177+
})
178+
}
179+
180+
# if config is provided, merge it with the body
181+
if ($config) {
182+
Merge-Hashtable -table1 $body -table2 $config
183+
}
184+
185+
Invoke-RestMethod -Uri $endpoint -Headers $headers -Method Post -Body ($body | ConvertTo-Json -Depth 10)
186+
}
187+
188+
189+
function Add-FileToOpenAI {
190+
<#
191+
.SYNOPSIS
192+
Upload a file to OpenAI.
193+
.DESCRIPTION
194+
Upload a file to OpenAI.
195+
.PARAMETER apiKey
196+
The OpenAI API key.
197+
.PARAMETER endpoint
198+
The OpenAI API endpoint.
199+
.PARAMETER fullname
200+
The full path of the file to upload. currently only supports pdf, docx, txt, and md files.
201+
#>
202+
[CmdletBinding()]
203+
param(
204+
[string]$apiKey = $env:OPENAI_API_KEY,
205+
[string]$endpoint = $env:OPENAI_API_ENDPOINT,
206+
[Parameter(Mandatory = $true, Position = 0 , ValueFromPipeline = $true)]
207+
[string[]]$fullname
208+
)
209+
210+
BEGIN {
211+
if (-not $apiKey) {
212+
Write-Error "API Key is required. Please provide the API key using -apiKey parameter or set the OPENAI_API_KEY environment variable."
213+
return
214+
}
215+
if (-not $endpoint) {
216+
$endpoint = "https://api.openai.com/v1/files"
217+
}
218+
219+
$headers = @{
220+
"OpenAI-Beta" = "assistants=v2"
221+
}
222+
223+
if ($endpoint -match "azure") {
224+
$headers.Add("api-key", $apiKey)
225+
$endpoint = $endpoint + "openai/files?api-version=2024-05-01-preview"
226+
}
227+
else {
228+
$headers.Add("Authorization", "Bearer $apiKey")
229+
}
230+
231+
}
232+
233+
PROCESS {
234+
235+
# Define the file path
236+
$filePath = $fullname
237+
238+
# Define the purpose (e.g., "assistants", "vision", "batch", or "fine-tune")
239+
$purpose = "assistants"
240+
241+
# Create a new web request
242+
$request = [System.Net.WebRequest]::Create($endpoint)
243+
$request.Method = "POST"
244+
245+
# add the item of headers to request.Headers
246+
$headers.GetEnumerator() | ForEach-Object {
247+
$request.Headers.Add($_.Key, $_.Value)
248+
}
249+
250+
# Create a boundary for the multipart/form-data content
251+
$boundary = [System.Guid]::NewGuid().ToString()
252+
253+
# Set the content type and boundary
254+
$request.ContentType = "multipart/form-data; boundary=$boundary"
255+
256+
$name = "{0}-{1}" -f (Get-FileHash $fullname).Hash, (Get-Item $fullname).Name
257+
258+
# Create the request body
259+
$body = @"
260+
--$boundary
261+
Content-Disposition: form-data; name="file"; filename="$name"
262+
Content-Type: application/octet-stream
263+
264+
$(Get-Content $filePath)
265+
266+
--$boundary
267+
Content-Disposition: form-data; name="purpose"
268+
269+
$purpose
270+
--$boundary--
271+
"@
272+
273+
# Convert the body to bytes
274+
$bodyBytes = [System.Text.Encoding]::UTF8.GetBytes($body)
275+
276+
# Set the content length
277+
$request.ContentLength = $bodyBytes.Length
278+
279+
# Get the request stream and write the body
280+
$requestStream = $request.GetRequestStream()
281+
$requestStream.Write($bodyBytes, 0, $bodyBytes.Length)
282+
$requestStream.Close()
283+
284+
# Get the response
285+
$response = $request.GetResponse()
286+
287+
# Read the response content
288+
$responseStream = $response.GetResponseStream()
289+
$reader = [System.IO.StreamReader]::new($responseStream)
290+
$responseContent = $reader.ReadToEnd()
291+
$reader.Close()
292+
293+
# Print the response content
294+
$responseContent
295+
296+
}
297+
}

0 commit comments

Comments
 (0)