🦙 LlamaIndex.TS : インストールとセットアップ / スターター・チュートリアル (翻訳/解説)
翻訳 : クラスキャット セールスインフォメーション
作成日時 : 11/09/2023
* 本ページは、LlamaIndex.TS の以下のドキュメントを翻訳した上で適宜、補足説明したものです:
* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。
- 人工知能研究開発支援
- 人工知能研修サービス(経営者層向けオンサイト研修)
- テクニカルコンサルティングサービス
- 実証実験(プロトタイプ構築)
- アプリケーションへの実装
- 人工知能研修サービス
- PoC(概念実証)を失敗させないための支援
- お住まいの地域に関係なく Web ブラウザからご参加頂けます。事前登録 が必要ですのでご注意ください。
◆ お問合せ : 本件に関するお問い合わせ先は下記までお願いいたします。
- クラスキャット セールス・マーケティング本部 セールス・インフォメーション
- sales-info@classcat.com ; Website: www.classcat.com ; ClassCatJP
🦙 LlamaIndex.TS : インストールとセットアップ
NPM からインストール
NodeJS v18 またはそれ以上を使用していることを確実にしてください。
npm
npm install llamaindex
Yarn
yarn add llamaindex
pnpm
pnpm add llamaindex
環境変数
この例はデフォルトで OpenAI を使用します。このように Open AI キーをセットアップする必要があります :
export OPENAI_API_KEY="sk-......" # Replace with your key from https://platform.openai.com/account/api-keys
毎回自動的にそれをロードしたい場合には、それを .zshrc/.bashrc に追加します。
WARNING: OpenAI キーをバージョン管理にチェックインしないでください。
🦙 LlamaIndex.TS : スターター・チュートリアル
NPM を使用して LlamaIndex.TS をインストールして OpenAI キーをセットアップしたら、最初のアプリケーションを始める準備ができました。
新しいフォルダで :
npm
npm install typescript
npm install @types/node
npx tsc --init # if needed
Yarn
yarn add typescript
yarn add @types/node
npx tsc --init # if needed
pnpm
pnpm add typescript
pnpm add @types/node
npx tsc --init # if needed
ファイル example.ts を作成します。このコードはサンプルデータをロードし、ドキュメントを作成し、それからインデックスを作成し (これは OpenAI を使用して埋め込みを作成します)、それからデータについての質問に答えるクエリーエンジンを作成します。
// example.ts
import fs from "fs/promises";
import { Document, VectorStoreIndex } from "llamaindex";
async function main() {
// Node 内に abramov.txt からエッセイをロードします
const essay = await fs.readFile(
"node_modules/llamaindex/examples/abramov.txt",
"utf-8",
);
// essay を使用して Document オブジェクトを作成します
const document = new Document({ text: essay });
// テキストをスプリットして埋め込みを作成します。それらを VectorStoreIndex にストアします。
const index = await VectorStoreIndex.fromDocuments([document]);
// Query the index
const queryEngine = index.asQueryEngine();
const response = await queryEngine.query(
"What did the author do in college?",
);
// Output response
console.log(response.toString());
}
main();
Then you can run it using
npx ts-node example.ts
Ready to learn more? Check out our NextJS playground at https://llama-playground.vercel.app/. The source is available at https://github.com/run-llama/ts-playground
以上