1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

はじめに

Pinconeはベクトルを保存できるデータベースです。文章や画像、音声等のデータの特徴量を抽出し、ベクトル変換して保存する仕組みです。1年?2年前?くらいから単語だけは聞いた事があったのですが、余裕が出てきたので触っていこうと思います!!

前準備

pip install pinecone-client

pineconeのクライアントツールをインストールします。

ソースコード

sample.python
import os
from dotenv import load_dotenv
from pinecone import Pinecone, ServerlessSpec

# .envファイルから環境変数を読み込む
load_dotenv()

# 環境変数からAPIキーを取得
api_key = os.getenv("PINECONE_API_KEY")

# Pineconeクライアントのインスタンスを作成
pc = Pinecone(api_key=api_key)

# インデックス名
index_name = "example-index"

# インデックスが存在しない場合に作成
if index_name not in pc.list_indexes().names():
    pc.create_index(
        name=index_name,
        dimension=3,  # ベクトルの次元数
        metric='euclidean',
        spec=ServerlessSpec(
            cloud='aws',
            region='us-east-1'  # us-west-2ではなく、us-east-1を使用
        )
    )

# インデックスに接続
index = pc.Index(index_name)

# ベクトルデータの挿入
index.upsert(vectors=[
    ("id1", [0.1, 0.2, 0.3]),
    ("id2", [0.4, 0.5, 0.6]),
    ("id3", [0.7, 0.8, 0.9])
])

# ベクトルデータの検索
query_result = index.query(
    vector=[0.1, 0.2, 0.3],
    top_k=2
)
print(query_result)

実行
~/develop/python$ python3 pinecone_example.py
/Users/kawamurakouji/Library/Python/3.9/lib/python/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020
  warnings.warn(
{'matches': [{'id': 'id1', 'score': 2.98023224e-08, 'values': []},
             {'id': 'id2', 'score': 0.27000016, 'values': []}],
 'namespace': '',
 'usage': {'read_units': 5}}

スクリーンショット 2024-07-13 15.03.30.png

スクリーンショット 2024-07-13 15.04.59.png

確かに、DBに保存できていますね。ただ、難しいのは特徴量を抽出する事、類似度の計算時に特徴量を計算する事。この2点をどうやって扱えばいいのか。。。そこがまだ全然イメージついていないです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?