つばろぐ

主に C#, .NET, Azure の備忘録です。たまに日記。

Teams AI Libraryを試して、Azure OpenAI Serviceに苦戦した

devblogs.microsoft.com

この記事で紹介されていた Teams AI Library を試してみました。
具体的にはこのクイックスタートガイドです。

learn.microsoft.com

手順を進める上で苦戦した内容をまとめます。

1. アプリフォルダの位置が違う

次のコマンドを実行して、アプリ フォルダーに移動します。
cd samples/js/samples/04.ai.a.teamsChefBot

実際は js/samples/04.ai.a.teamsChefBot です。

2. VS Code の既定のターミナル設定の影響を受けた

私は以前から Git Bash を既定のターミナルとして使用していたため、VS Code の設定 (settings.json) に次の設定を入れていました。

"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\sh.exe",

しかし 04.ai.a.teamsChefBotデバッグ実行すると、どうやらシェルの空白入りのパスが解釈できないようで、実行に失敗しました。
/d が Dドライブと誤解されたようです。

実行するタスク: npm run dev:teamsfx 

/d: /d: Is a directory

 *  ターミナル プロセス "C:\Program Files\Git\bin\sh.exe /d /c npm run dev:teamsfx" が終了コード 126 で終了しました。 
 *  ターミナルはタスクで再利用されます、閉じるには任意のキーを押してください。

根本原因としては VS Code"terminal.integrated.shell.windows" という設定自体がもう古い設定のようです。
いまは "terminal.integrated.defaultProfile.windows" という設定項目となるようです。

"terminal.integrated.defaultProfile.windows": "Git Bash"

3. Node v18 ではエラーが起きる

Node v18 をインストールしている環境では、VS Codeデバッグを始めると次のエラーが起きました。
なので Node v16 を使うようにしました。

> teamschef-demo@1.0.0 dev:teamsfx
> env-cmd --silent -f .localSettings yarn dev

yarn run v1.22.19
$ nodemon --watch ./src --exec node --inspect=9239 --signal SIGINT -r ts-node/register ./src/index.ts
[nodemon] 1.19.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): src\**\*
[nodemon] watching extensions: ts,json
[nodemon] starting `node --inspect=9239 -r ts-node/register ./src/index.ts`
Debugger listening on ws://127.0.0.1:9239/60de0cef-e726-4090-b289-1a8c44117461
For help, see: https://nodejs.org/en/docs/inspector
C:\Users\yuta\source\github\teams\teams-ai\js\samples\04.ai.a.teamsChefBot\node_modules\restify\lib\request.js:848
    Request.prototype.closed = function closed() {
                             ^
TypeError: Cannot set property closed of #<Readable> which has only a getter
    at patch (C:\Users\yuta\source\github\teams\teams-ai\js\samples\04.ai.a.teamsChefBot\node_modules\restify\lib\request.js:848:30)
    at Object.<anonymous> (C:\Users\yuta\source\github\teams\teams-ai\js\samples\04.ai.a.teamsChefBot\node_modules\restify\lib\server.js:33:1)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Object.require.extensions.<computed> [as .js] (C:\Users\yuta\source\github\teams\teams-ai\js\samples\04.ai.a.teamsChefBot\node_modules\ts-node\src\index.ts:1608:43)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Function.Module._load (node:internal/modules/cjs/loader:958:12)
    at Module.require (node:internal/modules/cjs/loader:1141:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at Object.<anonymous> (C:\Users\yuta\source\github\teams\teams-ai\js\samples\04.ai.a.teamsChefBot\node_modules\restify\lib\index.js:10:14)
[nodemon] app crashed - waiting for file changes before starting...

似たような Issue があったので報告しておきました。

github.com

4. マニフェストファイルが見つからない

Teams アプリのマニフェストファイルは appManifestフォルダ に含まれていますが、パッケージを展開するための YAMLファイル (teamsapp.local.yml) では appPackage フォルダを参照しているようです。

github.com

とりえず appManifestフォルダを複製して、appPackage フォルダを用意することでこのエラーは解消し、Teams アプリを追加できるようになりました。

5. Azure OpenAI Service に対応できない

元々このサンプルが使用するのが ChatGPT であることに途中で気付いたので、Azure OpenAI Service に対応させようとしてみました。結果的にはうまくいっていません。

同様の Issue があったので読みながら作業をしてみました。

github.com

JS SDK のファイルを確認すると AzureOpenAIPlannerクラス があったので、以下の部分を Azure OpenAI Service に合わせました。

github.com

const planner = new AzureOpenAIPlanner({
    apiKey: process.env.OPENAI_API_KEY,
    defaultModel: 'text-davinci-003',
    logRequests: true,
    endpoint: 'https://xxx.openai.azure.com', // 実際は自分のリソースのエンドポイント
    apiVersion: '2023-05-15'
});

ボットにメッセージを送ってみると 401 エラーとなりました。

 [onTurnError] unhandled error: AxiosError: Request failed with status code 401

後続のコメントOpenAIModerator にもエンドポイントが必要だということで、足しました。

const moderator = new OpenAIModerator({
    apiKey: process.env.OPENAI_API_KEY,
    moderate: 'both',
    endpoint: 'https://xxx.openai.azure.com', // 実際は自分のリソースのエンドポイント
});

github.com

ボットにメッセージを送ってみると、今度は 404 エラーとなりました。おそらくエンドポイントが違うんでしょう。

 [onTurnError] unhandled error: AxiosError: Request failed with status code 404

とりあえずここで時間切れとして、一旦諦めました。
ChatGPT ではなく Azure OpenAI Service を使いたいので、このサンプルをちゃんと動かしたいなーと思ってはいます。