🤖
GraphAIをNode.jsで試す
GraphAIのNode.jsで試す
GraphAIを動作させるのに必要な最小限のnpmはgraphaiとvanilla agents です。
最も簡単に使えるAgentである@graphai/vanilla (他にnpmの依存がないagentをvanillaと呼んでいます)を使って簡単なGraphData
を作り、動かしてみます。
最初にnpmの初期化をして、必要なnpmを入れます。typescriptを実行するためにts-nodeも入れます。
npm init
yarn add graphai @graphai/vanilla ts-node
以下が最小限のスクリプトです。graphai.tsで保存します。
このGraphData
には2つのnodeが含まれます。
Static node
のnode1
とComputed node
のnode2
です。
Static node
のnode1
で、固定のメッセージを定義します。
そのデータを受け取ったComputed node
のnode2
でcopyAgentを実行します。
copyAgent
はinputs
で受け取ったデータをそのまま返却値とて返すagentです。
node2
はisResult: true
がセットされているので、GraphAIの実行結果としてnode2
の結果を返します。
TypeScriptのresult変数にその結果がセットされます。
import { GraphAI } from "graphai";
import * as agents from "@graphai/vanilla";
const graph_data = {
version: 0.5,
nodes: {
node1: {
value: "hello, GraphAI",
},
node2: {
agent: "copyAgent",
inputs: {text: ":node1"},
isResult: true,
},
},
};
export const main = async () => {
const graph = new GraphAI(graph_data, agents);
const result = await graph.run();
console.log(JSON.stringify(result));
};
if (process.argv[1] === __filename) {
main();
}
実行します
$ npx ts-node graphai.ts
{"node2": {text: "hello, GraphAI"}}
node1
で指定して文字列がnode2
に渡され、結果として表示されました。

人工知能を活用したアプリケーションやサービスを活用し、内発的動機付けで行動するエンジニア、起業家、社会起業家をサポートするコミュニティーです。 singularitysociety.org Supported by 週刊 Life is beautiful
Discussion