# Ollama ๋ก์ปฌ AI ๊ฐ์ด๋
## 1. Ollama๋?
๋ก์ปฌ์์ LLM์ ์คํํ ์ ์๋ ๋๊ตฌ์
๋๋ค. ์ธํฐ๋ท ์์ด AI๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
## 2. ์ค์น
### macOS
```bash
brew install ollama
```
### Linux
```bash
curl -fsSL https://ollama.com/install.sh | sh
```
### Windows
๊ณต์ ์ฌ์ดํธ์์ ์ค์น ํ์ผ ๋ค์ด๋ก๋
## 3. ๊ธฐ๋ณธ ๋ช
๋ น์ด
```bash
ollama run llama3 # Llama 3 ์คํ
ollama run mistral # Mistral ์คํ
ollama run codellama # ์ฝ๋ ์ ์ฉ ๋ชจ๋ธ
ollama run llava # ์ด๋ฏธ์ง ๋ถ์ ๋ชจ๋ธ
ollama list # ์ค์น๋ ๋ชจ๋ธ ๋ชฉ๋ก
ollama pull llama3 # ๋ชจ๋ธ ๋ค์ด๋ก๋
ollama rm llama3 # ๋ชจ๋ธ ์ญ์
ollama show llama3 # ๋ชจ๋ธ ์ ๋ณด
```
## 4. ์ธ๊ธฐ ๋ชจ๋ธ
| ๋ชจ๋ธ | ํฌ๊ธฐ | ์ฉ๋ |
|------|------|------|
| llama3:8b | 4.7GB | ๋ฒ์ฉ |
| llama3:70b | 40GB | ๊ณ ์ฑ๋ฅ |
| mistral | 4.1GB | ๋น ๋ฅธ ์๋ต |
| codellama | 3.8GB | ์ฝ๋ฉ |
| llava | 4.5GB | ์ด๋ฏธ์ง+ํ
์คํธ |
## 5. API ์ฌ์ฉ
```bash
# ์๋ฒ ์์ (๊ธฐ๋ณธ 11434 ํฌํธ)
ollama serve
```
```python
import requests
response = requests.post("http://localhost:11434/api/generate", json={
"model": "llama3",
"prompt": "Hello, how are you?",
"stream": False
})
print(response.json()["response"])
```
## 6. Python ๋ผ์ด๋ธ๋ฌ๋ฆฌ
```bash
pip install ollama
```
```python
import ollama
response = ollama.chat(model="llama3", messages=[
{"role": "user", "content": "Why is the sky blue?"}
])
print(response["message"]["content"])
# ์คํธ๋ฆฌ๋ฐ
for chunk in ollama.chat(model="llama3", messages=[...], stream=True):
print(chunk["message"]["content"], end="")
```
## 7. ์ปค์คํ
๋ชจ๋ธ (Modelfile)
```
# Modelfile
FROM llama3
SYSTEM You are a helpful Korean assistant. Always respond in Korean.
PARAMETER temperature 0.7
```
```bash
ollama create my-korean-ai -f Modelfile
ollama run my-korean-ai
```
## 8. LangChain ์ฐ๋
```python
from langchain_community.llms import Ollama
llm = Ollama(model="llama3")
response = llm.invoke("Explain quantum computing")
print(response)
```
## 9. GPU ๊ฐ์
NVIDIA GPU๊ฐ ์์ผ๋ฉด ์๋์ผ๋ก GPU ์ฌ์ฉ
```bash
# GPU ๋ฉ๋ชจ๋ฆฌ ํ์ธ
nvidia-smi
# ํ๊ฒฝ ๋ณ์๋ก GPU ์ง์
CUDA_VISIBLE_DEVICES=0 ollama run llama3
```
## 10. ์ค์ ํ์ฉ
### ์ฝ๋ ๋ฆฌ๋ทฐ ๋ด
```python
import ollama
code = open("main.py").read()
response = ollama.chat(model="codellama", messages=[
{"role": "user", "content": f"Review this code:
{code}"}
])
print(response["message"]["content"])
```
### ๋ฌธ์ ์์ฝ
```python
doc = open("document.txt").read()
response = ollama.chat(model="llama3", messages=[
{"role": "system", "content": "Summarize in Korean, 3 bullet points"},
{"role": "user", "content": doc}
])
```