RAG を１０倍速で実装しまっせ！

![blog-01.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3969394/c8888690-5436-65f1-5c44-8d6cbf11a813.png)

一部英語やけれども、英語に強い諸君らは全然問題ないやろうから、このまま行くで！

# 1. カピバラDBのインストール

pip

```bash
pip install capybaradb
```

npm

```bash
npm install capybaradb
```

# 2. シークレットの設定

```
CAPYBARA_API_KEY="your_api_key"
CAPYBARA_PROJECT_ID="your_project_id"
```

これらのシークレット変数は [カピバラDB](https://capybaradb.co)　から無料で取得できまっせ！　

# 3. SDKの立ち上げ

Javascript

```jsx
import { CapybaraDB} from "capybaradb";
import dotenv from "dotenv";

dotenv.config();

const client = new CapybaraDB();
const db = client.db("test");
const collection = db.collection("test");
```

Python

```python
from capybaradb import CapybaraDB
from dotenv import load_dotenv

load_dotenv()

capybara = CapybaraDB()
db = capybara.db("test")
collection = db.collection("test")
```

# 4. サンプルデータの準備

自分で用意したデータセットもしくは以下のサンプルデータセットを使ったらええで。

以下は世界の有名都市、パリ、ニューヨーク、東京、リオデジャネイロに関する簡単なデータセットや。

メタデータは city と bio だけやけれども、好きなように追加してOKやからね。

```jsx
import {EmbText} from "capybaradb";
const docs = [
  {
    city: "Paris",
    bio: new EmbText(
      "Known as the 'City of Light,' Paris is celebrated for its romantic ambiance, iconic landmarks like the Eiffel Tower and Notre-Dame Cathedral, and world-renowned art museums such as the Louvre. The city is a hub of fashion, cuisine, and culture, with charming cafes and picturesque streets that captivate visitors."
    ),
  },Ï
  {
    city: "Tokyo",
    description: new EmbText(
      "A dazzling fusion of tradition and modernity, Tokyo offers skyscrapers, neon-lit streets, and centuries-old temples. The city is famous for its cutting-edge technology, vibrant pop culture, and culinary delights, including sushi and ramen. Tokyo is also a gateway to traditional Japanese customs, such as tea ceremonies and cherry blossom festivals."
    ),
  },
  {
    city: "New York City",
    description: new EmbText(
      "Known as 'The Big Apple,' New York City is a global center for finance, entertainment, and art. Iconic landmarks include Times Square, the Statue of Liberty, and Central Park. The city's diverse neighborhoods, from Manhattan to Brooklyn, are home to a melting pot of cultures and cuisines."
    ),
  },
  {
    city: "Rio de Janeiro",
    description: new EmbText(
      "Set against a backdrop of lush mountains and stunning beaches, Rio is famous for its vibrant Carnaval celebrations, samba music, and the Christ the Redeemer statue. The city offers breathtaking views from Sugarloaf Mountain and is a paradise for beach lovers at Copacabana and Ipanema."
    ),
  },
];
```

Python

```python
from capybaradb import EmbText
docs = [
  {
    city: "Paris",
    bio: EmbText(
      "Known as the 'City of Light,' Paris is celebrated for its romantic ambiance, iconic landmarks like the Eiffel Tower and Notre-Dame Cathedral, and world-renowned art museums such as the Louvre. The city is a hub of fashion, cuisine, and culture, with charming cafes and picturesque streets that captivate visitors."
    ),
  },Ï
  {
    city: "Tokyo",
    description: EmbText(
      "A dazzling fusion of tradition and modernity, Tokyo offers skyscrapers, neon-lit streets, and centuries-old temples. The city is famous for its cutting-edge technology, vibrant pop culture, and culinary delights, including sushi and ramen. Tokyo is also a gateway to traditional Japanese customs, such as tea ceremonies and cherry blossom festivals."
    ),
  },
  {
    city: "New York City",
    description: EmbText(
      "Known as 'The Big Apple,' New York City is a global center for finance, entertainment, and art. Iconic landmarks include Times Square, the Statue of Liberty, and Central Park. The city's diverse neighborhoods, from Manhattan to Brooklyn, are home to a melting pot of cultures and cuisines."
    ),
  },
  {
    city: "Rio de Janeiro",
    description: EmbText(
      "Set against a backdrop of lush mountains and stunning beaches, Rio is famous for its vibrant Carnaval celebrations, samba music, and the Christ the Redeemer statue. The city offers breathtaking views from Sugarloaf Mountain and is a paradise for beach lovers at Copacabana and Ipanema."
    ),
  },
];
```

# 5. データの保存（エンべディングの必要なし！）

普通は保存の前に、データを細かく区切って、エンべディングのAPIを使ってベクトルデータを作って、ベクトルと元のテキストの対応関係がごっちゃならんようにしてって忙しいインやけれども、カピバラDB使ってたらそのまま保存できるで！

データベース側がその辺管理してくれるんやわ。

Javascript

```jsx
const response = await collection.insert(docs);
```

Python

```python
response = collection.insert(docs)
```

# 6. 意味検索でデータの取り出し（エンべディングの必要なし！）

ここでもそのままテキストぶちこむだけで簡単に意味検索できるで！
検索テキストのエンべディングを作ったり、検索してヒットしたベクトルデータを対応するテキストに置き換えたりする必要がないねん。

Javascript

```jsx
const query = "Global city iconic landmarks, cultural diversity, finance, entertainment, art"

const queryResult = await collection.query(query)
```

Python

```python
query = "Global city iconic landmarks, cultural diversity, finance, entertainment, art"

query_result = rescollection.query(query)
```

# 想定されるデータのリスポンス

```bash
{
  matches: [
    {
      chunk: "Known as 'The Big Apple,' New York City is a global center for finance, entertainment, and art. Iconic landmarks include Times Square, the Statue of Liberty, and Central Park. The city's diverse",
      path: 'bio',
      chunk_n: 0,
      score: 0.637317419,
      document: {ObjectId("6764051ace073a82cc8ab6b2")}
    },
    {
      chunk: 'A dazzling fusion of tradition and modernity, Tokyo offers skyscrapers, neon-lit streets, and centuries-old temples. The city is famous for its cutting-edge technology, vibrant pop culture, and',
      path: 'bio',
      chunk_n: 0,
      score: 0.562242568,
      document: {ObjectId("6864051ace073a82cc8ab6de")}
    },
    {
      chunk: "The city's diverse neighborhoods, from Manhattan to Brooklyn, are home to a melting pot of cultures and cuisines.",
      path: 'bio',
      chunk_n: 1,
      score: 0.508277535,
      document: {ObjectId("7764051ace073a82cc8ab6js")}
    }
  ]
}
```

# 8. さらに詳しく・・・

ここまでのチュートリアルでいかにして簡単に関連度の高いテキストデータを取り出すかを学んだらか、もうRAGの山場は超えたで。

あとはLLMのAPIを呼び出す際にプロンプトに取り出した関連度の高いテキストを与えてやることで、RAGを簡単に実装できるからな。

さらに詳しくカピバラDBの使い方をもっと詳しく知りたいという大変意欲の高いデベロッパーの方々は [カピバラDB ドキュメント](https://docs.capybaradb.co)　を見てもうたらええからね。多分９９％くらいの読者の方々は見るんちゃうかな。

# ほな、　Happy building!!