๐Ÿค– AI/ML ๐ŸŒฟ ์ค‘๊ธ‰

Ollama ๋กœ์ปฌ AI

๋กœ์ปฌ์—์„œ LLM ์‹คํ–‰ํ•˜๊ธฐ

โฑ๏ธ 5์‹œ๊ฐ„ ๐Ÿ‘๏ธ 20 ์กฐํšŒ
๐Ÿ“– ๊ฐ•์ขŒ ๋‚ด์šฉ
# 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}
])
```