{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "os.environ['LANGCHAIN_TRACING_V2'] = 'true'\n", "os.environ['LANGCHAIN_ENDPOINT'] = 'https://api.smith.langchain.com'\n", "# os.environ['LANGCHAIN_API_KEY'] = <your api key>\n", "\n", "os.environ['OPENAI_API_BASE'] = 'http://192.168.1.7:1234/v1/'\n", "os.environ['OPENAI_API_KEY'] = 'lm-studio'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load documents" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "\n", "from langchain_community.document_loaders import TextLoader\n", "\n", "\n", "data_path = Path(\"vs_data\")\n", "\n", "documents = [\n", " TextLoader(doc_path).load()[0]\n", " for doc_path in data_path.iterdir()\n", "]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Split documents" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "from langchain.text_splitter import RecursiveCharacterTextSplitter\n", "text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(\n", " chunk_size=300, \n", " chunk_overlap=50)\n", "\n", "splits = text_splitter.split_documents(documents)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Index" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "from langchain_openai import OpenAIEmbeddings\n", "from langchain_community.vectorstores import Chroma\n", "\n", "vectorstore = Chroma.from_documents(documents=splits, \n", " embedding=OpenAIEmbeddings(model='nomic-ai/nomic-embed-text-v1.5-GGUF', check_embedding_ctx_length=False, timeout=0.1, max_retries=50))\n", "\n", "retriever = vectorstore.as_retriever()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create chain" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'The evolution of Lightning Ring is Thunder Loop. To get Thunder Loop, you need to evolve Lightning Ring with Duplicator.'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from langchain.prompts import ChatPromptTemplate\n", "from langchain_core.output_parsers import StrOutputParser\n", "from langchain_openai import ChatOpenAI\n", "from langchain_core.runnables import RunnablePassthrough\n", "\n", "template = \"\"\"Answer the question based on the context below. If the\n", "question cannot be answered using the information provided answer\n", "with \"I don't know\".\n", "\n", "Context: {context}\n", "\n", "Question: {question}\n", "\n", "Answer: \"\"\"\n", "\n", "prompt = ChatPromptTemplate.from_template(template)\n", "\n", "llm = ChatOpenAI(temperature=0)\n", "\n", "def format_docs(docs):\n", " return \"\\n\\n\".join(doc.page_content for doc in docs)\n", "\n", "rag_chain = (\n", " {\"context\": retriever | format_docs, \n", " \"question\": RunnablePassthrough()} \n", " | prompt\n", " | llm\n", " | StrOutputParser()\n", ")\n", "\n", "rag_chain.invoke(\"What is the evolution of Lightning ring and how do i get it?\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.10.12" } }, "nbformat": 4, "nbformat_minor": 2 }