Ollama上で実行しているPhi-3 miniにPythonから「なぜ空は青いのか」と聞いてみます。
Pythonスクリプト
import requests import json def post_request(): url = "http://localhost:11434/api/generate" data = { "model": "phi3:latest", "prompt": "Why is the sky blue?", "options": { "max_tokens": 500, "temperature": 0.5, } } response = requests.post(url, json=data) answer_text ="" if response.status_code == 200: for line in response.iter_lines(): try: decoded_line = json.loads(line.decode('utf-8')) match decoded_line: case {"response": answer}: answer_text += answer except: pass return answer_text if __name__ == "__main__": result = post_request() print(result)
回答
The sky appears blue to the human eye because of a phenomenon called Rayleigh scattering. As sunlight reaches Earth's atmosphere, it interacts with molecules in the air, which are much smaller than the wavelength of visible light. Shorter wavelengths (blue and violet) are scattered more efficiently by these small particles than longer wavelengths (red, orange). However, even though both blue and violet light are scattered, our eyes are more sensitive to blue light, and because the upper atmosphere absorbs a significant amount of violet light, the sky looks blue instead. Additionally, sunlight reaches us from all directions; thus, we see the sky as blue during daytime when looking upward into the sky's dome. It is also worth noting that at sunrise and sunset, the sky often appears red or orange because the light has to pass through more atmosphere, which scatters shorter wavelengths out of our line of sight, leaving mostly longer wavelength colors like red and orange.
DeepLに翻訳してもらったのがこちらです。
人間の目には空が青く見えるが、これはレイリー散乱と呼ばれる現象によるものだ。太陽光が地球の大気に到達すると、可視光の波長よりもはるかに小さい空気中の分子と相互作用する。波長の短い光(青や紫)は、波長の長い光(赤やオレンジ)よりも、これらの小さな粒子によって効率よく散乱される。 しかし、青と紫の両方の光が散乱されるとはいえ、私たちの目は青い光により敏感であり、上層大気はかなりの量の紫の光を吸収するため、空は青く見える。さらに、太陽光はあらゆる方向から届くため、日中、上空のドームを見上げると空が青く見える。 また、日の出や日没時には、空が赤やオレンジに見えることが多いが、これは光がより多くの大気を通過する必要があるため、短波長の光は視線の外に散乱され、赤やオレンジのような長波長の色が主に残るためである。
補足
こちらのチュートリアルにはこのように書かれています。curl http://localhost:11434/api/generate -d '{
"model": "llama3",
"prompt": "Why is the sky blue?"
}'Pythonで実行するためBingに付属しているCopilotに変換してもらいました。
質問
以下のコマンドを実行するためのpythonスクリプトを書いて下さい。
curl http://localhost:11434/api/generate -d '{
"model": "llama3",
"prompt": "Why is the sky blue?"
}'回答
import requests url = "http://localhost:11434/api/generate" data = { "model": "llama3", "prompt": "Why is the sky blue?" } response = requests.post(url, json=data)
そのまま使えるスクリプトを返してくれました。