SSブログ

横浜のバラ [ヨコハマ]

イングリッシュローズの庭
IMG_0523.jpg

IMG_0527.jpg

P1140209.jpg

ベーリック・ホール
P1140196.jpg


山手234番館
P1140197.jpg

P1140199.jpg


ホテルニューグランド
IMG_0544.jpg

山下公園
IMG_0537.jpg




最近話題のChatGPTなどのLLM (Large Language Models)の分野では、モデルの他に、LLMを利用してドキュメントの検索を行ったりチャットボットを作るための仕組みを提供するLangChainやLlamaIndexなどが公開されています。

LangChainが、普通のパソコン (大容量メモリやGPUが不要)でも動かせるGPT4ALLやLlama-cppをサポートした(参照: https://python.langchain.com/en/latest/modules/models/llms/integrations.html )ので、メモリ16GBのノートパソコンで、LLukas22/alpaca-native-7B-4bit-ggjt ( https://huggingface.co/LLukas22/alpaca-native-7B-4bit-ggjt )という4.2GBのサイズのモデルと組み合わせて動かしてみました。


# モデルのダウンロード
from huggingface_hub import hf_hub_download

Llama-cpp_MODEL_PATH = "ggjt-model.bin"
# Download the model.
hf_hub_download(repo_id="LLukas22/alpaca-native-7B-4bit-ggjt", 
  filename=Llama-cpp_MODEL_PATH, local_dir=".")

from langchain.llms import LlamaCpp
from langchain import PromptTemplate, LLMChain

# QA用のテンプレートを作る
template = """
Question: {question}
Answer: Let's think step by step.
"""

prompt = PromptTemplate(template=template, 
  input_variables=["question"])
llm = LlamaCpp(model_path=Llama-cpp_MODEL_PATH)

# NSGA上瀬谷基地について聞いてみます。
question = "What is the NSGA Kami seya base ?"

llm_chain.run(question)

"1. First, we need to know what 'NSGA' stands for. It stands for Non-Standard Generalized Array. \n2. Next, we need to know what a 'Kami seya' is. A Kami seya is an ancient Japanese castle tower, usually made of wood and built on the top of a mountain or hill. \n3. Finally, we need to understand how NSGA Kami Seya Base is related to these two things. NSGA Kami Seya Base is an open-source project that provides a general-purpose library for manipulating 2D geometric shapes using linear algebra, which can be used to build 3D computer graphics and animation. This library implements optimized geometry algorithms, such as the ones used in Kami Seya, a traditional Japanese castle tower. These algorithms help reduce computational time and memory usage, allowing users to create complex 3D scenes with minimal resources."

"NSGA Kami Seya Base is an open-source project."とか、予想通り、よくわからない答えが返ってきました。

上瀬谷基地についての情報を得るために、
Navy Information Operations Command (NIOC) Yokosuka, Japanのサイト
https://www.navifor.usff.navy.mil/Organization/Operational-Support/NIOC-Hawaii/NIOC-Hawaii-N3J-Department/About-Us/History/

Naval Computer and Telecommunications Station (NCTS) Yokosuka, Japanのサイト
https://www.navifor.usff.navy.mil/Organization/Operational-Support/NCTS-Far-East-Yokosuka/About-Us/History/
を読み込ませてから、同じ質問をしてみます。

こんな仕組み
LangCain_QA.jpg

from langchain.embeddings import LlamaCppEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import FAISS
from langchain.document_loaders import TextLoader

# Embeddingsの作成、LlamaCppEmbeddings関数は、CPUを利用する場合
# n_threadsでスレッド数を指定できる。
embeddings = LlamaCppEmbeddings(model_path=Llama-cpp_MODEL_PATH,
  n_ctx= 2048, n_threads = 8)

from langchain.document_loaders import TextLoader

# 上瀬谷基地に関するテキストの読み込み
loader = TextLoader('data/kamiseya.txt')
documents = loader.load()
text_splitter = RecursiveCharacterTextSplitter(
    chunk_size = 1000, 
    chunk_overlap = 50,
    length_function = len)
docs = text_splitter.split_documents(documents)

# テキストをFAISSで扱えるベクトルデータにする
db = FAISS.from_documents(docs, embeddings)
db.save_local("faiss_index")

# Use llama-cpp as the LLM for langchain
callback_manager
    = CallbackManager([StreamingStdOutCallbackHandler()])

llm = LlamaCpp(
    model_path=Llama-cpp_MODEL_PATH,
    n_ctx= 2048,
    callback_manager=callback_manager, 
    verbose=True,
    use_mlock=True
)

# 質問文とドキュメントのデータでQAができるようにする。
retriever = db.as_retriever()
qa = RetrievalQA.from_chain_type(llm=llm,
    chain_type="stuff", retriever=retriever)

response = qa.run(query)

print(response)

The National Security Agency's Kami Seya (NSA-K) was a U.S. Navy facility located in Yokosuka, Japan which provided communication services to various naval units and agencies until it was disestablished in October 2014. It is known for being one of the first two National Security Agency (NSA) sites, the other being Fort Meade, Maryland. It was used during World War II as a Fleet Radio Unit, tracking enemy vessels and providing radio communications for U.S. naval forces in the Pacific Ocean area. The National Security Agency's Kami Seya (NSA-K) was a U.S. Navy facility located in Yokosuka, Japan which provided communication services to various naval units and agencies until it was disestablished in October 2014. It is known for being one of the first two National Security Agency (NSA) sites, the other being Fort Meade, Maryland. It was used during World War II as a Fleet Radio Unit, tracking enemy vessels and providing radio communications for U.S. naval forces in the Pacific Ocean area.

上瀬谷基地は、横須賀にあるとか、ちょっと間違っているところもありますが、かなり良い答えが返ってきました。

P1140315.jpg

ChatGPTなどのLLMは、言語モデルだけでなく、言語モデルとテキストやPDF、Microsoft Word、Pandas DataFrame、Slackなどさまざまなデータソースと接続させたり、ユーザーの要求を「どのような手段を使ってどういう順番で解決するか」を 自動的に決定してくれるエージェントと呼ばれる機能と組み合わせる仕組みなどのエコシステムができていて、この分野の進化はまだまだ続きそうですね。


nice!(14)  コメント(0) 

nice! 14

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。