Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions README_FA.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# کتابخانه پایتون Ollama - مستندات فارسی

کتابخانه رسمی پایتون برای اجرای مدل‌های زبانی بزرگ (LLM) به صورت کاملا لوکال و آفلاین با [Ollama](https://ollama.com).

[English README](./README.md)

این مستندات برای جامعه فارسی‌زبان و توسعه‌دهندگان ایرانی نوشته شده تا سریع‌ترین راه برای ساخت چت‌بات فارسی، پردازش فاکتور و RAG را داشته باشند.

## 1. نصب و راه‌اندازی

```bash
pip install ollama
ollama serve
ollama pull llama3.1:8b
```

## 2. شروع سریع - اولین چت فارسی

```python
import ollama

response = ollama.chat(model='llama3.1:8b', messages=[
{
'role': 'user',
'content': 'سلام! یک شعر کوتاه درباره تهران بگو.'
},
])

print(response['message']['content'])
```

## 3. چت استریم (برای ربات تلگرام و وب‌سایت)

```python
import ollama

stream = ollama.chat(
model='llama3.1:8b',
messages=[{'role': 'user', 'content': 'درباره هوش مصنوعی توضیح بده'}],
stream=True,
)

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

## 4. چت‌بات با حافظه مکالمه

```python
import ollama

messages = []

while True:
user_input = input("شما: ")
if user_input == "خروج":
break
messages.append({'role': 'user', 'content': user_input})
response = ollama.chat(model='llama3.1:8b', messages=messages)
bot_reply = response['message']['content']
print(f"ربات: {bot_reply}")
messages.append({'role': 'assistant', 'content': bot_reply})
```

## 5. پردازش تصویر - خواندن فاکتور فارسی با LLaVA

```python
import ollama

response = ollama.chat(
model='llava:7b',
messages=[{
'role': 'user',
'content': 'این تصویر یک فاکتور فارسی است. مبلغ کل و تاریخ را استخراج کن.',
'images': ['./factor.jpg']
}]
)

print(response['message']['content'])
```

## 6. نسخه Async برای FastAPI

```python
import asyncio
from ollama import AsyncClient

async def main():
client = AsyncClient()
response = await client.chat(model='llama3.1:8b', messages=[
{'role': 'user', 'content': 'سلام'}
])
print(response['message']['content'])

asyncio.run(main())
```

## 7. راهنمای سخت‌افزار

| مدل | RAM لازم | VRAM لازم |
| :--- | :--- | :--- |
| llama3.1:8b | 8GB | 6GB |
| llava:7b | 8GB | 6GB |
| llama3.1:70b | 64GB | 40GB |

ساخته شده برای کامیونیتی برنامه‌نویسان فارسی
44 changes: 44 additions & 0 deletions examples/persian_chatbot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import ollama

def persian_chatbot():
"""
یک چت‌بات فارسی تعاملی با قابلیت استریم و حافظه مکالمه
برای تست: python examples/persian_chatbot.py
"""
print("چت‌بات فارسی Ollama (برای خروج 'خروج' بنویسید)")
print("="*50)

messages = [
{
'role': 'system',
'content': 'تو یک دستیار فارسی باهوش، مودب و دقیق هستی. همیشه به زبان فارسی جواب بده.'
}
]

while True:
user_input = input("\nشما: ")
if user_input.lower() in ['خروج', 'exit', 'q', 'quit']:
print("خدانگهدار!")
break

messages.append({'role': 'user', 'content': user_input})

print("ربات: ", end="")
full_response = ""

try:
stream = ollama.chat(model='llama3.1:8b', messages=messages, stream=True)
for chunk in stream:
content = chunk['message']['content']
print(content, end='', flush=True)
full_response += content
print()
except Exception as e:
print(f"\nخطا: {e}")
print("آیا مطمئنید ollama serve روشن است؟")
continue

messages.append({'role': 'assistant', 'content': full_response})

if __name__ == "__main__":
persian_chatbot()