top of page

ICP Ninja & AIを使ったICP dapp開発の強化

  • 執筆者の写真: ICP Japan
    ICP Japan
  • 5月12日
  • 読了時間: 7分

更新日:7月7日

この3部構成のシリーズでは、ICP Ninja — インターネットコンピュータ上でのアプリケーション開発とデプロイメントの方法を変革する革新的なウェブベースの開発環境を探ります。
この記事では、ICP NinjaのAIアシスタントに焦点を当て、コードの説明、修正、最適化を支援することで、どのように開発体験を向上させるかを紹介します。この強力なツールがどのように開発プロセスをスムーズにするか、初心者と経験豊富な開発者のための実践的な例を示します。

ICP Ninjaの概要については、「ICP Ninja: Your Dojo for the Internet Computer」記事をご覧ください。最新のプラットフォーム強化についての詳細は、最初の記事「ICP Ninja update: Coulomb Milestone unleashed」をご覧いただき、プロジェクト共有、認証オプション、クロスチェーン機能についてご確認ください。

今回は、ICP Ninjaに統合されたAIアシスタントがインターネットコンピュータ開発者にとって開発体験をどのように変革するかをご紹介します。この記事では、Coulombマイルストーンの一部として最近追加された強力なAI機能を探り、どのようにこのインテリジェントアシスタントがあなたの個人的なコーディングパートナーとして機能するかを示します。

ICP Ninjaとは?


ICP Ninjaは、ICP上でのdapp開発とデプロイを楽しく、迅速に、教育的に行えるように設計されたウェブベースの開発環境です。いくつかの例示プロジェクトから選ぶか、新しいプロジェクトを開始し、ウェブブラウザで直接dappの構築を始めることができます。

Coulombマイルストーンの一環として、ICP Ninjaに追加された新機能は以下の通りです:

  • 開発者の生産性向上: カニスターのログ、ライブプロジェクトのコンパイルログ、新しいコードエディターが開発者が他の人気IDEと似た環境でより生産的に作業できるようサポートします。
  • AI学習アシスタント: ICPのドキュメントと開発者リソースに特化してトレーニングされたAI学習アシスタントは、ICPに関する質問に答えたり、カニスターコードを説明したり、コードの修正や変更を行ったりします。
  • プロジェクト共有とエクスポート: 作成したプロジェクトを直接リンクで他の人に送信してコードを共有したり、GitHubリポジトリにエクスポートしたりできます。
  • インターネットアイデンティティまたはGitHubでのログインとプロジェクト保存: 好きなアカウントでログインして進行状況を保存し、どのデバイスからでも作業を再開できます。

ICP AI学習アシスタント


ICP Ninjaには、質問に答えたり、さらに学ぶためのリソースへのリンクを提供するだけでなく、個人的なコーディングアシスタントとして機能する新しいICP AI学習アシスタントが搭載されています。コードスニペットの変更、修正、説明を求めることができ、コードを改善して最適化し、プロジェクトがベストプラクティスを満たしていることを確認できます。
以下の「Counter dapp」例を見て、AIがどのように役立つかを確認してみましょう:

actor Counter {

  stable var counter = 0;

  // Get the value of the counter.
  public query func get() : async Nat {
    return counter;
  };

  // Set the value of the counter.
  public func set(n : Nat) : async () {
    counter := n;
  };

  // Increment the value of the counter.
  public func inc() : async () {
    counter += 1;
  };
};

コードの説明


ICP Ninja内で、AIにインタラクションさせたいコードを右クリックし、「Ask AI — Explain」を選択します。この例の場合、AIは以下のような説明を返します:

1. The code defines an actor named Counter. In Motoko, actors are used to create services that can be deployed on the Internet Computer Actors.

2. It declares a stable var counter = 0;. The stable keyword is crucial here as it ensures that the counter variable's value persists across canister upgrades. This is an example of Motoko's orthogonal persistence feature Stable Variables.

3. The actor defines three public functions:

a. get(): This is a query function that returns the current value of the counter. It's marked as public query, which means it can be called from outside the actor and is optimized for fast reads Query and Update Calls.
b. set(n : Nat): This function sets the counter to a specified value n. It's a public update function, meaning it can modify the actor's state.
c. inc(): This function increments the counter by 1. It's also a public update function.

The use of the stable keyword for the counter variable ensures that its value is preserved across canister upgrades. This means that even if you update the canister's code, the counter's value will not be reset to 0.

stableキーワードを使うことで、カウンターの値がカニスターのアップグレードを超えて保持されることを保証しています。これにより、カニスターのコードを更新しても、カウンターの値が0にリセットされることはありません。

コードの修正


コードにエラーがある場合、例えばアクターが定義されていない場合など、AIに修正を求めることができます。「Ask AI — Fix」を選択すると、AIは適切にコンパイルされる修正済みバージョンを返し、変更点を説明します:
This code now defines a complete and functional counter actor for the Internet Computer. Here's what was fixed:

1. The code is now wrapped in an actor Counter { ... } declaration. In Motoko, actors are used to create services that can be deployed on the Internet Computer Actors.

2. The stable var counter = 0; declaration is now correctly placed within the actor. The stable keyword ensures that the counter variable's value persists across canister upgrades Stable Variables.

3. The three public functions (get, set, and inc) are now properly encapsulated within the actor.

This fixed version is now a complete Motoko program that can be compiled and deployed as a canister on the Internet Computer. It provides a simple interface for getting, setting, and incrementing a persistent counter value.


コードの改善


AIにコードを改善してもらう際、コードはベストプラクティスに従って修正され、適用可能な最適化が行われます。最適化の例としては、速度やコストを最適化するために更新呼び出しをクエリ呼び出しに置き換えたり、コードを効率化するために複数の関数を一つにまとめたりすることがあります。

上記のカウンターの例では、AIが「Ask AI — Improve」オプションを選択した際に提案した改善点は以下の通りです:
Improvements made:

1. Added imports for Nat and Error modules from the base library.

2. The inc() function now returns the new counter value, which can be useful for clients.

3. Added a dec() function to decrease the counter. It uses a Result type to handle the case where the counter is already at zero.

4. Added a reset() function to set the counter back to zero.

5. Removed the explicit return statement in the get() function, as Motoko implicitly returns the last expression.

These improvements make the counter more functional and robust. The use of the Result type in the dec() function demonstrates how to handle potential errors in a more idiomatic way in Motoko.

This improved version aligns with best practices seen in various Internet Computer examples, such as the Counter example and the Persistent Storage example, while adding more functionality and error handling.

コードの修正


最後に、AI学習アシスタントにコードを修正してもらうことができます。「Ask AI — Modify」を選択し、コードをどのように変更したいかを指定するプロンプトを入力します。例えば、「カウンターの初期値を100に設定し、カウンターを10ずつ増加させるようにコードを修正してください」と頼むことができます。AIは以下のように修正されたコードを返します:


actor Counter {


  stable var counter : Nat = 100;


  // Get the value of the counter.
  public query func get() : async Nat {
    counter
  };


  // Set the value of the counter.
  public func set(n : Nat) : async () {
    counter := n;
  };


  // Increment the value of the counter by 10.
  public func inc() : async Nat {
    counter += 10;
    counter
  };
};

ICP Ninja AI例プロジェクト


AI学習アシスタントはICP NinjaにおけるAIの唯一のアプリケーションではありません。プラットフォームには新しい例プロジェクトとしてAIチャットボットが登場しました。この例では、Ollamaモデルを使用しており、自分のAIアプリケーションをカスタマイズして作成するための出発点を提供します。ボットの名前やアイコンをカスタマイズしたり、特定のプロンプトに対して指定した応答をプログラムしたりできます。

この例プロジェクトは、ICP上での分散型AI(DeAI)を示しています。カニスターのスマートコントラクトは、オンチェーンまたはオフチェーンで実行されている言語学習モデルと直接やり取りできます。ICPにホストされたAIアプリケーションは改竄不可能、検証可能、停止不可能です。オンチェーンでデプロイされた他のAI例には、顔認識サンプルや画像分類の例があります。

LLMチャットボットの例は、MotokoまたはRustで利用可能です。ぜひ、これを使って開発を開始してみてください。

開発を始める


ICP NinjaでICP dappの開発を始めるには、開発者ドキュメントをご覧いただくか、ICP上での分散型AIについて学んでください。
また、Coulombマイルストーンの詳細についてもご確認いただけます。

Commenti


bottom of page