openai-Powershell 模块重要升级,和一点技术细节

这两天在更新 openai-powershell 模块,目前最新的3.0.0.6 包含了一个很重要的更新,就是能支持AAD来做身份验证,而不仅仅是用apikey的方式,这主要针对Azure OpenAI这种服务,提供符合企业级安全和合规要求的能力。

a94455f33fcf50e1f612d424858b5427.png

要使用这个新功能,你可以通过添加一个或多个环境配置到一个json文件中,下面是一个真实的例子。

{
    "profiles": [
        {
            "name": "substrateLLMs",
            "endpoint": "xxxx",
            "model": "xxxxx",
            "config": {
                "temperature": 0.1
            },
            "headers": {
                "X-AppName": "PowerShell",
                "X-ModelType": "{{model}}",
                "X-RequestId": "{{guid}}",
                "X-ScenarioGUID": "{{guid}}"
            },
            "auth": {
                "type": "aad",
                "aad": {
                    "tenantId": "xxxx",
                    "clientId": "xxxx",
                    "redirectUri": "xxx",
                    "scopes": [
                        "xxxx"
                    ]
                }
            }
        },
        {
            "name": "test",
            "model": "gpt-4-turbo",
            "config": {
                "temperature": 0.1
            },
            "headers": {
                "X-AppName": "PowerShell",
                "X-ModelType": "{{model}}",
                "X-RequestId": "{{guid}}",
                "X-ScenarioGUID": "{{guid}}"
            },
            "auth": {
                "type": "aad",
                "aad": {
                    "clientId": "ab5d499b-4c67-493d-804b-377b8b0e629f",
                    "tenantId": "91dde955-43a9-40a9-a406-694cffb04f28",
                    "redirectUri": "msalab5d499b-4c67-493d-804b-377b8b0e629f://auth",
                    "scopes": [
                        ".default"
                    ]
                }
            }
        }
    ]
}

然后我可以通过如下的方式来调用,这就让我们这个工具可以兼容任何的LLMs了。

chat -environment substrateLLMs

在开发过程中遇到一些奇怪的问题,百思不得其解,但是后来还是解决了,这里做一点记录。

  1. Powershell 5.1 对于Json 文件的处理

    PowerShell 5.1 在ConvertFrom-Json的处理中有不少问题,首先它要求 Json 文件必须是有一个根元素的,而不能直接就是一个数组的定义。这一点我还能忍,但它把Json 会转换为一个PSObject,而不是哈希表,在很多代码中都会有问题。我尝试写过一个把PSObject转换为哈希表的函数,但效果不好,直到我找到了这个模块(https://www.powershellgallery.com/packages/ConvertTo-Hashtable/0.1),整个世界又清净多了。

# 参考 
function ConvertTo-Hashtable {
    [CmdletBinding()]
    [OutputType('hashtable')]
    param (
        [Parameter(ValueFromPipeline)]
        $InputObject
    )
    process {
        ## Return null if the input is null. This can happen when calling the function
        ## recursively and a property is null
        if ($null -eq $InputObject) {
            return $null
        }


        ## Check if the input is an array or collection. If so, we also need to convert
        ## those types into hash tables as well. This function will convert all child
        ## objects into hash tables (if applicable)
        if ($InputObject -is [System.Collections.IEnumerable] -and $InputObject -isnot [string]) {
            $collection = @(
                foreach ($object in $InputObject) {
                    ConvertTo-Hashtable -InputObject $object
                }
            )
            ## Return the array but don't enumerate it because the object may be pretty complex
            Write-Output -NoEnumerate $collection
        }
        elseif ($InputObject -is [psobject]) {
            $hash = @{}
            foreach ($property in $InputObject.PSObject.Properties) {
                $hash[$property.Name] = ConvertTo-Hashtable -InputObject $property.Value
            }
            $hash
        }
        else {
            ## If the object isn't an array, collection, or other object, it's already a hash table
            ## So just return it.
            $InputObject
        }
    }
}

在PowerShell 7中,ConvertFrom-Json 已经自动能转换为哈希表了,请参考下面的例子。

e675c056e399793ec89a8c3cdc865802.png

2. 有时候我们的代码需要依赖别人的模块,你当然可以要求用户预先安装那些模块,但这并不容易,尤其是假设这种依赖只发生在某些逻辑成立的情况下,我们可能需要按需安装模块。

下面是我写的一个代码片段,可以参考。

function Confirm-DependencyModule {
    param(
        [string]$ModuleName
    )


    if (-not (Get-Module -ListAvailable -Name $ModuleName)) {
        Install-Module -Name $ModuleName `
            -Force -AllowClobber `
            -Scope CurrentUser `
            -Repository PSGallery
    }
}

3. 另外我还经常需要合并两个哈希表。下面我是写的代码片段。

function Merge-Hashtable {
    [CmdletBinding()]
    param (
        [hashtable]$table1,
        [hashtable]$table2
    )


    foreach ($key in $table2.Keys) {
        if ($table1.ContainsKey($key)) {
            $table1[$key] = $table2[$key]  
            # 用第二个hashtable的值覆盖第一个hashtable的值
        }
        else {
            $table1.Add($key, $table2[$key])
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值