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の覚書

Last updated at Posted at 2024-08-01

langchainとは

LangChainは、LLMを活用した開発のためのフレームワークです。次々と開発される新しい仕組みに追従するため、抽象レイヤーを用いて開発を行うことができます。LangChian自身も開発のスピードが早く、2024/1/6 にバージョン 0.0.354 から 0.1.0 へのアップデートが行われ、LangChain 初の安定バージョンとしてリリースされています。PythonとJavaScriptの両方から利用ができるという点も非常に大きなメリットです。

一方で、0.2からは大きくパッケージの構成なども変わり、0.1系で使われてきた仕組みをそのまま利用することができない場合も多い点には注意が必要となっています。

IMG_0058.jpeg

この辺りの最新の情報のキャッチアップは、

LnagChainの公式ブログ
https://blog.langchain.dev/
などを見ておくと機能追加や、最新情報は理解できるかと思います。

また、下記のawesome-langchainなどでは、langchainを活用したオープンソースなどが紹介されていたりするためユースケースなどを知ることなどができます。

実行環境

簡単に実行するために、GoogleColabを活用します。
また、ブラウザからFlowiseAIというアプリを利用することで手軽にlangchainを実装することもできます。

Langchainの基本的な使い方

Langchainには6つのモジュールが用意されています。

Model/IO...言語モデルを使いやすくする
Retrieval...未知のデータを扱う
Memory...過去の対話を記憶
Chains...複数の処理をまとめる
Agents...自律的に外部と干渉する
Callbacks...イベント発生時の処理を行う

となっています。これから一つずつ事例を書いていこうと思います。

Model/IO...言語モデルを使いやすくする


Retrieval...未知のデータを扱う

!pip install llama-index
!pip install python-dotenv
!pip install llama-index llama-index-readers-web
!pip install html2text
!pip freeze | grep -e "openai" -e "llama" -e "langchain"
import os
from google.colab import userdata
os.environ['OPENAI_API_KEY'] = userdata.get('OPENAI_API_KEY')
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.core import ServiceContext, SQLDatabase, VectorStoreIndex
from llama_index.core.indices.struct_store import SQLTableRetrieverQueryEngine
from llama_index.core.objects import SQLTableNodeMapping, ObjectIndex, SQLTableSchema
from llama_index.legacy import LLMPredictor
from llama_index.core import SummaryIndex
from llama_index.readers.web import SimpleWebPageReader
from IPython.display import Markdown, display
import os
documents = SimpleWebPageReader(html_to_text=True).load_data([
"https://ja.m.wikipedia.org/wiki/%E3%82%AB%E3%83%96%E3%83%88%E3%83%A0%E3%82%B7"
])
index = SummaryIndex.from_documents(documents)
query_engine = index.as_query_engine()
response = query_engine.query("カブトムシの種類を5つ教えて")
display(Markdown(f"<b>{response}</b>"))

IMG_0048.jpeg

IMG_0049.jpeg

IMG_0050.jpeg


Memory...過去の対話を記憶


Chains...複数の処理をまとめる


Agents...自律的に外部と干渉する


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?