{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#!python3 -m pip install --upgrade langchain deeplake openai tiktoken" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "from getpass import getpass\n", "\n", "os.environ[\"OPENAI_API_KEY\"] = getpass()\n", "# Please manually enter OpenAI Key" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "os.environ[\"ACTIVELOOP_TOKEN\"] = getpass()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "DEEPLAKE_ACCOUNT_NAME = \"YOUR-ACCOUNT-NAME\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from langchain.document_loaders import WebBaseLoader\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "loader = WebBaseLoader(\"https://docs.modular.com/mojo/programming-manual.html\")\n", "docs = loader.load()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from langchain.text_splitter import CharacterTextSplitter\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n", "texts = text_splitter.split_documents(docs)\n", "print(f\"{len(texts)}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from langchain.embeddings.openai import OpenAIEmbeddings\n", "\n", "embeddings = OpenAIEmbeddings()\n", "embeddings\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from langchain.vectorstores import DeepLake\n", "\n", "db = DeepLake.from_documents(\n", " texts, embeddings, dataset_path=f\"hub://{DEEPLAKE_ACCOUNT_NAME}/mojo-docs\"\n", ")\n", "db" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "db = DeepLake(\n", " dataset_path=f\"hub://{DEEPLAKE_ACCOUNT_NAME}/mojo-docs\",\n", " read_only=True,\n", " embedding_function=embeddings,\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "retriever = db.as_retriever()\n", "retriever.search_kwargs[\"distance_metric\"] = \"cos\"\n", "retriever.search_kwargs[\"fetch_k\"] = 20\n", "retriever.search_kwargs[\"maximal_marginal_relevance\"] = True\n", "retriever.search_kwargs[\"k\"] = 20" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from langchain.chat_models import ChatOpenAI\n", "from langchain.chains import ConversationalRetrievalChain\n", "\n", "model = ChatOpenAI(model=\"gpt-3.5-turbo\") # 'ada' 'gpt-3.5-turbo' 'gpt-4',\n", "qa = ConversationalRetrievalChain.from_llm(model, retriever=retriever)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-> **Question**: What is the difference between def and fn? \n", "\n", "**Answer**: From an interface level, def and fn are always interchangeable, meaning that there is nothing a ‘def’ can provide that a fn cannot (or vice versa). However, the difference between the two is that a fn is more limited and controlled on the inside of its body. Fns have a number of limitations compared to defs, such as more strict scoping, explicit type annotations, and immutability by default for argument passing. Fns are designed to provide more control and predictability for systems programming, while defs are more flexible and dynamic for high-level programming and scripting. It is up to the programmer to decide which to use based on their use-case. \n", "\n" ] } ], "source": [ "questions = [\n", " \"What is the difference between def and fn?\",\n", "]\n", "chat_history = []\n", "\n", "for question in questions:\n", " result = qa({\"question\": question, \"chat_history\": chat_history})\n", " chat_history.append((question, result[\"answer\"]))\n", " print(f\"-> **Question**: {question} \\n\")\n", " print(f\"**Answer**: {result['answer']} \\n\")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "**Answer**: From an interface level, def and fn are always interchangeable, meaning that there is nothing a ‘def’ can provide that a fn cannot (or vice versa). However, the difference between the two is that a fn is more limited and controlled on the inside of its body. Fns have a number of limitations compared to defs, such as more strict scoping, explicit type annotations, and immutability by default for argument passing. Fns are designed to provide more control and predictability for systems programming, while defs are more flexible and dynamic for high-level programming and scripting. It is up to the programmer to decide which to use based on their use-case." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": ".venv", "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.6" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }