PWSH 调用 AI

微软有一个 AISH 工具,但是命令行调用功能不完善。下面使用 https://github.com/chenxizhang/openai-powershell

安装

# 安装
Install-Module -Name code365scripts.openai -Scope CurrentUser

#更新
Update-Module -Name code365scripts.openai

# 删除
Uninstall-Module -Name code365scripts.openai

运行

gpt -api_key $key -model 'qwen-max' -endpoint 'https://ai.xxx.fun/v1' -system '你是一位通用 AI 助手,帮助人们解决日常生活上的问题' -prompt '{{from}}到{{to}}的距离是多长' -config @{temperature=0.3;max_tokens=1024} -context @{from='北京'; to='天津'}

-endpoint -- 指定 API 地址, 或使用环境变量 $env:OPENAI_API_ENDPOINT, 如果使用本地 OLLAMA , 则指定为 local 
-api_key -- 指定 API Key, 或使用环境变量 $env:OPENAI_API_KEY
-model -- 指定模型, 或使用环境变量 $env:OPENAI_API_MODEL
-system -- 传递系统角色, 可以为文件或是网址
-prompt -- 传递用户输入, 可以为文件或是网址
-context -- 传递上下文变量, 会替换 `prompt`  中的 {{xxx}} 变量
-config -- 传递配置参数
-functions -- 传递函数
-profile -- 指定配置, 使用 `~/.openai-powershell/profile.json` 中的配置
注:环境变量可使用 `SETX OPENAI_API_KEY xxx` 命令保存为系统变量, linux 系统中: https://www.cnblogs.com/amboyna/archive/2008/03/08/1096024.html

调用 functions

gpt -context @{location='北京'} -system '你是一位天气预报员, 能告诉我指定城市的天气' -prompt '请告诉我{{location}}的天气' -functions  getCurrentWeather
# 定义函数,注释的格式要与下面例子中的格式的一致
function getCurrentWeather {    
    <#
        .SYNOPSIS 
            获取指定地址的天气
        .DESCRIPTION 
            获取指定地址的天气
        .PARAMETER location 
            待获取天气的地址
#>
    param (        
        [string]$location
    )

    return "$($location)的天气是晴天, 30度, 西南风3级"    
}

使用 profile.json

如果有多个配置需要切换的话,创建 ~/.openai-powershell/profile.json 文件,然后调用时指定 profile 参数

 gpt  "你好" -profile kimi
{
    "profiles": [
        {
            "name": "openai",
            "api_key": "xxxx",
            "model": "gpt-4o"
        },
        {
            "name": "azure",
            "endpoint": "xxxx",
            "api_key": "xxxx",
            "model": "gpt-4o"
        },
        {
            "name": "kimi",
            "endpoint": "https://api.moonshot.cn/v1/chat/completions",
            "model": "moonshot-v1-32k",
            "api_key": "xxxxx"
        },
        {
            "name": "zhipu",
            "endpoint": "https://open.bigmodel.cn/api/paas/v4/chat/completions",
            "model": "glm-4",
            "api_key": "xxxx"
        },
        {
            "name": "advance",
            "endpoint": "xxxxx",
            "model": "gpt-4-turbo-chat-completions",
            "config": {
                "temperature": 0.1
            },
            "headers": {
                "X-AppName": "PowerShell",
                "X-ModelType": "{{model}}",
                "X-RequestId": "{{guid}}",
                "X-ScenarioGUID": "{{guid}}"
            },
            "auth": {
                "type": "aad",
                "aad": {
                    "tenantId": "xxxxx",
                    "clientId": "xxxx",
                    "redirectUri": "http://localhost:8400",
                    "scopes": [
                        "api://xxxx/access"
                    ]
                }
            }
        }
    ]
}

生成图像

还没有试 https://github.com/chenxizhang/openai-powershell/discussions/202

其它

还有一种 chat 模式, 以对话的模式运动, 试用时报错。没有再试。参数与 gpt 大部分相同。地址稍有区别 ,gpt 使用地址 https://api.xxx.cn/v1chat 使用地址 https://api.xxx.cn/v1/chat/completions

上一篇
下一篇