0
0

More than 1 year has passed since last update.

WSL2/Ubuntu20.04にSemantic Kernelをインストールし、サンプルを起動する

Last updated at Posted at 2023-06-24

はじめに

LangChainを一通り触ってみたところで、そろそろSemantic Kernelにも手を出さないといけないなということで、インストールしてみましたが、思ったより面倒だったので記録します。

TL;DR

Semantic Kernelのインストールが、というよりnpmyarnのインストールに苦戦したので、半分くらいはその解消方法です。

前提

環境

  • Windows WSL2 Ubuntu 20.04
  • VSCode

要件

ドキュメントによると、以下のインストールが必要です。

  • gitが操作できるもの(git, GitHubアプリ)
  • VSCodeかVisual Studio

事前準備

  • OpenAIのAPIキーか、Azure OpenAI Serviceの認証情報をメモしておく

チュートリアルを実施するには以下が追加で必要です。

  • .Net 7 SDKが使えること
  • VS Codeなどで、Polyglot Notebookが使えること

インストール

ではインストールしていきます。Visual Studioを使っている人は上級者な気がするので、以降はVSCodeで解説します。

  • gitやVSCodeをインストールします
  • https://github.com/microsoft/semantic-kernel をクローンします
  • VSCodeでクローンしたsemantic-kernelのフォルダを開きます
  • VSCodeの拡張機能(Extensions)で、.NET 7 SDKをインストールします
  • VSCodeの拡張機能(Extensions)で、Polyglot Notebookをインストールします

Kernelサーバーを起動させる

VSCodeでターミナルを立ち上げ、semantic-kernel/samples/dotnet/KernelHttpServer階層でfunc start --csharpとすれば動くはずですが、筆者の環境では動きませんでした。ちなみに、APIキーなどはアプリの方で入力するようでこちらでは不要でした。

$ func start --csharp
/mnt/c/Users/*****/AppData/Roaming/npm/func: 12: node: not found

node.jsが入っていないようなのでインストールしますが、筆者の場合sudo apt install nodejsだとうまく入らなかったのでnvmを使いました。(インストール方法はこちら

こんどこそ動くはず……

node:events:491
      throw er; // Unhandled 'error' event
      ^

Error: spawn /mnt/c/Users/******/AppData/Roaming/npm/node_modules/azure-functions-core-tools/bin/func ENOENT
    at ChildProcess._handle.onexit (node:internal/child_process:283:19)
    at onErrorNT (node:internal/child_process:476:16)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
Emitted 'error' event on ChildProcess instance at:
    at ChildProcess._handle.onexit (node:internal/child_process:289:12)
    at onErrorNT (node:internal/child_process:476:16)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'spawn /mnt/c/Users/******/AppData/Roaming/npm/node_modules/azure-functions-core-tools/bin/func',
  path: '/mnt/c/Users/******/AppData/Roaming/npm/node_modules/azure-functions-core-tools/bin/func',
  spawnargs: [ 'start', '--csharp' ]
}

Node.js v18.16.1

が動かず。どうもWindows用のパスを見に行っているようなので、Windows用Node.jsをアンインストールします。また、azure-functions-core-toolsがうまくないようなのでインストールしなおします。

azure-functions-core-toolsが問題だったようなので、Windows用のNode.jsのアンインストールは必要だったのかわかりません。

$ npm uninstall -g azure-functions-core-tools

up to date in 191ms
$ npm install -g azure-functions-core-tools
npm WARN deprecated debug@4.1.1: Debug versions >=3.2.0 <3.2.7 || >=4 <4.3.1 have a low-severity ReDos regression when used in a Node.js environment. It is recommended you upgrade to 3.2.7 or 4.3.1. (https://github.com/visionmedia/debug/issues/797)

added 35 packages in 1m

3 packages are looking for funding
  run `npm fund` for details

メッセージを見るに、元々入っていなかったのかな?もう一度実行してみると、

$ func start --csharp
MSBuild version 17.6.3+07e294721 for .NET
  Determining projects to restore...
  Restored /home/canada/work/semantic-kernel/samples/dotnet/github-skills/GitHubSkills.csproj (in 11.76 sec).
  Restored /home/canada/work/semantic-kernel/samples/dotnet/KernelHttpServer/KernelHttpServer.csproj (in 19.09 sec).
  GitHubSkills -> /home/canada/work/semantic-kernel/samples/dotnet/KernelHttpServer/bin/output/GitHubSkills.dll
  KernelHttpServer -> /home/canada/work/semantic-kernel/samples/dotnet/KernelHttpServer/bin/output/KernelHttpServer.dll
  Determining projects to restore...
  Restored /tmp/a0nxaynq.myh/WorkerExtensions.csproj (in 9.67 sec).
  WorkerExtensions -> /tmp/a0nxaynq.myh/buildout/Microsoft.Azure.Functions.Worker.Extensions.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:54.56



Azure Functions Core Tools
Core Tools Version:       4.0.5198 Commit hash: N/A  (64-bit)
Function Runtime Version: 4.21.1.20667

[2023-06-24T04:04:09.604Z] Found /home/canada/work/semantic-kernel/samples/dotnet/KernelHttpServer/KernelHttpServer.csproj. Using for user secrets file configuration.

Functions:

        CreatePlan: [POST] http://localhost:7071/api/planner/createplan

        ExecutePlan: [POST] http://localhost:7071/api/planner/execute/{maxSteps?}

        InvokeFunction: [POST] http://localhost:7071/api/skills/{skillName}/invoke/{functionName}

        Ping: [GET] http://localhost:7071/api/ping

For detailed output, run func with --verbose flag.
[2023-06-24T04:04:12.874Z] Worker process started and initialized.
[2023-06-24T04:04:16.506Z] Host lock lease acquired by instance ID '000000000000000000000000A2755F17'.

ちゃんと動きました。こちらは後で使うので起動したままにしておきます。

アプリの起動

semantic-kernel/samples/apps/chat-summary-webapp-reactを実行させてみます。

こちらは下準備が必要で、.env.exampleというファイルを.envにリネームして必要な認証情報(OpenAI APIならAPIキー)を入力します。Azure OpenAI Service用の項目もあるので必要ないところはそのままで大丈夫です。

yarn installyarn startで起動するそうなのですが、yarnが入っていません。

入れてみますが、

$ sudo apt install yarn
……
$ yarn install
00h00m00s 0/0: : ERROR: [Errno 2] No such file or directory: 'install'

やはりうまくいきません。

このようなサイトを見つけたのでその通りにしてみます(一応あやしいURLからダウンロードしようとしてないかは確認しています)。

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update 
sudo apt install yarn

このあとyarn install, yarn startするとやっと動きました……。

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0