{ "cells": [ { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "from langchain.vectorstores import Chroma \n", "from langchain_openai import OpenAIEmbeddings" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "from langchain_core.documents import Document\n", "\n", "documents = [\n", " Document(\n", " page_content=\"Dogs are great companions, known for their loyalty and friendliness.\",\n", " metadata={\"source\": \"mammal-pets-doc\"},\n", " ),\n", " Document(\n", " page_content=\"Cats are independent pets that often enjoy their own space.\",\n", " metadata={\"source\": \"mammal-pets-doc\"},\n", " ),\n", " Document(\n", " page_content=\"Goldfish are popular pets for beginners, requiring relatively simple care.\",\n", " metadata={\"source\": \"fish-pets-doc\"},\n", " ),\n", " Document(\n", " page_content=\"Parrots are intelligent birds capable of mimicking human speech.\",\n", " metadata={\"source\": \"bird-pets-doc\"},\n", " ),\n", " Document(\n", " page_content=\"Rabbits are social animals that need plenty of space to hop around.\",\n", " metadata={\"source\": \"mammal-pets-doc\"},\n", " ),\n", "]" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "import os\n", "db_name = \"test.db\"\n", "persist_directory = os.path.join(\"../vector_embedding\", db_name)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "vector_store = Chroma.from_documents(\n", " documents,\n", " embedding=OpenAIEmbeddings(),\n", " persist_directory= persist_directory\n", ")" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['1a334a0b-6669-4f14-a970-5a53434dbcdb',\n", " '8e2c9a27-80b3-45ef-bee0-637defe360e6',\n", " '884f0d44-8fb6-45e6-9b6c-256dbb5b8620',\n", " '3cc66429-a94c-41bc-a9b0-e2f5f4a7bfbb',\n", " '3c6af7ac-9070-466a-a35d-a6c151080667']" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vector_store.add_documents(documents)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/tmp/ipykernel_28475/485603143.py:1: LangChainDeprecationWarning: Since Chroma 0.4.x the manual persistence method is no longer supported as docs are automatically persisted.\n", " vector_store.persist()\n" ] } ], "source": [ "vector_store.persist()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "from langchain_core.documents import Document\n", "from langchain_core.prompts import ChatPromptTemplate\n", "from langchain_core.runnables import RunnablePassthrough" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "retriever = vector_store.as_retriever(\n", " search_type = \"similarity\",\n", " search_kwargs={\n", " 'k': 2,\n", " }\n", ")" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[Document(metadata={'source': 'mammal-pets-doc'}, page_content='Cats are independent pets that often enjoy their own space.'),\n", " Document(metadata={'source': 'mammal-pets-doc'}, page_content='Dogs are great companions, known for their loyalty and friendliness.')],\n", " [Document(metadata={'source': 'mammal-pets-doc'}, page_content='Dogs are great companions, known for their loyalty and friendliness.'),\n", " Document(metadata={'source': 'fish-pets-doc'}, page_content='Goldfish are popular pets for beginners, requiring relatively simple care.')]]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "retriever.batch([\"cats\", \"food\"])" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "message =\"\"\"\n", "\n", "Write more on the type of animals based on this context only.\n", "\n", "{question}\n", "\n", "Context:\n", "{context}\n", "\"\"\"\n", "\n", "prompt = ChatPromptTemplate.from_messages([(\"human\", message)])" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "from langchain_openai import ChatOpenAI\n", "\n", "model = ChatOpenAI(model=\"gpt-4o-mini\")" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "rag_chain = {\"context\": retriever,\"question\": RunnablePassthrough()} | prompt | model" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Based on the provided context, cats can be characterized as independent animals that typically prefer to have their own space. This independence sets them apart from other pets, such as dogs, which are known for their companionship and loyalty. \n", "\n", "Cats are often solitary hunters by nature, which contributes to their self-sufficient demeanor. They can entertain themselves for extended periods and may not require constant interaction or attention from their owners. This trait makes them suitable for individuals or families with busy lifestyles who may not be able to dedicate as much time to a pet.\n", "\n", "Additionally, cats are known for their grooming habits and often spend a significant amount of time cleaning themselves. They are also playful animals, enjoying toys and engaging in activities that mimic hunting behavior, such as pouncing and chasing.\n", "\n", "In terms of personality, cats can vary greatly. Some may be more social and enjoy interacting with humans, while others may be more reserved and prefer to observe from a distance. Their unique personalities can provide a rich and rewarding experience for pet owners who appreciate their distinctive traits.\n", "\n", "Overall, cats represent a diverse group of animals that thrive on independence while still providing companionship in a more subtle and often more relaxed manner compared to other pets.\n" ] } ], "source": [ "response = rag_chain.invoke(\"tell me about cats\")\n", "\n", "print(response.content)" ] }, { "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.12.6" } }, "nbformat": 4, "nbformat_minor": 2 }