LoginSignup
21
17

LangChain TemplatesでLangChainのアプリをお手軽に作る

Last updated at Posted at 2023-11-02

最近、よく作るようなLangChainアプリを簡単に作れるLangChain Templatesというテンプレート機能がLangChainから公開されました。

この記事では実際にテンプレート機能を使ってChromaDBを使ったRAG(Retrieval Augmented Generation)を実現するアプリを10分ほどで作って起動してみます。

LANGCHAIN BLOG - LangChain Templates - OCT 31, 2023

インストール

pipenvのプロジェクトを作成してlangchain cliをインストールします。

pipenv install "langchain-cli[serve]"

ここではpipenvを使ってますが、poetryなど各自好きなものに読み替えてください。

プロジェクト作成 & テンプレート追加

プロジェクトを作成してテンプレートを追加します。

ここではChromaDBでRAGを行うrag-chromaというテンプレートを使ってみます。

pipenv run langchain app new my-app --package rag-chroma

Yを選ぶとテンプレートに追加すべきコードを表示してくれます。今のところ自動で追加してくれるほど賢くはないです。

generate route code for these packages? [Y/n]: Y

Great! Add the following to your app:

rag_chromaテンプレートの場合、以下が表示されます。

from rag_chroma import chain as rag_chroma_chain

add_routes(app, rag_chroma_chain, path="/rag-chroma")

テンプレートの修正

テンプレートを修正して自分のアプリにします。

ここではmy-app/app/server.pyに先ほど表示されたコードを追加する最低限の修正のみを行います。

from fastapi import FastAPI
from langserve import add_routes
from rag_chroma import chain as rag_chroma_chain # <---追加

app = FastAPI()

# Edit this to add the chain you want to add
#add_routes(app, NotImplemented) <--- 削除・コメントアウト
add_routes(app, rag_chroma_chain, path="/rag-chroma") # <---追加

if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, host="0.0.0.0", port=8000)

アプリ(テンプレート)の実行

cd my-app/app/server.py
pipenv run langchain serve
pipenv run langchain serve
INFO:     Will watch for changes in these directories: ['/Users/s/mydev/langpj/my-app']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [74077] using WatchFiles
INFO:     Started server process [74079]
INFO:     Waiting for application startup.

 __          ___      .__   __.   _______      _______. _______ .______     ____    ____  _______
|  |        /   \     |  \ |  |  /  _____|    /       ||   ____||   _  \    \   \  /   / |   ____|
|  |       /  ^  \    |   \|  | |  |  __     |   (----`|  |__   |  |_)  |    \   \/   /  |  |__
|  |      /  /_\  \   |  . `  | |  | |_ |     \   \    |   __|  |      /      \      /   |   __|
|  `----./  _____  \  |  |\   | |  |__| | .----)   |   |  |____ |  |\  \----.  \    /    |  |____
|_______/__/     \__\ |__| \__|  \______| |_______/    |_______|| _| `._____|   \__/     |_______|

LANGSERVE: Playground for chain "/rag-chroma" is live at:
LANGSERVE:  │
LANGSERVE:  └──> /rag-chroma/playground
LANGSERVE:
LANGSERVE: See all available routes at /docs

アプリにアクセス

アプリのURL

とりあえず試す用の画面が表示されます。

アプリの概要

  • harrison worked at kensho 「HarrisonさんがKenshoで働いている」と書かれたドキュメントがChromaDBに入ってます
  • AIはドキュメントの内容だけをもとに回答せよと指示されています。
  • したがってharrisonさんやkensho関連の質問をしましょう。他の質問には答えられません。

実行結果

InputでHarrisonはどこで働いてるか聞くとOutputで答えが出力されます。Intermediate stepsはRAGの途中経過が表示されます。

スクリーンショット 2023-11-02 22.58.02.png

別の質問です。

スクリーンショット 2023-11-02 22.58.49.png

APIドキュメント

OpenAPIのドキュメントが表示されます。

全てがREST APIになっているので好きなフレームワークでフロントエンドを書けば良いです。

スクリーンショット 2023-11-02 22.47.48.png

テンプレートの種類

2023年11月2時点で約50ほどのテンプレートがあります。

まとめ

こんな感じで10分くらいでLangChainとChromaでRAGを実現するアプリを起動できて便利です。

21
17
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
21
17