0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

RAG を10倍速で実装しまっせ!

Last updated at Posted at 2024-12-24

blog-01.png

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

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

pip

pip install capybaradb

npm

npm install capybaradb

2. シークレットの設定

CAPYBARA_API_KEY="your_api_key"
CAPYBARA_PROJECT_ID="your_project_id"

これらのシークレット変数は カピバラDB から無料で取得できまっせ! 

3. SDKの立ち上げ

Javascript

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

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やからね。

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

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

const response = await collection.insert(docs);

Python

response = collection.insert(docs)

6. 意味検索でデータの取り出し(エンべディングの必要なし!)

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

Javascript

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

const queryResult = await collection.query(query)

Python

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

query_result = rescollection.query(query)

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

{
  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 ドキュメント を見てもうたらええからね。多分99%くらいの読者の方々は見るんちゃうかな。

ほな、 Happy building!!

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?