Skip to content

Commit

Permalink
Reorganizado e adicionado novos providers
Browse files Browse the repository at this point in the history
  • Loading branch information
rrg92 committed Jul 25, 2024
1 parent 7f22908 commit 92031e8
Show file tree
Hide file tree
Showing 6 changed files with 719 additions and 74 deletions.
129 changes: 83 additions & 46 deletions powershai/powershai.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,9 @@ if(!$Global:POWERSHAI_SETTINGS){
$Global:POWERSHAI_SETTINGS = @{
provider = 'openai' #ollama, huggingface
baseUrl = $null
providers = @{
grok = @{
RequireToken = $true
DefaultUrl = "https://api.groq.com/openai/v1"
DefaultModel = "llama-3.1-70b-versatile"
}

ollama = @{
RequireToken = $false
DefaultUrl = "http://localhost:11434/v1"
ApiUrl = "http://localhost:11434/api"
DefaultModel = $null
}


}

#providers settings!
providers = @{}
}
}

Expand Down Expand Up @@ -355,6 +342,10 @@ function PowerShaiProviderFunc {
$FuncPrefix = $POWERSHAI_SETTINGS.funcbase;
$FullFuncName = $FuncPrefix + $FuncName;

if(!$FuncParams){
$FuncParams = @{}
}

if(Get-Command $FullFuncName -EA SilentlyContinue){
& $FullFuncName @FuncParams
} else {
Expand All @@ -365,45 +356,91 @@ function PowerShaiProviderFunc {

}

# Configura o default model!
function Set-AiDefaultModel {
[CmdletBinding()]
param($model)
# function
function GetProviderData {
param($name, $Key)

$provider = $POWERSHAI_SETTINGS.providers[$name];

$UserDefined = $provider.UserDefined;

$SelectedModel = Get-AiModels | ? { $_.name -eq $model }
$UserSetting = $UserDefined[$Key];
$DefaultSetting = $provider[$key];

if(!$SelectedModel){
throw "POWERSHAI_SET_MODEL: Model NotFound $Model";
if($UserDefined.contains($Key)){
return $UserSetting
}

return $DefaultSetting
}

function SetProviderData {
param($name, $Key, $value)

$provider = $POWERSHAI_SETTINGS.providers[$name];

$UserDefined = $provider.UserDefined;
$UserDefined[$key] = $value;
}

function GetCurrentProviderData {
param($key)

$Provider = Get-AiCurrentProvider
$Provider.DefaultModel = $model;
GetProviderData -name $Provider.name -key $key
}

function SetCurrentProviderData {
param($key, $value)
$Provider = Get-AiCurrentProvider
SetProviderData -name $provider.name -key $key -value $value;
}

# obtem a lista de providers !
function Get-AiProviders {
param()

@($POWERSHAI_SETTINGS.providers.keys) | %{
$Provider = $POWERSHAI_SETTINGS.providers[$_];
$ProviderInfo = [PSCustomObject](@{name = $_} + $Provider.info)

$ProviderInfo
}
}


# Get all models!
function Get-AiModels {
[CmdletBinding()]
param()
$AiProvider = $POWERSHAI_SETTINGS.provider;
PowerShaiProviderFunc "GetModels"
}


# Configura o default model!
function Set-AiDefaultModel {
[CmdletBinding()]
param($model)

$models = $null
#TODO: Migrar tudo para PowerShaiProviderFunc
$ElegibleModels = @(Get-AiModels | ? { $_.name -like $model+"*" })
$ExactModel = $ElegibleModels | ?{ $_.name -eq $model }

if($AiProvider -eq 'ollama'){
$Models = Get-OllamaTags
}
elseif($AiProvider -eq "maritalk"){
$Models = PowerShaiProviderFunc "GetModels"
write-verbose "ElegibleModels: $($ElegibleModels|out-string)"

if($ElegibleModels.count -ge 2 -and !$ExactModel){
throw "POWERSHAI_DEFAULTMODEL_AMBIGUOUS: Existem vários modelos com o mesmo nome $model e nenhum com este nome exato. Seja mais específico."
}
else {
$Models = (InvokeOpenai 'models' -m 'GET').data
$Models | Add-Member -Type noteproperty -Name name -Value $null
$Models | %{ $_.name = $_.id }

$model = $ElegibleModels[0].name

if($ElegibleModels.count -eq 1 -and !$ExactModel){
write-warning "Modelo exato $model nao encontrado. Usnado o único mais próximo: $model"
}

return $Models | select ;
SetCurrentProviderData DefaultModel $model;
}


# Funcao generica de chat. Segue a especificacao da OpenAI
function Get-AiChat {
[CmdletBinding()]
Expand All @@ -429,7 +466,7 @@ function Get-AiChat {
$FuncParams = $PsBoundParameters;

if(!$model){
$FuncParams['model'] = $Provider.DefaultModel;
$FuncParams['model'] = GetCurrentProviderData DefaultModel
}

PowerShaiProviderFunc "Chat" -FuncParams $FuncParams;
Expand Down Expand Up @@ -2213,8 +2250,8 @@ foreach($File in $ProvidersFiles){

$ProviderData = . $File.FullName;

if($ProviderData -eq $null){
$ProviderData = @{};
if(!$ProviderData.info.desc){
throw "POWERSHAI_PROVIDER_NODESC: Provider $ProviderName must have desc"
}

if($ProviderData -isnot [hashtable]){
Expand All @@ -2224,17 +2261,17 @@ foreach($File in $ProvidersFiles){
$ProviderData.name = $ProviderName;
$ExistingProvider = $POWERSHAI_SETTINGS.providers[$ProviderName];

$CurrentDefaultModel = $null

if($ExistingProvider){
$CurrentDefaultModel = $ExistingProvider.DefaultModel;
$UserDefinedSettings = $ExistingProvider.UserDefined;
}

$POWERSHAI_SETTINGS.providers[$ProviderName] = $ProviderData

if($CurrentDefaultModel){
$ProviderData.DefaultModel = $CurrentDefaultModel
if(!$UserDefinedSettings){
$UserDefinedSettings = @{};
}

$POWERSHAI_SETTINGS.providers[$ProviderName] = $ProviderData
$ProviderData.UserDefined = $UserDefinedSettings;
}

Export-ModuleMember -Function * -Alias * -Cmdlet *
Loading

0 comments on commit 92031e8

Please sign in to comment.