openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.6.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

scripts/Export-Api.ps1

315lines · modecode

1<#
2.SYNOPSIS
3 Cross-platform API export script for OpenAI .NET SDK.
4
5.DESCRIPTION
6 This script supports Windows, macOS, and Linux environments. It automatically detects the current platform
7 and uses the appropriate paths for .NET and NuGet packages.
8#>
9
10function Invoke-DotNetBuild {
11 param(
12 [Parameter(Mandatory = $true)]
13 [string]$ProjectPath
14 )
15
16 Write-Output "Building $($ProjectPath)..."
17 Write-Output ""
18 & dotnet build $ProjectPath
19 Write-Output ""
20}
21
22function Invoke-GenAPI {
23 param (
24 [Parameter(Mandatory = $true)]
25 [string]$TargetFramework,
26
27 [Parameter(Mandatory = $true)]
28 [string]$AssemblyPath,
29
30 [Parameter(Mandatory = $true)]
31 [string]$Destination
32 )
33
34 Write-Output "Generating $($Destination)..."
35 Write-Output ""
36
37 Write-Output " Detected platform paths:"
38 Write-Output ""
39
40 # Set platform-specific paths using PowerShell automatic variables (PowerShell 6.0+)
41 # Fall back to manual detection for older PowerShell versions
42 $isWindowsPlatform = $false
43 $isMacOSPlatform = $false
44
45 if (Get-Variable -Name "IsWindows" -ErrorAction SilentlyContinue) {
46 $isWindowsPlatform = $IsWindows
47 $isMacOSPlatform = $IsMacOS
48 } else {
49 # Fallback for Windows PowerShell 5.1 and earlier
50 $isWindowsPlatform = [System.Environment]::OSVersion.Platform -eq [System.PlatformID]::Win32NT
51 $isMacOSPlatform = [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::OSX)
52 }
53
54 if ($isWindowsPlatform) {
55 $dotnetPacksPath = Join-Path $env:ProgramFiles "dotnet\packs\Microsoft.NETCore.App.Ref"
56 $nugetPackagesPath = Join-Path $env:UserProfile ".nuget\packages"
57 } elseif ($isMacOSPlatform) {
58 $dotnetPacksPath = "/usr/local/share/dotnet/packs/Microsoft.NETCore.App.Ref"
59 $nugetPackagesPath = Join-Path $env:HOME ".nuget/packages"
60 } else {
61 # Linux or other Unix-like systems
62 $dotnetPacksPath = "/usr/share/dotnet/packs/Microsoft.NETCore.App.Ref"
63 $nugetPackagesPath = Join-Path $env:HOME ".nuget/packages"
64 }
65
66 Write-Output " * .NET packs:"
67 Write-Output " $($dotnetPacksPath)"
68 Write-Output ""
69 Write-Output " * NuGet packages:"
70 Write-Output " $($nugetPackagesPath)"
71 Write-Output ""
72
73 Write-Output " Assembly reference paths:"
74 Write-Output ""
75
76 # .NET
77 $netRef = $null
78 if (Test-Path $dotnetPacksPath) {
79 $netRef = Get-ChildItem -Recurse `
80 -Path $dotnetPacksPath `
81 -Include "net8.0" | Select-Object -Last 1
82 }
83
84 # If not found in primary location, try alternative locations
85 if (-not $netRef) {
86 $alternativePaths = @()
87 if ($isWindowsPlatform) {
88 $alternativePaths += "${env:ProgramFiles(x86)}\dotnet\packs\Microsoft.NETCore.App.Ref"
89 } elseif ($isMacOSPlatform) {
90 $alternativePaths += "/usr/local/share/dotnet/packs/Microsoft.NETCore.App.Ref"
91 $alternativePaths += "/Library/Frameworks/Microsoft.NETCore.App.Ref"
92 } else {
93 $alternativePaths += "/usr/lib/dotnet/packs/Microsoft.NETCore.App.Ref"
94 $alternativePaths += "/opt/dotnet/packs/Microsoft.NETCore.App.Ref"
95 }
96
97 foreach ($altPath in $alternativePaths) {
98 if (Test-Path $altPath) {
99 $netRef = Get-ChildItem -Recurse -Path $altPath -Include "net8.0" | Select-Object -Last 1
100 if ($netRef) { break }
101 }
102 }
103 }
104
105 Write-Output " * .NET:"
106 Write-Output " $($netRef)"
107 Write-Output ""
108
109 # System.ClientModel
110 $systemClientModelPath = Join-Path $nugetPackagesPath "system.clientmodel\1.7.0"
111 $systemClientModelRef = $null
112 if (Test-Path $systemClientModelPath) {
113 $systemClientModelRef = Get-ChildItem `
114 -Path $systemClientModelPath `
115 -Include $(($TargetFramework -eq "netstandard2.0") ? "netstandard2.0" : "net8.0") `
116 -Recurse |
117 Select-Object -Last 1
118 }
119
120 Write-Output " * System.ClientModel:"
121 Write-Output " $($systemClientModelRef)"
122 Write-Output ""
123
124 # System.Net.ServerSentEvents
125 $systemNetServerSentEventsPath = Join-Path $nugetPackagesPath "system.net.serversentevents\9.0.9"
126 $systemNetServerSentEventsRef = $null
127 if (Test-Path $systemNetServerSentEventsPath) {
128 $systemNetServerSentEventsRef = Get-ChildItem `
129 -Path $systemNetServerSentEventsPath `
130 -Include $(($TargetFramework -eq "netstandard2.0") ? "netstandard2.0" : "net8.0") `
131 -Recurse |
132 Select-Object -Last 1
133 }
134
135 Write-Output " * System.Net.ServerSentEvents:"
136 Write-Output " $($systemNetServerSentEventsRef)"
137 Write-Output ""
138
139 # Microsoft.Extensions.Logging.Abstractions
140 $microsoftExtensionsLoggingAbstractionsPath = Join-Path $nugetPackagesPath "microsoft.extensions.logging.abstractions\8.0.3"
141 $microsoftExtensionsLoggingAbstractionsRef = $null
142 if (Test-Path $microsoftExtensionsLoggingAbstractionsPath) {
143 $microsoftExtensionsLoggingAbstractionsRef = Get-ChildItem `
144 -Path $microsoftExtensionsLoggingAbstractionsPath `
145 -Include $(($TargetFramework -eq "netstandard2.0") ? "netstandard2.0" : "net8.0") `
146 -Recurse |
147 Select-Object -Last 1
148 }
149
150 Write-Output " * Microsoft.Extensions.Logging.Abstractions:"
151 Write-Output " $($microsoftExtensionsLoggingAbstractionsRef)"
152 Write-Output ""
153
154 # Microsoft.Extensions.DependencyInjection.Abstractions
155 $microsoftExtensionsDependencyInjectionAbstractionsPath = Join-Path $nugetPackagesPath "microsoft.extensions.dependencyinjection.abstractions\8.0.2"
156 $microsoftExtensionsDependencyInjectionAbstractionsRef = $null
157 if (Test-Path $microsoftExtensionsDependencyInjectionAbstractionsPath) {
158 $microsoftExtensionsDependencyInjectionAbstractionsRef = Get-ChildItem `
159 -Path $microsoftExtensionsDependencyInjectionAbstractionsPath `
160 -Include $(($TargetFramework -eq "netstandard2.0") ? "netstandard2.0" : "net8.0") `
161 -Recurse |
162 Select-Object -Last 1
163 }
164
165 Write-Output " * Microsoft.Extensions.DependencyInjection.Abstractions:"
166 Write-Output " $($microsoftExtensionsDependencyInjectionAbstractionsRef)"
167 Write-Output ""
168
169 # System.Memory.Data
170 $systemMemoryDataPath = Join-Path $nugetPackagesPath "system.memory.data\8.0.1"
171 $systemMemoryDataRef = $null
172 if (Test-Path $systemMemoryDataPath) {
173 $systemMemoryDataRef = Get-ChildItem `
174 -Path $systemMemoryDataPath `
175 -Include $(($TargetFramework -eq "netstandard2.0") ? "netstandard2.0" : "net6.0") `
176 -Recurse |
177 Select-Object -Last 1
178 }
179
180 Write-Output " * System.Memory.Data:"
181 Write-Output " $($systemMemoryDataRef)"
182 Write-Output ""
183
184 if ($TargetFramework -eq "netstandard2.0") {
185 # System.Diagnostics.DiagnosticSource
186 $systemDiagnosticsDiagnosticSourcePath = Join-Path $nugetPackagesPath "system.diagnostics.diagnosticsource\8.0.1"
187 $systemDiagnosticsDiagnosticSourceRef = $null
188 if (Test-Path $systemDiagnosticsDiagnosticSourcePath) {
189 $systemDiagnosticsDiagnosticSourceRef = Get-ChildItem `
190 -Path $systemDiagnosticsDiagnosticSourcePath `
191 -Include $(($TargetFramework -eq "netstandard2.0") ? "netstandard2.0" : "net5.0") `
192 -Recurse |
193 Select-Object -Last 1
194 }
195
196 Write-Output " * System.Diagnostics.DiagnosticSource:"
197 Write-Output " $($systemDiagnosticsDiagnosticSourceRef)"
198 Write-Output ""
199
200 # Microsoft.Bcl.AsyncInterfaces
201 $microsoftBclAsyncInterfacesPath = Join-Path $nugetPackagesPath "microsoft.bcl.asyncinterfaces\8.0.0"
202 $microsoftBclAsyncInterfacesRef = $null
203 if (Test-Path $microsoftBclAsyncInterfacesPath) {
204 $microsoftBclAsyncInterfacesRef = Get-ChildItem `
205 -Path $microsoftBclAsyncInterfacesPath `
206 -Include "netstandard2.0" `
207 -Recurse |
208 Select-Object -Last 1
209 }
210
211 Write-Output " * Microsoft.Bcl.AsyncInterfaces:"
212 Write-Output " $($microsoftBclAsyncInterfacesRef)"
213 Write-Output ""
214 }
215
216 Write-Output " NOTE: If any of the above are empty, tool output may be inaccurate."
217 Write-Output ""
218
219 # Build genapi command arguments, excluding null references
220 $genapiArgs = @(
221 "--assembly", $AssemblyPath
222 "--output-path", $Destination
223 )
224
225 if ($netRef) { $genapiArgs += @("--assembly-reference", $netRef) }
226 if ($systemClientModelRef) { $genapiArgs += @("--assembly-reference", $systemClientModelRef) }
227 if ($systemNetServerSentEventsRef) { $genapiArgs += @("--assembly-reference", $systemNetServerSentEventsRef) }
228 if ($microsoftExtensionsLoggingAbstractionsRef) { $genapiArgs += @("--assembly-reference", $microsoftExtensionsLoggingAbstractionsRef) }
229 if ($microsoftExtensionsDependencyInjectionAbstractionsRef) { $genapiArgs += @("--assembly-reference", $microsoftExtensionsDependencyInjectionAbstractionsRef) }
230 if ($systemMemoryDataRef) { $genapiArgs += @("--assembly-reference", $systemMemoryDataRef) }
231 if ($systemDiagnosticsDiagnosticSourceRef) { $genapiArgs += @("--assembly-reference", $systemDiagnosticsDiagnosticSourceRef) }
232 if ($microsoftBclAsyncInterfacesRef) { $genapiArgs += @("--assembly-reference", $microsoftBclAsyncInterfacesRef) }
233
234 & genapi @genapiArgs
235
236 Write-Output "Cleaning up $($Destination)..."
237 Write-Output ""
238
239 $content = Get-Content $Destination -Raw
240
241 # Remove empty lines.
242 $content = $content -creplace '//.*\r?\n', ''
243 $content = $content -creplace '\r?\n\r?\n', "`n"
244 $content = $content -creplace '\r?\n *{', " {"
245
246 # Remove fully-qualified names.
247 $content = $content -creplace "System\.ComponentModel\.", ""
248 $content = $content -creplace "System\.ClientModel.Primitives\.", ""
249 $content = $content -creplace "System\.ClientModel\.", ""
250 $content = $content -creplace "System\.Collections\.Generic\.", ""
251 $content = $content -creplace "System\.Collections\.", ""
252 $content = $content -creplace "System\.Threading.Tasks\.", ""
253 $content = $content -creplace "System\.Threading\.", ""
254 $content = $content -creplace "System\.Text.Json\.", ""
255 $content = $content -creplace "System\.Text\.", ""
256 $content = $content -creplace "System\.IO\.", ""
257 $content = $content -creplace "System\.", ""
258 $content = $content -creplace "Assistants\.", ""
259 $content = $content -creplace "Audio\.", ""
260 $content = $content -creplace "Batch\.", ""
261 $content = $content -creplace "Chat\.", ""
262 $content = $content -creplace "Common\.", ""
263 $content = $content -creplace "Containers\.", ""
264 $content = $content -creplace "Conversations\.", ""
265 $content = $content -creplace "Embeddings\.", ""
266 $content = $content -creplace "Evals\.", ""
267 $content = $content -creplace "Files\.", ""
268 $content = $content -creplace "FineTuning\.", ""
269 $content = $content -creplace "Graders\.", ""
270 $content = $content -creplace "Images\.", ""
271 $content = $content -creplace "Models\.", ""
272 $content = $content -creplace "Moderations\.", ""
273 $content = $content -creplace "Realtime\.", ""
274 $content = $content -creplace "Responses\.", ""
275 $content = $content -creplace "VectorStores\.", ""
276 $content = $content -creplace "Videos\.", ""
277
278 # Remove Diagnostics.DebuggerStepThrough attribute.
279 $content = $content -creplace ".*Diagnostics.DebuggerStepThrough.*\n", ""
280
281 # Remove ModelReaderWriterBuildable attributes.
282 $content = $content -creplace '\[ModelReaderWriterBuildable\(typeof\([^\)]+\)\)\]\s*', ''
283
284 # Remove internal APIs.
285 $content = $content -creplace " * internal.*`n", ""
286
287 # Remove IJsonModel/IPersistableModel interface method entries.
288 $content = $content -creplace " .*(IJsonModel|IPersistableModel).*`n", ""
289 # $content = $content -creplace " protected (virtual|override) .* (Json|Persistable)Model(Create|Write)Core.*`n", ""
290
291 # Other cosmetic simplifications.
292 $content = $content -creplace "partial class", "class"
293 $content = $content -creplace ".*private.*dummy.*`n", ""
294 $content = $content -creplace " { throw null; }", ";"
295 $content = $content -creplace " { }", ";"
296 $content = $content -creplace "Diagnostics.CodeAnalysis.Experimental", "Experimental"
297 $content = $content -creplace "Diagnostics.CodeAnalysis.SetsRequiredMembers", "SetsRequiredMembers"
298
299 Set-Content -Path $Destination -Value $content -NoNewline
300}
301
302$repoRootPath = Join-Path $PSScriptRoot .. -Resolve
303$projectPath = Join-Path $repoRootPath "src\OpenAI.csproj"
304
305Invoke-DotNetBuild -ProjectPath $projectPath
306
307$targetFramework = "netstandard2.0"
308$assemblyPath = Join-Path $repoRootPath "src\bin\Debug\$($targetFramework)\OpenAI.dll"
309$destination = Join-Path $repoRootPath "api\OpenAI.$($targetFramework).cs"
310Invoke-GenAPI -TargetFramework $targetFramework -AssemblyPath $assemblyPath -Destination $destination
311
312$targetFramework = "net8.0"
313$assemblyPath = Join-Path $repoRootPath "src\bin\Debug\$($targetFramework)\OpenAI.dll"
314$destination = Join-Path $repoRootPath "api\OpenAI.$($targetFramework).cs"
315Invoke-GenAPI -TargetFramework $targetFramework -AssemblyPath $assemblyPath -Destination $destination