From 7d9695686abcec079bd983e423b64525e4250085 Mon Sep 17 00:00:00 2001 From: kurokobo <2920259+kurokobo@users.noreply.github.com> Date: Thu, 28 Nov 2024 22:57:07 +0900 Subject: [PATCH] release: 0.1.0 --- .gitignore | 2 + Docs/README.ja.md | 698 +++++++++++++++++++- Docs/README.md | 698 +++++++++++++++++++- PSDify.psd1 | Bin 2776 -> 3772 bytes Private/Convert-UnixTimeToLocalDateTime.ps1 | 3 + Public/Add-DifyDocument.ps1 | 166 +++++ Public/Add-DifyFile.ps1 | 89 +++ Public/Connect-Dify.ps1 | 2 +- Public/Disconnect-Dify.ps1 | 26 + Public/Export-DifyApp.ps1 | 1 + Public/Get-DifyApp.ps1 | 9 +- Public/Get-DifyAppAPIKey.ps1 | 35 + Public/Get-DifyDSLContent.ps1 | 16 + Public/Get-DifyDocument.ps1 | 91 +++ Public/Get-DifyDocumentIndexingStatus.ps1 | 93 +++ Public/Get-DifyKnowledge.ps1 | 83 +++ Public/Get-DifyKnowledgeTag.ps1 | 9 + Public/Get-DifyModel.ps1 | 2 +- Public/Get-DifySystemModel.ps1 | 1 + Public/Get-DifyTag.ps1 | 9 +- Public/Get-DifyVersion.ps1 | 1 + Public/Initialize-Dify.ps1 | 3 + Public/Invoke-DifyRestMethod.ps1 | 68 +- Public/New-DifyAppAPIKey.ps1 | 32 + Public/New-DifyKnowledge.ps1 | 59 ++ Public/New-DifyModel.ps1 | 5 +- Public/Remove-DifyAppAPIKey.ps1 | 34 + Public/Remove-DifyKnowledge.ps1 | 34 + Public/Send-DifyChatMessage.ps1 | 86 +++ Public/Set-DifyDSLContent.ps1 | 27 + Public/Set-DifySystemModel.ps1 | 18 +- Public/Set-PSDifyConfiguration.ps1 | 17 + README.ja.md | 50 +- README.md | 49 +- 34 files changed, 2461 insertions(+), 55 deletions(-) create mode 100644 Public/Add-DifyDocument.ps1 create mode 100644 Public/Add-DifyFile.ps1 create mode 100644 Public/Disconnect-Dify.ps1 create mode 100644 Public/Get-DifyAppAPIKey.ps1 create mode 100644 Public/Get-DifyDSLContent.ps1 create mode 100644 Public/Get-DifyDocument.ps1 create mode 100644 Public/Get-DifyDocumentIndexingStatus.ps1 create mode 100644 Public/Get-DifyKnowledge.ps1 create mode 100644 Public/Get-DifyKnowledgeTag.ps1 create mode 100644 Public/New-DifyAppAPIKey.ps1 create mode 100644 Public/New-DifyKnowledge.ps1 create mode 100644 Public/Remove-DifyAppAPIKey.ps1 create mode 100644 Public/Remove-DifyKnowledge.ps1 create mode 100644 Public/Send-DifyChatMessage.ps1 create mode 100644 Public/Set-DifyDSLContent.ps1 create mode 100644 Public/Set-PSDifyConfiguration.ps1 diff --git a/.gitignore b/.gitignore index 9eb6bd8..2af17e8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ Dist/ +Demo/ +Logs/ DSLs/ diff --git a/Docs/README.ja.md b/Docs/README.ja.md index d81a973..ad070c5 100644 --- a/Docs/README.ja.md +++ b/Docs/README.ja.md @@ -1,3 +1,697 @@ -# PSDify に含まれるコマンドレットの一覧 +# PSDify のコマンドレット一覧 -TODO +PSDify には次のコマンドレットが含まれます。 + +精緻なヘルプは用意していないので、コマンドレットの使い方は、このページの使用例を参照してください。 + +| カテゴリ | コマンドレット | 説明 | +| --- | --- | --- | +| 認証 | [Connect-Dify](#connect-dify) | Dify にパスワード認証またはメール認証によるログイン処理を要求し、PSDify の他のコマンドレットによる操作が行えるようにします。 | +| 認証 | [Disconnect-Dify](#disconnect-dify) | Dify にログアウト処理を要求します。 | +| アプリの管理 | [Get-DifyApp](#get-difyapp) | アプリの情報を取得します。 | +| アプリの管理 | [Remove-DifyApp](#remove-difyapp) | アプリを削除します。 | +| アプリの管理 | [Import-DifyApp](#import-difyapp) | アプリをローカルの DSL ファイルからインポートします。 | +| アプリの管理 | [Export-DifyApp](#export-difyapp) | アプリを DSL ファイルとしてエクスポートします。 | +| アプリの管理 | [Get-DifyDSLContent](#get-difydslcontent--set-difydslcontent) | DSL ファイルの内容を文字列として取得します。 | +| アプリの管理 | [Set-DifyDSLContent](#get-difydslcontent--set-difydslcontent) | 文字列を DSL ファイルとして書き込みます。 | +| アプリの管理 | [Get-DifyAppAPIKey](#get-difyappapikey) | アプリの API キーを取得します。 | +| アプリの管理 | [New-DifyAppAPIKey](#new-difyappapikey) | アプリの API キーを作成します。 | +| アプリの管理 | [Remove-DifyAppAPIKey](#remove-difyappapikey) | アプリの API キーを削除します。 | +| ナレッジの管理 | [Get-DifyKnowledge](#get-difyknowledge) | ナレッジの情報を取得します。 | +| ナレッジの管理 | [New-DifyKnowledge](#new-difyknowledge) | 空のナレッジを作成します。 | +| ナレッジの管理 | [Remove-DifyKnowledge](#remove-difyknowledge) | ナレッジを削除します。 | +| ナレッジの管理 | [Get-DifyDocument](#get-difydocument) | ナレッジ内のドキュメントの情報を取得します。 | +| ナレッジの管理 | [Add-DifyDocument](#add-difydocument) | ナレッジにドキュメントをアップロードします。 | +| メンバの管理 | [Get-DifyMember](#get-difymember) | ワークスペースのメンバの情報を取得します。 | +| メンバの管理 | [New-DifyMember](#new-difymember) | ワークスペースに新しいメンバを追加(招待)します。 | +| メンバの管理 | [Remove-DifyMember](#remove-difymember) | ワークスペースからメンバを削除します。 | +| メンバの管理 | [Set-DifyMemberRole](#set-difymemberrole) | ワークスペースのメンバのロールを変更します。 | +| モデルの管理 | [Get-DifyModel](#get-difymodel) | ワークスペースのモデルの情報を取得します。 | +| モデルの管理 | [New-DifyModel](#new-difymodel) | ワークスペースに新しいモデルを追加します。 | +| モデルの管理 | [Get-DifySystemModel](#get-difysystemmodel) | ワークスペースのシステムモデルの情報を取得します。 | +| モデルの管理 | [Set-DifySystemModel](#set-difysystemmodel) | ワークスペースのシステムモデルを変更します。 | +| タグの管理 | [Get-DifyTag](#get-difytag) | タグの情報を取得します。 | +| タグの管理 | [Get-DifyAppTag](#get-difyapptag) | アプリ用のタグの情報を取得します。 | +| タグの管理 | [Get-DifyKnowledgeTag](#get-difyknowledgetag) | ナレッジ用のタグの情報を取得します。 | +| 情報取得 | [Get-DifyVersion](#get-difyversion) | Dify のバージョン情報を取得します。 | +| 情報取得 | [Get-DifyProfile](#get-difyprofile) | 認証したアカウントの情報を取得します。 | +| インスタンスの初期設定 | [Initialize-Dify](#initialize-dify) | 管理者アカウントを作成します(コミュニティ版のみ)。 | +| その他 | [Set-PSDifyConfiguration](#set-psdifyconfiguration) | HTTPS 接続時の証明書の検証を無効化できます。 | +| その他 | [Add-DifyFile](#add-difyfile) | ファイルをアップロードします。 | +| その他 | [Get-DifyDocumentIndexingStatus](#get-difydocumentindexingstatus) | ドキュメントのインデキシング状況を取得します。 | +| その他 | [Invoke-DifyRestMethod](#invoke-difyrestmethod) | REST API を呼び出します。 | +| チャットの操作 | [Send-DifyChatMessage](#send-difychatmessage) | アプリにチャットメッセージを送信します。 | + +## ✨ 認証 + +### Connect-Dify + +Dify にパスワード認証またはメール認証によるログイン処理を要求し、PSDify の他のコマンドレットによる操作が行えるようにします。 + +> [!NOTE] +> 認証が成功すると、次の環境変数が自動で追加されます。 +> +> ```powershell +> $env:PSDIFY_CONSOLE_TOKEN = "..." +> $env:PSDIFY_CONSOLE_REFRESH_TOKEN = "..." +> ``` + +#### メールによる認証(クラウド版向け) + +SSO により認証したアカウントでも、紐づけられたメールアドレスを使ってメール認証が行えます。 + +```powershell +# メールによる認証(実行後、メールで届いたコードを手動で入力する) +Connect-Dify -AuthMethod "Code" -Email "dify@example.com" +``` + +> [!NOTE] +> 引数は、環境変数を使うと省略できます。 +> +> ```powershell +> $env:PSDIFY_URL = "https://cloud.dify.ai" +> $env:PSDIFY_AUTH_METHOD = "Code" +> $env:PSDIFY_EMAIL = "dify@example.com" +> ``` + +#### パスワードによる認証(コミュニティ版向け) + +> [!NOTE] +> コミュニティ版で HTTPS 化に自己署名証明書を使っている場合は、`Connect-Dify` の実行前に証明書の検証を無効にする必要があります。 +> +> ```powershell +> # 証明書の検証を無効にする +> Set-PSDifyConfiguration -IgnoreSSLVerification $true +> ``` + +```powershell +# パスワードによる認証(実行後、パスワードを手動で入力する) +Connect-Dify -Server "https://dify.example.com" -Email "dify@example.com" + +# パスワードによる認証(パスワードを事前に指定する) +$DifyPassword = ConvertTo-SecureString -String "AwesomeDify123!" -AsPlainText -Force +Connect-Dify -Server "https://dify.example.com" -Email "dify@example.com" -Password $DifyPassword +``` + +> [!NOTE] +> 引数は、環境変数を使うと省略できます。 +> +> ```powershell +> $env:PSDIFY_URL = "https://dify.example.com" +> $env:PSDIFY_AUTH_METHOD = "Password" +> $env:PSDIFY_EMAIL = "dify@example.com" +> $env:PSDIFY_PASSWORD = "AwesomeDify123!" +> $env:PSDIFY_DISABLE_SSL_VERIFICATION = "true" # 自己署名証明書を使っている場合 +> ``` + +### Disconnect-Dify + +Dify にログアウト処理を要求します。 + +```powershell +# ログアウト(発行済みトークンの無効化) +Disconnect-Dify + +# 強制ログアウト(ログアウト処理の成否にかかわらずローカルの環境変数を削除) +Disconnect-Dify -Force +``` + +## ✨ アプリの管理 + +### Get-DifyApp + +アプリの情報を取得します。 + +```powershell +# アプリの取得(全て) +Get-DifyApp + +# アプリの取得(ID で指定) +Get-DifyApp -Id "..." + +# アプリの取得(名前で指定、完全一致) +Get-DifyApp -Name "..." + +# アプリの取得(名前で指定、部分一致) +Get-DifyApp -Search "..." + +# アプリの取得(モードで指定) +## モード: "chat", "workflow", "agent-chat", "channel", "all" +Get-DifyApp -Mode "chat" + +# アプリの取得(タグで指定、複数指定可) +Get-DifyApp -Tags "...", "..." + +# アプリの取得(組み合わせ例) +Get-DifyApp -Name "..." -Mode "chat" +``` + +### Remove-DifyApp + +アプリを削除します。 + +```powershell +# アプリの削除(Get-DifyApp から直接パイプで指定) +Get-DifyApp -Name "..." | Remove-DifyApp + +# アプリの削除(Get-DifyApp で取得した結果で指定) +$AppsToBeRemoved = Get-DifyApp -Name "..." +Remove-DifyApp -App $AppsToBeRemoved +``` + +### Import-DifyApp + +アプリをローカルの DSL ファイルからインポートします。 + +```powershell +# アプリのインポート(ファイルパスで指定、ワイルドカード可、複数指定可) +Import-DifyApp -Path "DSLs/*.yml" +Import-DifyApp -Path "DSLs/demo1.yml", "DSLs/demo2.yml" + +# アプリのインポート(Get-Item や Get-ChildItem からパイプで指定) +Get-Item -Path "DSLs/*.yml" | Import-DifyApp + +# アプリのインポート(Get-ChildItem で取得した結果で指定) +$DSLFiles = Get-ChildItem -Path "DSLs/*.yml" +Import-DifyApp -Item $DSLFiles +``` + +### Export-DifyApp + +アプリを DSL ファイルとしてエクスポートします。デフォルトでは `DSLs` ディレクトリに保存されます。 + +```powershell +# アプリのエクスポート(Get-DifyApp から直接パイプで指定) +Get-DifyApp | Export-DifyApp + +# アプリのエクポート(Get-DifyApp で取得した結果で指定) +$AppsToBeExported = Get-DifyApp +Export-DifyApp -App $AppsToBeExported + +# アプリのエクスポート(保存先のディレクトリを変更) +Get-DifyApp | Export-DifyApp -Path "./path/to/your/directory" + +# アプリのエクスポート(シークレットを含める) +Get-DifyApp | Export-DifyApp -IncludeSecret +``` + +### Get-DifyDSLContent / Set-DifyDSLContent + +DSL ファイルの内容を文字列として取得したり、文字列を DSL ファイルとして書き込んだりします。 + +既存の DSL ファイルの一部を書き換えて別のファイルとして保存する場合などに便利です。DSL ファイルを一貫して BOM なしの UTF-8 で扱うために用意しています。 + +```powershell +# DSL ファイルの内容の取得 +$RawContent = Get-DifyDSLContent -Path "DSLs/old.yml" + +# DSL ファイル中の古いナレッジの ID を新しいナレッジ ID に書き換えて別の DSL ファイルとして保存 +$RawContent -replace "8b960203-299d-4345-b953-3308663dd790", "574d9556-189a-4d35-b296-09231b859667" | Set-DifyDSLContent -Path "DSLs/new.yml" +``` + +### Get-DifyAppAPIKey + +アプリの API キーを取得します。 + +```powershell +# API キーの取得(Get-DifyApp から直接パイプで指定) +Get-DifyApp -Name "..." | Get-DifyAppAPIKey + +# API キーの取得(Get-DifyApp で取得した結果で指定) +$AppsToGetAPIKey = Get-DifyApp -Name "..." +Get-DifyAppAPIKey -App $AppsToGetAPIKey +``` + +### New-DifyAppAPIKey + +アプリの API キーを作成します。 + +```powershell +# API キーの作成(Get-DifyApp から直接パイプで指定) +Get-DifyApp -Name "..." | New-DifyAppAPIKey + +# API キーの作成(Get-DifyApp で取得した結果で指定) +$AppsToCreateAPIKey = Get-DifyApp -Name "..." +``` + +### Remove-DifyAppAPIKey + +アプリの API キーを削除します。 + +```powershell +# API キーの削除(Get-DifyAppAPIKey から直接パイプで指定) +Get-DifyApp -Name "..." | Get-DifyAppAPIKey | Remove-DifyAppAPIKey + +# API キーの削除(Get-DifyAppAPIKey で取得した結果で指定) +$APIKeysToBeRemoved = Get-DifyApp -Name "..." | Get-DifyAppAPIKey +Remove-DifyAppAPIKey -APIKey $APIKeysToBeRemoved +``` + +## ✨ ナレッジの管理 + +### Get-DifyKnowledge + +ナレッジの情報を取得します。 + +```powershell +# ナレッジの取得(全て) +Get-DifyKnowledge + +# ナレッジの取得(ID で指定) +Get-DifyKnowledge -Id "..." + +# ナレッジの取得(名前で指定、完全一致) +Get-DifyKnowledge -Name "..." + +# ナレッジの取得(名前で指定、部分一致) +Get-DifyKnowledge -Search "..." + +# ナレッジの取得(タグで指定、複数指定可) +Get-DifyKnowledge -Tags "...", "..." +``` + +### New-DifyKnowledge + +空のナレッジを作成します。 + +```powershell +# ナレッジの作成 +New-DifyKnowledge -Name "My New Knowledge" + +# ナレッジの作成(説明文を追加) +New-DifyKnowledge -Name "My New Knowledge" -Description "This is a new knowledge." +``` + +### Remove-DifyKnowledge + +ナレッジを削除します。 + +```powershell +# ナレッジの削除(Get-DifyKnowledge から直接パイプで指定) +Get-DifyKnowledge -Name "..." | Remove-DifyKnowledge + +# ナレッジの削除(Get-DifyKnowledge で取得した結果で指定) +$KnowledgesToBeRemoved = Get-DifyKnowledge -Name "..." +Remove-DifyKnowledge -Knowledge $KnowledgesToBeRemoved +``` + +### Get-DifyDocument + +ナレッジ内のドキュメントの情報を取得します。 + +```powershell +# ドキュメントの取得(全て、Get-Knowledge から直接パイプで指定) +Get-DifyKnowledge -Name "..." | Get-DifyDocument + +# ドキュメントの取得(全て、Get-DifyKnowledge で取得した結果で指定) +$Knowledge = Get-DifyKnowledge -Name "..." +Get-DifyDocument -Knowledge $Knowledge + +# ドキュメントの取得(ID で指定) +Get-DifyKnowledge -Name "..." | Get-DifyDocument -Id "..." + +# ドキュメントの取得(名前で指定、完全一致) +Get-DifyKnowledge -Name "..." | Get-DifyDocument -Name "..." + +# ドキュメントの取得(名前で指定、部分一致) +Get-DifyKnowledge -Name "..." | Get-DifyDocument -Search "..." +``` + +### Add-DifyDocument + +ナレッジにドキュメントをアップロードします。デフォルトで、自動、高品質、システムモデル、ベクトル検索の設定が適用されます。 + +現時点では細かい設定変更には対応していません。 + +```powershell +# 前提: アップロードするナレッジを取得 +$Knowledge = Get-DifyKnowledge -Name "My New Knowledge" + +# ドキュメントのアップロード(ファイルパスで指定、ワイルドカード可、複数指定可) +Add-DifyDocument -Knowledge $Knowledge -Path "Docs/*.md" + +# ドキュメントのアップロード(Get-Item や Get-ChildItem からパイプで指定) +Get-Item -Path "Docs/*.md" | Add-DifyDocument -Knowledge $Knowledge + +# ドキュメントのアップロード(任意のモデルを利用) +$EmbeddingModel = Get-DifyModel -Provider "openai" -Name "text-embedding-3-small" +Add-DifyDocument -Knowledge $Knowledge -Path "Docs/*.md" -IndexMode "high_quality" -Model $EmbeddingModel + +# ドキュメントのアップロード(エコノミーモードを利用) +Add-DifyDocument -Knowledge $Knowledge -Path "Docs/*.md" -IndexMode "economy" + +# インデキシングの完了を待つ +Add-DifyDocument -Knowledge $Knowledge -Path "Docs/*.md" -Wait + +# 待つ時間を変更 +Add-DifyDocument -Knowledge $Knowledge -Path "Docs/*.md" -Wait -Interval 10 -Timeout 600 +``` + +## ✨ メンバの管理 + +### Get-DifyMember + +ワークスペースのメンバの情報を取得します。 + +```powershell +# メンバの取得(全て) +Get-DifyMember + +# メンバの取得(ID で指定) +Get-DifyMember -Id "..." + +# メンバの取得(名前で指定) +Get-DifyMember -Name "..." + +# メンバの取得(メールアドレスで指定) +Get-DifyMember -Email "..." +``` + +### New-DifyMember + +ワークスペースに新しいメンバを追加(招待)します。 + +```powershell +# メンバの招待 +## ロール: "admin", "editor", "normal" +New-DifyMember -Email "dify@example.com" -Role "normal" -Language "en-US" +``` + +### Remove-DifyMember + +ワークスペースからメンバを削除します。 + +```powershell +# メンバの削除(Get-DifyMember から直接パイプで指定) +Get-DifyMember -Name "..." | Remove-DifyMember + +# メンバの削除(Get-DifyMember で取得した結果で指定) +$MembersToBeRemoved = Get-DifyMember -Name "..." +Remove-DifyMember -Member $MembersToBeRemoved +``` + +### Set-DifyMemberRole + +ワークスペースのメンバのロールを変更します。 + +```powershell +# メンバのロールの変更(Get-DifyMember から直接パイプで指定) +Get-DifyMember -Name "..." | Set-DifyMemberRole -Role "editor" + +# メンバのロールの変更(Get-DifyMember で取得した結果で指定) +$MembersToBeChanged = Get-DifyMember -Name "..." +Set-DifyMemberRole -Member $MembersToBeChanged -Role "editor" +``` + +## ✨ モデルの管理 + +### Get-DifyModel + +ワークスペースのモデルの情報を取得します。 + +```powershell +# モデルの取得(全て) +Get-DifyModel + +# モデルの取得(プロバイダで指定) +Get-DifyModel -Provider "..." + +# モデルの取得(モデルの種類で指定) +## モデルの種類: "predefined", "customizable" +Get-DifyModel -From "..." + +# モデルの取得(モデルの名前で指定) +Get-DifyModel -Name "..." + +# モデルの取得(モデルのタイプで指定) +## モデルタイプ: "llm", "text-embedding", "speech2text", "moderation", "tts", "rerank" +Get-DifyModel -Type "..." + +# モデルの取得(組み合わせ例) +Get-DifyModel -Provider "..." -Type "llm" +``` + +### New-DifyModel + +ワークスペースに新しいモデルを追加します。`Credential` で渡す必要のある情報はプロバイダやモデルに依存するため、ブラウザの開発者ツールなどで実際の HTTP リクエストを確認してください。 + +```powershell +# モデルの追加(事前定義モデルの追加、OpenAI の例) +New-DifyModel -Provider "openai" -From "predefined" ` + -Credential @{ + "openai_api_key" = "sk-proj-****************" + } + +# モデルの追加(事前定義モデルの追加、Cohere の例) +New-DifyModel -Provider "cohere" -From "predefined" ` + -Credential @{ + "api_key" = "****************************************" + } + +# モデルの追加(カスタムモデルの追加、OpenAI の例) +New-DifyModel -Provider "openai" -From "customizable" ` + -Type "llm" -Name "gpt-4o-mini" ` + -Credential @{ + "openai_api_key" = "sk-proj-****************" + } + +# モデルの追加(カスタムモデルの追加、Cohere の例) +New-DifyModel -Provider "cohere" -From "customizable" ` + -Type "llm" -Name "command-r-plus" ` + -Credential @{ + "mode" = "chat" + "api_key" = "****************************************" + } +``` + +### Get-DifySystemModel + +ワークスペースのシステムモデルの情報を取得します。 + +```powershell +# システムモデルの取得 +Get-DifySystemModel + +# システムモデルの取得(モデルのタイプで指定) +## タイプ: "llm", "text-embedding", "rerank", "speech2text", "tts" +Get-DifySystemModel -Type "..." +``` + +### Set-DifySystemModel + +ワークスペースのシステムモデルを変更します。 + +```powershell +# システムモデルの変更 +Set-DifySystemModel -Type "llm" -Provider "openai" -Name "gpt-4o-mini" + +# システムモデルの変更(Get-DifySystemModel から直接パイプで指定) +Get-DifySystemModel -Type "llm" -Provider "openai" -Name "gpt-4o-mini" | Set-DifySystemModel + +# システムモデルの変更(Get-DifySystemModel で取得した結果で指定) +$SystemModelToBeChanged = Get-DifySystemModel -Type "llm" -Provider "openai" -Name "gpt-4o-mini" +Set-DifySystemModel -Model $SystemModelToBeChanged +``` + +## ✨ タグの管理 + +### Get-DifyTag + +タグの情報を取得します。 + +```powershell +# タグの取得(タイプで指定) +## タイプ: "app", "knowledge" +Get-DifyTag -Type "app" + +# タグの取得(ID で指定) +Get-DifyTag -Type "app" -Id "..." + +# タグの取得(名前で指定) +Get-DifyTag -Type "app" -Name "..." +``` + +### Get-DifyAppTag + +アプリ用のタグの情報を取得します。`Get-DifyTag -Type "app"` と同じです。 + +```powershell +# アプリ用のタグの取得 +Get-DifyAppTag + +# アプリ用のタグの取得(ID で指定) +Get-DifyAppTag -Id "..." + +# アプリ用のタグの取得(名前で指定) +Get-DifyAppTag -Name "..." +``` + +### Get-DifyKnowledgeTag + +ナレッジ用のタグの情報を取得します。`Get-DifyTag -Type "knowledge"` と同じです。 + +```powershell +# ナレッジ用のタグの取得 +Get-DifyKnowledgeTag + +# ナレッジ用のタグの取得(ID で指定) +Get-DifyKnowledgeTag -Id "..." + +# ナレッジ用のタグの取得(名前で指定) +Get-DifyKnowledgeTag -Name "..." +``` + +## ✨ 情報取得 + +### Get-DifyVersion + +Dify のバージョン情報を取得します。 + +```powershell +# バージョン情報の取得 +Get-DifyVersion +``` + +### Get-DifyProfile + +認証したアカウントの情報を取得します。 + +```powershell +# アカウント情報の取得 +Get-DifyProfile +``` + +## ✨ インスタンスの初期設定 + +### Initialize-Dify + +管理者アカウントを作成します(コミュニティ版のみ)。正常に完了すると、管理者アカウントで `Connect-Dify` を実行した状態と同じになります。 + +```powershell +# 管理者アカウントの作成(実行後、管理者アカウントのパスワードを手動で入力する) +Initialize-Dify -Server "https://dify.example.com" -Email "dify@example.com" + +# 管理者アカウントの作成(パスワードを事前に指定する) +$DifyPassword = ConvertTo-SecureString -String "AwesomeDify123!" -AsPlainText -Force +Initialize-Dify -Server "https://dify.example.com" -Email "dify@example.com" -Password $DifyPassword + +# 管理者アカウントの作成(Dify に INIT_PASSWORD を指定していて、パスワードを事前に指定する場合) +$DifyInitPassword = ConvertTo-SecureString -String "AwesomeDifyInitPassword123!" -AsPlainText -Force +$DifyPassword = ConvertTo-SecureString -String "AwesomeDify123!" -AsPlainText -Force +Initialize-Dify -Server "https://dify.example.com" -Email "dify@example.com" -InitPassword $DifyInitPassword -Password $DifyPassword +``` + +> [!NOTE] +> 引数は、環境変数を使うと省略できます。 +> +> ```powershell +> $env:PSDIFY_URL = "https://dify.example.com" +> $env:PSDIFY_AUTH_METHOD = "Password" +> $env:PSDIFY_EMAIL = "dify@example.com" +> $env:PSDIFY_PASSWORD = "AwesomeDify123!" +> $env:PSDIFY_DISABLE_SSL_VERIFICATION = "true" # 自己署名証明書を使っている場合 +> $env:PSDIFY_INIT_PASSWORD = "AwesomeDifyInitPassword123!" # Dify に INIT_PASSWORD を指定している場合 +> ``` + +## ✨ その他 + +### Set-PSDifyConfiguration + +HTTPS 接続時の証明書の検証を有効化または無効化できます。 + +```powershell +# 証明書の検証を無効にする +Set-PSDifyConfiguration -IgnoreSSLVerification $true + +# 証明書の検証を有効にする +Set-PSDifyConfiguration -IgnoreSSLVerification $false +``` + +### Add-DifyFile + +ファイルをアップロードします。 + +```powershell +# ファイルのアップロード(ファイルパスで指定、ワイルドカード可、複数指定可) +Add-DifyFile -Path "Files/*" + +# ファイルのアップロード(Get-Item や Get-ChildItem からパイプで指定) +Get-Item -Path "Files/*" | Add-DifyFile + +# ファイルのアップロード(ソースを明示) +Get-Item -Path "Files/*" | Add-DifyFile -Source "..." +``` + +### Get-DifyDocumentIndexingStatus + +ドキュメントのインデキシング状況を取得します。 + +```powershell +# インデキシング状況の取得(Add-DifyDocument から直接パイプで指定) +Add-DifyDocument -Knowledge $Knowledge -Path "Docs/*.md" | Get-DifyDocumentIndexingStatus + +# インデキシング状況の取得(Add-DifyDocument で取得した結果で指定) +$DocumentToCheckIndexingStatus = Add-DifyDocument -Knowledge $Knowledge -Path "Docs/*.md" +Get-DifyDocumentIndexingStatus -Document $DocumentToCheckIndexingStatus + +# インデキシング状況の取得(ナレッジとバッチ ID で指定) +Get-DifyDocumentIndexingStatus -Knowledge $Knowledge -Batch "..." + +# インデキシング状況の取得(完了を待つ) +Get-DifyDocumentIndexingStatus -Knowledge $Knowledge -Batch "..." -Wait + +# インデキシング状況の取得(待つ時間を変更) +Get-DifyDocumentIndexingStatus -Knowledge $Knowledge -Batch "..." -Wait -Interval 10 -Timeout 600 +``` + +### Invoke-DifyRestMethod + +REST API を呼び出します。 + +```powershell +# REST API の呼び出し(GET) +$Query = @{ + "page" = 1 + "limit" = 100 +} +Invoke-DifyRestMethod -Method "GET" -Uri "https://dify.example.com/console/api/apps" -Query $Query -Token $env:PSDIFY_CONSOLE_TOKEN + +# REST API の呼び出し(POST) +$Body = @{ + "model_settings" = @( + @{ + "model_type" = "llm" + "provider" = "openai" + "model" = "gpt-4o-mini" + } + ) +} | ConvertTo-Json +Invoke-DifyRestMethod -Method "POST" -Uri "https://dify.example.com/console/api/workspaces/current/default-model" -Body $Body -Token $env:PSDIFY_CONSOLE_TOKEN + +# REST API の呼び出し(セッションを使う場合) +$DifySession = New-Object Microsoft.PowerShell.Commands.WebRequestSession +Invoke-DifyRestMethod -Method "GET" -Uri "https://dify.example.com/console/api/setup" -Session $DifySession +``` + +## ✨ チャットの操作 + +### Send-DifyChatMessage + +アプリにチャットメッセージを送信します。動作には次の環境変数が必要です。 + +- `$env:PSDIFY_APP_URL` +- `$env:PSDIFY_APP_TOKEN` + +送信した内容と応答は `Logs` フォルダに保存されます。 + +```powershell +# 環境変数の設定 +$env:PSDIFY_APP_URL = "https://dify.example.com" +$env:PSDIFY_APP_TOKEN = "app-****************" + +# チャットメッセージの送信 +Send-DifyChatMessage -Message "Hello, Dify!" + +# チャットメッセージの送信(新しいセッションを開始) +Send-DifyChatMessage -Message "Hello, Dify!" -NewSession +``` diff --git a/Docs/README.md b/Docs/README.md index c701091..9ff9710 100644 --- a/Docs/README.md +++ b/Docs/README.md @@ -1,3 +1,697 @@ -# List of cmdlets included in PSDify +# List of PSDify Cmdlets -TODO +PSDify includes the following cmdlets. + +Detailed help is not provided, so please refer to the examples on this page for cmdlet usage. + +| Category | Cmdlet | Description | +| --- | --- | --- | +| Authentication | [Connect-Dify](#connect-dify) | Authenticate with Dify using password or email authentication, enabling operations with other PSDify cmdlets. | +| Authentication | [Disconnect-Dify](#disconnect-dify) | Log out from Dify. | +| Application Management | [Get-DifyApp](#get-difyapp) | Retrieve app information. | +| Application Management | [Remove-DifyApp](#remove-difyapp) | Delete apps. | +| Application Management | [Import-DifyApp](#import-difyapp) | Import apps from local DSL files. | +| Application Management | [Export-DifyApp](#export-difyapp) | Export apps to DSL files. | +| Application Management | [Get-DifyDSLContent](#get-difydslcontent--set-difydslcontent) | Retrieve content from DSL file as string. | +| Application Management | [Set-DifyDSLContent](#get-difydslcontent--set-difydslcontent) | Write content to DSL file. | +| Application Management | [Get-DifyAppAPIKey](#get-difyappapikey) | Retrieve app API keys. | +| Application Management | [New-DifyAppAPIKey](#new-difyappapikey) | Create app API keys. | +| Application Management | [Remove-DifyAppAPIKey](#remove-difyappapikey) | Delete app API keys. || +| Knowledge Management | [Get-DifyKnowledge](#get-difyknowledge) | Retrieve knowledge information. | +| Knowledge Management | [New-DifyKnowledge](#new-difyknowledge) | Add new empty knowledge. | +| Knowledge Management | [Remove-DifyKnowledge](#remove-difyknowledge) | Delete knowledge. | +| Knowledge Management | [Get-DifyDocument](#get-difydocument) | Retrieve document information in knowledge. | +| Knowledge Management | [Add-DifyDocument](#add-difydocument) | Upload documents to knowledge. | +| Member Management | [Get-DifyMember](#get-difymember) | Retrieve workspace member information. | +| Member Management | [New-DifyMember](#new-difymember) | Add (invite) a new member to the workspace. | +| Member Management | [Remove-DifyMember](#remove-difymember) | Remove members from the workspace. | +| Member Management | [Set-DifyMemberRole](#set-difymemberrole) | Change members' role in the workspace. | +| Model Management | [Get-DifyModel](#get-difymodel) | Retrieves workspace model information. | +| Model Management | [New-DifyModel](#new-difymodel) | Add new models to the workspace. | +| Model Management | [Get-DifySystemModel](#get-difysystemmodel) | Retrieve system model information for the workspace. | +| Model Management | [Set-DifySystemModel](#set-difysystemmodel) | Change the system model in the workspace. | +| Tag Management | [Get-DifyTag](#get-difytag) | Retrieve tag information. | +| Tag Management | [Get-DifyAppTag](#get-difyapptag) | Retrieve tag information for apps. | +| Tag Management | [Get-DifyKnowledgeTag](#get-difyknowledgetag) | Retrieve tag information for knowledge. | +| Information Retrieval | [Get-DifyVersion](#get-difyversion) | Retrieve Dify version information. | +| Information Retrieval | [Get-DifyProfile](#get-difyprofile) | Retrieve authenticated account information. | +| Instance Initialization | [Initialize-Dify](#initialize-dify) | Create an admin account (Community Edition only). | +| Miscellaneous | [Set-PSDifyConfiguration](#set-psdifyconfiguration) | Disables SSL certificate verification for HTTPS connections. | +| Miscellaneous | [Add-DifyFile](#add-difyfile) | Upload files. | +| Miscellaneous | [Get-DifyDocumentIndexingStatus](#get-difydocumentindexingstatus) | Retrieve document indexing status. | +| Miscellaneous | [Invoke-DifyRestMethod](#invoke-difyrestmethod) | Invoke REST API methods. | +| Chat Operations | [Send-DifyChatMessage](#send-difychatmessage) | Send chat messages to the app. | + +## ✨ Authentication + +### Connect-Dify + +Authenticate with Dify using password or email-based login, enabling operations with other PSDify cmdlets. + +> [!NOTE] +> Upon successful authentication, the following environment variables will be set: +> +> ```powershell +> $env:PSDIFY_CONSOLE_TOKEN = "..." +> $env:PSDIFY_CONSOLE_REFRESH_TOKEN = "..." +> ``` + +#### Email Authentication (For Cloud Edition) + +SSO-authenticated accounts can also log in via email authentication using the associated email address. + +```powershell +# Email authentication (enter the code manually manually which will be sent to your email address after execution) +Connect-Dify -AuthMethod "Code" -Email "dify@example.com" +``` + +> [!NOTE] +> You can use environment variables to simplify cmdlet arguments. +> +> ```powershell +> $env:PSDIFY_URL = "https://cloud.dify.ai" +> $env:PSDIFY_AUTH_METHOD = "Code" +> $env:PSDIFY_EMAIL = "dify@example.com" +> ``` + +#### Password Authentication (For Community Edition) + +> [!NOTE] +> If using a self-signed certificate for HTTPS in the Community Edition, disable certificate verification before invoking `Connect-Dify`. +> +> ```powershell +> # Disable certificate verification +> Set-PSDifyConfiguration -IgnoreSSLVerification $true +> ``` + +```powershell +# Password authentication (enter the password manually after execution) +Connect-Dify -Server "https://dify.example.com" -Email "dify@example.com" + +# Password authentication (use predefined password) +$DifyPassword = ConvertTo-SecureString -String "AwesomeDify123!" -AsPlainText -Force +Connect-Dify -Server "https://dify.example.com" -Email "dify@example.com" -Password $DifyPassword +``` + +> [!NOTE] +> You can use environment variables to simplify cmdlet arguments. +> +> ```powershell +> $env:PSDIFY_URL = "https://dify.example.com" +> $env:PSDIFY_AUTH_METHOD = "Password" +> $env:PSDIFY_EMAIL = "dify@example.com" +> $env:PSDIFY_PASSWORD = "AwesomeDify123!" +> $env:PSDIFY_DISABLE_SSL_VERIFICATION = "true" # For self-signed certificates +> ``` + +### Disconnect-Dify + +Log out from Dify. + +```powershell +# Logout (invalidate issued tokens) +Disconnect-Dify + +# Force logout (remove local environment variables regardless of logout success) +Disconnect-Dify -Force +``` + +## ✨ Application Management + +### Get-DifyApp + +Retrieve app information. + +```powershell +# Get all apps +Get-DifyApp + +# Get apps by ID +Get-DifyApp -Id "..." + +# Get apps by name (complete match) +Get-DifyApp -Name "..." + +# Get apps by name (partial match) +Get-DifyApp -Search "..." + +# Get apps by mode +## Modes: "chat", "workflow", "agent-chat", "channel", "all" +Get-DifyApp -Mode "chat" + +# Get apps by tags (multiple tags can be specified) +Get-DifyApp -Tags "...", "..." + +# Example: Combine filters +Get-DifyApp -Name "..." -Mode "chat" +``` + +### Remove-DifyApp + +Delete apps. + +```powershell +# Delete app (specify directly from Get-DifyApp) +Get-DifyApp -Name "..." | Remove-DifyApp + +# Delete app (use result from Get-DifyApp) +$AppsToBeRemoved = Get-DifyApp -Name "..." +Remove-DifyApp -App $AppsToBeRemoved +``` + +### Import-DifyApp + +Import apps from local DSL files. + +```powershell +# Import apps (specify file paths, supports wildcards and multiple paths) +Import-DifyApp -Path "DSLs/*.yml" +Import-DifyApp -Path "DSLs/demo1.yml", "DSLs/demo2.yml" + +# Import apps (specify directly from Get-Item or Get-ChildItem) +Get-Item -Path "DSLs/*.yml" | Import-DifyApp + +# Import apps (use result from Get-ChildItem) +$DSLFiles = Get-ChildItem -Path "DSLs/*.yml" +Import-DifyApp -Item $DSLFiles +``` + +### Export-DifyApp + +Export apps to DSL files. By default, files are saved in the `DSLs` directory. + +```powershell +# Export app (specify directly from Get-DifyApp) +Get-DifyApp | Export-DifyApp + +# Export app (use result from Get-DifyApp) +$AppsToBeExported = Get-DifyApp +Export-DifyApp -App $AppsToBeExported + +# Export app (change target directory) +Get-DifyApp | Export-DifyApp -Path "./path/to/your/directory" + +# Export app (include secrets) +Get-DifyApp | Export-DifyApp -IncludeSecret +``` + +### Get-DifyDSLContent / Set-DifyDSLContent + +Retrieve content from DSL file as string and write content to DSL file. + +This is useful when you want to rewrite part of an existing DSL file and save it as another file. It is designed to handle DSL files consistently in UTF-8 without BOM. + +```powershell +# Retrieve content from DSL file +$RawContent = Get-DifyDSLContent -Path "DSLs/old.yml" + +# Rewrite the old knowledge ID in the DSL file to the new knowledge ID and save it as another DSL file +$RawContent -replace "8b960203-299d-4345-b953-3308663dd790", "574d9556-189a-4d35-b296-09231b859667" | Set-DifyDSLContent -Path "DSLs/new.yml" +``` + +### Get-DifyAppAPIKey + +Retrieve the API key of the app. + +```powershell +# Get API key (specify directly from Get-DifyApp) +Get-DifyApp -Name "..." | Get-DifyAppAPIKey + +# Get API key (use result from Get-DifyApp) +$AppsToGetAPIKey = Get-DifyApp -Name "..." +Get-DifyAppAPIKey -App $AppsToGetAPIKey +``` + +### New-DifyAppAPIKey + +Create an API key for the app. + +```powershell +# Create an API key (specify directly from Get-DifyApp) +Get-DifyApp -Name "..." | New-DifyAppAPIKey + +# Create an API key (use result from Get-DifyApp) +$AppsToCreateAPIKey = Get-DifyApp -Name "..." +``` + +### Remove-DifyAppAPIKey + +Delete the API key of the app. + +```powershell +# Delete the API key (specify directly from Get-DifyAppAPIKey) +Get-DifyApp -Name "..." | Get-DifyAppAPIKey | Remove-DifyAppAPIKey + +# Delete the API key (use result from Get-DifyAppAPIKey) +$APIKeysToBeRemoved = Get-DifyApp -Name "..." | Get-DifyAppAPIKey +Remove-DifyAppAPIKey -APIKey $APIKeysToBeRemoved +``` + +## ✨ Knowledge Management + +### Get-DifyKnowledge + +Retrieve knowledge information. + +```powershell +# Get all knowledge +Get-DifyKnowledge + +# Get knowledge by ID +Get-DifyKnowledge -Id "..." + +# Get knowledge by name (complete match) +Get-DifyKnowledge -Name "..." + +# Get knowledge by name (partial match) +Get-DifyKnowledge -Search "..." + +# Get knowledge by tags (multiple tags can be specified) +Get-DifyKnowledge -Tags "...", "..." +``` + +### New-DifyKnowledge + +Add new empty knowledge. + +```powershell +# Add new knowledge +New-DifyKnowledge -Name "My New Knowledge" + +# Add new knowledge with description +New-DifyKnowledge -Name "My New Knowledge" -Description "This is a new knowledge." +``` + +### Remove-DifyKnowledge + +Delete knowledge. + +```powershell +# Delete knowledge (specify directly from Get-DifyKnowledge) +Get-DifyKnowledge -Name "..." | Remove-DifyKnowledge + +# Delete knowledge (use result from Get-DifyKnowledge) +$KnowledgeToBeRemoved = Get-DifyKnowledge -Name "..." +Remove-DifyKnowledge -Knowledge $KnowledgeToBeRemoved +``` + +### Get-DifyDocument + +Retrieve document information in knowledge. + +```powershell +# Get all documents (specify directly from Get-DifyKnowledge) +Get-DifyKnowledge -Name "..." | Get-DifyDocument + +# Get all documents (use result from Get-DifyKnowledge) +$Knowledge = Get-DifyKnowledge -Name "..." +Get-DifyDocument -Knowledge $Knowledge + +# Get documents by ID +Get-DifyKnowledge -Name "..." | Get-DifyDocument -Id "..." + +# Get documents by name (complete match) +Get-DifyKnowledge -Name "..." | Get-DifyDocument -Name "..." + +# Get documents by name (partial match) +Get-DifyKnowledge -Name "..." | Get-DifyDocument -Search "..." +``` + +### Add-DifyDocument + +Upload documents to knowledge. By default, the settings for Automatic, High Quality with system model, and Vector Aearch are applied. + +Currently, detailed configuration is not implemented. + +```powershell +# Prerequisite: Get the knowledge to upload the document +$Knowledge = Get-DifyKnowledge -Name "My New Knowledge" + +# Upload documents (specify file paths, supports wildcards and multiple paths) +Add-DifyDocument -Knowledge $Knowledge -Path "Docs/*.md" + +# Upload documents (specify from Get-Item or Get-ChildItem via pipe) +Get-Item -Path "Docs/*.md" | Add-DifyDocument -Knowledge $Knowledge + +# Upload documents (use any model) +$EmbeddingModel = Get-DifyModel -Provider "openai" -Name "text-embedding-3-small" +Add-DifyDocument -Knowledge $Knowledge -Path "Docs/*.md" -IndexMode "high_quality" -Model $EmbeddingModel + +# Upload documents (use economy mode) +Add-DifyDocument -Knowledge $Knowledge -Path "Docs/*.md" -IndexMode "economy" + +# Wait for indexing to complete +Add-DifyDocument -Knowledge $Knowledge -Path "Docs/*.md" -Wait + +# Custom wait settings +Add-DifyDocument -Knowledge $Knowledge -Path "Docs/*.md" -Wait -Interval 10 -Timeout 600 +``` + +## ✨ Member Management + +### Get-DifyMember + +Retrieve workspace member information. + +```powershell +# Get all members +Get-DifyMember + +# Get member by ID +Get-DifyMember -Id "..." + +# Get member by name +Get-DifyMember -Name "..." + +# Get member by email +Get-DifyMember -Email "..." +``` + +### New-DifyMember + +Add (invite) a new member to the workspace. + +```powershell +# Invite a new member +## Roles: "admin", "editor", "normal" +New-DifyMember -Email "dify@example.com" -Role "normal" -Language "en-US" +``` + +### Remove-DifyMember + +Remove members from the workspace. + +```powershell +# Remove members (specify directly from Get-DifyMember) +Get-DifyMember -Name "..." | Remove-DifyMember + +# Remove members (use result from Get-DifyMember) +$MembersToBeRemoved = Get-DifyMember -Name "..." +Remove-DifyMember -Member $MembersToBeRemoved +``` + +### Set-DifyMemberRole + +Change members' role in the workspace. + +```powershell +# Change role (specify directly from Get-DifyMember) +Get-DifyMember -Name "..." | Set-DifyMemberRole -Role "editor" + +# Change role (use result from Get-DifyMember) +$MembersToBeChanged = Get-DifyMember -Name "..." +Set-DifyMemberRole -Member $MembersToBeChanged -Role "editor" +``` + +## ✨ Model Management + +### Get-DifyModel + +Retrieve workspace model information. + +```powershell +# Get all models +Get-DifyModel + +# Get models by provider +Get-DifyModel -Provider "..." + +# Get models by type +## Types: "predefined", "customizable" +Get-DifyModel -From "..." + +# Get models by name +Get-DifyModel -Name "..." + +# Get models by model type +## Model types: "llm", "text-embedding", "speech2text", "moderation", "tts", "rerank" +Get-DifyModel -Type "..." + +# Example: Combine filters +Get-DifyModel -Provider "..." -Type "llm" +``` + +### New-DifyModel + +Add new models to the workspace. The credentials required depend on the provider and the model. Check the actual HTTP requests using browser developer tools for precise details. + +```powershell +# Add predefined models (example for OpenAI) +New-DifyModel -Provider "openai" -From "predefined" ` + -Credential @{ + "openai_api_key" = "sk-proj-****************" + } + +# Add predefined models (example for Cohere) +New-DifyModel -Provider "cohere" -From "predefined" ` + -Credential @{ + "api_key" = "****************************************" + } + +# Add customizable models (example for OpenAI) +New-DifyModel -Provider "openai" -From "customizable" ` + -Type "llm" -Name "gpt-4o-mini" ` + -Credential @{ + "openai_api_key" = "sk-proj-****************" + } + +# Add customizable models (example for Cohere) +New-DifyModel -Provider "cohere" -From "customizable" ` + -Type "llm" -Name "command-r-plus" ` + -Credential @{ + "mode" = "chat" + "api_key" = "****************************************" + } +``` + +### Get-DifySystemModel + +Retrieve system model information for the workspace. + +```powershell +# Get system model +Get-DifySystemModel + +# Get system model by type +## Types: "llm", "text-embedding", "rerank", "speech2text", "tts" +Get-DifySystemModel -Type "..." +``` + +### Set-DifySystemModel + +Change the system model in the workspace. + +```powershell +# Change system model +Set-DifySystemModel -Type "llm" -Provider "openai" -Name "gpt-4o-mini" + +# Change system model (specify directly from Get-DifySystemModel) +Get-DifySystemModel -Type "llm" -Provider "openai" -Name "gpt-4o-mini" | Set-DifySystemModel + +# Change system model (use result from Get-DifySystemModel) +$SystemModelToBeChanged = Get-DifySystemModel -Type "llm" -Provider "openai" -Name "gpt-4o-mini" +Set-DifySystemModel -Model $SystemModelToBeChanged +``` + +## ✨ Tag Management + +### Get-DifyTag + +Retrieve tag information. + +```powershell +# Get tags by type +## Types: "app", "knowledge" +Get-DifyTag -Type "app" + +# Get tags by ID +Get-DifyTag -Type "app" -Id "..." + +# Get tags by name +Get-DifyTag -Type "app" -Name "..." +``` + +### Get-DifyAppTag + +Retrieve tag information for apps. This is equivalent to `Get-DifyTag -Type "app"`. + +```powershell +# Get app tags +Get-DifyAppTag + +# Get app tags by ID +Get-DifyAppTag -Id "..." + +# Get app tags by name +Get-DifyAppTag -Name "..." +``` + +### Get-DifyKnowledgeTag + +Retrieve tag information for knowledge. This is equivalent to `Get-DifyTag -Type "knowledge"`. + +```powershell +# Get knowledge tags +Get-DifyKnowledgeTag + +# Get knowledge tags by ID +Get-DifyKnowledgeTag -Id "..." + +# Get knowledge tags by name +Get-DifyKnowledgeTag -Name "..." +``` + +## ✨ Information Retrieval + +### Get-DifyVersion + +Retrieve the version information for Dify. + +```powershell +# Get version information +Get-DifyVersion +``` + +### Get-DifyProfile + +Retrieve the authenticated account's profile information. + +```powershell +# Get account profile +Get-DifyProfile +``` + +## ✨ Instance Initialization + +### Initialize-Dify + +Create an admin account (Community Edition only). After successful completion, it is equivalent to logging in with the admin account using `Connect-Dify`. + +```powershell +# Create an admin account (enter the password manually after execution) +Initialize-Dify -Server "https://dify.example.com" -Email "dify@example.com" + +# Create an admin account (use predefined password) +$DifyPassword = ConvertTo-SecureString -String "AwesomeDify123!" -AsPlainText -Force +Initialize-Dify -Server "https://dify.example.com" -Email "dify@example.com" -Password $DifyPassword + +# Create an admin account (if you've specify INIT_PASSWORD for Dify and use predefined init password) +$DifyInitPassword = ConvertTo-SecureString -String "AwesomeDifyInitPassword123!" -AsPlainText -Force +$DifyPassword = ConvertTo-SecureString -String "AwesomeDify123!" -AsPlainText -Force +Initialize-Dify -Server "https://dify.example.com" -Email "dify@example.com" -InitPassword $DifyInitPassword -Password $DifyPassword +``` + +> [!NOTE] +> You can use environment variables to simplify cmdlet arguments. +> +> ```powershell +> $env:PSDIFY_URL = "https://dify.example.com" +> $env:PSDIFY_AUTH_METHOD = "Password" +> $env:PSDIFY_EMAIL = "dify@example.com" +> $env:PSDIFY_PASSWORD = "AwesomeDify123!" +> $env:PSDIFY_DISABLE_SSL_VERIFICATION = "true" # For self-signed certificates +> $env:PSDIFY_INIT_PASSWORD = "AwesomeDifyInitPassword123!" # If INIT_PASSWORD is set in Dify +> ``` + +## ✨ Miscellaneous + +### Set-PSDifyConfiguration + +Enable or disable SSL certificate verification for HTTPS connections. + +```powershell +# Disable certificate verification +Set-PSDifyConfiguration -IgnoreSSLVerification $true + +# Enable certificate verification +Set-PSDifyConfiguration -IgnoreSSLVerification $false +``` + +### Add-DifyFile + +Upload files. + +```powershell +# Upload files (specify file paths, supports wildcards and multiple paths) +Add-DifyFile -Path "Files/*" + +# Upload files (specify from Get-Item or Get-ChildItem via pipe) +Get-Item -Path "Files/*" | Add-DifyFile + +# Upload files (specify source information) +Get-Item -Path "Files/*" | Add-DifyFile -Source "..." +``` + +### Get-DifyDocumentIndexingStatus + +Retrieve document indexing status. + +```powershell +# Get indexing status (specify directly from Add-DifyDocument) +Add-DifyDocument -Knowledge $Knowledge -Path "Docs/*.md" | Get-DifyDocumentIndexingStatus + +# Get indexing status (specify from Add-DifyDocument result) +$DocumentToCheckIndexingStatus = Add-DifyDocument -Knowledge $Knowledge -Path "Docs/*.md" +Get-DifyDocumentIndexingStatus -Document $DocumentToCheckIndexingStatus + +# Get indexing status (specify knowledge and batch ID) +Get-DifyDocumentIndexingStatus -Knowledge $Knowledge -Batch "..." + +# Get indexing status (wait for completion) +Get-DifyDocumentIndexingStatus -Knowledge $Knowledge -Batch "..." -Wait + +# Get indexing status (change waiting time) +Get-DifyDocumentIndexingStatus -Knowledge $Knowledge -Batch "..." -Wait -Interval 10 -Timeout 600 +``` + +### Invoke-DifyRestMethod + +Invokes REST API methods. + +```powershell +# Invoke REST API (GET) +$Query = @{ + "page" = 1 + "limit" = 100 +} +Invoke-DifyRestMethod -Method "GET" -Uri "https://dify.example.com/console/api/apps" -Query $Query -Token $env:PSDIFY_CONSOLE_TOKEN + +# Invoke REST API (POST) +$Body = @{ + "model_settings" = @( + @{ + "model_type" = "llm" + "provider" = "openai" + "model" = "gpt-4o-mini" + } + ) +} | ConvertTo-Json +Invoke-DifyRestMethod -Method "POST" -Uri "https://dify.example.com/console/api/workspaces/current/default-model" -Body $Body -Token $env:PSDIFY_CONSOLE_TOKEN + +# Invoke REST API (using a session) +$DifySession = New-Object Microsoft.PowerShell.Commands.WebRequestSession +Invoke-DifyRestMethod -Method "GET" -Uri "https://dify.example.com/console/api/setup" -Session $DifySession +``` + +## ✨ Chat Operations + +### Send-DifyChatMessage + +Send chat messages to the app. The following environment variables are required for operation. + +- `$env:PSDIFY_APP_URL` +- `$env:PSDIFY_APP_TOKEN` + +The content sent and the response are saved in the `Logs` folder. + +```powershell +# Set environment variables +$env:PSDIFY_APP_URL = "https://dify.example.com" +$env:PSDIFY_APP_TOKEN = "app-****************" + +# Send chat messages +Send-DifyChatMessage -Message "Hello, Dify!" + +# Send chat messages (start a new session) +Send-DifyChatMessage -Message "Hello, Dify!" -NewSession +``` diff --git a/PSDify.psd1 b/PSDify.psd1 index 15e728f7552b00391923104cc54a22b1639483ae..6292c83606363401e82a85b6b4afc180f32b15e8 100644 GIT binary patch delta 502 zcmca1x<_`xELKAXJqCk~^KUavZetdccVtKbLR|(IhD?SuhDsov&ydVe%8<*D%8*i|OaVQavu>jIk?7H7y#28si%(^3R^3=zD}3>jdL`2w9)41^$uy=Re~+`uk? z!;{h=IiN{AljGREChuX90P;Ch1AqYni!w;Cqyd929T;FmVDmFULA7})yD>8WjfQyX delta 78 zcmdlZdqZ@>ELH;sJqE*#^KUb8D=@&}!9&txf1sm%x2 bt}#yL<6Jg*7Mt4S1zZ9^iLlK*+y=}5mjf6| diff --git a/Private/Convert-UnixTimeToLocalDateTime.ps1 b/Private/Convert-UnixTimeToLocalDateTime.ps1 index b300d25..2a26518 100644 --- a/Private/Convert-UnixTimeToLocalDateTime.ps1 +++ b/Private/Convert-UnixTimeToLocalDateTime.ps1 @@ -3,5 +3,8 @@ function Convert-UnixTimeToLocalDateTime { param ( [Int64] $UnixTime ) + if (-not $UnixTime) { + return $null + } return ([datetimeoffset]::FromUnixTimeSeconds($UnixTime)).LocalDateTime } diff --git a/Public/Add-DifyDocument.ps1 b/Public/Add-DifyDocument.ps1 new file mode 100644 index 0000000..61222fd --- /dev/null +++ b/Public/Add-DifyDocument.ps1 @@ -0,0 +1,166 @@ +function Add-DifyDocument { + [CmdletBinding()] + param( + [Parameter(ValueFromPipeline = $true)] + [PSCustomObject[]] $Item = @(), + [String[]] $Path = @(), + [PSCustomObject] $Knowledge, + [String] $ChunkMode = "automatic", + [String] $IndexMode = "high_quality", + [PSCustomObject] $EmbeddingModel = $null, + [String] $RetrievalMode = "semantic_search", + [Switch] $Wait = $false, + [Int] $Interval = 5, + [Int] $Timeout = 300 + ) + + begin { + $Files = @() + } + + process { + foreach ($ItemObject in $Item) { + $Files += $ItemObject + } + } + end { + if (-not $Files -and -not $Path) { + throw "Path is required" + } + if ($Path) { + $Files += Get-ChildItem -Path $Path + } + + if (-not $Knowledge) { + throw "Knowledge is required" + } + if (@($Knowledge).Count -gt 1) { + throw "Only one knowledge can be specified" + } + + $ValidChunkModes = @("automatic", "custom") + if (-not $ValidChunkModes.Contains($ChunkMode)) { + throw "Invalid value for ChunkMode. Must be one of: $($ValidChunkModes -join ', ')" + } + $ValidIndexModes = @("high_quality", "economy") + if (-not $ValidIndexModes.Contains($IndexMode)) { + throw "Invalid value for IndexMode. Must be one of: $($ValidIndexModes -join ', ')" + } + $ValidRetrievalModes = @("semantic_search", "full_text_search", "hybrid_search") + if (-not $ValidRetrievalModes.Contains($RetrievalMode)) { + throw "Invalid value for RetrievalMode. Must be one of: $($ValidRetrievalModes -join ', ')" + } + + # not implemented + if ($ChunkMode -eq "custom") { + throw "ChunkMode: custom is not implemented" + } + if ($RetrievalMode -eq "full_text_search") { + throw "RetrievalMode: full_text_search is not implemented" + } + if ($RetrievalMode -eq "hybrid_search") { + throw "RetrievalMode: hybrid_search is not implemented" + } + + # set embedding model + $DefaultEmbeddingModel = Get-DifySystemModel -Type "text-embedding" + if ($IndexMode -eq "high_quality" -and -not $DefaultEmbeddingModel -and -not $EmbeddingModel) { + throw "Model is required for IndexMode: high_quality" + } + if ($IndexMode -eq "high_quality" -and -not $EmbeddingModel) { + $EmbeddingModel = $DefaultEmbeddingModel + } + + # upload files + $UploadedFiles = Add-DifyFile -Path $Files -Source "datasets" + $UploadedFileIds = $UploadedFiles | Select-Object -ExpandProperty Id + + # add document + $Endpoint = "$($env:PSDIFY_URL)/console/api/datasets/$($Knowledge.Id)/documents" + $Method = "POST" + $Body = @{"data_source" = @{ + "type" = "upload_file" + "info_list" = @{ + "data_source_type" = "upload_file" + "file_info_list" = @{ + "file_ids" = @($UploadedFileIds) + } + } + } + "indexing_technique" = $IndexMode + "process_rule" = @{ + "rules" = @{} + "mode" = $ChunkMode + } + "doc_form" = "text_model" + "doc_language" = "English" + "retrieval_model" = @{ + "search_method" = $RetrievalMode + "reranking_enable" = $false + "reranking_mode" = $null + "reranking_model" = @{ + "reranking_provider_name" = $null + "reranking_model_name" = $null + } + "weights" = $null + "top_k" = 3 + "score_threshold_enabled" = $false + "score_threshold" = 0 + } + "embedding_model" = $EmbeddingModel.Model + "embedding_model_provider" = $EmbeddingModel.Provider + } | ConvertTo-Json -Depth 10 + try { + $Response = Invoke-DifyRestMethod -Uri $Endpoint -Method $Method -Body $Body -Token $env:PSDIFY_CONSOLE_TOKEN + } + catch { + throw "Failed to add documents to knowledge: $_" + } + + if (-not $Response.documents) { + throw "Failed to add documents to knowledge" + } + + $Documents = @() + foreach ($Document in $Response.documents) { + $CreatedBy = $Members | Where-Object { $_.Id -eq $Document.created_by } | Select-Object -ExpandProperty Email + if (-not $CreatedBy) { + $CreatedBy = $Document.created_by + } + $UploadedBy = $Members | Where-Object { $_.Id -eq $Document.data_source_detail_dict.upload_file.created_by } | Select-Object -ExpandProperty Email + if (-not $UploadedBy) { + $UploadedBy = $Document.data_source_detail_dict.upload_file.created_by + } + $DocumentObject = [PSCustomObject]@{ + Batch = $Response.batch + KnowledgeId = $Knowledge.Id + Id = $Document.id + Name = $Document.name + DataSourceType = $Document.data_source_type + WordCount = $Document.word_count + HitCount = $Document.hit_count + IndexingStatus = $Document.indexing_status + Enabled = $Document.enabled + Archived = $Document.archived + CreatedBy = $CreatedBy + CreatedAt = Convert-UnixTimeToLocalDateTime($Document.created_at) + UploadedBy = $UploadedBy + UploadedAt = Convert-UnixTimeToLocalDateTime($Document.data_source_detail_dict.upload_file.created_at) + } + $Documents += $DocumentObject + } + + if ($Wait) { + $null = Get-DifyDocumentIndexingStatus -Document $Documents -Wait -Interval $Interval -Timeout $Timeout + $UpdatedAllDocuments = Get-DifyDocument -Knowledge $Knowledge + $UpdatedDocuments = @() + foreach ($Document in $Documents) { + $UpdatedDocuments += $UpdatedAllDocuments | Where-Object { $_.Id -eq $Document.Id } + } + return $UpdatedDocuments + } + else { + return $Documents + } + } +} diff --git a/Public/Add-DifyFile.ps1 b/Public/Add-DifyFile.ps1 new file mode 100644 index 0000000..c6efa45 --- /dev/null +++ b/Public/Add-DifyFile.ps1 @@ -0,0 +1,89 @@ +function Add-DifyFile { + [CmdletBinding()] + param( + [Parameter(ValueFromPipeline = $true)] + [PSCustomObject[]] $Item = @(), + [String[]] $Path = @(), + [String] $Source + ) + + begin { + $Files = @() + } + + process { + foreach ($ItemObject in $Item) { + $Files += $ItemObject + } + } + + end { + if (-not $Files -and -not $Path) { + throw "Path is required" + } + if ($Path) { + $Files += Get-ChildItem -Path $Path + } + + $UploadedFiles = @() + foreach ($File in $Files) { + + $Boundary = "----WebKitFormBoundary" + [System.Guid]::NewGuid().ToString("N") + $UTF8NoBOM = New-Object "System.Text.UTF8Encoding" -ArgumentList @($false) + $TemporaryFile = New-TemporaryFile + Write-Verbose "using $($TemporaryFile.FullName) as temporary file" + + $FileStream = New-Object System.IO.FileStream($TemporaryFile, [System.IO.FileMode]::Append) + $BinaryWriter = New-Object System.IO.BinaryWriter($FileStream) + + $BinaryWriter.Write($UTF8NoBOM.GetBytes("--$($Boundary)`r`n")) + $BinaryWriter.Write($UTF8NoBOM.GetBytes("Content-Disposition: form-data; name=`"file`"; filename=`"$($File.Name)`"`r`n")) + $BinaryWriter.Write($UTF8NoBOM.GetBytes("Content-Type: application/octet-stream`r`n`r`n")) + $BinaryWriter.Write([System.IO.File]::ReadAllBytes($File.FullName)) + $BinaryWriter.Write($UTF8NoBOM.GetBytes("`r`n--$($Boundary)--`r`n")) + $BinaryWriter.Close() + + $Endpoint = "$($env:PSDIFY_URL)/console/api/files/upload" + $ContentType = "multipart/form-data; boundary=$($Boundary)" + $Method = "POST" + if ($Source) { + $Query = @{ + "source" = $Source + } + } + else { + $Query = $null + } + + try { + $Response = Invoke-DifyRestMethod -Uri $Endpoint -Method $Method -ContentType $ContentType -Query $Query -InFile $TemporaryFile -Token $env:PSDIFY_CONSOLE_TOKEN + } + catch { + throw "Failed to upload file: $_" + } + + if (-not $Response.id) { + throw "Failed to upload file" + } + + $Members = Get-DifyMember + + $CreatedBy = $Members | Where-Object { $_.Id -eq $Response.created_by } | Select-Object -ExpandProperty Email + if (-not $CreatedBy) { + $CreatedBy = $Response.created_by + } + + $UploadedFile = [PSCustomObject]@{ + Id = $Response.id + Name = $Response.name + Size = $Response.size + Extension = $Response.extension + MimeType = $Response.mime_type + CreatedBy = $CreatedBy + CreatedAt = Convert-UnixTimeToLocalDateTime($Response.created_at) + } + $UploadedFiles += $UploadedFile + } + return $UploadedFiles + } +} diff --git a/Public/Connect-Dify.ps1 b/Public/Connect-Dify.ps1 index c8ad3d7..05b1a99 100644 --- a/Public/Connect-Dify.ps1 +++ b/Public/Connect-Dify.ps1 @@ -107,7 +107,7 @@ function Connect-Dify { } # Login to Dify - $Endpoint = "$($env:PSDIFY_URL)/console/api/email-code-login/validity" + $Endpoint = "$($Server)/console/api/email-code-login/validity" $Method = "POST" $Body = @{ "email" = $Email diff --git a/Public/Disconnect-Dify.ps1 b/Public/Disconnect-Dify.ps1 new file mode 100644 index 0000000..2389936 --- /dev/null +++ b/Public/Disconnect-Dify.ps1 @@ -0,0 +1,26 @@ +function Disconnect-Dify { + [CmdletBinding()] + param( + [Switch] $Force = $false + ) + + $Endpoint = "$($env:PSDIFY_URL)/console/api/logout" + $Method = "GET" + try { + $Response = Invoke-DifyRestMethod -Uri $Endpoint -Method $Method -Token $env:PSDIFY_CONSOLE_TOKEN + } + catch { + if (-not $Force) { + throw "Failed to logout: $_" + } + } + if (-not $Force -and (-not $Response.result -or $Response.result -ne "success")) { + throw "Failed to logout" + } + + Remove-Item Env:\PSDIFY_URL -ErrorAction SilentlyContinue + Remove-Item Env:\PSDIFY_CONSOLE_TOKEN -ErrorAction SilentlyContinue + Remove-Item Env:\PSDIFY_CONSOLE_REFRESH_TOKEN -ErrorAction SilentlyContinue + Remove-Item Env:\PSDIFY_VERSION -ErrorAction SilentlyContinue + Remove-Item Env:\PSDIFY_DISABLE_SSL_VERIFICATION -ErrorAction SilentlyContinue +} diff --git a/Public/Export-DifyApp.ps1 b/Public/Export-DifyApp.ps1 index 6eb84ee..491856f 100644 --- a/Public/Export-DifyApp.ps1 +++ b/Public/Export-DifyApp.ps1 @@ -11,6 +11,7 @@ function Export-DifyApp { if (-not (Test-Path -Path $Path)) { $null = New-Item -Path $Path -ItemType Directory } + $Path = Convert-Path -Path $Path $ExportedApps = @() $Apps = @() } diff --git a/Public/Get-DifyApp.ps1 b/Public/Get-DifyApp.ps1 index 6c521bb..1162b06 100644 --- a/Public/Get-DifyApp.ps1 +++ b/Public/Get-DifyApp.ps1 @@ -3,6 +3,7 @@ function Get-DifyApp { param( [String] $Id = "", [String] $Name = "", + [String] $Search = "", [String] $Mode = "", [String[]] $Tags = @() ) @@ -16,8 +17,8 @@ function Get-DifyApp { "page" = 1 "limit" = 100 } - if ($Name) { - $Query.name = $Name + if ($Search) { + $Query.name = $Search } if ($Mode) { $Query.mode = $Mode @@ -75,5 +76,9 @@ function Get-DifyApp { $Query.page++ } + if ($Name) { + $Apps = $Apps | Where-Object { $_.Name -eq $Name } + } + return $Apps } diff --git a/Public/Get-DifyAppAPIKey.ps1 b/Public/Get-DifyAppAPIKey.ps1 new file mode 100644 index 0000000..a5c36c6 --- /dev/null +++ b/Public/Get-DifyAppAPIKey.ps1 @@ -0,0 +1,35 @@ +function Get-DifyAppAPIKey { + [CmdletBinding()] + param( + [Parameter(ValueFromPipeline = $true)] + [PSCustomObject] $App = $null + ) + + end { + if (-not $App) { + throw "App is required" + } + + $Endpoint = "$($env:PSDIFY_URL)/console/api/apps/$($App.Id)/api-keys" + $Method = "GET" + try { + $Response = Invoke-DifyRestMethod -Uri $Endpoint -Method $Method -Token $env:PSDIFY_CONSOLE_TOKEN + } + catch { + throw "Failed to obtain api keys: $_" + } + + $APIKeys = @() + foreach ($APIKey in $Response.data) { + $APIKeys += [PSCustomObject]@{ + AppId = $App.Id + Id = $APIKey.id + Type = $APIKey.type + Token = $APIKey.token + LastUsedAt = Convert-UnixTimeToLocalDateTime($APIKey.last_used_at) + CreatedAt = Convert-UnixTimeToLocalDateTime($APIKey.created_at) + } + } + return $APIKeys + } +} diff --git a/Public/Get-DifyDSLContent.ps1 b/Public/Get-DifyDSLContent.ps1 new file mode 100644 index 0000000..07d9e72 --- /dev/null +++ b/Public/Get-DifyDSLContent.ps1 @@ -0,0 +1,16 @@ +function Get-DifyDSLContent { + [CmdletBinding()] + param( + [String] $Path = "" + ) + + if (-not $Path) { + throw "Path is required" + } + $File = Get-Item -Path $Path + + $UTF8NoBOM = New-Object "System.Text.UTF8Encoding" -ArgumentList @($false) + $RawContent = [System.IO.File]::ReadAllText($File.FullName, $UTF8NoBOM) + + return $RawContent +} diff --git a/Public/Get-DifyDocument.ps1 b/Public/Get-DifyDocument.ps1 new file mode 100644 index 0000000..de4e6f9 --- /dev/null +++ b/Public/Get-DifyDocument.ps1 @@ -0,0 +1,91 @@ +function Get-DifyDocument { + [CmdletBinding()] + param( + [Parameter(ValueFromPipeline = $true)] + [PSCustomObject] $Knowledge, + [String] $Id = "", + [String] $Name = "", + [String] $Search = "" + ) + + begin { + $Knowledges = @() + } + + process { + foreach ($KnowledgeObject in $Knowledge) { + $Knowledges += $KnowledgeObject + } + } + + end { + if (-not $Knowledges) { + throw "Knowledge is required" + } + if ($Knowledges.Count -gt 1) { + throw "Only one knowledge is allowed" + } + + $Query = @{ + "page" = 1 + "limit" = 100 + } + if ($Search) { + $Query.keyword = $Search + } + + $Members = Get-DifyMember + + $Endpoint = "$($env:PSDIFY_URL)/console/api/datasets/$($Knowledge.Id)/documents" + $Method = "GET" + $Documents = @() + $HasMore = $true + while ($HasMore) { + try { + $Response = Invoke-DifyRestMethod -Uri $Endpoint -Method $Method -Query $Query -Token $env:PSDIFY_CONSOLE_TOKEN + } + catch { + throw "Failed to obtain documents: $_" + } + + foreach ($Document in $Response.data) { + $CreatedBy = $Members | Where-Object { $_.Id -eq $Document.created_by } | Select-Object -ExpandProperty Email + if (-not $CreatedBy) { + $CreatedBy = $Document.created_by + } + $UploadedBy = $Members | Where-Object { $_.Id -eq $Document.data_source_detail_dict.upload_file.created_by } | Select-Object -ExpandProperty Email + if (-not $UploadedBy) { + $UploadedBy = $Document.data_source_detail_dict.upload_file.created_by + } + $DocumentObject = [PSCustomObject]@{ + KnowledgeId = $Knowledge.Id + Id = $Document.id + Name = $Document.name + DataSourceType = $Document.data_source_type + WordCount = $Document.word_count + HitCount = $Document.hit_count + IndexingStatus = $Document.indexing_status + Enabled = $Document.enabled + Archived = $Document.archived + CreatedBy = $CreatedBy + CreatedAt = Convert-UnixTimeToLocalDateTime($Document.created_at) + UploadedBy = $UploadedBy + UploadedAt = Convert-UnixTimeToLocalDateTime($Document.data_source_detail_dict.upload_file.created_at) + } + if ($Id -and $DocumentObject.Id -eq $Id) { + return $DocumentObject + } + $Documents += $DocumentObject + } + + $HasMore = $Response.has_more + $Query.page++ + } + + if ($Name) { + $Documents = $Documents | Where-Object { $_.Name -eq $Name } + } + + return $Documents + } +} diff --git a/Public/Get-DifyDocumentIndexingStatus.ps1 b/Public/Get-DifyDocumentIndexingStatus.ps1 new file mode 100644 index 0000000..6c6fc71 --- /dev/null +++ b/Public/Get-DifyDocumentIndexingStatus.ps1 @@ -0,0 +1,93 @@ +function Get-DifyDocumentIndexingStatus { + [CmdletBinding()] + param( + [Parameter(ValueFromPipeline = $true)] + [PSCustomObject] $Document = $null, + [PSCustomObject] $Knowledge = $null, + [String] $Batch, + [Switch] $Wait = $false, + [Int] $Interval = 5, + [Int] $Timeout = 300 + ) + + begin { + $Documents = @() + } + + process { + foreach ($DocumentObject in $Document) { + $Documents += $DocumentObject + } + } + + end { + if (-not $Documents -and -not $Batch -and -not $Knowledge) { + throw "Document is required" + } + if (-not $Documents -and (-not $Batch -or -not $Knowledge)) { + throw "Batch and Knowledge are required" + } + if ($Documents) { + $Knowledge = Get-DifyKnowledge -Id $Documents[0].KnowledgeId + $Batch = $Documents[0].Batch + } + + $AllDocuments = Get-DifyDocument -Knowledge $Knowledge + + $Now = Get-Date + $WaitUntil = $Now.AddSeconds($Timeout) + + while ($Now -lt $WaitUntil) { + $Endpoint = "$($env:PSDIFY_URL)/console/api/datasets/$($Knowledge.Id)/batch/$Batch/indexing-status" + $Method = "GET" + + try { + $Response = Invoke-DifyRestMethod -Uri $Endpoint -Method $Method -Token $env:PSDIFY_CONSOLE_TOKEN + } + catch { + throw "Failed to obtain indexing status: $_" + } + + $Statuses = @() + foreach ($Status in $Response.data) { + $CurrentDocument = $AllDocuments | Where-Object { $_.Id -eq $Status.id } + $Statuses += [PSCustomObject]@{ + KnowledgeId = $Knowledge.Id + Id = $CurrentDocument.id + Name = $CurrentDocument.name + IndexingStatus = $Status.indexing_status + ProcessingStartedAt = Convert-UnixTimeToLocalDateTime($Status.processing_started_at) + ParsingCompletedAt = Convert-UnixTimeToLocalDateTime($Status.parsing_completed_at) + CleaningCompletedAt = Convert-UnixTimeToLocalDateTime($Status.cleaning_completed_at) + SplittingCompletedAt = Convert-UnixTimeToLocalDateTime($Status.splitting_completed_at) + CompletedAt = Convert-UnixTimeToLocalDateTime($Status.completed_at) + PausedAt = Convert-UnixTimeToLocalDateTime($Status.paused_at) + Error = $Status.error + StoppedAt = Convert-UnixTimeToLocalDateTime($Status.stopped_at) + CompletedSegments = $Status.completed_segments + TotalSegments = $Status.total_segments + } + } + + if (-not $Wait) { + return $Statuses + } + + $InProgress = $false + foreach ($Status in $Statuses) { + if (-not $Status.CompletedAt) { + $InProgress = $true + break + } + } + + if (-not $InProgress) { + return $Statuses + } + + Start-Sleep -Seconds $Interval + $Now = Get-Date + } + return $Statuses + } +} diff --git a/Public/Get-DifyKnowledge.ps1 b/Public/Get-DifyKnowledge.ps1 new file mode 100644 index 0000000..f22246e --- /dev/null +++ b/Public/Get-DifyKnowledge.ps1 @@ -0,0 +1,83 @@ +function Get-DifyKnowledge { + [CmdletBinding()] + param( + [String] $Id = "", + [String] $Name = "", + [String] $Search = "", + [String[]] $Tags = @() + ) + + $Query = @{ + "page" = 1 + "limit" = 100 + } + if ($Search) { + $Query.keyword = $Search + } + if ($Tags) { + $QueryTags = Get-DifyKnowledgeTag -Name $Tags + $QueryTagString = ($QueryTags | ForEach-Object { "tag_ids=$($_.Id)" }) -join "&" + } + + $Members = Get-DifyMember + + $Endpoint = "$($env:PSDIFY_URL)/console/api/datasets" + $Method = "GET" + $Knowledges = @() + $HasMore = $true + while ($HasMore) { + try { + $QueryString = ($Query.GetEnumerator() | ForEach-Object { "$($_.Key)=$($_.Value)" }) -join "&" + if ($QueryTagString) { + $QueryString += "&$QueryTagString" + } + $Uri = "$($Endpoint)?$($QueryString)" + $Response = Invoke-DifyRestMethod -Uri $Uri -Method $Method -Token $env:PSDIFY_CONSOLE_TOKEN + } + catch { + throw "Failed to obtain knowledges: $_" + } + + foreach ($Knowledge in $Response.data) { + $KnowledgeTags = @() + foreach ($Tag in $Knowledge.tags) { + $KnowledgeTags += $Tag.name + } + $CreatedBy = $Members | Where-Object { $_.Id -eq $Knowledge.created_by } | Select-Object -ExpandProperty Email + if (-not $CreatedBy) { + $CreatedBy = $Knowledge.created_by + } + $UpdatedBy = $Members | Where-Object { $_.Id -eq $Knowledge.updated_by } | Select-Object -ExpandProperty Email + if (-not $UpdatedBy) { + $UpdatedBy = $Knowledge.updated_by + } + $KnowledgeObject = [PSCustomObject]@{ + Id = $Knowledge.id + Name = $Knowledge.name + Description = $Knowledge.description + Permission = $Knowledge.permission + AppCount = $Knowledge.app_count + DocumentCount = $Knowledge.document_count + WordCount = $Knowledge.word_count + CreatedBy = $CreatedBy + CreatedAt = Convert-UnixTimeToLocalDateTime($Knowledge.created_at) + UpdatedBy = $UpdatedBy + UpdatedAt = Convert-UnixTimeToLocalDateTime($Knowledge.updated_at) + Tags = $KnowledgeTags + } + if ($Id -and $KnowledgeObject.Id -eq $Id) { + return $KnowledgeObject + } + $Knowledges += $KnowledgeObject + } + + $HasMore = $Response.has_more + $Query.page++ + } + + if ($Name) { + $Knowledges = $Knowledges | Where-Object { $_.Name -eq $Name } + } + + return $Knowledges +} diff --git a/Public/Get-DifyKnowledgeTag.ps1 b/Public/Get-DifyKnowledgeTag.ps1 new file mode 100644 index 0000000..003fa91 --- /dev/null +++ b/Public/Get-DifyKnowledgeTag.ps1 @@ -0,0 +1,9 @@ +function Get-DifyKnowledgeTag { + [CmdletBinding()] + param( + [String[]] $Id = @(), + [String[]] $Name = @() + ) + + return Get-DifyTag -Id $Id -Name $Name -Type "knowledge" +} diff --git a/Public/Get-DifyModel.ps1 b/Public/Get-DifyModel.ps1 index 71d5d7a..f41069e 100644 --- a/Public/Get-DifyModel.ps1 +++ b/Public/Get-DifyModel.ps1 @@ -15,7 +15,7 @@ function Get-DifyModel { } } } - $ValidTypes = @("llm", "text-embedding", "speech2text", "moderation", "tts") + $ValidTypes = @("llm", "text-embedding", "speech2text", "moderation", "tts", "rerank") if ($Type) { foreach ($TypeObj in $Type) { if ($TypeObj -notin $ValidTypes) { diff --git a/Public/Get-DifySystemModel.ps1 b/Public/Get-DifySystemModel.ps1 index e82bfa3..568785f 100644 --- a/Public/Get-DifySystemModel.ps1 +++ b/Public/Get-DifySystemModel.ps1 @@ -3,6 +3,7 @@ function Get-DifySystemModel { param( [String[]] $Type = @() ) + $ValidTypes = @("llm", "text-embedding", "rerank", "speech2text", "tts") if ($Type) { foreach ($TypeObj in $Type) { diff --git a/Public/Get-DifyTag.ps1 b/Public/Get-DifyTag.ps1 index b16efd9..663282a 100644 --- a/Public/Get-DifyTag.ps1 +++ b/Public/Get-DifyTag.ps1 @@ -6,13 +6,16 @@ function Get-DifyTag { [String] $Type = "" ) - $Query = @{} - if ($Type) { - $Query.type = $Type + $ValidTypes = @("knowledge", "app") + if (-not $Type) { + throw "Type is required. Must be one of: $($ValidTypes -join ', ')" } $Endpoint = "$($env:PSDIFY_URL)/console/api/tags" $Method = "GET" + $Query = @{ + "type" = $Type + } $Tags = @() try { $Response = Invoke-DifyRestMethod -Uri $Endpoint -Method $Method -Query $Query -Token $env:PSDIFY_CONSOLE_TOKEN diff --git a/Public/Get-DifyVersion.ps1 b/Public/Get-DifyVersion.ps1 index e672eb5..cf561ba 100644 --- a/Public/Get-DifyVersion.ps1 +++ b/Public/Get-DifyVersion.ps1 @@ -16,6 +16,7 @@ function Get-DifyVersion { $Version = $Response.version return [PSCustomObject]@{ + "Server" = $env:PSDIFY_URL "Version" = $Version } } diff --git a/Public/Initialize-Dify.ps1 b/Public/Initialize-Dify.ps1 index 4277777..48aaa0b 100644 --- a/Public/Initialize-Dify.ps1 +++ b/Public/Initialize-Dify.ps1 @@ -46,6 +46,9 @@ function Initialize-Dify { } if ($SetUpStatus -eq "not_started" -and $InitStatus -eq "not_started") { Write-Verbose "stage to validate init password" + if ($env:PSDIFY_INIT_PASSWORD) { + $InitPassword = ConvertTo-SecureString -String $env:PSDIFY_INIT_PASSWORD -AsPlainText -Force + } if (-not $InitPassword) { $InitPassword = Read-Host -Prompt "Enter init password for $Server" -AsSecureString } diff --git a/Public/Invoke-DifyRestMethod.ps1 b/Public/Invoke-DifyRestMethod.ps1 index 53cc82b..3c7b035 100644 --- a/Public/Invoke-DifyRestMethod.ps1 +++ b/Public/Invoke-DifyRestMethod.ps1 @@ -7,9 +7,14 @@ function Invoke-DifyRestMethod { [Object] $Body = $null, [Hashtable] $Query = $null, [String] $Token = $null, - [Microsoft.PowerShell.Commands.WebRequestSession] $Session = $null + [Microsoft.PowerShell.Commands.WebRequestSession] $Session = $null, + [String] $InFile = $null ) + if ($Uri -notmatch "^https?://") { + throw "No Uri provided. Ensure you have connected to Dify by running Connect-Dify first." + } + $Headers = @{} if ($Token) { $Headers = @{ @@ -20,28 +25,49 @@ function Invoke-DifyRestMethod { $Uri = $Uri + "?" + (($Query.GetEnumerator() | ForEach-Object { "$($_.Key)=$($_.Value)" }) -join "&") } - try { - Write-Verbose "request: $Method $Uri" - if (@("POST", "PUT", "PATCH") -notcontains $Method) { - if ($Session) { - return Invoke-RestMethod -Uri "$Uri" -Method $Method -ContentType $ContentType -Headers $Headers -WebSession $Session -ErrorAction Stop - } - else { - return Invoke-RestMethod -Uri "$Uri" -Method $Method -ContentType $ContentType -Headers $Headers -ErrorAction Stop - } + $RestMethodParams = @{ + Uri = $Uri + Method = $Method + ContentType = $ContentType + Headers = $Headers + ErrorAction = 'Stop' + } + if ($Session) { + $RestMethodParams.WebSession = $Session + } + if (@("POST", "PUT", "PATCH") -contains $Method) { + if ($Body) { + $RestMethodParams.Body = $Body + } + if ($InFile) { + $RestMethodParams.InFile = $InFile + } + } + + if ($env:PSDIFY_DISABLE_SSL_VERIFICATION -eq "true") { + if ($PSVersionTable.PSVersion.Major -ge 6) { + Write-Verbose "Disabling SSL certificate check for PowerShell 6 or higher" + $RestMethodParams.SkipCertificateCheck = $true } else { - if ($Session) { - return Invoke-RestMethod -Uri "$Uri" -Method $Method -ContentType $ContentType -Headers $Headers -Body $Body -WebSession $Session -ErrorAction Stop - } - else { - return Invoke-RestMethod -Uri "$Uri" -Method $Method -ContentType $ContentType -Headers $Headers -Body $Body -ErrorAction Stop + Write-Verbose "Disabling SSL certificate check for PowerShell 5 or lower" + $DefaultCertPolicy = [System.Net.ServicePointManager]::CertificatePolicy + if (-not ([System.Management.Automation.PSTypeName]'PSDifyTrustAllCertsPolicy').Type) { + Add-Type -TypeDefinition "using System.Net; using System.Security.Cryptography.X509Certificates; public class PSDifyTrustAllCertsPolicy : ICertificatePolicy { public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) { return true; } }" } + [System.Net.ServicePointManager]::CertificatePolicy = New-Object -TypeName PSDifyTrustAllCertsPolicy } + } + try { + Write-Verbose "request: $Method $Uri" + return Invoke-RestMethod @RestMethodParams } catch { - if ($PSVersionTable.PSVersion.Major -lt 6) { + if ($PSVersionTable.PSVersion.Major -ge 6) { + throw $_.ErrorDetails.Message + } + else { if ($_.Exception.Response) { $StreamReader = New-Object System.IO.StreamReader($_.Exception.Response.GetResponseStream()) $StreamReader.BaseStream.Position = 0 @@ -53,8 +79,14 @@ function Invoke-DifyRestMethod { throw $_.Exception.Message } } - else { - throw $_.ErrorDetails.Message + } + finally { + if ($InFile) { + Remove-Item $InFile -Force -ErrorAction SilentlyContinue + } + if ($env:PSDIFY_DISABLE_SSL_VERIFICATION -eq "true" -and $PSVersionTable.PSVersion.Major -le 5) { + Write-Verbose "Enabling SSL certificate check for PowerShell 5 or lower" + [System.Net.ServicePointManager]::CertificatePolicy = $DefaultCertPolicy } } } diff --git a/Public/New-DifyAppAPIKey.ps1 b/Public/New-DifyAppAPIKey.ps1 new file mode 100644 index 0000000..ddf484a --- /dev/null +++ b/Public/New-DifyAppAPIKey.ps1 @@ -0,0 +1,32 @@ +function New-DifyAppAPIKey { + [CmdletBinding()] + param( + [Parameter(ValueFromPipeline = $true)] + [PSCustomObject] $App = $null + ) + + end { + if (-not $App) { + throw "App is required" + } + + $Endpoint = "$($env:PSDIFY_URL)/console/api/apps/$($App.Id)/api-keys" + $Method = "POST" + try { + $Response = Invoke-DifyRestMethod -Uri $Endpoint -Method $Method -Token $env:PSDIFY_CONSOLE_TOKEN + } + catch { + throw "Failed to create new api key: $_" + } + + $APIKey = [PSCustomObject]@{ + AppId = $App.Id + Id = $Response.id + Type = $Response.type + Token = $Response.token + LastUsedAt = Convert-UnixTimeToLocalDateTime($Response.last_used_at) + CreatedAt = Convert-UnixTimeToLocalDateTime($Response.created_at) + } + return $APIKey + } +} diff --git a/Public/New-DifyKnowledge.ps1 b/Public/New-DifyKnowledge.ps1 new file mode 100644 index 0000000..3e449a3 --- /dev/null +++ b/Public/New-DifyKnowledge.ps1 @@ -0,0 +1,59 @@ +function New-DifyKnowledge { + [CmdletBinding()] + param( + [String] $Name, + [String] $Description = "" + ) + + if (-not $Name) { + throw "Name is required" + } + + $Endpoint = "$($env:PSDIFY_URL)/console/api/datasets" + $Method = "POST" + $Body = @{ + "name" = $Name + "description" = $Description + } | ConvertTo-Json + try { + $Response = Invoke-DifyRestMethod -Uri $Endpoint -Method $Method -Body $Body -Token $env:PSDIFY_CONSOLE_TOKEN + } + catch { + throw "Failed to create knowledge: $_" + } + + if (-not $Response.id) { + throw "Failed to create knowledge" + } + + $Members = Get-DifyMember + + $KnowledgeTags = @() + foreach ($Tag in $Response.tags) { + $KnowledgeTags += $Tag.name + } + $CreatedBy = $Members | Where-Object { $_.Id -eq $Response.created_by } | Select-Object -ExpandProperty Email + if (-not $CreatedBy) { + $CreatedBy = $Response.created_by + } + $UpdatedBy = $Members | Where-Object { $_.Id -eq $Response.updated_by } | Select-Object -ExpandProperty Email + if (-not $UpdatedBy) { + $UpdatedBy = $Response.updated_by + } + $KnowledgeObject = [PSCustomObject]@{ + Id = $Response.id + Name = $Response.name + Description = $Response.description + Permission = $Response.permission + AppCount = $Response.app_count + DocumentCount = $Response.document_count + WordCount = $Response.word_count + CreatedBy = $CreatedBy + CreatedAt = Convert-UnixTimeToLocalDateTime($Response.created_at) + UpdatedBy = $UpdatedBy + UpdatedAt = Convert-UnixTimeToLocalDateTime($Response.updated_at) + Tags = $KnowledgeTags + } + + return $KnowledgeObject +} diff --git a/Public/New-DifyModel.ps1 b/Public/New-DifyModel.ps1 index 102a8cf..36cb130 100644 --- a/Public/New-DifyModel.ps1 +++ b/Public/New-DifyModel.ps1 @@ -1,5 +1,4 @@ function New-DifyModel { - [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUsePSCredentialType')] [CmdletBinding()] param( [String] $Provider, @@ -31,7 +30,7 @@ function New-DifyModel { "enabled" = $false "configs" = @() } - } | ConvertTo-Json + } | ConvertTo-Json -Depth 10 try { $Response = Invoke-DifyRestMethod -Uri $Endpoint -Method $Method -Body $Body -Token $env:PSDIFY_CONSOLE_TOKEN } @@ -68,7 +67,7 @@ function New-DifyModel { "enabled" = $false "configs" = @() } - } | ConvertTo-Json + } | ConvertTo-Json -Depth 10 try { $Response = Invoke-DifyRestMethod -Uri $Endpoint -Method $Method -Body $Body -Token $env:PSDIFY_CONSOLE_TOKEN } diff --git a/Public/Remove-DifyAppAPIKey.ps1 b/Public/Remove-DifyAppAPIKey.ps1 new file mode 100644 index 0000000..72ee8f8 --- /dev/null +++ b/Public/Remove-DifyAppAPIKey.ps1 @@ -0,0 +1,34 @@ +function Remove-DifyAppAPIKey { + [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')] + param( + [Parameter(ValueFromPipeline = $true)] + [PSCustomObject[]] $APIKey = @() + ) + + begin { + $APIKeys = @() + } + + process { + foreach ($APIKeyObject in $APIKey) { + $APIKeys += $APIKeyObject + } + } + + end { + foreach ($APIKey in $APIKeys) { + $Endpoint = "$($env:PSDIFY_URL)/console/api/apps/$($APIKey.AppId)/api-keys/$($APIKey.Id)" + $Method = "DELETE" + if ($PSCmdlet.ShouldProcess("$($APIKey.Name) ($($APIKey.Id))", "Remove APIKey")) { + try { + $null = Invoke-DifyRestMethod -Uri $Endpoint -Method $Method -Token $env:PSDIFY_CONSOLE_TOKEN + } + catch { + throw "Failed to remove api key: $_" + } + } + } + + return + } +} diff --git a/Public/Remove-DifyKnowledge.ps1 b/Public/Remove-DifyKnowledge.ps1 new file mode 100644 index 0000000..e0d4b8a --- /dev/null +++ b/Public/Remove-DifyKnowledge.ps1 @@ -0,0 +1,34 @@ +function Remove-DifyKnowledge { + [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')] + param( + [Parameter(ValueFromPipeline = $true)] + [PSCustomObject[]] $Knowledge = @() + ) + + begin { + $Knowledges = @() + } + + process { + foreach ($KnowledgeObject in $Knowledge) { + $Knowledges += $KnowledgeObject + } + } + + end { + foreach ($Knowledge in $Knowledges) { + $Endpoint = "$($env:PSDIFY_URL)/console/api/datasets/$($Knowledge.Id)" + $Method = "DELETE" + if ($PSCmdlet.ShouldProcess("$($Knowledge.Name) ($($Knowledge.Id))", "Remove Knowledge")) { + try { + $null = Invoke-DifyRestMethod -Uri $Endpoint -Method $Method -Token $env:PSDIFY_CONSOLE_TOKEN + } + catch { + throw "Failed to remove knowledge: $_" + } + } + } + + return + } +} diff --git a/Public/Send-DifyChatMessage.ps1 b/Public/Send-DifyChatMessage.ps1 new file mode 100644 index 0000000..478b713 --- /dev/null +++ b/Public/Send-DifyChatMessage.ps1 @@ -0,0 +1,86 @@ +function Send-DifyChatMessage { + [CmdletBinding()] + param ( + [Switch] $NewSession = $false, + [String] $Message = "", + [Hashtable] $Inputs = @{} + ) + + if (-not $Message) { + throw "Message is required" + } + if (-not $env:PSDIFY_APP_URL) { + throw "Specify URL of the api server as `$env:PSDIFY_APP_URL before using this cmdlet, e.g. https://api.dify.ai" + } + if (-not $env:PSDIFY_APP_TOKEN) { + throw "Specify the token as `$env:PSDIFY_APP_TOKEN before using this cmdlet" + } + + $ConversationId = if ($NewSession -or -not $env:PSDIFY_CONVERSATION_ID) { "" } else { $env:PSDIFY_CONVERSATION_ID } + $IsNewSession = -not $ConversationId + + # display the message from user + if ($IsNewSession) { + Write-Host -ForegroundColor DarkGray ("-" * 72) + } + Write-Host -BackgroundColor DarkYellow -NoNewline " User " + Write-Host "" + Write-Host -ForegroundColor Yellow $Message + Write-Host "" + + $Endpoint = "$($env:PSDIFY_APP_URL)/v1/chat-messages" + $Method = "POST" + $Body = @{ + "inputs" = $Inputs + "query" = $Message + "response_mode" = "blocking" + "conversation_id" = $ConversationId + "user" = "PSDify" + "files" = @() + } | ConvertTo-Json + $StartTime = Get-Date + try { + $Response = Invoke-DifyRestMethod -Uri $Endpoint -Method $Method -Body $Body -Token $env:PSDIFY_APP_TOKEN + } + catch { + throw "Failed to send chat message: $_" + } + $EndTime = Get-Date + + $env:PSDIFY_CONVERSATION_ID = $Response.conversation_id + + # display the message from dify + Write-Host -BackgroundColor DarkCyan -NoNewline " Dify " + Write-Host "" + Write-Host -ForegroundColor Cyan $Response.answer + Write-Host "" + + # display the metadata + Write-Host -BackgroundColor DarkGray -NoNewline " Meta " + Write-Host "" + if ($IsNewSession) { + Write-Host -ForegroundColor DarkGray "Conversation ID: $($Response.conversation_id) (New)" + } + else { + Write-Host -ForegroundColor DarkGray "Conversation ID: $($Response.conversation_id)" + } + Write-Host -ForegroundColor DarkGray "Duration: $([int]($EndTime - $StartTime).TotalMilliseconds) ms ($($StartTime.ToString("HH:mm:ss"))) to ($($EndTime.ToString("HH:mm:ss")))" + Write-Host "" + + # save the log + $Log = @{ + "Message" = $Message + "Response" = $Response.answer + "ConversationId" = $Response.conversation_id + "IsNewSession" = (-not $ConversationId) + "StartTime" = $StartTime + "EndTime" = $EndTime + "Duration" = [int]($EndTime - $StartTime).TotalMilliseconds + } + if (-not (Test-Path -Path ".\Logs")) { + $null = New-Item -Path ".\Logs" -ItemType Directory + } + $Log | ConvertTo-Json -Compress | Out-File -Append -FilePath ".\Logs\ChatMessages-$(Get-Date -Format 'yyyy-MM-dd').json" + + return +} diff --git a/Public/Set-DifyDSLContent.ps1 b/Public/Set-DifyDSLContent.ps1 new file mode 100644 index 0000000..a02499f --- /dev/null +++ b/Public/Set-DifyDSLContent.ps1 @@ -0,0 +1,27 @@ +function Set-DifyDSLContent { + [CmdletBinding()] + param( + [Parameter(ValueFromPipeline = $true)] + [String] $Content = "", + [String] $Path = "" + ) + + if (-not $Path) { + throw "Path is required" + } + if (-not $Content) { + throw "Content is required" + } + + if (-not (Test-Path -Path $Path)) { + $File = New-Item -Path $Path -ItemType File + } + else { + $File = Get-Item -Path $Path + } + + $UTF8NoBOM = New-Object "System.Text.UTF8Encoding" -ArgumentList @($false) + [System.IO.File]::WriteAllText($File.FullName, $Content, $UTF8NoBOM) + + return $File +} diff --git a/Public/Set-DifySystemModel.ps1 b/Public/Set-DifySystemModel.ps1 index 943af47..53a5d54 100644 --- a/Public/Set-DifySystemModel.ps1 +++ b/Public/Set-DifySystemModel.ps1 @@ -15,13 +15,15 @@ function Set-DifySystemModel { process { foreach ($ModelObject in $Model) { - if ($ModelObject.Type -notin $ValidTypes) { - throw "Invalid value for Type. Must be one of: $($ValidTypes -join ', ')" - } - $Models += @{ - "model_type" = $ModelObject.Type - "provider" = $ModelObject.Provider - "model" = $ModelObject.Model + if ($ModelObjectl) { + if ($ModelObject.Type -notin $ValidTypes) { + throw "Invalid value for Type. Must be one of: $($ValidTypes -join ', ')" + } + $Models += @{ + "model_type" = $ModelObject.Type + "provider" = $ModelObject.Provider + "model" = $ModelObject.Model + } } } } @@ -50,7 +52,7 @@ function Set-DifySystemModel { $Method = "POST" $Body = @{ "model_settings" = @($Models) - } | ConvertTo-Json + } | ConvertTo-Json -Depth 10 Write-Verbose $Body try { $Response = Invoke-DifyRestMethod -Uri $Endpoint -Method $Method -Body $Body -Token $env:PSDIFY_CONSOLE_TOKEN diff --git a/Public/Set-PSDifyConfiguration.ps1 b/Public/Set-PSDifyConfiguration.ps1 new file mode 100644 index 0000000..a0bd26d --- /dev/null +++ b/Public/Set-PSDifyConfiguration.ps1 @@ -0,0 +1,17 @@ +function Set-PSDifyConfiguration { + [CmdletBinding()] + param ( + [Boolean] $IgnoreSSLVerification = $false + ) + + switch ($IgnoreSSLVerification) { + $true { + $env:PSDIFY_DISABLE_SSL_VERIFICATION = "true" + Write-Host -ForegroundColor Yellow "[PSDify] SSL verification is disabled" + } + $false { + Remove-Item Env:\PSDIFY_DISABLE_SSL_VERIFICATION -ErrorAction SilentlyContinue + Write-Host -ForegroundColor Green "[PSDify] SSL verification is enabled" + } + } +} diff --git a/README.ja.md b/README.ja.md index ebfb87c..429b4ad 100644 --- a/README.ja.md +++ b/README.ja.md @@ -1,5 +1,5 @@ -# PSDify: Dify の管理系操作用 PowerShell モジュール +# PSDify: Dify のワークスペース管理用 PowerShell モジュール [🇺🇸 **English**](./README.md) [🇯🇵 **日本語**](./README.ja.md) @@ -17,31 +17,42 @@ - [テスト済み環境](#テスト済み環境) - [クイックスタート](#クイックスタート) - [インストール](#インストール) - - [Dify への接続](#dify-への接続) + - [認証](#認証) - [アプリの管理](#アプリの管理) + - [ナレッジの管理](#ナレッジの管理) - [メンバの管理](#メンバの管理) - [モデルの管理](#モデルの管理) - [コミュニティ版のインスタンスの初期設定](#コミュニティ版のインスタンスの初期設定) ## 概要 -[Dify](https://github.com/langgenius/dify) の、主に管理系操作をコマンドラインから行えるようにすることを目指した PowerShell モジュールです。 +[Dify](https://github.com/langgenius/dify) のワークスペース管理をコマンドラインから行えるようにすることを目指した PowerShell モジュールです。 例として、次のような操作を行えます。 - ✨ **アプリのエクスポートとインポート** +- ✨ **ナレッジの作成とファイルのアップロード** - ✨ **メンバの取得、招待、削除、ロール変更** - ✨ **モデルの追加、システムモデルの変更** - ✨ **コミュニティ版のインスタンスの初期設定** +- ✨ **アプリへのチャットの送信** + +完全な一覧は [📚ドキュメント](./Docs/README.ja.md) を参照してください。 ## テスト済み環境 -- Windows PowerShell (PowerShell 5.1) -- PowerShell 7.4 +| バージョン | Dify
(Community) | Dify
(Cloud) | +| :---: | :---: | :---: | +| 0.12.1 | ✅ PSDify 0.0.1 | ✅ PSDify 0.0.1 | +| 0.11.2 | ✅ PSDify 0.0.1 | ✅ PSDify 0.0.1 | + +> [!NOTE] +> Windows PowerShell (PowerShell 5.1) と PowerShell 7.4 で動作を確認しています。 +> Dify Enterprise Edition(マルチワークスペース環境)はサポートしていません。 ## クイックスタート -利用できるコマンドレットの完全な一覧は [📚ドキュメント](./Docs/README.ja.md) を参照してください。 +利用できるコマンドレットの完全な一覧と、より詳しい使い方は [📚ドキュメント](./Docs/README.ja.md) を参照してください。 ### インストール @@ -49,14 +60,14 @@ Install-Module -Name PSDify ``` -### Dify への接続 +### 認証 ```powershell -# パスワードによる認証(コミュニティ版向け) -Connect-Dify -AuthMethod "Password" -Server "https://dify.example.com" -Email "dify@example.com" - # メールによる認証(クラウド版向け) -Connect-Dify -AuthMethod "Code" -Server "https://dify.example.com" -Email "dify@example.com" +Connect-Dify -AuthMethod "Code" -Email "dify@example.com" + +# パスワードによる認証(コミュニティ版向け) +Connect-Dify -Server "https://dify.example.com" -Email "dify@example.com" ``` ### アプリの管理 @@ -72,6 +83,23 @@ Get-DifyApp | Export-DifyApp Get-Item -Path "DSLs/*.yml" | Import-DifyApp ``` +### ナレッジの管理 + +```powershell +# ナレッジの取得 +Get-DifyKnowledge + +# ナレッジの作成 +New-DifyKnowledge -Name "My New Knowledge" + +# ナレッジへのファイルのアップロード +$Knowledge = Get-DifyKnowledge -Name "My New Knowledge" +Get-Item -Path "Docs/*.md" | Add-DifyDocument -Knowledge $Knowledge + +# アップロード後、インデキシングの完了を待つ +Get-Item -Path "Docs/*.md" | Add-DifyDocument -Knowledge $Knowledge -Wait +``` + ### メンバの管理 ```powershell diff --git a/README.md b/README.md index 1639913..601afb3 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ -# PSDify: A PowerShell Module for Administrative Operations for Dify + +# PSDify: A PowerShell Module for Workspace Management for Dify [🇺🇸 **English**](./README.md) [🇯🇵 **日本語**](./README.ja.md) @@ -9,21 +10,44 @@ > - 🚨 The **Enterprise Edition** of Dify (multi-workspace environments) is **not supported**. > - 🚨 Currently, the focus is on "**making it work**." This means **error handling and documentation are incomplete**, and it does not strictly follow PowerShell best practices. + +## Table of Contents + +- [Overview](#overview) +- [Tested Environments](#tested-environments) +- [Quick Start](#quick-start) + - [Installation](#installation) + - [Connecting to Dify](#connecting-to-dify) + - [Managing Apps](#managing-apps) + - [Managing Knowledge](#managing-knowledge) + - [Managing Members](#managing-members) + - [Managing Models](#managing-models) + - [Initializing a Community Edition Instance](#initializing-a-community-edition-instance) + ## Overview -PSDify is a PowerShell module designed to enable administrative operations for [Dify](https://github.com/langgenius/dify) from the command line. +PSDify is a PowerShell module designed to enable workspace management for [Dify](https://github.com/langgenius/dify) from the command line. Here are some examples of what you can do with PSDify: - ✨ **Export and import apps** +- ✨ **Create knowledge and upload files** - ✨ **Manage members: retrieve, invite, remove, and change roles** - ✨ **Add models and update system models** - ✨ **Initialize instances for the Community Edition** +For a full list of available cmdlets, refer to the [📚Documentation](./Docs/README.md). + ## Tested Environments -- Windows PowerShell (PowerShell 5.1) -- PowerShell 7.4 +| Version | Dify
(Community) | Dify
(Cloud) | +| :---: | :---: | :---: | +| 0.12.1 | ✅ PSDify 0.0.1 | ✅ PSDify 0.0.1 | +| 0.11.2 | ✅ PSDify 0.0.1 | ✅ PSDify 0.0.1 | + +> [!NOTE] +> This module has been tested with Windows PowerShell (PowerShell 5.1) and PowerShell 7.4. +> The Enterprise Edition of Dify (multi-workspace environments) is not supported. ## Quick Start @@ -58,6 +82,23 @@ Get-DifyApp | Export-DifyApp Get-Item -Path "DSLs/*.yml" | Import-DifyApp ``` +### Managing Knowledge + +```powershell +# Retrieve knowledge +Get-DifyKnowledge + +# Create knowledge +New-DifyKnowledge -Name "My New Knowledge" + +# Upload files to knowledge +$Knowledge = Get-DifyKnowledge -Name "My New Knowledge" +Get-Item -Path "Docs/*.md" | Add-DifyDocument -Knowledge $Knowledge + +# Wait for indexing to complete after uploading +Get-Item -Path "Docs/*.md" | Add-DifyDocument -Knowledge $Knowledge -Wait +``` + ### Managing Members ```powershell