Welcome to Mashykom WebSite



Ollama と LangChains を利用して大規模言語モデルを操作

 LangChain は、OpnAI GPT、GoogleのGemini、Meta社のLlama、Anthropic社のClaudeなどの、大規模言語モデル (LLM) を利用して、ユーザーがアプリケーションを構築するためのオープンソースのツールです。MITライセンス下のオープンソースプロジェクトにて開発・頒布されており、MITライセンスに準拠すれば誰でも無料で使うことができます。

 LangChain は、現時点(2024年8月時点)では、PythonとTypeScript(JavaScript)によるライブラリが公開されています。OpenAI社のChatGPTに限らず、GoogleのGemini、Meta社のLlama、Anthropic社のClaudeなど、大規模言語モデルを使い分けて使用することが容易になります。LangChain では、Python、TypeScript、JavaScriptによるライブラリが公開されていますが、LangChainを扱う際、基本的にはPythonを利用することが最も便利です。Pythonは公式ドキュメントやチュートリアルが充実しています。豊富なライブラリとフレームワークも充実しています。

 周知の通り、LLM は、大量のデータで事前にトレーニングされた大規模な深層学習モデルで、質問に対する応答を言語で生成できます。LangChain は、これらの各種LLMのモデルが生成する情報をカスタマイズしたり、正確性や関連性を向上させるためのツールを提供します。なので、以下のような便利な生成系AIアプリケーションを簡単に作成できます。例えば、AIチャットボット、文章やドキュメントの自動要約、AI検索、社内文書などを利用したRPAなどの構築に利用できます。

 Langchain では、ローカルなPCで大規模言語モデルを利用したチャットボットなどを構築するツールの一つとして ollama というパッケージが利用できます。ollama はオープンソースの大規模言語モデル(LLM)をローカルで実行できるオープンソースのツールです。様々なテキスト推論、マルチモーダル、Embeddingモデルを簡単にローカルに実行できます。ollama は llama.cpp と類似のライブラリで、GGUF 形式の量子化モデルであれば、簡単にを取り扱うことができます。

Last updated:2024.9.20





Ollama パッケージのインストールと LLM のダウンロード


 Ollama パッケージをダウンロードします。ダウンロードするためのPCのシステム要件は以下の通りです。
OS: macOS 12+, Linux, Windows 10+
RAM: 最低8GB(16GB以上推奨)
ストレージ: モデルによって異なるが、最低10GB以上の空き容量

 ダウンロードはこのサイトから実行します。ダウンロードしたら、各自のPCにインストールします。基本的には、PCのターミナルのコマンドラインでの操作となります。

 Ollama パッケージがインストールできたら、LLM モデルをダウンロードする必要があります。そのために、以下のコマンドを打ちます。ここでは、Meta-Llama-3-8B-Instruct.Q4_K_M.gguf モデルを使用します。Llama 3 に対応する gguf フォーマット形式のコード Hugging Face のQuantFactoryにあります。


$ollama run llama3.1


(ターミナルでの表示)
pulling manifest
pulling 8eeb52dfb3bb... 100% ▕████████████████▏ 4.7 GB
pulling 948af2743fc7... 100% ▕████████████████▏ 1.5 KB
pulling 0ba8f0e314b4... 100% ▕████████████████▏ 12 KB
pulling 56bb8bd477a5... 100% ▕████████████████▏ 96 B
pulling 1a4c3c319823... 100% ▕████████████████▏ 485 B
verifying sha256 digest
writing manifest
success
>>> from langchain_community.llms import Ollama
... llm = Ollama(model="llama2")
It looks like you're importing the `Ollama` class from the
`langchain_community.llms` module and creating an instance of it. Here's a
breakdown of what this code is doing:
 ---

 このコマンドを実行すると、上のように、ダウンロード終了後、Python 環境下に入ります。続けてプロンプトを入力する必要がないときは、「CNTRL + d」で Python 環境から抜けて下さい。この時点では、ollama はバックグラウンドで起動中となっています。

 ダウンロードできる LLM の具体例については、Ollama の GitHub を参照ください。なお、ollama ライブラリからモデルをollamaで使用するようにカスタマイズするときは、以下のコマンドを使用します。


$ollama pull llama3.1


 この ollama を使った例を試してみましょう。以下のコマンドを打ってみる。Python のプロンプトが出ます。「アメリカの首都はどこですか?」と聞きます。


$ ollama run llama3.1
>>> アメリカの首都はどこですか?

ワシントンD.C.です。

>>> Send a message (/? for help)

 となります。次に、「フィボナッチ数列を表示するPython コードを書いて下さい」と入力します。


>>> フィボナッチ数列を表示するPython コードを書いて下さい
```python
def fibonacci(n):
fib_sequence = [0, 1]
while len(fib_sequence) < n: fib_sequence.append(fib_sequence[-1] + fib_sequence[-2]) return fib_sequence
  print(fibonacci(10)) # Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] ``` このコードは、与えられた数 `n` の長さでフィボナッチ数列を生成します。>>> Send a
  message (/? for help)


 時間はかかりますが、Python コードを書いてくれました。

 モデルを削除するときは以下のコマンドを使います。


# remove the model

$ollama rm llama3.1



ollama で日本語 LLM を利用

 ollamaで用意されているモデルには、日本語大規模言語モデルは含まれていません。例えば、Llama-3-ELYZA-JP-8Bは含まれていません。しかし、追加の設定をすることで、ollamaでカスタムモデルを使えるようになります。ollamaを使えば、ChatGPTのようなインターフェイスで、Llama-3-ELYZA-JP-8Bをローカル環境で動かすことができます。Llama-3-ELYZA-JP-8Bを、Ollama で使用できるようにするための設定について説明します。

 Llama-3-ELYZA-JP-8Bモデルが配置されているディレクトリに、Modelfileを作成し、以下の内容を記入します。


FROM ./Llama-3-ELYZA-JP-8B-q4_k_m.gguf

 次に、Modelfile が保存されているディレクトリに行き、以下のコマンドを入力します。


$ollama create Llama-3-ELYZA-JP-8B-q4_k_m.gguf -f Modelfile


$ollama create Llama-3-ELYZA-JP-8B-q4_k_m.gguf -f Modelfile
transferring model data ⠙
transferring model data 100%
using existing layer sha256:91553c45080b11d95be21bb67961c9a5d2ed7556275423efaaad6df54ba9beae
using autodetected template llama3-instruct
using existing layer sha256:56bb8bd477a519ffa694fc449c2413c6f0e1d3b1c88fa7e3c9d88d3ae49d4dcb
creating new layer sha256:8c5a54bb0798ec86dd17472940220d71508bbc418713efab8777ed0b85079e6f
writing manifest

という表示が出れば完了です。

 Llama-3-ELYZA-JP-8B-q4_k_m.gguf を早速利用してみましょう。以下のコマンドを打ちます。


$ ollama run Llama-3-ELYZA-JP-8B-q4_k_m.gguf


 Python 環境に入り、プロンプトが出ますので、「フィボナッチ数列を表示するコードを書いて下さい」と打ってみましょう。


>>> フィボナッチ数列を表示するコードを書いて下さい
以下はPythonでフィボナッチ数列を表示するコードです。

```
def fibonacci(n):
    if n <= 1:
        return n
    else:
        a, b = 0, 1
        for i in range(2, n+1):
            a, b = b, a + b
        return b

for i in range(10):

>>> 富士山について教えて下さい
富士山は、日本列島の中心に位置する活火山で、標高は3,776メートルです。日本最高峰であると同時に、世界遺産にも登録されています。

特徴:

1. 登頂可能な世界最高峰: 富士山は、登山道が整備され、安全な条件下で登頂することができます。
2. 火山活動が活発: 富士山は、約3万年前から現在までの間に20回以上の噴火を繰り返してきました。直近では、2011年の小規模な噴火がありました。
3. 構成岩石: 富士山の主な構成岩石は安山岩で、上部の緑色岩体は蛇紋岩やチャートです。

登山:

1. 主な登山ルート: 登山道は5つあります。富士スバルライン五合目から須走口、御殿場口、吉田口、馬返し、坂本口と多くのルートがあります。
2. 最高高度点: 富士山の最高高度点は、標高3,776メートルの剣ヶ峰です。

富士山周辺:

---


 というように、返答を返してくれます。このように、日本語LLMも GGUF 形式の量子化モデルに変換すれば、Ollama で利用できます。GGUF 形式の量子化モデルへの変換は、llama.cpp を使用すれば可能です。


ollama-python で日本語 LLM を利用


 ollama-pythonはPython環境でOllamaが使えるオープンソースのライブラリです。ollama-pythonを利用すれば、ローカルPCで、LLMをAPIで簡単に活用できたり、RAGやエージェントへの統合も柔軟に行えます。ollama-python ライブラリはこの GitHub サイトにコードとその説明があります。Python 環境下で以下のようにインストールします。


!pip install ollama


 ollama を「ollama run yourmodel」で起動してから、このライブラリを作動させて下さい。以下の例では、上で紹介した llama3.1 と日本語LLMの Llama-3-ELYZA-JP-8B-q4_k_m.gguf を使用します。以下のコードをJupyter Notebook で実行して下さい。モデルは llama3.1 です。


import ollama
from ollama import chat


messages = [
{
'role': 'user',
'content': 'Why is the sky blue?',
},
]

response = chat('llama3.1', messages=messages)
print(response['message']['content'])


3分35秒ほどかかりますが、以下の答えが返ってきます。


The sky appears blue to us because of a phenomenon called Rayleigh scattering. Here's a simplified explanation:

1. **Sunlight and Atmosphere**: When sunlight enters Earth's atmosphere, it encounters tiny molecules of gases such as
nitrogen (N2) and oxygen (O2). These are much smaller than the wavelength of light.

2. **Scattering**: The nitrogen and oxygen molecules scatter the light in all directions. However, shorter wavelengths
like blue and violet get scattered more because they have more energy compared to longer wavelengths. This scattering
effect is known as Rayleigh scattering, named after the British physicist Lord Rayleigh who first explained it in the
late 19th century.

3. **Why Blue Appears More Prominently**: The sky appears blue due to this scattering effect where shorter wavelengths
like blue and violet are scattered more than other wavelengths. Our eyes perceive blue light because it reaches them
from all directions (since it is scattered everywhere), while red light is not scattered as much, so our eyes see less
of it in comparison.

4. **Time of Day**: The apparent color can change with the time of day due to how sunlight hits the atmosphere at
different angles and how dust particles or pollution might be present in the air. At sunrise and sunset, the sky can
take on hues of red or orange because we are seeing light that has traveled further through the atmosphere.

5. **Variations**: In certain conditions, such as during thunderstorms when there is heavy rain or in areas with high
levels of atmospheric dust, the sky might appear more gray or hazy due to other factors affecting sunlight.

In summary, the sky appears blue because of the scattering of sunlight by nitrogen and oxygen molecules in the
atmosphere, which favors shorter wavelengths like blue.

 次に、日本語LLMの Llama-3-ELYZA-JP-8B-q4_k_m.gguf を使用しましょう。


import ollama
import requests
import json

url = 'http://localhost:11434/api/chat'

data = {
"model":"Llama-3-ELYZA-JP-8B-q4_k_m.gguf",
"messages": [
{
"role": "user",
"content": "プレゼンテーションを作成する時の要点を教えて下さい。"
}
],
"stream": False
}

response = requests.post(url, data=json.dumps(data))
response_data = response.json()
print(response_data)


返答に5分27秒かかります。

{'model': 'Llama-3-ELYZA-JP-8B-q4_k_m.gguf', 'created_at': '2024-09-23T05:10:57.984711Z', 'message': {'role':
'assistant', 'content': 'プレゼンテーションを作成する際の要点は以下の通りです。\n\n1. 目的とターゲット:\n\t* プレゼンテーションの目的は何か?\n\t* 誰に伝えるのか? (ターゲット)\n2.
メッセージの明確化:\n\t* 何を伝えたいのか? (メッセージ)\n\t* 一言で表すと何になるか?\n\n3. ストーリー構築:\n\t* はじめに、結論、中身、最後にという流れで話す\n\t*
登場人物の設定や背景を含むエピソードなどで面白くする\n\n4. 説得力のある内容:\n\t*具体例やデータを用いて説得力を高める\n\t* メリットや利点、デメリットやリスクも伝える\n\n5. 簡潔なスライド作成:\n\t*
一枚のスライドに一つのメッセージを載せる\n\t* 文字は少なくし、画像や図で補う\n\t* 色は二色までに絞り、文字は黒や白にする\n\n6. 聞き手が集中できる環境:\n\t* 蛍光灯の明るさや音量等を調整する\n7.
フィードバックと改善:\n\t* プレゼンテーション後にはフィードバックを求める'}, 'done_reason': 'stop', 'done': True, 'total_duration': 325827355686,
'load_duration': 26091059975, 'prompt_eval_count': 29, 'prompt_eval_duration': 17767156000, 'eval_count': 325,
'eval_duration': 281968192000}

 JSON形式でレスポンスが作成されています。 API の詳しい説明は、このサイトのdocs を参照ください。


LangChain で ollama を利用


 Pythonのインストールコマンド「pip install」を用いて、LangChainのライブラリをインストールしましょう。LangChain の公式ページを参照してください。Juphyter Notebook を使用します。llama3.1 にアクセスするために、コマンド「$ollama run llama3.1」を打って、Ollama が起動している状態で行なって下さい。


!pip install langchain
!pip install langchain-core
!pip install langchain-community


 通常、langchain だけのインストールで langchain-core と langchain-community のインストールも実行されますが念のためにこうします。 langchain のサイトの説明を参照して、Python コードを作成します。


#selecting of the model

from langchain_community.llms import Ollama

model = Ollama(model="llama3.1")


from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_template("tell me a short joke about {topic}")
output_parser = StrOutputParser()

chain = prompt | model | output_parser

print(chain.invoke({"topic": "ice cream"}))


 以下の結果が表示されます。


Here's one:

Why was the ice cream sad?

Because it had a meltdown! (get it?)

 少しコードの説明をします。model = Ollama(model="llama3.1") で model を定義します。prompt は ChatPromptTemplate で定義されています。chain の定義にある「|」 シンボルは、さまざまなコンポーネントを連結し、1 つのコンポーネントからの出力を次のコンポーネントへの入力として渡す Unix パイプ演算子に似ています。この cnain では、ユーザー入力がプロンプト テンプレートに渡され、プロンプトテンプレートの出力がモデルに渡され、モデルの出力が出力パーサーに渡されます。実際に何が起こっているのかの説明は公式サイトの docs の載っています。

Chatbot 用のコードを作成

 次に、少し複雑なコードを作成します。


#selecting of the model

from langchain_community.llms import Ollama

llm = Ollama(model="llama3.1")

#Prompt templates converting raw user input to better input to the LLM

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages([
("system", "あなたは優秀な AI アシスタントです。"),
("user", "{input}")
])

#combine these into a simple LLM chain

chain = prompt | llm
chain.invoke({"input": "以下の質問に答えてください。"})


'質問は何ですか?'

#Let's add a simple output parser to convert the chat message to a string.


from langchain_core.output_parsers import StrOutputParser

output_parser = StrOutputParser()


chain = prompt | llm | output_parser

chain.invoke({"input": "ロサンゼルスはアメリカのどの州にありますか?"})


 返答に34秒ほどかかります。

'ロサンゼルスは、カリフォルニア州にある都市です。'

LangChain で日本語LLM の Llama-3-ELYZA-JP-8B-q4_k_m.gguf を利用


 日本語LLM の Llama-3-ELYZA-JP-8B-q4_k_m.gguf を利用してみます。以下のようなコードを打ちました。


#selecting of the model

from langchain_community.llms import Ollama

llm = Ollama(model="Llama-3-ELYZA-JP-8B-q4_k_m.gguf")

#Prompt templates converting raw user input to better input to the LLM

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages([
("system", "あなたは優秀な AI アシスタントです。"),
("user", "{input}")
])

#combine these into a simple LLM chain

chain = prompt | llm
chain.invoke({"input": "以下の質問に答えてください。"})

#Let's add a simple output parser to convert the chat message to a string.


from langchain_core.output_parsers import StrOutputParser

output_parser = StrOutputParser()


chain = prompt | llm | output_parser

print(chain.invoke({"input": "出雲大社に行く方法について教えて下さい"}))


出雲大社は、島根県出雲市にあります。出雲大社へ行くには、主に以下の交通手段があります。

1. 鉄道:
* JR山陰本線で出雲市駅まで来る。
* 出雲市駅から徒歩約15分で出雲大社前停留所に到着。そこからバスで5分程度で出雲大社の前に到着。
2. バス:
* 出雲市駅から出雲大社行きのバスが運行されています。
* バスは、出雲大社前で降りて徒歩0分で出雲大社に到着します。
3. タクシー:
* 出雲市駅や松江駅などからタクシーを利用することができます。所要時間は約20-30分で料金はおよそ4,000円です。

開放的で広い境内と、神話に登場する神々が祀られている出雲大社は日本有数のパワースポットとして知られています。多くの参拝客が訪れるため、交通手段を事前に調べて計画しておくことをお勧めします。

 返答に3分40秒かかりました。日本語LLM を利用すれば、日本語でプロンプトが書けますので、便利です。


 注意:LangChain を利用するとき、OpenAI と Anthropic のAPI KEY では注意が必要です。 

 OpenAI のケース:有料のアカウントでないと以下のエラーが出ました。 RateLimitError: Error code: 429 - {'error': {'message': 'You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors.', 'type': 'insufficient_quota', 'param': None, 'code': 'insufficient_quota'}}

 Claude 3 のケース:有料アカウントでないと以下のエラーが出ました。BadRequestError: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'Your credit balance is too low to access the Claude API. Please go to Plans & Billing to upgrade or purchase credits.'}}


このページの先頭に戻る