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

LangChain・LlamaIndexをFlatkey AIに接続してマルチモデル対応にする方法

0
Posted at

はじめに

LangChainやLlamaIndexで開発していると、こんな場面が出てきます。

  • 「このタスクはClaudeの方が精度が高い」
  • 「コストを下げるためにDeepSeekを試したい」
  • 「モデルを切り替えるたびに設定を変えるのが面倒」

Flatkey AIはOpenAI互換のAPIゲートウェイです。LangChainとLlamaIndexはどちらもOpenAI互換エンドポイントをサポートしているため、base_url を1行変えるだけでFlatkey経由のマルチモデル接続に切り替えられます。


LangChain + Flatkey

インストール

pip install langchain langchain-openai

基本接続

from langchain_openai import ChatOpenAI

# Flatkey AI 経由でClaudeを使う
llm = ChatOpenAI(
    model="anthropic/claude-sonnet-4-5",
    api_key="sk-あなたのFlatKeyキー",
    base_url="https://router.flatkey.ai/v1"
)

response = llm.invoke("Pythonでシングルトンパターンを実装してください")
print(response.content)

モデルを動的に切り替える

from langchain_openai import ChatOpenAI

FLATKEY_CONFIG = {
    "api_key": "sk-あなたのFlatKeyキー",
    "base_url": "https://router.flatkey.ai/v1"
}

def get_llm(task: str) -> ChatOpenAI:
    model_map = {
        "coding":      "anthropic/claude-sonnet-4-5",
        "analysis":    "anthropic/claude-opus-4-8",
        "translation": "openai/gpt-4o-mini",
        "reasoning":   "deepseek/deepseek-r1",
    }
    return ChatOpenAI(
        model=model_map.get(task, "anthropic/claude-sonnet-4-5"),
        **FLATKEY_CONFIG
    )

# タスク別に最適なモデルを自動選択
coding_llm   = get_llm("coding")
analysis_llm = get_llm("analysis")

RAGチェーンの例

from langchain_openai import ChatOpenAI
from langchain.chains import RetrievalQA

# LLM(Flatkey経由)
llm = ChatOpenAI(
    model="anthropic/claude-sonnet-4-5",
    api_key="sk-あなたのFlatKeyキー",
    base_url="https://router.flatkey.ai/v1"
)

# RAGチェーン構築
qa_chain = RetrievalQA.from_chain_type(
    llm=llm,
    retriever=vectorstore.as_retriever(search_kwargs={"k": 3}),
)

result = qa_chain.invoke({"query": "製品の返品ポリシーを教えてください"})
print(result["result"])

LlamaIndex + Flatkey

インストール

pip install llama-index llama-index-llms-openai

基本接続

from llama_index.llms.openai import OpenAI
from llama_index.core import Settings

# Flatkey AI 経由でモデルを設定
Settings.llm = OpenAI(
    model="anthropic/claude-sonnet-4-5",
    api_key="sk-あなたのFlatKeyキー",
    api_base="https://router.flatkey.ai/v1"
)

QueryEngineの例

from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.llms.openai import OpenAI
from llama_index.core import Settings

Settings.llm = OpenAI(
    model="anthropic/claude-sonnet-4-5",
    api_key="sk-あなたのFlatKeyキー",
    api_base="https://router.flatkey.ai/v1"
)

# ドキュメントをロードしてインデックスを作成
documents = SimpleDirectoryReader("./docs").load_data()
index = VectorStoreIndex.from_documents(documents)

# クエリ実行
query_engine = index.as_query_engine()
response = query_engine.query("このドキュメントの要点を3つにまとめてください")
print(response)

LangChain vs LlamaIndex の接続比較

LangChain LlamaIndex
パラメータ名 base_url api_base
クラス名 ChatOpenAI OpenAI
モデル指定 model= model=
切り替えの手軽さ ◎ ◎

どちらも既存コードの変更は最小限です。


Flatkey AIで始める

招待リンクから登録すると $10のテスト額度が追加付与されます(クレカ登録不要)。

👉 https://console.flatkey.ai/sign-up?aff=BunB


まとめ

  • LangChain は ChatOpenAI(base_url=...) で即接続
  • LlamaIndex は OpenAI(api_base=...) で即接続
  • model パラメータを変えるだけでClaude/GPT/DeepSeekを使い分け可能
  • 全モデルのコストをFlatkey AIのダッシュボードで一元管理
0
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
0
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?