OpenAI / LangChain / LlamaIndexとの統合

1. 位置づけと全体像

Pinecone は大規模な高次元ベクトルをミリ秒単位で検索できるマネージド・サービスです。OpenAI の埋め込みモデルで生成したベクトルを Pinecone に保存し、LangChain あるいは LlamaIndex が提供する高水準の Retriever/Query Engine と組み合わせることで、RAG(Retrieval-Augmented Generation)やエージェント型アプリを迅速に構築できます。これら 3 つを組み合わせた典型的なデータフローは次のとおりです。

  1. インジェスト:

    • ドキュメントをチャンク化

    • OpenAI Embedding API(例: text-embedding-3-small)でベクトル化

    • Pinecone に upsert(メタデータ・名前空間を付与)

  2. 検索:

    • クエリを同じ埋め込みモデルでベクトル化

    • Pinecone query(メタデータフィルタや Hybrid Search も可)

  3. 生成:

    • 取得したコンテキストを LLM(GPT-4 など)へプロンプトとして渡し回答を生成

このワークフローは「OP スタック」とも呼ばれます。docs.pinecone.io


2. OpenAI × Pinecone 直接統合

ステップ 主要 API 補足
依存パッケージ pinecone-client[grpc], openai Serverless 版 Pinecone を使う場合は ServerlessSpec を指定
ベクトル生成 client.embeddings.create text-embedding-3-small=1536 次元(低レイテンシ)、-3-large=3072 次元(高精度)
インデックス作成 pc.create_index(dimension=…) Metric は OpenAI 埋め込みなら dotproduct が推奨
ベクトル投入 index.upsert([...]) ID は文字列。高頻度更新時はバッチ (≤100) が推奨
クエリ index.query(vector=[xq], top_k=…) include_metadata=True で元文取得

ベストプラクティス

  • 埋め込みは 100〜512 件ずつまとめてリクエストして OpenAI の QPS 制限を回避

  • Serverless 環境では初回アクセス時に数秒のウォームアップが発生するため定期的に describe_index_stats でヘルスチェックを行う

  • 名前空間を活用してテナント分離し、メタデータフィルタでファインチューニング不要のトピック制御が可能

docs.pinecone.iolinkedin.com


3. LangChain との統合

3.1 PineconeVectorStore の利用

python
from langchain_pinecone import PineconeVectorStore from langchain_openai import OpenAIEmbeddings emb = OpenAIEmbeddings() vector_store = PineconeVectorStore.from_documents( docs, index_name="my-index", embedding=emb, namespace="finance" )
  • from_documents / from_texts は自動でベクトル化+upsert を実行

  • 既存インデックスを再利用する場合は from_existing_index

  • 追加データは add_documentsadd_texts

3.2 検索と RAG チェーン

python
from langchain.chains import RetrievalQA qa_chain = RetrievalQA.from_chain_type( llm=ChatOpenAI(), retriever=vector_store.as_retriever(search_kwargs={"k": 5}) ) qa_chain({"query": "自社の退職金制度を教えて"})
  • PineconeHybridSearchRetriever を指定すると BM25+ベクトルのハイブリッド検索が可能python.langchain.com

  • 会話履歴を保持したい場合は ConversationalRetrievalChain を利用

ポイント

  • namespacemetadata により LangChain 側のフィルタと Pinecone のフィルタが一致

  • LangSmith を併用すると RAG パイプライン全体をトレースしやすい

docs.pinecone.io


4. LlamaIndex との統合

4.1 IngestionPipeline に PineconeVectorStore を組み込む

python
from llama_index.ingestion import IngestionPipeline from llama_index.vector_stores.pinecone import PineconeVectorStore from pinecone.grpc import PineconeGRPC, ServerlessSpec from llama_index.node_parser import SemanticSplitterNodeParser from llama_index.embeddings import OpenAIEmbedding pc = PineconeGRPC(api_key=PINECONE_API_KEY) index = pc.Index("llama-index") vector_store = PineconeVectorStore(pinecone_index=index) pipeline = IngestionPipeline( transformations=[ SemanticSplitterNodeParser(embed_model=OpenAIEmbedding()), OpenAIEmbedding(), ], vector_store=vector_store, ) pipeline.run(documents=my_docs)

4.2 検索・生成

python
from llama_index.core import VectorStoreIndex from llama_index.core.retrievers import VectorIndexRetriever from llama_index.core.query_engine import RetrieverQueryEngine vs_index = VectorStoreIndex.from_vector_store(vector_store) retriever = VectorIndexRetriever(index=vs_index, similarity_top_k=5) engine = RetrieverQueryEngine(retriever=retriever) print(engine.query("HNSW の階層構造の利点は?"))

4.3 利用時の注意

  • SemanticSplitterNodeParser を使うと意味単位でチャンク化され、高精度検索が可能

  • 複数の VectorStore をまとめて Ensemble Retriever にすると属性ごとに Pinecone の namespace を切り替えなくても済む

  • LlamaIndex 0.11 から llama-index-vector-stores-pinecone が公式拡張として分離されているため、バージョンの整合性に注意

docs.pinecone.io


5. 発展的トピック

機能 概要
Pinecone Assistant API (2025-01) Pinecone が RAG エージェント作成を簡素化する API を公開。LangChain の LangGraph と併用し、複数ツール間メモリを Pinecone に永続化できるblocksandfiles.com
Serverless Gen 2 自動スケール/コスト最適化された新アーキテクチャ。リージョンは awsgcpazure を選択可。低頻度アクセスでも課金が抑えられるruntime.news
OpenAI 新埋め込み v3 text-embedding-3-small/large に対応。既存 index は dimension を 1536 または 3072 に合わせて再作成する必要があるpinecone.io

6. まとめと次のステップ

  1. プロトタイプ段階では LangChain または LlamaIndex の高水準 API を使い、OpenAI Embeddings+Pinecone Serverless でまず動かす。

  2. 運用設計では名前空間によるマルチテナント分離とメタデータスキーマを確定させ、LangSmith などでパイプラインを可視化。

  3. 最適化段階で Hybrid Search、再ランク(OpenAI re-rank)や Pinecone Assistant を追加して品質を向上。

これにより、スケーラブルかつ拡張性の高い RAG/生成 AI アプリケーションを短期間で構築できます。

ChatGPT4o 生成日:2025/06/20