Skip to content

Commit

Permalink
Migrate to PSOpenAI (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
potatoqualitee authored Jun 29, 2024
1 parent 731350a commit 818e975
Show file tree
Hide file tree
Showing 23 changed files with 836 additions and 214 deletions.
10 changes: 3 additions & 7 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ name: Run Tests

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
run-tests:
Expand All @@ -18,7 +13,7 @@ jobs:

- name: Cache PowerShell modules
id: cache-psmodules
uses: potatoqualitee/psmodulecache@v5.2
uses: potatoqualitee/psmodulecache@v6.2
with:
modules-to-cache: PSOpenAI

Expand All @@ -27,7 +22,8 @@ jobs:
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
run: |
#$testResult = Invoke-Pester -Path "./tests/finetuna.Tests.ps1" -PassThru
Import-Module ./finetuna.psd1 -ErrorAction Stop
$testResult = Invoke-Pester -Path "./tests/finetuna.Tests.ps1" -PassThru
if ($testResult.FailedCount -gt 0) {
throw "$($testResult.FailedCount) tests failed."
}
264 changes: 264 additions & 0 deletions demo.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "pwsh"
},
"polyglot_notebook": {
"kernelName": "pwsh"
}
},
"outputs": [],
"source": [
"# Set the provider configuration\n",
"Import-Module ./ -Force\n",
"Set-TuneProvider @splat"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "pwsh"
},
"polyglot_notebook": {
"kernelName": "pwsh"
}
},
"outputs": [],
"source": [
"# Grab a tuning file\n",
"Get-ChildItem -Recurse totbot-tee-tune.jsonl -OutVariable totbot"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "pwsh"
},
"polyglot_notebook": {
"kernelName": "pwsh"
}
},
"outputs": [],
"source": [
"# Check the validity of the training file\n",
"$totbot | Test-TuneFile -OutVariable testFile"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "pwsh"
},
"polyglot_notebook": {
"kernelName": "pwsh"
}
},
"outputs": [],
"source": [
"# Upload the file\n",
"$totbot | Send-TuneFile -OutVariable upload"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "pwsh"
},
"polyglot_notebook": {
"kernelName": "pwsh"
}
},
"outputs": [],
"source": [
"# check out the uploaded file\n",
"Get-TuneFile -Id $upload.id -OutVariable file"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "pwsh"
},
"polyglot_notebook": {
"kernelName": "pwsh"
}
},
"outputs": [],
"source": [
"# Start a tuning job\n",
"Start-TuneJob -FileId $upload.id -OutVariable startjob"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "pwsh"
},
"polyglot_notebook": {
"kernelName": "pwsh"
}
},
"outputs": [],
"source": [
"# Wait for it to complete\n",
"Wait-TuneJob -Id $startjob.id -OutVariable job"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "pwsh"
},
"polyglot_notebook": {
"kernelName": "pwsh"
}
},
"outputs": [],
"source": [
"# Get that job\n",
"Get-TuneJob | Select-Object -First 1 -OutVariable job"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "pwsh"
},
"polyglot_notebook": {
"kernelName": "pwsh"
}
},
"outputs": [],
"source": [
"# Retrieve events for a specific fine-tuning job\n",
"$job | Get-TuneJobEvent -OutVariable tunevent"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "pwsh"
},
"polyglot_notebook": {
"kernelName": "pwsh"
}
},
"outputs": [],
"source": [
"# Retrieve a custom model\n",
"Get-TuneModel -Custom"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "pwsh"
},
"polyglot_notebook": {
"kernelName": "pwsh"
}
},
"outputs": [],
"source": [
"# Delete a custom model\n",
"Get-TuneModel -Custom | Select-Object -First 1 |\n",
"Remove-TuneModel -Confirm:$false"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "pwsh"
},
"polyglot_notebook": {
"kernelName": "pwsh"
}
},
"outputs": [],
"source": [
"# Retrieve your preferred default model\n",
"Get-TuneModelDefault -OutVariable defaultModel"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "pwsh"
},
"polyglot_notebook": {
"kernelName": "pwsh"
}
},
"outputs": [],
"source": [
"# Measure token count for a given text\n",
"Measure-TuneToken -InputObject \"Have some finetuna\" -Model cl100k_base"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "pwsh"
},
"polyglot_notebook": {
"kernelName": "pwsh"
}
},
"outputs": [],
"source": [
"# Get the current provider configuration\n",
"Get-TuneProvider"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"polyglot_notebook": {
"kernelInfo": {
"defaultKernelName": "csharp",
"items": [
{
"aliases": [],
"name": "csharp"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
3 changes: 2 additions & 1 deletion finetuna.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
'Set-TuneProvider',
'Start-TuneJob',
'Stop-TuneJob',
'Test-TuneFile'
'Test-TuneFile',
'Wait-TuneJob'
)

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
Expand Down
23 changes: 6 additions & 17 deletions finetuna.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if (-not (Get-Module PSOpenAI)) {
}
# Get PSOpenAI's private functions
$modulepath = Get-Module -Name PSOpenAI -ListAvailable | Select-Object -First 1
$privatedir = Join-Path -Path $modulepath.ModuleBase -ChildPath private
$privatedir = Join-Path -Path $modulepath.ModuleBase -ChildPath Private
function ValidateModelName {
param([string]$ModelName)
if ($ModelName -notin $script:ValidModels -and -not $ModelName.StartsWith("ft:")) {
Expand All @@ -24,6 +24,7 @@ function Import-ModuleFile {
else { $ExecutionContext.InvokeCommand.InvokeScript($false, ([scriptblock]::Create([io.file]::ReadAllText($Path))), $null, $null) }
}

# import internal functions from PSOpenAI bc I have changes
foreach ($file in (Get-ChildItem -Path $privatedir -Filter *.ps1)) {
. Import-ModuleFile -Path $file.FullName
}
Expand All @@ -38,7 +39,6 @@ foreach ($function in (Get-ChildItem "$ModuleRoot\public" -Filter "*.ps1" -Recur
. Import-ModuleFile -Path $function.FullName
}

$PSDefaultParameterValues["*:ApiKey"] = $env:OpenAIKey
$PSDefaultParameterValues["*:NoTypeInformation"] = $true

Set-Alias -Name Invoke-TunedChat -Value Invoke-TuneChat
Expand Down Expand Up @@ -71,7 +71,9 @@ if (-not (Test-Path -Path $script:configdir)) {

$configFile = Join-Path -Path $script:configdir -ChildPath config.json

if (Test-Path -Path $configFile) {
$apiKey = (Get-OpenAIContext).ApiKey

if (-not $apiKey -and (Test-Path -Path $configFile)) {
$persisted = Get-Content -Path $configFile -Raw | ConvertFrom-Json
$splat = @{}
if ($persisted.ApiKey) { $splat.ApiKey = $persisted.ApiKey }
Expand All @@ -82,17 +84,4 @@ if (Test-Path -Path $configFile) {
if ($persisted.AuthType) { $splat.AuthType = $persisted.AuthType }
if ($persisted.Organization) { $splat.Organization = $persisted.Organization }
$null = Set-TuneProvider @splat
}


# get context values to pass to Get-OpenAIAPIParameter
$context = Get-OpenAIContext
$script:bigparms = @{
ApiKey = $context.ApiKey
AuthType = $context.AuthType
Organization = $context.Organization
ApiBase = $context.ApiBase
ApiVersion = $context.ApiVersion
TimeoutSec = $context.TimeoutSec
MaxRetryCount = $context.MaxRetryCount
}
}
4 changes: 2 additions & 2 deletions private/Get-MaskedKeyString.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ function Get-MaskedKeyString {
}

# this is a modified function that surfaces the API key that PSOpenAI will use
function Get-APIKey {
function Get-ApiKey {
[CmdletBinding()]
[OutputType([securestring])]
Param(
[Parameter(Position = 0)]
[AllowNull()]
[System.Management.Automation.PSObjectPropertyDescriptor]$ApiKey,
[psobject]$ApiKey,
[switch]$PlainText
)

Expand Down
Loading

0 comments on commit 818e975

Please sign in to comment.