見出し画像

Ollama Pythonライブラリで無料のローカルLLMを操作する

Ollama Pythonライブラリを紹介します。

この記事で提示するコードを打鍵していくと、実行できるようにしています。

Ollama Pythonライブラリにより、手軽にPythonでローカルLLMを取り扱うことができます。


最初に、ollamaライブラリをインストールします。

pip install ollama

次に、サンプルコードとして、test.pyとして作成します。

import ollama
response = ollama.chat(model='llama3', messages=[
  {
    'role': 'user',
    'content': 'Why is the sky blue?',
  },
])
print(response['message']['content'])


次に、コードを実行します。

python test.py


少し時間がかかりますが、実行結果は、次のようになりました。

What a great question!

The short answer: The sky appears blue because of the way that light scatters when it enters Earth's atmosphere.

Here's a more detailed explanation:

When sunlight enters our atmosphere, it encounters tiny molecules of gases like nitrogen (N2) and oxygen (O2). These molecules scatter the light in all directions, but they do so more effectively for shorter (blue) wavelengths than longer (red) wavelengths. This is known as Rayleigh scattering, named after the British physicist Lord Rayleigh, who first described the phenomenon in the late 19th century.

As a result of this scattering, the blue light is distributed throughout the atmosphere, giving the sky its blue appearance. The color we see is actually a combination of the direct sunlight and the scattered blue light.

Here are some interesting facts to add more depth to our discussion:

1. **The color of the sky can vary**: Depending on the time of day, atmospheric conditions, and the amount of dust or pollutants in the air, the sky can appear different shades of blue, from pale to deep.
2. **Scattering occurs at all wavelengths**: While Rayleigh scattering favors shorter wavelengths like blue and violet, it also occurs for longer wavelengths like red and orange. However, these longer wavelengths are scattered less efficiently, so they don't dominate the sky's color as much as the blue light does.
3. **Atmospheric conditions can affect the blue color**: For example, when there are more particles in the air (like dust, smoke, or pollution), they can scatter light in all directions, making the sky appear more hazy and reducing its blue intensity.

So, to summarize: The sky appears blue because of the scattering of sunlight by tiny molecules in our atmosphere, which favors shorter wavelengths like blue and violet.


次は、実行結果を完了するまで待たずに、streamingで表示してもらうコードとなります。また、日本語で入力して、日本語で回答してもらいます。

test2.pyに次のコードを記載してください。

import ollama

stream = ollama.chat(
    model='llama3',
    messages=[{'role': 'user', 'content': 'なぜ空は青いのですか?日本語で回答してください。'}],
    stream=True,
)

for chunk in stream:
  print(chunk['message']['content'], end='', flush=True)


test2.pyを実行します。

python test2.py

日本語でも理解されて、日本語で回答も得ています。

なぜ空は青いのか、という問いに答えるためには、まず地球上の光を考慮する必要があります。具体的には、太陽からの光が地球に到達する際における屈現現象(Scattering)によって、青色の部分が優先的に反射されることです。

一方、赤色や黄色などの長波長(Long wavelength)の光は、空気中での吸収や散乱により弱くなるため、青色の光が優勢となります。したがって、晴れの日には青い空を見ることができます。

また、地球上の大気圏においても、ナトリウム(Na)やカリウム(K)の原子が存在し、これらの元素は青緯波長の光を吸収するため、青色の部分が強くなることにも繋がります。

以上のように、空が青い理由には複数の要因が絡み合っており、単に「なぜ空は青いの」ではなく、より詳細に分析する必要があります。🌌


所感は、Ollama PythonライブラリでOllamaを取り扱えることにより手軽にローカルLLMを取り扱えるのはメリットがあります。

また、Ollama Pyhotnライブラリのコードの記載方法はOpenAIのAPIと類似しているところもあり、取り扱いやすいです。

この記事が気に入ったらサポートをしてみませんか?