Skip to content

Update requirements.txt #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 1, 2025
Merged
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
251 changes: 249 additions & 2 deletions LLM/09_rag_langchain.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"#from langchain_community.vectorstores.chroma import Chroma\n",
Expand Down Expand Up @@ -703,6 +705,251 @@
" print(f\"An error occurred: {str(e)}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Ipywidgets Implementation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import ipywidgets as widgets\n",
"from IPython.display import display, clear_output\n",
"import time\n",
"import os\n",
"import sys\n",
"\n",
"# Set up widgets for the interface\n",
"source_type_dropdown = widgets.Dropdown(\n",
" options=['url', 'local'],\n",
" value='local',\n",
" description='Source Type:',\n",
" style={'description_width': 'initial'},\n",
" layout=widgets.Layout(width='300px')\n",
")\n",
"\n",
"source_input = widgets.Text(\n",
" description='Source:',\n",
" placeholder='Enter URL or filename from data folder',\n",
" style={'description_width': 'initial'},\n",
" layout=widgets.Layout(width='600px')\n",
")\n",
"\n",
"file_list = widgets.Select(\n",
" options=[],\n",
" description='Available Files:',\n",
" disabled=False,\n",
" layout=widgets.Layout(width='400px', height='150px', display='none')\n",
")\n",
"\n",
"question_input = widgets.Text(\n",
" placeholder='Ask a question about the document...',\n",
" layout=widgets.Layout(width='800px')\n",
")\n",
"\n",
"submit_button = widgets.Button(\n",
" description='Ask',\n",
" button_style='primary',\n",
" layout=widgets.Layout(width='100px')\n",
")\n",
"\n",
"exit_button = widgets.Button(\n",
" description='Exit',\n",
" button_style='danger',\n",
" layout=widgets.Layout(width='100px')\n",
")\n",
"\n",
"model_status = widgets.HTML(value=\"<b>Status:</b> Ready to load document\")\n",
"response_area = widgets.Output(layout=widgets.Layout(border='1px solid #ddd', padding='10px', width='900px', height='300px'))\n",
"\n",
"# Main widget container (for easier cleanup)\n",
"main_container = widgets.VBox()\n",
"\n",
"# Setup components\n",
"embedding_fn = initialize_embedding_fn()\n",
"vector_store = None\n",
"qachain = None\n",
"\n",
"model_name_or_path = \"TheBloke/Llama-2-7B-Chat-GGUF\"\n",
"model_basename = \"llama-2-7b-chat.Q4_K_M.gguf\"\n",
"MODEL_PATH = hf_hub_download(repo_id=model_name_or_path, filename=model_basename)\n",
"chat_model = create_llm(MODEL_PATH)\n",
"\n",
"# Create base prompt template\n",
"prompt_template = \"\"\"\n",
"Answer the question based on the context below. Keep your answer concise.\n",
"If you don't know, just say \"I don't know.\"\n",
"\n",
"Context: {context}\n",
"\n",
"Question: {question}\n",
"Answer:\n",
"\"\"\"\n",
"prompt = PromptTemplate(template=prompt_template, input_variables=[\"context\", \"question\"])\n",
"chain_type_kwargs = {\"prompt\": prompt}\n",
"\n",
"# Function to update file list\n",
"def update_file_list():\n",
" data_dir = os.path.join(os.getcwd(), \"data\")\n",
" if os.path.exists(data_dir):\n",
" files = os.listdir(data_dir)\n",
" file_list.options = files\n",
" if files:\n",
" file_list.value = files[0]\n",
" else:\n",
" file_list.options = [\"No files found in data directory\"]\n",
"\n",
"# Handler for source type change\n",
"def source_type_changed(change):\n",
" if change['new'] == 'local':\n",
" update_file_list()\n",
" file_list.layout.display = 'block'\n",
" else:\n",
" file_list.layout.display = 'none'\n",
"\n",
"# Handler for file selection\n",
"def file_selected(change):\n",
" if change['new'] and source_type_dropdown.value == 'local':\n",
" source_input.value = change['new']\n",
"\n",
"# Widget handlers\n",
"def load_document_button_click(b):\n",
" global vector_store, qachain\n",
" \n",
" with response_area:\n",
" clear_output()\n",
" source = source_input.value\n",
" source_type = source_type_dropdown.value\n",
" \n",
" if not source:\n",
" print(\"Please enter a source URL or filename\")\n",
" return\n",
" \n",
" model_status.value = \"<b>Status:</b> Loading document...\"\n",
" try:\n",
" # Get or create embeddings for the current document\n",
" vector_store = get_or_create_embeddings(source, source_type, embedding_fn)\n",
" \n",
" # Setup retriever and chain\n",
" retriever = vector_store.as_retriever(search_kwargs={\"k\": 4})\n",
" qachain = chains.RetrievalQA.from_chain_type(\n",
" llm=chat_model,\n",
" retriever=retriever,\n",
" chain_type=\"stuff\",\n",
" chain_type_kwargs=chain_type_kwargs,\n",
" return_source_documents=False\n",
" )\n",
" \n",
" model_status.value = \"<b>Status:</b> Model ready! Ask your questions.\"\n",
" print(\"Document loaded successfully! You can now ask questions.\")\n",
" except Exception as e:\n",
" model_status.value = f\"<b>Status:</b> Error loading document\"\n",
" print(f\"Error: {str(e)}\")\n",
"\n",
"class WidgetStreamHandler(BaseCallbackHandler):\n",
" def __init__(self, output_widget):\n",
" self.output_widget = output_widget\n",
" self.generated_text = \"\"\n",
" \n",
" def on_llm_new_token(self, token, **kwargs):\n",
" self.generated_text += token\n",
" with self.output_widget:\n",
" clear_output(wait=True)\n",
" print(self.generated_text)\n",
"\n",
"def ask_question_button_click(b):\n",
" question = question_input.value\n",
" \n",
" if not question:\n",
" with response_area:\n",
" clear_output()\n",
" print(\"Please enter a question\")\n",
" return\n",
" \n",
" if vector_store is None or qachain is None:\n",
" with response_area:\n",
" clear_output()\n",
" print(\"Please load a document first\")\n",
" return\n",
" \n",
" with response_area:\n",
" clear_output()\n",
" model_status.value = \"<b>Status:</b> Generating response...\"\n",
" stream_handler = WidgetStreamHandler(response_area)\n",
" \n",
" try:\n",
" start_time = time.time()\n",
" qachain.invoke(\n",
" {\n",
" \"query\": question,\n",
" \"max_tokens\": 512,\n",
" \"temperature\": 0.7\n",
" },\n",
" config={\"callbacks\": [stream_handler]}\n",
" )\n",
" elapsed = time.time() - start_time\n",
" model_status.value = f\"<b>Status:</b> Response generated in {elapsed:.2f} seconds\"\n",
" \n",
" except Exception as e:\n",
" model_status.value = \"<b>Status:</b> Error generating response\"\n",
" print(f\"Error: {str(e)}\")\n",
"\n",
"def exit_application(b):\n",
" # Clear all widget outputs\n",
" with response_area:\n",
" clear_output()\n",
" \n",
" # Clean up resources\n",
" global vector_store, qachain\n",
" vector_store = None\n",
" qachain = None\n",
" \n",
" # Clear the main container and show exit message\n",
" main_container.children = [widgets.HTML(\"<h3>RAG application has been closed. You can run this cell again to restart.</h3>\")]\n",
"\n",
"# Connect handlers\n",
"source_type_dropdown.observe(source_type_changed, names='value')\n",
"file_list.observe(file_selected, names='value')\n",
"\n",
"load_document_button = widgets.Button(\n",
" description='Load Document',\n",
" button_style='info',\n",
" layout=widgets.Layout(width='150px')\n",
")\n",
"load_document_button.on_click(load_document_button_click)\n",
"submit_button.on_click(ask_question_button_click)\n",
"exit_button.on_click(exit_application)\n",
"\n",
"# Initialize the file list if \"local\" is selected\n",
"if source_type_dropdown.value == 'local':\n",
" update_file_list()\n",
" file_list.layout.display = 'block'\n",
"\n",
"# Create button row\n",
"button_row = widgets.HBox([question_input, submit_button, exit_button])\n",
"\n",
"# Create header with title and exit button\n",
"header = widgets.HTML(\"<h3>RAG Interactive Interface</h3>\")\n",
"\n",
"# Build the main container\n",
"main_container.children = [\n",
" header,\n",
" widgets.HBox([source_type_dropdown, source_input, load_document_button]),\n",
" file_list,\n",
" button_row,\n",
" model_status,\n",
" response_area\n",
"]\n",
"\n",
"# Display the interface\n",
"display(main_container)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -771,7 +1018,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.11.12"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion LLM/rag/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
langchain==0.3.19
langchain==0.3.24
langchain-community==0.3.23
langchain-core==0.3.56
langchain-huggingface==0.1.2
Expand Down