LLMs/1. Manual RAG Implementation.ipynb
2025-02-27 02:21:19 -08:00

127 lines
4.1 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "51aa96d8-9958-465c-8b0d-cd4f35584307",
"metadata": {},
"outputs": [],
"source": [
"from ollama import chat\n",
"from ollama import ChatResponse\n",
"\n",
"# Define file path \n",
"file_path = r'C:\\Users\\ASUS\\Downloads\\responses.txt'\n",
"\n",
"# First query and save\n",
"response = chat(model='partai/dorna-llama3', messages=[\n",
" {'role': 'user', 'content': 'چرا آسمان آبیست؟?'},\n",
"])\n",
"with open(file_path, 'w', encoding='utf-8') as f:\n",
" f.write(response['message']['content'] + '\\n\\n')\n",
"\n",
"# Second query and append\n",
"response = chat(model='partai/dorna-llama3', messages=[\n",
" {'role': 'user', 'content': 'چرا اینترنت قطع میشه؟'},\n",
"])\n",
"with open(file_path, 'a', encoding='utf-8') as f:\n",
" f.write(response['message']['content'] + '\\n\\n')\n",
"\n",
"print(f\"Responses saved to {file_path}\")\n",
"\n",
"Masih Moafi, [1/24/2025 11:57 PM]\n",
"from ollama import chat, embeddings\n",
"import numpy as np\n",
"from sklearn.metrics.pairwise import cosine_similarity\n",
"\n",
"# 1. Load and chunk document\n",
"with open(r'C:\\Users\\ASUS\\Downloads\\data.txt', 'r', encoding='utf-8') as f:\n",
" text = f.read()\n",
"\n",
"# Split with overlap\n",
"chunk_size = 1000\n",
"overlap = 200\n",
"chunks = [text[i:i+chunk_size] for i in range(0, len(text), chunk_size - overlap)]\n",
"\n",
"# 2. Create embeddings using dorna-llama3\n",
"chunk_embeddings = []\n",
"for chunk in chunks:\n",
" response = embeddings(model='partai/dorna-llama3', prompt=chunk)\n",
" chunk_embeddings.append(response['embedding'])\n",
"\n",
"# 3. Context retrieval system\n",
"def find_relevant_chunks(query, top_k=3):\n",
" # Generate query embedding\n",
" query_embed = embeddings(model='partai/dorna-llama3', prompt=query)['embedding']\n",
" \n",
" # Calculate similarities\n",
" scores = cosine_similarity([query_embed], chunk_embeddings)[0]\n",
" \n",
" # Return top chunks\n",
" best_indices = np.argsort(scores)[-top_k:][::-1]\n",
" return \"\\n---\\n\".join([chunks[i] for i in best_indices])\n",
"\n",
"# 4. RAG-enhanced chat function\n",
"def rag_chat(query):\n",
" # Retrieve context\n",
" context = find_relevant_chunks(query)\n",
" \n",
" # Create augmented prompt\n",
" prompt = f\"\"\"Answer the question using this context:\n",
"{context}\n",
"\n",
"Question: {query}\n",
"Answer clearly and concisely in Persian:\"\"\"\n",
" \n",
" # Get response\n",
" response = chat(model='partai/dorna-llama3', messages=[\n",
" {'role': 'user', 'content': prompt}\n",
" ])\n",
" \n",
" return response['message']['content']\n",
"\n",
"# Example usage\n",
"response = rag_chat(\"چرا اینترنت قطع میشود؟\")\n",
"print(\"پاسخ:\", response)\n",
"\n",
"Masih Moafi, [1/24/2025 11:57 PM]\n",
"from ollama import chat\n",
"from ollama import ChatResponse\n",
"\n",
"# Define file path \n",
"file_path = r'C:\\Users\\ASUS\\Downloads\\responses.txt'\n",
"\n",
"\n",
"response = chat(model='partai/dorna-llama3', messages=[\n",
" {'role': 'user', 'content': 'چرا اینترنت قطع میشه؟'},\n",
"])\n",
"with open(file_path, 'a', encoding='utf-8') as f:\n",
" f.write(response['message']['content'] + '\\n\\n')\n",
"\n",
"print(f\"Responses saved to {file_path}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}