├── .gitignore
├── README.md
├── notebooks
├── 01-Basic_Tutor.ipynb
├── 02-Basic_RAG.ipynb
├── 03-RAG_with_LlamaIndex.ipynb
├── 04-RAG_with_VectorStore.ipynb
├── 05-Improve_Prompts_+_Add_Source.ipynb
├── 06-Evaluate_RAG.ipynb
├── 07-RAG_Improve_Chunking.ipynb
├── 08-Finetune_Embedding.ipynb
├── 10-Adding_Reranking.ipynb
├── 11-Adding_Hybrid_Search.ipynb
├── 12-Improve_Query.ipynb
├── 13-Adding_Router.ipynb
├── 14-Adding_Chat.ipynb
├── 15-Use_OpenSource_Models.ipynb
├── 17-Using_LLMs_to_rank_chunks_as_the_Judge.ipynb
├── Advanced_Retriever.ipynb
├── Agents_with_OpenAI_Assistants.ipynb
├── Audio_and_Realtime.ipynb
├── Basic_Agent_Example.ipynb
├── Cohere_Better_Embedding_Model.ipynb
├── Cohere_and_Open_Source_Embedding_Model.ipynb
├── Crawl_a_Website.ipynb
├── DallE_3_and_ElevenLabs.ipynb
├── Evaluating_and_Iterating_Prompts.ipynb
├── Firecrawl_Scraping.ipynb
├── GPT_4o_mini_Fine_Tuning.ipynb
├── GraphRAG_Implementation.ipynb
├── HF_Inference.ipynb
├── How_to_make_an_API___Getting_Started.ipynb
├── How_to_make_an_API___HTTP_Methods.ipynb
├── Intro_to_Large_Language_Models_(LLMs).ipynb
├── Knowledge_Base_for_RAG.ipynb
├── Larger_Context_Larger_N.ipynb
├── Limitations_and_weaknesses_of_LLMs.ipynb
├── LlamaIndex_101.ipynb
├── LlamaParse.ipynb
├── Long_Context_Caching_vs_RAG.ipynb
├── Metadata_Filtering.ipynb
├── More_Api_And_Tools.ipynb
├── Observablity_And_Tracing.ipynb
├── Open_source_BetterEmbedding_Model.ipynb
├── Perplexity_Web_Api.ipynb
├── Prompting_101.ipynb
├── RAG_101.ipynb
├── Structured(JSON)_PDF_Data_Extraction.ipynb
└── Web_Search_API.ipynb
└── requirements.txt
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 |
6 | # C extensions
7 | *.so
8 |
9 | # Distribution / packaging
10 | .Python
11 | build/
12 | develop-eggs/
13 | dist/
14 | downloads/
15 | eggs/
16 | .eggs/
17 | lib/
18 | lib64/
19 | parts/
20 | sdist/
21 | var/
22 | wheels/
23 | share/python-wheels/
24 | *.egg-info/
25 | .installed.cfg
26 | *.egg
27 | MANIFEST
28 |
29 | # PyInstaller
30 | # Usually these files are written by a python script from a template
31 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
32 | *.manifest
33 | *.spec
34 |
35 | # Installer logs
36 | pip-log.txt
37 | pip-delete-this-directory.txt
38 |
39 | # Unit test / coverage reports
40 | htmlcov/
41 | .tox/
42 | .nox/
43 | .coverage
44 | .coverage.*
45 | .cache
46 | nosetests.xml
47 | coverage.xml
48 | *.cover
49 | *.py,cover
50 | .hypothesis/
51 | .pytest_cache/
52 | cover/
53 |
54 | # Translations
55 | *.mo
56 | *.pot
57 |
58 | # Django stuff:
59 | *.log
60 | local_settings.py
61 | db.sqlite3
62 | db.sqlite3-journal
63 |
64 | # Flask stuff:
65 | instance/
66 | .webassets-cache
67 |
68 | # Scrapy stuff:
69 | .scrapy
70 |
71 | # Sphinx documentation
72 | docs/_build/
73 |
74 | # PyBuilder
75 | .pybuilder/
76 | target/
77 |
78 | # Jupyter Notebook
79 | .ipynb_checkpoints
80 |
81 | # IPython
82 | profile_default/
83 | ipython_config.py
84 |
85 | # pyenv
86 | # For a library or package, you might want to ignore these files since the code is
87 | # intended to run in multiple environments; otherwise, check them in:
88 | # .python-version
89 |
90 | # pipenv
91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies
93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not
94 | # install all needed dependencies.
95 | #Pipfile.lock
96 |
97 | # poetry
98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99 | # This is especially recommended for binary packages to ensure reproducibility, and is more
100 | # commonly ignored for libraries.
101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102 | #poetry.lock
103 |
104 | # pdm
105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106 | #pdm.lock
107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108 | # in version control.
109 | # https://pdm.fming.dev/#use-with-ide
110 | .pdm.toml
111 |
112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113 | __pypackages__/
114 |
115 | # Celery stuff
116 | celerybeat-schedule
117 | celerybeat.pid
118 |
119 | # SageMath parsed files
120 | *.sage.py
121 |
122 | # Environments
123 | .env
124 | .venv
125 | env/
126 | venv/
127 | ENV/
128 | env.bak/
129 | venv.bak/
130 | ai-tutor/
131 | venv_ai_tutor/
132 |
133 | # Spyder project settings
134 | .spyderproject
135 | .spyproject
136 |
137 | # Rope project settings
138 | .ropeproject
139 |
140 | # mkdocs documentation
141 | /site
142 |
143 | # mypy
144 | .mypy_cache/
145 | .dmypy.json
146 | dmypy.json
147 |
148 | # Pyre type checker
149 | .pyre/
150 |
151 | # pytype static type analyzer
152 | .pytype/
153 |
154 | # Cython debug symbols
155 | cython_debug/
156 |
157 | # PyCharm
158 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
159 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
160 | # and can be added to the global gitignore or merged into this file. For a more nuclear
161 | # option (not recommended) you can uncomment the following to ignore the entire idea folder.
162 | #.idea/
163 |
164 | .vscode/
165 | notebooks/mini-llama-articles/
166 | .huggingface
167 |
168 | .DS_Store
169 |
170 | *.csv
171 | *.json
172 | *.jsonl
173 | *.html
174 | *.mdx
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # AI Tutor RAG System
2 |
3 | Welcome to the **AI Tutor RAG (Retrieval-Augmented Generation) System** repository! This repository contains a collection of Jupyter notebooks designed to support the RAG course, focusing on techniques for enhancing AI models with retrieval-based methods.
4 |
5 | ## Course Notebooks
6 |
7 | You can find all the course notebooks in the [notebooks](./notebooks) directory. These notebooks cover various aspects of building and fine-tuning RAG models, providing both theoretical background and practical, hands-on examples.
8 |
9 | ## Running the Notebooks
10 |
11 | You have two options for running the code in these notebooks:
12 |
13 | 1. **Run Locally**: You can clone the repository and run the notebooks on your local machine. To do this, ensure you have a Python installation with the necessary dependencies.
14 | 2. **Run on Google Colab**: Each notebook includes a link at the top to open it directly in Google Colab, making it easy to run without local setup.
15 |
16 | | Notebook No | Topic Name | Colab URL |
17 | |-------------|----------------------------------------------------|----------------------------------------------------------------------------------------------------------|
18 | | 1 | 01-Basic Tutor | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/01-Basic_Tutor.ipynb) |
19 | | 2 | 02-Basic RAG | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/02-Basic_RAG.ipynb) |
20 | | 3 | 03-RAG with LlamaIndex | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/03-RAG_with_LlamaIndex.ipynb) |
21 | | 4 | 04-RAG with VectorStore | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/04-RAG_with_VectorStore.ipynb) |
22 | | 5 | 05-Improve Prompts + Add Source | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/05-Improve_Prompts_+_Add_Source.ipynb) |
23 | | 6 | 06-Evaluate RAG | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/06-Evaluate_RAG.ipynb) |
24 | | 7 | 07-RAG Improve Chunking | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/07-RAG_Improve_Chunking.ipynb) |
25 | | 8 | 08-Finetune Embedding | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/08-Finetune_Embedding.ipynb) |
26 | | 9 | 10-Adding Reranking | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/10-Adding_Reranking.ipynb) |
27 | | 10 | 11-Adding Hybrid Search | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/11-Adding_Hybrid_Search.ipynb) |
28 | | 11 | 12-Improve Query | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/12-Improve_Query.ipynb) |
29 | | 12 | 13-Adding Router | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/13-Adding_Router.ipynb) |
30 | | 13 | 14-Adding Chat | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/14-Adding_Chat.ipynb) |
31 | | 14 | 15-Use OpenSource Models | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/15-Use_OpenSource_Models.ipynb) |
32 | | 15 | 17-Using LLMs to rank chunks as the Judge | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/17-Using_LLMs_to_rank_chunks_as_the_Judge.ipynb) |
33 | | 16 | Advanced Retriever | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/Advanced_Retriever.ipynb) |
34 | | 17 | Agents with OpenAI Assistants | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/Agents_with_OpenAI_Assistants.ipynb) |
35 | | 18 | Audio and Realtime | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/Audio_and_Realtime.ipynb) |
36 | | 19 | Basic Agent Example | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/Basic_Agent_Example.ipynb) |
37 | | 20 | Cohere Better Embedding Model | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/Cohere_Better_Embedding_Model.ipynb) |
38 | | 21 | Cohere and Open Source Embedding Model | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/Cohere_and_Open_Source_Embedding_Model.ipynb) |
39 | | 22 | Crawl a Website | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/Crawl_a_Website.ipynb) |
40 | | 23 | DallE 3 and ElevenLabs | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/DallE_3_and_ElevenLabs.ipynb) |
41 | | 24 | Evaluating and Iterating Prompts | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/Evaluating_and_Iterating_Prompts.ipynb) |
42 | | 25 | Firecrawl Scraping | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/Firecrawl_Scraping.ipynb) |
43 | | 26 | GPT 4o mini Fine Tuning | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/GPT_4o_mini_Fine_Tuning.ipynb) |
44 | | 27 | GraphRAG Implementation | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/GraphRAG_Implementation.ipynb) |
45 | | 28 | HF Inference | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/HF_Inference.ipynb) |
46 | | 29 | How to make an API - Getting Started | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/How_to_make_an_API___Getting_Started.ipynb) |
47 | | 30 | How to make an API - HTTP Methods | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/How_to_make_an_API___HTTP_Methods.ipynb) |
48 | | 31 | Intro to Large Language Models (LLMs) | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/Intro_to_Large_Language_Models_(LLMs).ipynb) |
49 | | 32 | Knowledge Base for RAG | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/Knowledge_Base_for_RAG.ipynb) |
50 | | 33 | Larger Context Larger N | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/Larger_Context_Larger_N.ipynb) |
51 | | 34 | Limitations and weaknesses of LLMs | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/Limitations_and_weaknesses_of_LLMs.ipynb) |
52 | | 35 | LlamaIndex 101 | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/LlamaIndex_101.ipynb) |
53 | | 36 | LlamaParse | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/LlamaParse.ipynb) |
54 | | 37 | Long Context Caching vs RAG | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/Long_Context_Caching_vs_RAG.ipynb) |
55 | | 38 | Metadata Filtering | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/Metadata_Filtering.ipynb) |
56 | | 39 | More Api And Tools | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/More_Api_And_Tools.ipynb) |
57 | | 40 | Observability And Tracing | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/Observablity_And_Tracing.ipynb) |
58 | | 41 | Open source BetterEmbedding Model | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/Open_source_BetterEmbedding_Model.ipynb) |
59 | | 42 | Perplexity Web Api | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/Perplexity_Web_Api.ipynb) |
60 | | 43 | Prompting 101 | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/Prompting_101.ipynb) |
61 | | 44 | RAG 101 | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/RAG_101.ipynb) |
62 | | 45 | Structured (JSON) PDF Data Extraction | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/Structured(JSON)_PDF_Data_Extraction.ipynb) |
63 | | 46 | Web Search API | [](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/Web_Search_API.ipynb) |
64 |
65 |
66 | ## About This Repository
67 |
68 | - **Audience**: Designed for students and professionals interested in AI and natural language processing.
69 | - **Topics Covered**: The notebooks cover foundational and advanced concepts in retrieval-augmented generation, including:
70 | - Data retrieval techniques
71 | - Model integration with retrieval systems
72 | - Practical applications of RAG in real-world scenarios
73 |
74 | ## Getting Started
75 |
76 | Clone the repository and explore the notebooks at your own pace. Whether running them locally or in Colab, these notebooks will guide you step-by-step, enhancing your learning experience.
77 |
--------------------------------------------------------------------------------
/notebooks/01-Basic_Tutor.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "
"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {
13 | "id": "DMXyyXD0xix9"
14 | },
15 | "source": [
16 | "# Install Packages and Setup Variables\n"
17 | ]
18 | },
19 | {
20 | "cell_type": "code",
21 | "execution_count": 1,
22 | "metadata": {
23 | "colab": {
24 | "base_uri": "https://localhost:8080/"
25 | },
26 | "id": "o4Q0N2omkAoZ",
27 | "outputId": "b53ed7a5-fceb-45ea-ab5f-5c6ccaa31069"
28 | },
29 | "outputs": [
30 | {
31 | "name": "stdout",
32 | "output_type": "stream",
33 | "text": [
34 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m337.0/337.0 kB\u001b[0m \u001b[31m5.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
35 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m76.4/76.4 kB\u001b[0m \u001b[31m2.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
36 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m78.0/78.0 kB\u001b[0m \u001b[31m3.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
37 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m2.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
38 | "\u001b[?25h"
39 | ]
40 | }
41 | ],
42 | "source": [
43 | "!pip install -q openai==1.37.0"
44 | ]
45 | },
46 | {
47 | "cell_type": "code",
48 | "execution_count": 2,
49 | "metadata": {
50 | "id": "xxK7EAAvr2aT"
51 | },
52 | "outputs": [],
53 | "source": [
54 | "import os\n",
55 | "\n",
56 | "# Set the \"OPENAI_API_KEY\" in the Python environment. Will be used by OpenAI client later.\n",
57 | "os.environ[\"OPENAI_API_KEY\"] = \"\"\n"
58 | ]
59 | },
60 | {
61 | "cell_type": "markdown",
62 | "metadata": {
63 | "id": "68RbStS-xpbL"
64 | },
65 | "source": [
66 | "# Load the API client\n"
67 | ]
68 | },
69 | {
70 | "cell_type": "code",
71 | "execution_count": 3,
72 | "metadata": {
73 | "id": "La8hdWqJkFkh"
74 | },
75 | "outputs": [],
76 | "source": [
77 | "from openai import OpenAI\n",
78 | "\n",
79 | "# Defining the \"client\" object that enables\n",
80 | "# us to connect to OpenAI API endpoints.\n",
81 | "client = OpenAI()"
82 | ]
83 | },
84 | {
85 | "cell_type": "markdown",
86 | "metadata": {
87 | "id": "CC-sa_uv6J2C"
88 | },
89 | "source": [
90 | "# Query the API\n"
91 | ]
92 | },
93 | {
94 | "cell_type": "code",
95 | "execution_count": 4,
96 | "metadata": {
97 | "id": "7JRrn0uIsBfg"
98 | },
99 | "outputs": [],
100 | "source": [
101 | "# Define two questions: 1) Related to AI, 2) Unrelated topic.\n",
102 | "# These questions will be used to evaluate model's performance.\n",
103 | "QUESTION_AI = \"List a number of famous artificial intelligence frameworks?\"\n",
104 | "QUESTION_NOT_AI = (\n",
105 | " \"What is the name of the highest mountain in the world and its height?\"\n",
106 | ")"
107 | ]
108 | },
109 | {
110 | "cell_type": "code",
111 | "execution_count": 5,
112 | "metadata": {
113 | "id": "CcP26IauuBuV"
114 | },
115 | "outputs": [],
116 | "source": [
117 | "# Defining a function to answer a question using \"gpt-4o-mini\" model.\n",
118 | "def ask_ai_tutor(question):\n",
119 | " try:\n",
120 | " # Formulating the system prompt and condition the model to answer only AI-related questions.\n",
121 | " system_prompt = (\n",
122 | " \"You are an AI tutor specialized in answering artificial intelligence-related questions. \"\n",
123 | " \"Only answer AI-related question, else say that you cannot answer this question.\"\n",
124 | " )\n",
125 | "\n",
126 | " # Create a user prompt with the user's question\n",
127 | " prompt = f\"Please provide an informative and accurate answer to the following question.\\nQuestion: {question}\\nAnswer:\"\n",
128 | "\n",
129 | " # Call the OpenAI API\n",
130 | " response = client.chat.completions.create(\n",
131 | " model=\"gpt-4o-mini\",\n",
132 | " temperature=0,\n",
133 | " messages=[\n",
134 | " {\"role\": \"system\", \"content\": system_prompt},\n",
135 | " {\"role\": \"user\", \"content\": prompt},\n",
136 | " ],\n",
137 | " )\n",
138 | "\n",
139 | " # Return the AI's response\n",
140 | " return response.choices[0].message.content.strip()\n",
141 | "\n",
142 | " except Exception as e:\n",
143 | " return f\"An error occurred: {e}\""
144 | ]
145 | },
146 | {
147 | "cell_type": "code",
148 | "execution_count": 9,
149 | "metadata": {
150 | "colab": {
151 | "base_uri": "https://localhost:8080/"
152 | },
153 | "id": "W_dbwURpufR7",
154 | "outputId": "511b2584-7324-4397-b4dd-e7bb1c691cc2"
155 | },
156 | "outputs": [
157 | {
158 | "name": "stdout",
159 | "output_type": "stream",
160 | "text": [
161 | "Some famous artificial intelligence frameworks include:\n",
162 | "\n",
163 | "1. **TensorFlow**: An open-source library developed by Google for numerical computation and machine learning, widely used for deep learning applications.\n",
164 | "\n",
165 | "2. **PyTorch**: An open-source machine learning library developed by Facebook's AI Research lab, known for its flexibility and ease of use, particularly in research.\n",
166 | "\n",
167 | "3. **Keras**: A high-level neural networks API, written in Python and capable of running on top of TensorFlow, Theano, or Microsoft Cognitive Toolkit (CNTK).\n",
168 | "\n",
169 | "4. **Scikit-learn**: A Python library for machine learning that provides simple and efficient tools for data mining and data analysis, built on NumPy, SciPy, and Matplotlib.\n",
170 | "\n",
171 | "5. **Caffe**: A deep learning framework made with expression, speed, and modularity in mind, developed by the Berkeley Vision and Learning Center (BVLC).\n",
172 | "\n",
173 | "6. **MXNet**: A flexible and efficient deep learning framework that supports both symbolic and imperative programming, developed by Apache.\n",
174 | "\n",
175 | "7. **Chainer**: A Python-based deep learning framework that allows for dynamic computation graphs, making it easier to work with complex architectures.\n",
176 | "\n",
177 | "8. **Fastai**: A library built on top of PyTorch that simplifies training neural networks and provides high-level components for building deep learning models.\n",
178 | "\n",
179 | "These frameworks are widely used in both academia and industry for various AI applications.\n"
180 | ]
181 | }
182 | ],
183 | "source": [
184 | "# Ask the AI-related question.\n",
185 | "RES_AI = ask_ai_tutor(QUESTION_AI)\n",
186 | "print(RES_AI)"
187 | ]
188 | },
189 | {
190 | "cell_type": "code",
191 | "execution_count": 10,
192 | "metadata": {
193 | "colab": {
194 | "base_uri": "https://localhost:8080/"
195 | },
196 | "id": "37YuVJQquhpN",
197 | "outputId": "a07d9117-fbf8-44c5-d720-ae342f4d3c90"
198 | },
199 | "outputs": [
200 | {
201 | "name": "stdout",
202 | "output_type": "stream",
203 | "text": [
204 | "I cannot answer this question.\n"
205 | ]
206 | }
207 | ],
208 | "source": [
209 | "# Ask the unrelated question.\n",
210 | "RES_NOT_AI = ask_ai_tutor(QUESTION_NOT_AI)\n",
211 | "print(RES_NOT_AI)"
212 | ]
213 | },
214 | {
215 | "cell_type": "markdown",
216 | "metadata": {
217 | "id": "NRBgk6WToIK0"
218 | },
219 | "source": [
220 | "# History\n"
221 | ]
222 | },
223 | {
224 | "cell_type": "code",
225 | "execution_count": 8,
226 | "metadata": {
227 | "colab": {
228 | "base_uri": "https://localhost:8080/"
229 | },
230 | "id": "0_6GN2XsoEyM",
231 | "outputId": "e0c028ec-10e8-4d14-b248-1027f5d55c43"
232 | },
233 | "outputs": [
234 | {
235 | "name": "stdout",
236 | "output_type": "stream",
237 | "text": [
238 | "TensorFlow is an open-source machine learning framework developed by Google that facilitates numerical computation and machine learning tasks. It is particularly well-known for its capabilities in deep learning, allowing users to build and train complex neural networks. TensorFlow provides a flexible architecture that enables deployment across various platforms, from mobile devices to large-scale distributed systems. Its extensive ecosystem includes tools and libraries for model building, training, and deployment, making it a popular choice among researchers and developers in the AI community. TensorFlow also supports a wide range of applications, including image recognition, natural language processing, and reinforcement learning.\n"
239 | ]
240 | }
241 | ],
242 | "source": [
243 | "response = client.chat.completions.create(\n",
244 | " model=\"gpt-4o-mini\",\n",
245 | " temperature=0,\n",
246 | " messages=[\n",
247 | " {\n",
248 | " \"role\": \"system\",\n",
249 | " \"content\": \"You are an AI tutor specialized in answering artificial intelligence-related questions. Only answer AI-related question, else say that you cannot answer this question.\",\n",
250 | " },\n",
251 | " {\n",
252 | " \"role\": \"user\",\n",
253 | " \"content\": \"Please provide an informative and accurate answer to the following question.\\nQuestion: List a number of famous artificial intelligence frameworks?\\nAnswer:\",\n",
254 | " },\n",
255 | " {\"role\": \"assistant\", \"content\": RES_AI},\n",
256 | " {\n",
257 | " \"role\": \"user\",\n",
258 | " \"content\": \"Please provide an informative and accurate answer to the following question.\\nQuestion: What is the name of the highest mountain in the world and its height?\\nAnswer:\",\n",
259 | " },\n",
260 | " {\"role\": \"assistant\", \"content\": RES_NOT_AI},\n",
261 | " {\n",
262 | " \"role\": \"user\",\n",
263 | " \"content\": \"Please provide an informative and accurate answer to the following question.\\nQuestion: Can you write a summary of the first suggested AI framework in the first question?\\nAnswer:\",\n",
264 | " },\n",
265 | " ],\n",
266 | ")\n",
267 | "\n",
268 | "print(response.choices[0].message.content.strip())"
269 | ]
270 | },
271 | {
272 | "cell_type": "code",
273 | "execution_count": null,
274 | "metadata": {
275 | "id": "DKCXmzLVxt51"
276 | },
277 | "outputs": [],
278 | "source": []
279 | }
280 | ],
281 | "metadata": {
282 | "colab": {
283 | "provenance": []
284 | },
285 | "kernelspec": {
286 | "display_name": "Python 3",
287 | "name": "python3"
288 | },
289 | "language_info": {
290 | "codemirror_mode": {
291 | "name": "ipython",
292 | "version": 3
293 | },
294 | "file_extension": ".py",
295 | "mimetype": "text/x-python",
296 | "name": "python",
297 | "nbconvert_exporter": "python",
298 | "pygments_lexer": "ipython3",
299 | "version": "3.12.4"
300 | }
301 | },
302 | "nbformat": 4,
303 | "nbformat_minor": 0
304 | }
305 |
--------------------------------------------------------------------------------
/notebooks/Agents_with_OpenAI_Assistants.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {
6 | "colab_type": "text",
7 | "id": "view-in-github"
8 | },
9 | "source": [
10 | "
\n"
11 | ]
12 | },
13 | {
14 | "cell_type": "code",
15 | "execution_count": 1,
16 | "metadata": {
17 | "colab": {
18 | "base_uri": "https://localhost:8080/"
19 | },
20 | "id": "UPKtF57twVp8",
21 | "outputId": "0af0655d-d8a0-478b-d7f3-8ce55292454a"
22 | },
23 | "outputs": [
24 | {
25 | "name": "stdout",
26 | "output_type": "stream",
27 | "text": [
28 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m328.5/328.5 kB\u001b[0m \u001b[31m2.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
29 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m1.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
30 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m77.9/77.9 kB\u001b[0m \u001b[31m3.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
31 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m2.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
32 | "\u001b[?25h"
33 | ]
34 | }
35 | ],
36 | "source": [
37 | "!pip install -q openai==1.37.0"
38 | ]
39 | },
40 | {
41 | "cell_type": "code",
42 | "execution_count": 2,
43 | "metadata": {
44 | "id": "gf1VoYD-Y7TL"
45 | },
46 | "outputs": [],
47 | "source": [
48 | "import os\n",
49 | "\n",
50 | "# Set the \"OPENAI_API_KEY\" in the Python environment. Will be used by OpenAI client later.\n",
51 | "os.environ[\"OPENAI_API_KEY\"] = \"[OPENAI_API_KEY]\""
52 | ]
53 | },
54 | {
55 | "cell_type": "markdown",
56 | "metadata": {
57 | "id": "mLTbUTtthHGG"
58 | },
59 | "source": [
60 | "# Math Tutor\n"
61 | ]
62 | },
63 | {
64 | "cell_type": "code",
65 | "execution_count": 5,
66 | "metadata": {
67 | "id": "QxYu2uw9YoG8"
68 | },
69 | "outputs": [],
70 | "source": [
71 | "from openai import OpenAI\n",
72 | "\n",
73 | "client = OpenAI()\n",
74 | "\n",
75 | "assistant = client.beta.assistants.create(\n",
76 | " name=\"Math Tutor\",\n",
77 | " instructions=\"You are a personal math tutor. Write and run code to answer math questions.\",\n",
78 | " model=\"gpt-4o\",\n",
79 | " tools=[{\"type\": \"code_interpreter\"}],\n",
80 | ")"
81 | ]
82 | },
83 | {
84 | "cell_type": "code",
85 | "execution_count": 6,
86 | "metadata": {
87 | "id": "zdAu65wDY43T"
88 | },
89 | "outputs": [],
90 | "source": [
91 | "thread = client.beta.threads.create()"
92 | ]
93 | },
94 | {
95 | "cell_type": "code",
96 | "execution_count": 7,
97 | "metadata": {
98 | "id": "AeiK-j7NZIJI"
99 | },
100 | "outputs": [],
101 | "source": [
102 | "message = client.beta.threads.messages.create(\n",
103 | " thread_id=thread.id,\n",
104 | " role=\"user\",\n",
105 | " content=\"I need to solve the equation `3x + 11 = 14`. Can you help me?\",\n",
106 | ")"
107 | ]
108 | },
109 | {
110 | "cell_type": "code",
111 | "execution_count": 8,
112 | "metadata": {
113 | "id": "-PWEekBTZJSR"
114 | },
115 | "outputs": [],
116 | "source": [
117 | "run = client.beta.threads.runs.create_and_poll(\n",
118 | " thread_id=thread.id, assistant_id=assistant.id\n",
119 | ")"
120 | ]
121 | },
122 | {
123 | "cell_type": "code",
124 | "execution_count": 13,
125 | "metadata": {
126 | "id": "SKcOwN2XZKTy"
127 | },
128 | "outputs": [],
129 | "source": [
130 | "messages = list(client.beta.threads.messages.list(thread_id=thread.id, run_id=run.id))"
131 | ]
132 | },
133 | {
134 | "cell_type": "code",
135 | "execution_count": 21,
136 | "metadata": {
137 | "colab": {
138 | "base_uri": "https://localhost:8080/"
139 | },
140 | "id": "ndRo014JZSLo",
141 | "outputId": "7186ef9a-7fb9-4e4b-c1cf-365f4d0d3bdc"
142 | },
143 | "outputs": [
144 | {
145 | "name": "stdout",
146 | "output_type": "stream",
147 | "text": [
148 | "Of course! To solve the equation \\(3x + 11 = 14\\), follow these steps:\n",
149 | "\n",
150 | "1. **Isolate the variable term**:\n",
151 | " Subtract 11 from both sides of the equation.\n",
152 | " \\[\n",
153 | " 3x + 11 - 11 = 14 - 11\n",
154 | " \\]\n",
155 | " Simplifies to:\n",
156 | " \\[\n",
157 | " 3x = 3\n",
158 | " \\]\n",
159 | "\n",
160 | "2. **Solve for \\( x \\)**:\n",
161 | " Divide both sides of the equation by 3.\n",
162 | " \\[\n",
163 | " x = \\frac{3}{3}\n",
164 | " \\]\n",
165 | " Simplifies to:\n",
166 | " \\[\n",
167 | " x = 1\n",
168 | " \\]\n",
169 | "\n",
170 | "So, the solution to the equation is \\( x = 1 \\).\n",
171 | "\n",
172 | "Let's verify this by substituting \\( x = 1 \\) back into the original equation to confirm that both sides are equal.\n",
173 | "\n",
174 | "\\[\n",
175 | "3(1) + 11 = 14\n",
176 | "\\]\n",
177 | "\\[\n",
178 | "3 + 11 = 14\n",
179 | "\\]\n",
180 | "\\[\n",
181 | "14 = 14\n",
182 | "\\]\n",
183 | "\n",
184 | "The left and right sides are equal, so the solution \\( x = 1 \\) is correct.\n"
185 | ]
186 | }
187 | ],
188 | "source": [
189 | "print(messages[0].content[0].text.value)"
190 | ]
191 | },
192 | {
193 | "cell_type": "markdown",
194 | "metadata": {
195 | "id": "cgE3EEaHhFEh"
196 | },
197 | "source": [
198 | "# Customer Support\n"
199 | ]
200 | },
201 | {
202 | "cell_type": "code",
203 | "execution_count": 3,
204 | "metadata": {
205 | "colab": {
206 | "base_uri": "https://localhost:8080/"
207 | },
208 | "id": "P-zDilXchGGU",
209 | "outputId": "9e9e306a-61fb-4617-f5a2-99eedd8f6bd2"
210 | },
211 | "outputs": [
212 | {
213 | "name": "stdout",
214 | "output_type": "stream",
215 | "text": [
216 | "--2024-07-23 17:09:57-- https://personales.unican.es/corcuerp/linux/resources/LinuxCommandLineCheatSheet_1.pdf\n",
217 | "Resolving personales.unican.es (personales.unican.es)... 193.144.193.111\n",
218 | "Connecting to personales.unican.es (personales.unican.es)|193.144.193.111|:443... connected.\n",
219 | "HTTP request sent, awaiting response... 200 OK\n",
220 | "Length: 267816 (262K) [application/pdf]\n",
221 | "Saving to: ‘LinuxCommandLineCheatSheet_1.pdf’\n",
222 | "\n",
223 | "LinuxCommandLineChe 100%[===================>] 261.54K 314KB/s in 0.8s \n",
224 | "\n",
225 | "2024-07-23 17:09:58 (314 KB/s) - ‘LinuxCommandLineCheatSheet_1.pdf’ saved [267816/267816]\n",
226 | "\n"
227 | ]
228 | }
229 | ],
230 | "source": [
231 | "!wget https://personales.unican.es/corcuerp/linux/resources/LinuxCommandLineCheatSheet_1.pdf"
232 | ]
233 | },
234 | {
235 | "cell_type": "code",
236 | "execution_count": 5,
237 | "metadata": {
238 | "id": "IqLR9ss9lKrz"
239 | },
240 | "outputs": [],
241 | "source": [
242 | "from openai import OpenAI\n",
243 | "\n",
244 | "client = OpenAI()"
245 | ]
246 | },
247 | {
248 | "cell_type": "code",
249 | "execution_count": 6,
250 | "metadata": {
251 | "colab": {
252 | "base_uri": "https://localhost:8080/"
253 | },
254 | "id": "VevcGLDCjdUi",
255 | "outputId": "049f6306-84f6-434d-f3c7-dc0741bbbfb6"
256 | },
257 | "outputs": [
258 | {
259 | "name": "stdout",
260 | "output_type": "stream",
261 | "text": [
262 | "completed\n",
263 | "FileCounts(cancelled=0, completed=1, failed=0, in_progress=0, total=1)\n"
264 | ]
265 | }
266 | ],
267 | "source": [
268 | "# Create a vector store caled \"Financial Statements\"\n",
269 | "vector_store = client.beta.vector_stores.create(name=\"Tech Support\")\n",
270 | "\n",
271 | "# Ready the files for upload to OpenAI\n",
272 | "file_streams = [open(\"LinuxCommandLineCheatSheet_1.pdf\", \"rb\")]\n",
273 | "\n",
274 | "# Use the upload and poll SDK helper to upload the files, add them to the vector store,\n",
275 | "# and poll the status of the file batch for completion.\n",
276 | "file_batch = client.beta.vector_stores.file_batches.upload_and_poll(\n",
277 | " vector_store_id=vector_store.id, files=file_streams\n",
278 | ")\n",
279 | "\n",
280 | "# You can print the status and the file counts of the batch to see the result of this operation.\n",
281 | "print(file_batch.status)\n",
282 | "print(file_batch.file_counts)"
283 | ]
284 | },
285 | {
286 | "cell_type": "code",
287 | "execution_count": 7,
288 | "metadata": {
289 | "id": "pTzfL1XCjdXT"
290 | },
291 | "outputs": [],
292 | "source": [
293 | "assistant = client.beta.assistants.create(\n",
294 | " name=\"Tech Support\",\n",
295 | " instructions=\"You are a tech support chatbot. Use the product manual to respond accurately to customer inquiries.\",\n",
296 | " model=\"gpt-4o\",\n",
297 | " tools=[{\"type\": \"file_search\"}],\n",
298 | " tool_resources={\"file_search\": {\"vector_store_ids\": [vector_store.id]}},\n",
299 | ")"
300 | ]
301 | },
302 | {
303 | "cell_type": "code",
304 | "execution_count": 8,
305 | "metadata": {
306 | "id": "FSTCsotRjdPj"
307 | },
308 | "outputs": [],
309 | "source": [
310 | "# Create a thread and attach the file to the message\n",
311 | "thread = client.beta.threads.create(\n",
312 | " messages=[\n",
313 | " {\n",
314 | " \"role\": \"user\",\n",
315 | " \"content\": \"What 'ls' command do?\",\n",
316 | " }\n",
317 | " ]\n",
318 | ")"
319 | ]
320 | },
321 | {
322 | "cell_type": "code",
323 | "execution_count": 9,
324 | "metadata": {
325 | "id": "jdD5yJK2jdMu"
326 | },
327 | "outputs": [],
328 | "source": [
329 | "run = client.beta.threads.runs.create_and_poll(\n",
330 | " thread_id=thread.id, assistant_id=assistant.id\n",
331 | ")"
332 | ]
333 | },
334 | {
335 | "cell_type": "code",
336 | "execution_count": 10,
337 | "metadata": {
338 | "colab": {
339 | "base_uri": "https://localhost:8080/"
340 | },
341 | "id": "p0w3ts1DjdKW",
342 | "outputId": "a1720556-12df-42ff-bfd4-a001f3bb2565"
343 | },
344 | "outputs": [
345 | {
346 | "name": "stdout",
347 | "output_type": "stream",
348 | "text": [
349 | "The `ls` command in Linux is used to list the contents of a directory. The common usage of `ls` can be extended with options to display detailed information about files and directories. For example:\n",
350 | "\n",
351 | "- `ls -al` lists all files, including hidden ones, in a long listing format that provides detailed information such as permissions, number of links, owner, group, size, and timestamp【4:0†source】【4:1†source】.\n"
352 | ]
353 | }
354 | ],
355 | "source": [
356 | "messages = list(client.beta.threads.messages.list(thread_id=thread.id, run_id=run.id))\n",
357 | "\n",
358 | "print(messages[0].content[0].text.value)"
359 | ]
360 | },
361 | {
362 | "cell_type": "code",
363 | "execution_count": 24,
364 | "metadata": {
365 | "colab": {
366 | "base_uri": "https://localhost:8080/"
367 | },
368 | "id": "p1KafLldjdFI",
369 | "outputId": "0f1f388f-c04a-4eda-fe6b-9a00d83f0070"
370 | },
371 | "outputs": [
372 | {
373 | "data": {
374 | "text/plain": [
375 | "[FileCitationAnnotation(end_index=394, file_citation=FileCitation(file_id='file-EMNwQYbq7rGni9Ct4V7B8XTR'), start_index=382, text='【4:0†source】', type='file_citation'),\n",
376 | " FileCitationAnnotation(end_index=406, file_citation=FileCitation(file_id='file-EMNwQYbq7rGni9Ct4V7B8XTR'), start_index=394, text='【4:1†source】', type='file_citation')]"
377 | ]
378 | },
379 | "execution_count": 24,
380 | "metadata": {},
381 | "output_type": "execute_result"
382 | }
383 | ],
384 | "source": [
385 | "messages[0].content[0].text.annotations"
386 | ]
387 | }
388 | ],
389 | "metadata": {
390 | "colab": {
391 | "authorship_tag": "ABX9TyOyF/2q5TUS6bkOmPxn67kV",
392 | "include_colab_link": true,
393 | "provenance": []
394 | },
395 | "kernelspec": {
396 | "display_name": "Python 3",
397 | "name": "python3"
398 | },
399 | "language_info": {
400 | "name": "python",
401 | "version": "3.12.4"
402 | }
403 | },
404 | "nbformat": 4,
405 | "nbformat_minor": 0
406 | }
407 |
--------------------------------------------------------------------------------
/notebooks/Audio_and_Realtime.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "
"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "# Adding Speech with OpenAI’s GPT4o Audio\n",
15 | "\n",
16 | "In this lesson, we see how to leverage the new audio capabilities of GPT4o with the \"gpt-4o-audio-preview\" model. We'll see how to write code that registers our voices, sends it to the model, and plays back the audio response. We'll also learn how to parse audio streaming output and play it as soon as the first audio chunks arrive. Last, we integrate this with the AI tutor knowledge base, getting to a script that listens to the user query, instructs the LLM to use the knowledge base to retrieve information for answering the query, and plays back the final audio response."
17 | ]
18 | },
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {},
22 | "source": [
23 | "## Libraries and Environment Variables"
24 | ]
25 | },
26 | {
27 | "cell_type": "markdown",
28 | "metadata": {},
29 | "source": [
30 | "The code has been tested with the following libraries installed:\n",
31 | "\n",
32 | "```\n",
33 | "chromadb==0.5.3\n",
34 | "huggingface-hub==0.26.2\n",
35 | "llama-index==0.10.49\n",
36 | "llama-index-embeddings-openai==0.1.11\n",
37 | "numpy==1.26.4\n",
38 | "openai==1.54.3\n",
39 | "PyAudio==0.2.14\n",
40 | "sounddevice==0.5.1\n",
41 | "wavio==0.0.9\n",
42 | "```"
43 | ]
44 | },
45 | {
46 | "cell_type": "code",
47 | "execution_count": null,
48 | "metadata": {},
49 | "outputs": [],
50 | "source": [
51 | "import os\n",
52 | "\n",
53 | "os.environ[\"OPENAI_API_KEY\"] = \"\""
54 | ]
55 | },
56 | {
57 | "cell_type": "markdown",
58 | "metadata": {},
59 | "source": [
60 | "## Load Knowledge Base and Create Retriever\n",
61 | "\n",
62 | "In this section, we download our 500 blog dataset and create a vector retriever with it."
63 | ]
64 | },
65 | {
66 | "cell_type": "code",
67 | "execution_count": null,
68 | "metadata": {},
69 | "outputs": [],
70 | "source": [
71 | "from llama_index.core import Settings\n",
72 | "from llama_index.embeddings.openai import OpenAIEmbedding\n",
73 | "\n",
74 | "Settings.embed_model = OpenAIEmbedding(model=\"text-embedding-3-small\")"
75 | ]
76 | },
77 | {
78 | "cell_type": "code",
79 | "execution_count": null,
80 | "metadata": {},
81 | "outputs": [],
82 | "source": [
83 | "# Download 500 blog dataset as knowledge base\n",
84 | "from huggingface_hub import hf_hub_download\n",
85 | "\n",
86 | "hf_hub_download(repo_id=\"jaiganesan/ai_tutor_knowledge\", filename=\"vectorstore.zip\", repo_type=\"dataset\", local_dir=\".\")"
87 | ]
88 | },
89 | {
90 | "cell_type": "code",
91 | "execution_count": null,
92 | "metadata": {},
93 | "outputs": [],
94 | "source": [
95 | "import chromadb\n",
96 | "from llama_index.vector_stores.chroma import ChromaVectorStore\n",
97 | "from llama_index.core import VectorStoreIndex\n",
98 | "\n",
99 | "# Load the vector store from the local storage\n",
100 | "db = chromadb.PersistentClient(path=\"/Users/fabio/Desktop/temp/ai_tutor_knowledge\")\n",
101 | "chroma_collection = db.get_collection(\"ai_tutor_knowledge\")\n",
102 | "vector_store = ChromaVectorStore(chroma_collection=chroma_collection)\n",
103 | "\n",
104 | "# Create the index based on the vector store\n",
105 | "vector_index = VectorStoreIndex.from_vector_store(vector_store=vector_store)\n",
106 | "\n",
107 | "# Create retriever\n",
108 | "vector_retriever = vector_index.as_retriever(similarity_top_k=5)"
109 | ]
110 | },
111 | {
112 | "cell_type": "code",
113 | "execution_count": null,
114 | "metadata": {},
115 | "outputs": [],
116 | "source": [
117 | "# Test the retriever with a query\n",
118 | "nodes = vector_retriever.retrieve(\"How does RAG work?\")\n",
119 | "for node in nodes:\n",
120 | " print(node.metadata[\"title\"])\n",
121 | " print(node.metadata[\"url\"])\n",
122 | " print(\"-\" * 5)"
123 | ]
124 | },
125 | {
126 | "cell_type": "markdown",
127 | "metadata": {},
128 | "source": [
129 | "## Registering Audio and Generating Audio Responses with GPT4o\n",
130 | "\n",
131 | "In this section, we see how to (1) register audio from your microphone, (2) send the audio to GPT4o to generate an audio response, and (3) play the audio response and show its transcript."
132 | ]
133 | },
134 | {
135 | "cell_type": "code",
136 | "execution_count": null,
137 | "metadata": {},
138 | "outputs": [],
139 | "source": [
140 | "import sounddevice as sd\n",
141 | "import numpy as np\n",
142 | "import wavio\n",
143 | "import base64\n",
144 | "from openai import OpenAI\n",
145 | "import tempfile\n",
146 | "import json\n",
147 | "import simpleaudio as sa"
148 | ]
149 | },
150 | {
151 | "cell_type": "code",
152 | "execution_count": null,
153 | "metadata": {},
154 | "outputs": [],
155 | "source": [
156 | "def record_audio(key=\"q\", sample_rate=44100, channels=1):\n",
157 | " \"\"\"Record audio from the microphone until the user sends the \"q\" key.\"\"\"\n",
158 | " print(f\"Recording... Press '{key}' to stop.\")\n",
159 | " audio_data = []\n",
160 | "\n",
161 | " # Define a callback function to capture audio data\n",
162 | " def callback(indata, frames, time, status):\n",
163 | " audio_data.append(indata.copy())\n",
164 | "\n",
165 | " # Open audio input stream and start recording\n",
166 | " with sd.InputStream(samplerate=sample_rate, channels=channels, callback=callback):\n",
167 | " while True:\n",
168 | " if input() == key:\n",
169 | " break\n",
170 | " print(\"Stopped recording.\")\n",
171 | "\n",
172 | " # Combine audio data and return as a numpy array\n",
173 | " audio_data = np.concatenate(audio_data, axis=0)\n",
174 | "\n",
175 | " # Save the audio to a temporary file\n",
176 | " with tempfile.NamedTemporaryFile(suffix=\".wav\", delete=False) as audio_file:\n",
177 | " wavio.write(audio_file.name, audio_data, sample_rate, sampwidth=2)\n",
178 | " audio_file_path = audio_file.name\n",
179 | "\n",
180 | " return audio_file_path"
181 | ]
182 | },
183 | {
184 | "cell_type": "code",
185 | "execution_count": null,
186 | "metadata": {},
187 | "outputs": [],
188 | "source": [
189 | "def send_audio_to_llm(audio_file_path, prompt):\n",
190 | " \"\"\"Sends an audio file to the OpenAI API and returns the audio completion.\"\"\"\n",
191 | " # Read the temp file and encode as base64\n",
192 | " with open(audio_file_path, \"rb\") as audio_file:\n",
193 | " encoded_audio = base64.b64encode(audio_file.read()).decode('utf-8')\n",
194 | "\n",
195 | " messages = [\n",
196 | " {\n",
197 | " \"role\": \"user\",\n",
198 | " \"content\": [\n",
199 | " {\n",
200 | " \"type\": \"text\",\n",
201 | " \"text\": prompt\n",
202 | " },\n",
203 | " {\n",
204 | " \"type\": \"input_audio\",\n",
205 | " \"input_audio\": {\n",
206 | " \"data\": encoded_audio,\n",
207 | " \"format\": \"wav\"\n",
208 | " }\n",
209 | " }\n",
210 | " ]\n",
211 | " },\n",
212 | " ]\n",
213 | "\n",
214 | " # Send to OpenAI API\n",
215 | " completion = openai_client.chat.completions.create(\n",
216 | " model=\"gpt-4o-audio-preview\",\n",
217 | " modalities=[\"text\", \"audio\"],\n",
218 | " audio={\"voice\": \"alloy\", \"format\": \"pcm16\"},\n",
219 | " messages=messages\n",
220 | " )\n",
221 | "\n",
222 | " return completion"
223 | ]
224 | },
225 | {
226 | "cell_type": "code",
227 | "execution_count": null,
228 | "metadata": {},
229 | "outputs": [],
230 | "source": [
231 | "def play_sound(pcm_bytes, sample_rate=24000, channels=1, sample_width=2):\n",
232 | " \"\"\"Plays a sound from PCM bytes using simpleaudio\"\"\"\n",
233 | " play_obj = sa.play_buffer(\n",
234 | " pcm_bytes,\n",
235 | " num_channels=channels,\n",
236 | " bytes_per_sample=sample_width,\n",
237 | " sample_rate=sample_rate\n",
238 | " )\n",
239 | " play_obj.wait_done()"
240 | ]
241 | },
242 | {
243 | "cell_type": "code",
244 | "execution_count": null,
245 | "metadata": {},
246 | "outputs": [],
247 | "source": [
248 | "# Record audio until the user presses 'q'\n",
249 | "audio_file_path = record_audio()\n",
250 | "\n",
251 | "# Initialize OpenAI API client\n",
252 | "openai_client = OpenAI()\n",
253 | "\n",
254 | "# Print transcription result\n",
255 | "prompt = \"Transcribe the attached recording. Write only the transcription and nothing else.\"\n",
256 | "completion = send_audio_to_llm(audio_file_path, prompt)\n",
257 | "print(completion.choices[0].message.audio.transcript)\n",
258 | "\n",
259 | "# Play the audio response\n",
260 | "pcm_bytes = base64.b64decode(completion.choices[0].message.audio.data)\n",
261 | "play_sound(pcm_bytes)"
262 | ]
263 | },
264 | {
265 | "cell_type": "markdown",
266 | "metadata": {},
267 | "source": [
268 | "## Using Streaming Outputs\n",
269 | "\n",
270 | "In this section we see how to leveraging streaming outputs of the OpenAI API to retrieve the audio response chunk by chunk. This allows us to play the response audio with lower latency as we play the first bytes as soon as we receive them intead of waiting for the whole audio output."
271 | ]
272 | },
273 | {
274 | "cell_type": "code",
275 | "execution_count": null,
276 | "metadata": {},
277 | "outputs": [],
278 | "source": [
279 | "import pyaudio\n",
280 | "import threading\n",
281 | "import queue"
282 | ]
283 | },
284 | {
285 | "cell_type": "code",
286 | "execution_count": null,
287 | "metadata": {},
288 | "outputs": [],
289 | "source": [
290 | "def play_sound_from_queue(pcm_queue, sample_rate=24000, channels=1, sample_width=2):\n",
291 | " \"\"\"\n",
292 | " Play PCM audio data from a queue that is being filled over time.\n",
293 | "\n",
294 | " Args:\n",
295 | " pcm_queue: A Queue object from which PCM data is read.\n",
296 | " \"\"\"\n",
297 | " p = pyaudio.PyAudio()\n",
298 | " format = p.get_format_from_width(sample_width)\n",
299 | "\n",
300 | " # Open a blocking stream\n",
301 | " stream = p.open(format=format,\n",
302 | " channels=channels,\n",
303 | " rate=sample_rate,\n",
304 | " output=True)\n",
305 | "\n",
306 | " # Read data from the queue and write to the stream\n",
307 | " while True:\n",
308 | " data = pcm_queue.get()\n",
309 | " if data is None:\n",
310 | " break # No more data to play\n",
311 | " stream.write(data)\n",
312 | "\n",
313 | " # Clean up\n",
314 | " stream.stop_stream()\n",
315 | " stream.close()\n",
316 | " p.terminate()"
317 | ]
318 | },
319 | {
320 | "cell_type": "code",
321 | "execution_count": null,
322 | "metadata": {},
323 | "outputs": [],
324 | "source": [
325 | "def play_sound_and_print_transcript(stream):\n",
326 | " \"\"\"\n",
327 | " Starting from a stream of audio chunks (the response to the LLM call),\n",
328 | " plays the response audio and prints its transcript.\n",
329 | " \"\"\"\n",
330 | " pcm_queue = queue.Queue()\n",
331 | " has_playback_started = False\n",
332 | " for chunk in stream:\n",
333 | " if hasattr(chunk.choices[0].delta, \"audio\"):\n",
334 | " chunk_audio = chunk.choices[0].delta.audio\n",
335 | " if \"transcript\" in chunk_audio:\n",
336 | " print(chunk_audio[\"transcript\"], end=\"\") # Print the transcript\n",
337 | " elif \"data\" in chunk_audio:\n",
338 | " pcm_bytes = base64.b64decode(chunk_audio[\"data\"])\n",
339 | " pcm_queue.put(pcm_bytes) # Add the audio data to the queue\n",
340 | " if not has_playback_started:\n",
341 | " # Start the playback thread\n",
342 | " playback_thread = threading.Thread(target=play_sound_from_queue, args=(pcm_queue,))\n",
343 | " playback_thread.start()\n",
344 | " has_playback_started = True\n",
345 | " pcm_queue.put(None) # Signal end of data\n",
346 | " playback_thread.join() # Wait for playback to finish"
347 | ]
348 | },
349 | {
350 | "cell_type": "code",
351 | "execution_count": null,
352 | "metadata": {},
353 | "outputs": [],
354 | "source": [
355 | "# Get response from GPT4o (i.e. a stream of chunks of audio)\n",
356 | "with open(audio_file_path, \"rb\") as audio_file:\n",
357 | " encoded_audio = base64.b64encode(audio_file.read()).decode('utf-8')\n",
358 | "\n",
359 | "# Prepare messages\n",
360 | "messages = [\n",
361 | " {\n",
362 | " \"role\": \"user\",\n",
363 | " \"content\": [\n",
364 | " {\n",
365 | " \"type\": \"text\",\n",
366 | " \"text\": prompt\n",
367 | " },\n",
368 | " {\n",
369 | " \"type\": \"input_audio\",\n",
370 | " \"input_audio\": {\n",
371 | " \"data\": encoded_audio,\n",
372 | " \"format\": \"wav\"\n",
373 | " }\n",
374 | " }\n",
375 | " ]\n",
376 | " },\n",
377 | "]\n",
378 | "\n",
379 | "# Get streaming response from the LLM\n",
380 | "stream = openai_client.chat.completions.create(\n",
381 | " model=\"gpt-4o-audio-preview\",\n",
382 | " modalities=[\"text\", \"audio\"],\n",
383 | " audio={\"voice\": \"alloy\", \"format\": \"pcm16\"},\n",
384 | " messages=messages,\n",
385 | " stream=True,\n",
386 | ")\n",
387 | "\n",
388 | "# Play the audio response and print the transcript\n",
389 | "play_sound_and_print_transcript(stream)"
390 | ]
391 | },
392 | {
393 | "cell_type": "markdown",
394 | "metadata": {},
395 | "source": [
396 | "## Integrating Audio Inputs and Outputs with RAG\n",
397 | "\n",
398 | "In this section, we see how to (1) define the tool that retrieves relevant information from our knowledge base, (2) send the user query to the LLM specifying the available tools, (3) manage the LLM response if it asks to use a tool, (4) get the final audio response via streaming from the LLM leveraging the tool response, and (5) play the audio response."
399 | ]
400 | },
401 | {
402 | "cell_type": "code",
403 | "execution_count": null,
404 | "metadata": {},
405 | "outputs": [],
406 | "source": [
407 | "# This function will be used as tool for the LLM to retrieve resources\n",
408 | "def retrieve_resources(query: str) -> str:\n",
409 | " \"\"\"Given a query, retrieve relevant resources and return them as a formatted string.\"\"\"\n",
410 | " nodes = vector_retriever.retrieve(query)\n",
411 | "\n",
412 | " context_text = \"\"\n",
413 | " for i, node in enumerate(nodes):\n",
414 | " context_text += f\"\" + \"\\n\"\n",
415 | " context_text += \"\" + node.node.metadata[\"title\"] + \"\" + \"\\n\\n\"\n",
416 | " context_text += \"\" + \"\\n\" + node.node.text + \"\\n\" + \"\" + \"\\n\"\n",
417 | " context_text += f\"\" + \"\\n\\n\"\n",
418 | " context_text = context_text.strip()\n",
419 | "\n",
420 | " return context_text"
421 | ]
422 | },
423 | {
424 | "cell_type": "code",
425 | "execution_count": null,
426 | "metadata": {},
427 | "outputs": [],
428 | "source": [
429 | "# Define the tools for the LLM\n",
430 | "tools = [\n",
431 | " {\n",
432 | " \"type\": \"function\",\n",
433 | " \"function\": {\n",
434 | " \"name\": \"retrieve_resources\",\n",
435 | " \"description\": \"Given a query, find resources that are relevant to the query and useful for answering it. It leverages an internal knowledge base.\",\n",
436 | " \"parameters\": {\n",
437 | " \"type\": \"object\",\n",
438 | " \"properties\": {\n",
439 | " \"query\": {\n",
440 | " \"type\": \"string\",\n",
441 | " \"description\": \"A query that will be used (via embeddings similarity search) to find relevant resources.\"\n",
442 | " }\n",
443 | " },\n",
444 | " \"required\": [\"query\"],\n",
445 | " \"additionalProperties\": False\n",
446 | " },\n",
447 | " \"response\": {\n",
448 | " \"type\": \"string\",\n",
449 | " \"description\": \"A textual representation of the resources found that are relevant to the query.\"\n",
450 | " }\n",
451 | " }\n",
452 | " }\n",
453 | "]"
454 | ]
455 | },
456 | {
457 | "cell_type": "code",
458 | "execution_count": null,
459 | "metadata": {},
460 | "outputs": [],
461 | "source": [
462 | "system_prompt = \"\"\"\n",
463 | "You are a helpful assistant whose job is answering user queries about artificial intelligence topics.\n",
464 | "Leverage the \"retrieve_resources\" tool to find resources based on the user's query.\n",
465 | "You can use the tool at most once per user query.\n",
466 | "Always leverage the retrieved resources to provide a helpful response.\n",
467 | "If you can't find useful information, don't use your knowledge to make up an answer, just say that you can't find the information in your knowledge base.\n",
468 | "Speak fast.\n",
469 | "Be very concise. Answer with at most 50 words.\n",
470 | "\"\"\".strip()"
471 | ]
472 | },
473 | {
474 | "cell_type": "code",
475 | "execution_count": null,
476 | "metadata": {},
477 | "outputs": [],
478 | "source": [
479 | "def send_audio_to_llm(audio_file_path, system_prompt):\n",
480 | " \"\"\"Sends an audio file to the OpenAI API and returns the audio completion.\"\"\"\n",
481 | " # Read the temp file and encode as base64\n",
482 | " with open(audio_file_path, \"rb\") as audio_file:\n",
483 | " encoded_audio = base64.b64encode(audio_file.read()).decode('utf-8')\n",
484 | "\n",
485 | " # Define the messages to send to the LLM\n",
486 | " messages = [\n",
487 | " {\n",
488 | " \"role\": \"system\",\n",
489 | " \"content\": system_prompt\n",
490 | " },\n",
491 | " {\n",
492 | " \"role\": \"user\",\n",
493 | " \"content\": [\n",
494 | " {\n",
495 | " \"type\": \"input_audio\",\n",
496 | " \"input_audio\": {\n",
497 | " \"data\": encoded_audio,\n",
498 | " \"format\": \"wav\"\n",
499 | " }\n",
500 | " }\n",
501 | " ]\n",
502 | " },\n",
503 | " ]\n",
504 | "\n",
505 | " # Send to OpenAI API\n",
506 | " completion = openai_client.chat.completions.create(\n",
507 | " model=\"gpt-4o-audio-preview\",\n",
508 | " modalities=[\"text\", \"audio\"],\n",
509 | " audio={\"voice\": \"alloy\", \"format\": \"pcm16\"},\n",
510 | " messages=messages,\n",
511 | " tools=tools,\n",
512 | " )\n",
513 | "\n",
514 | " return completion"
515 | ]
516 | },
517 | {
518 | "cell_type": "code",
519 | "execution_count": null,
520 | "metadata": {},
521 | "outputs": [],
522 | "source": [
523 | "completion = send_audio_to_llm(audio_file_path, system_prompt)"
524 | ]
525 | },
526 | {
527 | "cell_type": "code",
528 | "execution_count": null,
529 | "metadata": {},
530 | "outputs": [],
531 | "source": [
532 | "# Show the response (spoiler: it's a function call)\n",
533 | "completion.choices[0].to_dict()"
534 | ]
535 | },
536 | {
537 | "cell_type": "code",
538 | "execution_count": null,
539 | "metadata": {},
540 | "outputs": [],
541 | "source": [
542 | "def manage_tool_call(completion):\n",
543 | " \"\"\"\n",
544 | " If the LLM completion contains a tool call, retrieve the resources and continue the conversation.\n",
545 | " The returned conversation is in the form of a stream.\n",
546 | " \"\"\"\n",
547 | " if completion.choices[0].finish_reason == \"tool_calls\":\n",
548 | " tool_call_id = completion.choices[0].message.tool_calls[0].id\n",
549 | " tool_name = completion.choices[0].message.tool_calls[0].function.name # not used\n",
550 | " tool_query = json.loads(completion.choices[0].message.tool_calls[0].function.arguments)[\"query\"]\n",
551 | " resources = retrieve_resources(tool_query)\n",
552 | "\n",
553 | " new_messages = messages + [\n",
554 | " completion.choices[0].message,\n",
555 | " {\n",
556 | " \"role\": \"tool\",\n",
557 | " \"content\": json.dumps({\n",
558 | " \"query\": tool_query,\n",
559 | " \"resources\": resources,\n",
560 | " }),\n",
561 | " \"tool_call_id\": tool_call_id\n",
562 | " },\n",
563 | " ]\n",
564 | "\n",
565 | " stream = openai_client.chat.completions.create(\n",
566 | " model=\"gpt-4o-audio-preview\",\n",
567 | " modalities=[\"text\", \"audio\"],\n",
568 | " audio={\"voice\": \"alloy\", \"format\": \"pcm16\"},\n",
569 | " messages=new_messages,\n",
570 | " stream=True,\n",
571 | " )\n",
572 | "\n",
573 | " return stream\n",
574 | " return None"
575 | ]
576 | },
577 | {
578 | "cell_type": "code",
579 | "execution_count": null,
580 | "metadata": {},
581 | "outputs": [],
582 | "source": [
583 | "# Run the tool call and play the audio response\n",
584 | "stream = manage_tool_call(completion)\n",
585 | "play_sound_and_print_transcript(stream)"
586 | ]
587 | },
588 | {
589 | "cell_type": "markdown",
590 | "metadata": {},
591 | "source": [
592 | "## Putting All Together\n",
593 | "\n",
594 | "Last, we put everything together in a single script so that (1) the user registers its question via audio, (2) the LLM generates a final audio response leveraging the retrieval tool, and (3) the audio response is played via streaming."
595 | ]
596 | },
597 | {
598 | "cell_type": "code",
599 | "execution_count": null,
600 | "metadata": {},
601 | "outputs": [],
602 | "source": [
603 | "# 1. Record audio until the user presses 'q'\n",
604 | "audio_file_path = record_audio()\n",
605 | "\n",
606 | "# 2. Send audio to GPT4o\n",
607 | "completion = send_audio_to_llm(audio_file_path, system_prompt)\n",
608 | "\n",
609 | "# 3. Manage tool call\n",
610 | "# NB: We're assuming that the first LLM response is always a tool call!\n",
611 | "stream = manage_tool_call(completion)\n",
612 | "\n",
613 | "# 4. Play final response\n",
614 | "play_sound_and_print_transcript(stream)"
615 | ]
616 | }
617 | ],
618 | "metadata": {
619 | "kernelspec": {
620 | "display_name": "llamaindexkernel",
621 | "language": "python",
622 | "name": "llamaindexkernel"
623 | },
624 | "language_info": {
625 | "codemirror_mode": {
626 | "name": "ipython",
627 | "version": 3
628 | },
629 | "file_extension": ".py",
630 | "mimetype": "text/x-python",
631 | "name": "python",
632 | "nbconvert_exporter": "python",
633 | "pygments_lexer": "ipython3",
634 | "version": "3.11.4"
635 | }
636 | },
637 | "nbformat": 4,
638 | "nbformat_minor": 2
639 | }
640 |
--------------------------------------------------------------------------------
/notebooks/Basic_Agent_Example.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "include_colab_link": true
8 | },
9 | "kernelspec": {
10 | "name": "python3",
11 | "display_name": "Python 3"
12 | },
13 | "language_info": {
14 | "name": "python"
15 | }
16 | },
17 | "cells": [
18 | {
19 | "cell_type": "markdown",
20 | "metadata": {
21 | "id": "view-in-github",
22 | "colab_type": "text"
23 | },
24 | "source": [
25 | "
"
26 | ]
27 | },
28 | {
29 | "cell_type": "markdown",
30 | "source": [
31 | "## Install requirements"
32 | ],
33 | "metadata": {
34 | "id": "-FwCJOqEQJUB"
35 | }
36 | },
37 | {
38 | "cell_type": "code",
39 | "source": [
40 | "pip install -U duckduckgo_search openai==1.51.0 --quiet"
41 | ],
42 | "metadata": {
43 | "id": "kfJYlxFVgJir"
44 | },
45 | "execution_count": null,
46 | "outputs": []
47 | },
48 | {
49 | "cell_type": "markdown",
50 | "source": [
51 | "## Setup Enviornment variables"
52 | ],
53 | "metadata": {
54 | "id": "CDKt6U0JmiOY"
55 | }
56 | },
57 | {
58 | "cell_type": "code",
59 | "source": [
60 | "import getpass\n",
61 | "import os\n",
62 | "\n",
63 | "os.environ[\"OPENAI_API_KEY\"] = \"\""
64 | ],
65 | "metadata": {
66 | "id": "p0QOcu-OYKw0",
67 | "colab": {
68 | "base_uri": "https://localhost:8080/"
69 | },
70 | "outputId": "fd4abce0-55cf-4ce2-f6ba-1d1b5c761f23"
71 | },
72 | "execution_count": null,
73 | "outputs": [
74 | {
75 | "name": "stdout",
76 | "output_type": "stream",
77 | "text": [
78 | "Enter your OPENAI API key: ··········\n"
79 | ]
80 | }
81 | ]
82 | },
83 | {
84 | "cell_type": "markdown",
85 | "source": [
86 | "## Setting Up Tools"
87 | ],
88 | "metadata": {
89 | "id": "aEu_qYO4ghI3"
90 | }
91 | },
92 | {
93 | "cell_type": "code",
94 | "source": [
95 | "from duckduckgo_search import DDGS\n",
96 | "\n",
97 | "def search(query):\n",
98 | " \"\"\"\n",
99 | " perform a search on for the given query and return the results\n",
100 | " :param query: the query to search for\n",
101 | " :return: the search results\n",
102 | " \"\"\"\n",
103 | " response = DDGS().text(query, max_results=5)\n",
104 | " results = [\n",
105 | " {\"snippet\": r[\"body\"], \"title\": r[\"title\"], \"link\": r[\"href\"]}\n",
106 | " for r in response\n",
107 | " ]\n",
108 | "\n",
109 | " formatted_results = \"\"\n",
110 | " for result in results:\n",
111 | " formatted_results += f\"Title: {result['title']}\\n\"\n",
112 | " formatted_results += f\"Snippet: {result['snippet']}\\n\"\n",
113 | " formatted_results += \"----\\n\"\n",
114 | "\n",
115 | " return formatted_results\n"
116 | ],
117 | "metadata": {
118 | "id": "-hrcnzMEiPBx"
119 | },
120 | "execution_count": null,
121 | "outputs": []
122 | },
123 | {
124 | "cell_type": "code",
125 | "source": [
126 | "def evaluate_expression(operation: str) -> float:\n",
127 | " \"\"\"\n",
128 | " perform a calculation on the given operation and return the result\n",
129 | " :param operation: the operation to perform, should be compatible to use with eval eg: operation: \"1+2\"\n",
130 | " :return: the result of the operation\n",
131 | " \"\"\"\n",
132 | " return eval(operation)"
133 | ],
134 | "metadata": {
135 | "id": "7g6kAKbIsP6p"
136 | },
137 | "execution_count": null,
138 | "outputs": []
139 | },
140 | {
141 | "cell_type": "code",
142 | "source": [
143 | "print(search(\"llama index\"))"
144 | ],
145 | "metadata": {
146 | "colab": {
147 | "base_uri": "https://localhost:8080/"
148 | },
149 | "id": "kzmjpvaBiARo",
150 | "outputId": "0099e5a1-fc57-4b04-e768-587abf75eb47"
151 | },
152 | "execution_count": null,
153 | "outputs": [
154 | {
155 | "output_type": "stream",
156 | "name": "stdout",
157 | "text": [
158 | "Title: LlamaIndex Newsletter 2024-10-22\n",
159 | "Snippet: VividNode, a cross-platform desktop app that enables chatting with models like GPT, Claude, Gemini, and Llama. It features a chat interface, image generation capabilities with DALL-E 3, and advanced settings for privacy and convenience, all integrated with LlamaIndex. Github Repo, Tweet.\n",
160 | "----\n",
161 | "Title: Which Tools to Use for LLM-Powered Applications: LangChain vs ...\n",
162 | "Snippet: In this section, you'll learn how to use LlamaIndex to create a queryable index from a collection of documents and interact with OpenAI's API for querying purposes. This is the code to do this: pip install llama-index %env OPENAI_API_KEY = \"your-api-key\" from llama_index.core import VectorStoreIndex, SimpleDirectoryReader\n",
163 | "----\n",
164 | "Title: Unleashing the Power of Gemini With LlamaIndex - DZone\n",
165 | "Snippet: pip install -q llama-index google-generativeai pip install llama-index-llms-gemini pip install llama-index-embeddings-gemini. Next, import the required modules from LlamaIndex: Python\n",
166 | "----\n",
167 | "Title: Gen AI - LLM RAG Two in One - LangChain + LlamaIndex - Udemy\n",
168 | "Snippet: Gen AI - Learn to develop RAG Applications using LangChain an LlamaIndex Frameworks using LLMs and Vector Databases\n",
169 | "----\n",
170 | "Title: Implementation of REAcT Agent using LlamaIndex and Gemini\n",
171 | "Snippet: !pip install llama-index !pip install duckduckgo-search !pip install llama-index-llms-gemini. First, we need to import the necessary components. Since the ReAct agent needs to interact with external tools to fetch data, we can achieve this using the Function Tool, which is defined within the LlamaIndex core tools.\n",
172 | "----\n",
173 | "\n"
174 | ]
175 | }
176 | ]
177 | },
178 | {
179 | "cell_type": "markdown",
180 | "source": [
181 | "## Creating Agent"
182 | ],
183 | "metadata": {
184 | "id": "MiLV8jjioV7U"
185 | }
186 | },
187 | {
188 | "cell_type": "code",
189 | "source": [
190 | "import re\n",
191 | "from openai import OpenAI\n",
192 | "\n",
193 | "class Agent:\n",
194 | " def __init__(self, client: OpenAI, tools):\n",
195 | " \"\"\"\n",
196 | " Initialize the Agent class.\n",
197 | "\n",
198 | " Args:\n",
199 | " client (OpenAI): The OpenAI client instance.\n",
200 | " tools (list): A list of tool functions.\n",
201 | " \"\"\"\n",
202 | " self.client = client\n",
203 | " self.tools = tools\n",
204 | " self.memory = []\n",
205 | "\n",
206 | " def get_agent_prompt(self) -> str:\n",
207 | " \"\"\"\n",
208 | " Generate the agent prompt string.\n",
209 | "\n",
210 | " Returns:\n",
211 | " str: The agent prompt string.\n",
212 | " \"\"\"\n",
213 | " tools_str = \"\\n\".join(\n",
214 | " [f\"Action:{tool.__name__}\\nAction Input:{tool.__doc__} \\n\" for tool in self.tools]\n",
215 | " )\n",
216 | " system_prompt = f\"\"\"\n",
217 | " You run in a loop of Thought, Action, Observation.\n",
218 | " At the end of the loop you output an Answer\n",
219 | " Use Thought to describe your thoughts about the question you have been asked.\n",
220 | " Use Action to run one of the actions available to you.\n",
221 | " Observation will be the result of running those actions.\n",
222 | "\n",
223 | " Your available actions are:\n",
224 | " {tools_str}\n",
225 | "\n",
226 | " Try to break the search query into multiple query for good results\n",
227 | "\n",
228 | " Always return in this format\n",
229 | " Question: the question\n",
230 | " Thought: your thoughts about the question\n",
231 | " Action: tool_name: the input to the action\n",
232 | "\n",
233 | " Example session:\n",
234 | " Question: New York is in which country?\n",
235 | " Thought: I should search for new york in web\n",
236 | " Action: search: new york\n",
237 | "\n",
238 | " You will be called again with this:\n",
239 | "\n",
240 | " Observation: New york is a city in the United States of America.\n",
241 | "\n",
242 | " You then output:\n",
243 | " Final Answer: New York is in the United States of America.\n",
244 | " \"\"\"\n",
245 | " return system_prompt\n",
246 | "\n",
247 | " def add_agent_steps_in_memory(self, role: str, content: str) -> None:\n",
248 | " \"\"\"\n",
249 | " Add agent steps to memory.\n",
250 | "\n",
251 | " Args:\n",
252 | " role (str): The role of the agent step.\n",
253 | " content (str): The content of the agent step.\n",
254 | " \"\"\"\n",
255 | " self.memory.append({\"role\": role, \"content\": content})\n",
256 | "\n",
257 | " def invoke(self, user_input: str, max_iterations: int = 10) -> str:\n",
258 | " \"\"\"\n",
259 | " Invoke the agent.\n",
260 | "\n",
261 | " Args:\n",
262 | " user_input (str): The user input string.\n",
263 | " max_iterations (int): The maximum number of iterations.\n",
264 | "\n",
265 | " Returns:\n",
266 | " str: The final answer.\n",
267 | " \"\"\"\n",
268 | " i = 0\n",
269 | " self.add_agent_steps_in_memory(\"system\", self.get_agent_prompt())\n",
270 | " current_message = user_input\n",
271 | " tool_names = [tool.__name__ for tool in self.tools]\n",
272 | "\n",
273 | " while i < max_iterations:\n",
274 | " print(f\"Iteration: {i+1}\")\n",
275 | " i += 1\n",
276 | " self.add_agent_steps_in_memory(\"user\", current_message)\n",
277 | " result = self.client.chat.completions.create(\n",
278 | " messages=self.memory,\n",
279 | " model=\"gpt-4o\",\n",
280 | " temperature=0.2\n",
281 | " )\n",
282 | " agent_output = result.choices[0].message.content\n",
283 | " print(agent_output)\n",
284 | " self.add_agent_steps_in_memory(\"assistant\", agent_output)\n",
285 | "\n",
286 | " if \"Action\" in agent_output:\n",
287 | " action = re.findall(r\"Action: ([a-z_]+): (.+)\", agent_output, re.IGNORECASE)\n",
288 | " print(action)\n",
289 | " chosen_tool = action[0][0]\n",
290 | " arg = action[0][1]\n",
291 | "\n",
292 | " if chosen_tool in tool_names:\n",
293 | " result_tool = eval(f\"{chosen_tool}('{arg}')\")\n",
294 | " current_message = f\"Observation:\\n {result_tool}\"\n",
295 | " print(f\"chosen tool: {chosen_tool}\")\n",
296 | " print(f\"arg: {arg}\")\n",
297 | " print(current_message)\n",
298 | " else:\n",
299 | " current_message = \"Observation: Tool not found\"\n",
300 | " continue\n",
301 | "\n",
302 | " print(\"----------------------------------------------------------\")\n",
303 | "\n",
304 | " if \"Final Answer\" in agent_output:\n",
305 | " return agent_output"
306 | ],
307 | "metadata": {
308 | "id": "jk3TxbcJnnKq"
309 | },
310 | "execution_count": null,
311 | "outputs": []
312 | },
313 | {
314 | "cell_type": "code",
315 | "source": [
316 | "client = OpenAI(api_key = os.environ[\"OPENAI_API_KEY\"])\n",
317 | "tools = [search, evaluate_expression]\n",
318 | "agent = Agent(client, tools)\n",
319 | "output = agent.invoke(\"what is differnce between stock price of microsoft and nvidia in 2024? Which has higher stock price?\")\n",
320 | "print(f\"Final Output-> {output}\")"
321 | ],
322 | "metadata": {
323 | "colab": {
324 | "base_uri": "https://localhost:8080/"
325 | },
326 | "id": "nc6Zqqjuwqqz",
327 | "outputId": "cd5257ee-316e-449f-a95c-be30a10d605c"
328 | },
329 | "execution_count": null,
330 | "outputs": [
331 | {
332 | "output_type": "stream",
333 | "name": "stdout",
334 | "text": [
335 | "Iteration: 1\n",
336 | "Question: what is difference between stock price of microsoft and nvidia in 2024? Which has higher stock price?\n",
337 | "Thought: I need to search for the stock prices of Microsoft and Nvidia in 2024 to determine the difference and which one is higher.\n",
338 | "Action: search: Microsoft stock price 2024\n",
339 | "\n",
340 | "\n",
341 | "[('search', 'Microsoft stock price 2024')]\n",
342 | "chosen tool: search\n",
343 | "arg: Microsoft stock price 2024\n",
344 | "Observation:\n",
345 | " Title: Microsoft (MSFT) Stock Forecast and Price Target 2024 - MarketBeat\n",
346 | "Snippet: Microsoft - Analysts' Recommendations and Stock Price Forecast (2024) How MarketBeat Calculates Price Target and Consensus Rating. ... According to the research reports of 31 Wall Street equities research analysts, the average twelve-month stock price forecast for Microsoft is $495.68, with a high forecast of $600.00 and a low forecast of $375. ...\n",
347 | "----\n",
348 | "Title: Microsoft Stock Forecast 2025-2030 | Is MSFT a Good Buy? - Techopedia\n",
349 | "Snippet: Their consensus one-year Microsoft (MSFT) stock forecast for 2024/2025 is for the price to rise 18.67% to $493.47. The highest projection is $600.00, while the lowest estimate warns of a drop to $375. However, others are more optimistic. A consensus MSFT price target from analyst views compiled by TipRanks puts the stock as a 'strong buy ...\n",
350 | "----\n",
351 | "Title: Where Will Microsoft Stock Be in 3 Years? | The Motley Fool\n",
352 | "Snippet: Current Price. $418.16. Price as of October 18, 2024, 4:00 p.m. ET. Microsoft's AI-powered growth should help the stock regain its mojo following a disappointing 2024. Shares of Microsoft (MSFT 0. ...\n",
353 | "----\n",
354 | "Title: Microsoft Corporation (MSFT) Stock Forecast & Price Targets - Stock ...\n",
355 | "Snippet: The 33 analysts with 12-month price forecasts for Microsoft stock have an average target of 494.73, with a low estimate of 375 and a high estimate of 600. The average target predicts an increase of 18.14% from the current stock price of 418.78. ... FY 2024 FY 2025 FY 2026 FY 2027 FY 2028 FY 2029 ; Period Ending Jun 30, 2020 Jun 30, 2021 Jun 30 ...\n",
356 | "----\n",
357 | "Title: What is the current Price Target and Forecast for Microsoft (MSFT)\n",
358 | "Snippet: The forecasts for Microsoft Corporation (MSFT) range from a low of $448 to a high of $600. The average price target represents a increase of $20.35 from the last closing price of $418.16. The ...\n",
359 | "----\n",
360 | "\n",
361 | "Iteration: 2\n",
362 | "Action: search: Nvidia stock price 2024\n",
363 | "[('search', 'Nvidia stock price 2024')]\n",
364 | "chosen tool: search\n",
365 | "arg: Nvidia stock price 2024\n",
366 | "Observation:\n",
367 | " Title: NVIDIA (NVDA) Stock Forecast and Price Target 2024 - MarketBeat\n",
368 | "Snippet: What is NVIDIA's forecast for 2024? According to the research reports of 43 Wall Street equities research analysts, the average twelve-month stock price forecast for NVIDIA is $143.07, with a high forecast of $200.00 and a low forecast of $65.00.\n",
369 | "----\n",
370 | "Title: Where Will Nvidia Stock Be in 5 Years? | The Motley Fool\n",
371 | "Snippet: Nvidia's stock price slump seems to have ended in the near term, with shares regaining momentum. ... Price as of October 22, 2024, 4:00 p.m. ET. The iconic chipmaker is once again testing its all ...\n",
372 | "----\n",
373 | "Title: Nvidia Stock Gets Bullish Upgrades on Blackwell, Enterprise ...\n",
374 | "Snippet: Wall Street keeps raising price targets for sky-high Nvidia stock. Here's why analysts are still so bullish. ... 2024-10-21T19:47:22Z Jennifer Sor. An curved arrow pointing right. Share. The ...\n",
375 | "----\n",
376 | "Title: Nvidia Stock Price Prediction: 2024, 2025, 2030 - Benzinga\n",
377 | "Snippet: Explore expert predictions for Nvidia's stock price in 2024, 2025 and 2030, with insights on market trends, AI growth, and future valuations.\n",
378 | "----\n",
379 | "Title: NVDA - Nvidia Corp Stock Price Forecast 2024, 2025, 2030 to 2050\n",
380 | "Snippet: Nvidia Corp Stock (NVDA) Price Forecast for 2025. Nvidia Corp Stock (NVDA) is expected to reach an average price of $ 3,887.16 in 2025, with a high prediction of $ 5,231.05 and a low estimate of $ 2,543.27. This indicates an +2716.78% rise from the last recorded price of $ 138.00. Month.\n",
381 | "----\n",
382 | "\n",
383 | "Iteration: 3\n",
384 | "Thought: I have found the average stock price forecasts for both Microsoft and Nvidia in 2024. Microsoft's average stock price is around $495.68, while Nvidia's average stock price is around $143.07. Now, I can determine the difference and which one has the higher stock price.\n",
385 | "\n",
386 | "Action: evaluate_expression: \"495.68 - 143.07\"\n",
387 | "[('evaluate_expression', '\"495.68 - 143.07\"')]\n",
388 | "chosen tool: evaluate_expression\n",
389 | "arg: \"495.68 - 143.07\"\n",
390 | "Observation:\n",
391 | " 495.68 - 143.07\n",
392 | "Iteration: 4\n",
393 | "Final Answer: The difference between the stock prices of Microsoft and Nvidia in 2024 is $352.61, with Microsoft having the higher stock price.\n",
394 | "----------------------------------------------------------\n",
395 | "Final Output-> Final Answer: The difference between the stock prices of Microsoft and Nvidia in 2024 is $352.61, with Microsoft having the higher stock price.\n"
396 | ]
397 | }
398 | ]
399 | }
400 | ]
401 | }
--------------------------------------------------------------------------------
/notebooks/Evaluating_and_Iterating_Prompts.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "[](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/Evaluating_and_Iterating_Prompts.ipynb)"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {
13 | "id": "_0uFIE3nYnqs"
14 | },
15 | "source": [
16 | "### Installing Necessary packages"
17 | ]
18 | },
19 | {
20 | "cell_type": "code",
21 | "execution_count": null,
22 | "metadata": {
23 | "colab": {
24 | "base_uri": "https://localhost:8080/"
25 | },
26 | "collapsed": true,
27 | "id": "RpCDuBbpa41t",
28 | "outputId": "a6d13600-d317-456f-8fd9-3b490e983631"
29 | },
30 | "outputs": [
31 | {
32 | "name": "stdout",
33 | "output_type": "stream",
34 | "text": [
35 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m386.9/386.9 kB\u001b[0m \u001b[31m9.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
36 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m76.4/76.4 kB\u001b[0m \u001b[31m2.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
37 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m78.0/78.0 kB\u001b[0m \u001b[31m3.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
38 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m325.2/325.2 kB\u001b[0m \u001b[31m5.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
39 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m2.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
40 | "\u001b[?25h"
41 | ]
42 | }
43 | ],
44 | "source": [
45 | "!pip install -q openai google-generativeai"
46 | ]
47 | },
48 | {
49 | "cell_type": "code",
50 | "execution_count": null,
51 | "metadata": {
52 | "id": "KRqtrsMQYsuD"
53 | },
54 | "outputs": [],
55 | "source": [
56 | "import os\n",
57 | "\n",
58 | "# Set the following API Keys in the Python environment. Will be used later.\n",
59 | "os.environ[\"OPENAI_API_KEY\"] = \"\"\n",
60 | "\n",
61 | "\n",
62 | "# from google.colab import userdata\n",
63 | "# os.environ[\"OPENAI_API_KEY\"] = userdata.get('OPENAI_API_KEY1')"
64 | ]
65 | },
66 | {
67 | "cell_type": "code",
68 | "execution_count": null,
69 | "metadata": {
70 | "id": "sgGQUCbXZLaf"
71 | },
72 | "outputs": [],
73 | "source": [
74 | "# Allows running asyncio in environments with an existing event loop in the notebook\n",
75 | "\n",
76 | "import nest_asyncio\n",
77 | "\n",
78 | "nest_asyncio.apply()"
79 | ]
80 | },
81 | {
82 | "cell_type": "markdown",
83 | "metadata": {
84 | "id": "Z2eMMueuZMRa"
85 | },
86 | "source": [
87 | "### Response Generation Using GPT-4o"
88 | ]
89 | },
90 | {
91 | "cell_type": "code",
92 | "execution_count": null,
93 | "metadata": {
94 | "id": "RHS9vut2ZOEj"
95 | },
96 | "outputs": [],
97 | "source": [
98 | "from openai import OpenAI\n",
99 | "client = OpenAI()"
100 | ]
101 | },
102 | {
103 | "cell_type": "code",
104 | "execution_count": null,
105 | "metadata": {
106 | "colab": {
107 | "base_uri": "https://localhost:8080/"
108 | },
109 | "id": "uKF4ugsCcRf-",
110 | "outputId": "23464136-555c-4767-8704-598011550a05"
111 | },
112 | "outputs": [
113 | {
114 | "name": "stdout",
115 | "output_type": "stream",
116 | "text": [
117 | "---\n",
118 | "Company | Foundation Year\n",
119 | "Pfizer | 1849\n",
120 | "Johnson & Johnson | 1886\n",
121 | "Roche | 1896\n",
122 | "Novartis | 1996\n",
123 | "Merck & Co. | 1891\n",
124 | "---\n"
125 | ]
126 | }
127 | ],
128 | "source": [
129 | "prompt_generation = \"\"\"\n",
130 | "Generate a table with the 5 most popular pharmaceutical companies and their foundation years.\n",
131 | "The response should include only the table, with no additional text.\n",
132 | "Use the following example format:\n",
133 | "---\n",
134 | "Company | Foundation Year\n",
135 | "Microsoft | 1975\n",
136 | "Apple | 1976\n",
137 | "Google | 1998\n",
138 | "Amazon | 1994\n",
139 | "Meta | 2004\n",
140 | "---\"\"\"\n",
141 | "\n",
142 | "\n",
143 | "# Making the API call\n",
144 | "response = client.chat.completions.create(\n",
145 | " model=\"gpt-4o-2024-08-06\",\n",
146 | " messages=[\n",
147 | " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
148 | " {\"role\": \"user\", \"content\": prompt_generation}\n",
149 | " ]\n",
150 | ")\n",
151 | "generated_response = response.choices[0].message.content\n",
152 | "print(generated_response)"
153 | ]
154 | },
155 | {
156 | "cell_type": "markdown",
157 | "metadata": {
158 | "id": "hFjpETnkHrV8"
159 | },
160 | "source": [
161 | "### Format Checking with GPT-4o Model (GPT-4o will act as a Judge)"
162 | ]
163 | },
164 | {
165 | "cell_type": "code",
166 | "execution_count": null,
167 | "metadata": {
168 | "colab": {
169 | "base_uri": "https://localhost:8080/"
170 | },
171 | "id": "SiW3vwKshVH8",
172 | "outputId": "3c8e46d6-58f6-404b-90e6-4f2f9ed85641"
173 | },
174 | "outputs": [
175 | {
176 | "name": "stdout",
177 | "output_type": "stream",
178 | "text": [
179 | "Yes\n"
180 | ]
181 | }
182 | ],
183 | "source": [
184 | "prompt_check_table_new = \"\"\"Your task is to verify if a given table matches the exact format and structure of a provided example table.\n",
185 | "\n",
186 | "Here's an example of the format that the table should have:\n",
187 | "---\n",
188 | "Company | Foundation Year\n",
189 | "Microsoft | 1975\n",
190 | "Apple | 1976\n",
191 | "Google | 1998\n",
192 | "Amazon | 1994\n",
193 | "Meta | 2004\n",
194 | "---\n",
195 | "\n",
196 | "Table to Check:\n",
197 | "{table_to_check}\n",
198 | "\n",
199 | "Instructions:\n",
200 | "- The table to check should match the structure, headers, and format of the Example Table exactly.\n",
201 | "- The column names must be \"Company\" and \"Foundation Year\".\n",
202 | "- The values in each row should have the company names and their corresponding foundation years.\n",
203 | "- If the given table matches the example table in all these aspects, write \"Yes\".\n",
204 | "- Write \"No\" if there are any differences in structure, headers, or if any company/year is missing or incorrect.\n",
205 | "\n",
206 | "Only respond with \"Yes\" or \"No\".\n",
207 | "\n",
208 | "\"\"\"\n",
209 | "\n",
210 | "formatted_prompt = prompt_check_table_new.format(table_to_check=generated_response)\n",
211 | "\n",
212 | "\n",
213 | "# using LLM as a Judge to check the format\n",
214 | "response = client.chat.completions.create(\n",
215 | " model=\"gpt-4o-2024-08-06\",\n",
216 | " messages=[\n",
217 | " {\"role\": \"system\", \"content\": \"You are a strict judge. Evaluate inputs based on the given criteria and provide only the required response\"},\n",
218 | " {\"role\": \"user\", \"content\": formatted_prompt}\n",
219 | " ],\n",
220 | " temperature= 0\n",
221 | ")\n",
222 | "print(response.choices[0].message.content)"
223 | ]
224 | },
225 | {
226 | "cell_type": "code",
227 | "execution_count": null,
228 | "metadata": {
229 | "colab": {
230 | "base_uri": "https://localhost:8080/"
231 | },
232 | "id": "BTn7Ge6V4Om3",
233 | "outputId": "8cb8c32a-cdcf-485a-9917-fad2c155ec64"
234 | },
235 | "outputs": [
236 | {
237 | "name": "stdout",
238 | "output_type": "stream",
239 | "text": [
240 | "Your task is to verify if a given table matches the exact format and structure of a provided example table.\n",
241 | "\n",
242 | "Here's an example of the format that the table should have:\n",
243 | "---\n",
244 | "Company | Foundation Year\n",
245 | "Microsoft | 1975\n",
246 | "Apple | 1976\n",
247 | "Google | 1998\n",
248 | "Amazon | 1994\n",
249 | "Meta | 2004\n",
250 | "---\n",
251 | "\n",
252 | "Table to Check:\n",
253 | "---\n",
254 | "Company | Foundation Year \n",
255 | "Pfizer | 1849 \n",
256 | "Roche | 1896 \n",
257 | "Sanofi | 1973 \n",
258 | "AbbVie | 2013 \n",
259 | "Novartis | 1996 \n",
260 | "---\n",
261 | "\n",
262 | "Instructions:\n",
263 | "- The table to check should match the structure, headers, and format of the Example Table exactly.\n",
264 | "- The column names must be \"Company\" and \"Foundation Year\".\n",
265 | "- The values in each row should have the company names and their corresponding foundation years.\n",
266 | "- If the given table matches the example table in all these aspects, write \"Yes\".\n",
267 | "- Write \"No\" if there are any differences in structure, headers, or if any company/year is missing or incorrect.\n",
268 | "\n",
269 | "Only respond with \"Yes\" or \"No\".\n",
270 | "\n",
271 | "\n"
272 | ]
273 | }
274 | ],
275 | "source": [
276 | "print(formatted_prompt)"
277 | ]
278 | },
279 | {
280 | "cell_type": "code",
281 | "execution_count": null,
282 | "metadata": {
283 | "id": "oLbGB9HSFvTT"
284 | },
285 | "outputs": [],
286 | "source": [
287 | "# Example Badly Formatted Table\n",
288 | "\n",
289 | "badly_formatted_table = \"\"\"\n",
290 | "Company | Foundation Year\n",
291 | "\n",
292 | "Microsoft | 1975\n",
293 | "\n",
294 | "Apple | 1976\n",
295 | "Google | 1998\n",
296 | "Amazon | 1994\n",
297 | "\n",
298 | "Meta | 2004\n",
299 | "\"\"\""
300 | ]
301 | },
302 | {
303 | "cell_type": "code",
304 | "execution_count": null,
305 | "metadata": {
306 | "colab": {
307 | "base_uri": "https://localhost:8080/"
308 | },
309 | "id": "yVebrs01FltN",
310 | "outputId": "3347f0a2-eeed-470e-86c7-c3fb9911d75d"
311 | },
312 | "outputs": [
313 | {
314 | "name": "stdout",
315 | "output_type": "stream",
316 | "text": [
317 | "No\n"
318 | ]
319 | }
320 | ],
321 | "source": [
322 | "formatted_prompt = prompt_check_table_new.format(table_to_check=badly_formatted_table)\n",
323 | "\n",
324 | "\n",
325 | "# using LLM as a Judge to check the format\n",
326 | "response = client.chat.completions.create(\n",
327 | " model=\"gpt-4o-2024-08-06\",\n",
328 | " messages=[\n",
329 | " {\"role\": \"system\", \"content\": \"You are a strict judge. Evaluate inputs based on the given criteria and provide only the required response\"},\n",
330 | " {\"role\": \"user\", \"content\": formatted_prompt}\n",
331 | " ],\n",
332 | " temperature= 0\n",
333 | ")\n",
334 | "print(response.choices[0].message.content)"
335 | ]
336 | },
337 | {
338 | "cell_type": "markdown",
339 | "metadata": {
340 | "id": "Nv0-5bWgHmif"
341 | },
342 | "source": [
343 | "### GPT-4o Acting as a Judge to check the company types (Pharmacitical or not)"
344 | ]
345 | },
346 | {
347 | "cell_type": "code",
348 | "execution_count": null,
349 | "metadata": {
350 | "colab": {
351 | "base_uri": "https://localhost:8080/"
352 | },
353 | "id": "DqU-oZZ8edsj",
354 | "outputId": "69a0d48c-7568-414d-e6a1-82dcd353db56"
355 | },
356 | "outputs": [
357 | {
358 | "name": "stdout",
359 | "output_type": "stream",
360 | "text": [
361 | "Yes\n"
362 | ]
363 | }
364 | ],
365 | "source": [
366 | "prompt_check_company_type = \"\"\"\n",
367 | "Your task is to verify if a given table contains only companies from the pharmaceutical industry.\n",
368 | "\n",
369 | "Here's an example of the format that the table should have:\n",
370 | "---\n",
371 | "Company | Foundation Year\n",
372 | "Microsoft | 1975\n",
373 | "Apple | 1976\n",
374 | "Google | 1998\n",
375 | "Amazon | 1994\n",
376 | "Meta | 2004\n",
377 | "---\n",
378 | "\n",
379 | "Here's the table to check:\n",
380 | "{table_to_check}\n",
381 | "\n",
382 | "Instructions:\n",
383 | "- The table to check should include only companies from the pharmaceutical industry.\n",
384 | "- The column names will be \"Company\" and \"Foundation Year\".\n",
385 | "- Evaluate whether each company in the table is part of the pharmaceutical sector.\n",
386 | "- If all companies in the table are from the pharmaceutical industry, write \"Yes\".\n",
387 | "- Write \"No\" if any company in the table is not from the pharmaceutical industry.\n",
388 | "\n",
389 | "Only respond with \"Yes\" or \"No\".\n",
390 | "\n",
391 | "\"\"\"\n",
392 | "\n",
393 | "formatted_prompt_company_type = prompt_check_table_new.format(table_to_check=generated_response)\n",
394 | "\n",
395 | "# API Call\n",
396 | "response = client.chat.completions.create(\n",
397 | " model=\"gpt-4o-2024-08-06\",\n",
398 | " messages=[\n",
399 | " {\"role\": \"system\", \"content\": \"You are a strict judge. Evaluate inputs based on the given criteria and provide only the required response\"},\n",
400 | " {\"role\": \"user\", \"content\": formatted_prompt_company_type}\n",
401 | " ],\n",
402 | " temperature= 0\n",
403 | ")\n",
404 | "print(response.choices[0].message.content)"
405 | ]
406 | },
407 | {
408 | "cell_type": "code",
409 | "execution_count": null,
410 | "metadata": {
411 | "id": "437zfxeJnqcJ"
412 | },
413 | "outputs": [],
414 | "source": []
415 | }
416 | ],
417 | "metadata": {
418 | "colab": {
419 | "provenance": []
420 | },
421 | "kernelspec": {
422 | "display_name": "Python 3",
423 | "name": "python3"
424 | },
425 | "language_info": {
426 | "name": "python"
427 | }
428 | },
429 | "nbformat": 4,
430 | "nbformat_minor": 0
431 | }
432 |
--------------------------------------------------------------------------------
/notebooks/Firecrawl_Scraping.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {
6 | "colab_type": "text",
7 | "id": "view-in-github"
8 | },
9 | "source": [
10 | "
"
11 | ]
12 | },
13 | {
14 | "cell_type": "markdown",
15 | "metadata": {
16 | "id": "z9h3lrtQRhYv"
17 | },
18 | "source": [
19 | "## Install the requirements"
20 | ]
21 | },
22 | {
23 | "cell_type": "code",
24 | "execution_count": null,
25 | "metadata": {
26 | "id": "AKBQrqNsjrLB"
27 | },
28 | "outputs": [],
29 | "source": [
30 | "pip install -q llama-index==0.10.30 openai==1.12.0 tiktoken==0.6.0 llama-index-readers-web firecrawl-py==1.2.3"
31 | ]
32 | },
33 | {
34 | "cell_type": "markdown",
35 | "metadata": {
36 | "id": "TKh_dKV6Rm9a"
37 | },
38 | "source": [
39 | "### SET THE ENVIRONMENT VARIABLES"
40 | ]
41 | },
42 | {
43 | "cell_type": "code",
44 | "execution_count": null,
45 | "metadata": {
46 | "id": "QZgBdtZRJfze"
47 | },
48 | "outputs": [],
49 | "source": [
50 | "import os\n",
51 | "os.environ[\"OPENAI_API_KEY\"] = \"\"\n",
52 | "FIRECRAWL_API_KEY = \"\""
53 | ]
54 | },
55 | {
56 | "cell_type": "markdown",
57 | "metadata": {
58 | "id": "tiBy7Q74Vkt_"
59 | },
60 | "source": [
61 | "# SCRAPE WITH FIRECRAWL"
62 | ]
63 | },
64 | {
65 | "cell_type": "markdown",
66 | "metadata": {
67 | "id": "GQP8VA_gRr2g"
68 | },
69 | "source": [
70 | "## IMPORT THE FIRECRAWL WEBREADER\n",
71 | "\n",
72 | "Firecrawl allows you to turn entire websites into LLM-ready markdown\n",
73 | "\n",
74 | "Get the API key here\n",
75 | "https://www.firecrawl.dev/app/api-keys"
76 | ]
77 | },
78 | {
79 | "cell_type": "code",
80 | "execution_count": null,
81 | "metadata": {
82 | "colab": {
83 | "base_uri": "https://localhost:8080/"
84 | },
85 | "id": "YuLILEoNj2lG",
86 | "outputId": "293ad6c5-4859-46bb-e25a-b588c014867a"
87 | },
88 | "outputs": [],
89 | "source": [
90 | "from llama_index.readers.web import FireCrawlWebReader"
91 | ]
92 | },
93 | {
94 | "cell_type": "code",
95 | "execution_count": null,
96 | "metadata": {
97 | "id": "x-ls9L0QWC3S"
98 | },
99 | "outputs": [],
100 | "source": [
101 | "\n",
102 | "# using firecrawl to crawl a website\n",
103 | "firecrawl_reader = FireCrawlWebReader(\n",
104 | " api_key=FIRECRAWL_API_KEY, # Replace with your actual API key from https://www.firecrawl.dev/\n",
105 | " mode=\"scrape\",\n",
106 | ")\n",
107 | "\n",
108 | "# Load documents from a single page URL\n",
109 | "documents = firecrawl_reader.load_data(url=\"https://towardsai.net/\")"
110 | ]
111 | },
112 | {
113 | "cell_type": "code",
114 | "execution_count": null,
115 | "metadata": {
116 | "id": "zL1BOBWhWrfe"
117 | },
118 | "outputs": [],
119 | "source": [
120 | "from llama_index.core import VectorStoreIndex\n",
121 | "\n",
122 | "index = VectorStoreIndex.from_documents(documents)\n",
123 | "query_engine = index.as_query_engine()"
124 | ]
125 | },
126 | {
127 | "cell_type": "code",
128 | "execution_count": null,
129 | "metadata": {
130 | "colab": {
131 | "base_uri": "https://localhost:8080/"
132 | },
133 | "id": "AV4Fg-haWspw",
134 | "outputId": "04642e46-1f4f-4e89-972a-326cca793f34"
135 | },
136 | "outputs": [],
137 | "source": [
138 | "res = query_engine.query(\"What is towards AI aim?\")\n",
139 | "\n",
140 | "print(res.response)\n",
141 | "\n",
142 | "print(\"-----------------\")\n",
143 | "# Show the retrieved nodes\n",
144 | "for src in res.source_nodes:\n",
145 | " print(\"Node ID\\t\", src.node_id)\n",
146 | " print(\"Title\\t\", src.metadata['title'])\n",
147 | " print(\"URL\\t\", src.metadata['sourceURL'])\n",
148 | " print(\"Score\\t\", src.score)\n",
149 | " print(\"Description\\t\", src.metadata.get(\"description\"))\n",
150 | " print(\"-_\"*20)"
151 | ]
152 | },
153 | {
154 | "cell_type": "markdown",
155 | "metadata": {
156 | "id": "lRjJrc7VVaNX"
157 | },
158 | "source": [
159 | "# CRAWL A WEBSITE"
160 | ]
161 | },
162 | {
163 | "cell_type": "markdown",
164 | "metadata": {
165 | "id": "Uj6dqla8SEwd"
166 | },
167 | "source": [
168 | "## Load The CSV\n",
169 | "\n",
170 | "CSV contains the list of tools and url of the page which we use to get information about the tool."
171 | ]
172 | },
173 | {
174 | "cell_type": "code",
175 | "execution_count": null,
176 | "metadata": {
177 | "id": "aWTgax6zZpLx"
178 | },
179 | "outputs": [],
180 | "source": [
181 | "import requests\n",
182 | "import csv\n",
183 | "\n",
184 | "# Google Sheets file URL (CSV export link)\n",
185 | "url = 'https://docs.google.com/spreadsheets/d/1gHB-aQJGt9Nl3cyOP2GorAkBI_Us2AqkYnfqrmejStc/export?format=csv'\n",
186 | "\n",
187 | "# Send a GET request to fetch the CSV file\n",
188 | "response = requests.get(url)\n",
189 | "\n",
190 | "response_list = []\n",
191 | "# Check if the request was successful\n",
192 | "if response.status_code == 200:\n",
193 | " # Decode the content to a string\n",
194 | " content = response.content.decode('utf-8')\n",
195 | "\n",
196 | " # Use the csv.DictReader to read the content as a dictionary\n",
197 | " csv_reader = csv.DictReader(content.splitlines(), delimiter=',')\n",
198 | " response_list = [row for row in csv_reader]\n",
199 | "else:\n",
200 | " print(f\"Failed to retrieve the file: {response.status_code}\")\n"
201 | ]
202 | },
203 | {
204 | "cell_type": "code",
205 | "execution_count": null,
206 | "metadata": {
207 | "id": "atFrRz4MgmaR"
208 | },
209 | "outputs": [],
210 | "source": [
211 | "import random\n",
212 | "\n",
213 | "start_index = random.randint(0, len(response_list) - 3)\n",
214 | "website_list = response_list[start_index:start_index+2] # crawling 2 website for demo"
215 | ]
216 | },
217 | {
218 | "cell_type": "code",
219 | "execution_count": null,
220 | "metadata": {
221 | "colab": {
222 | "base_uri": "https://localhost:8080/"
223 | },
224 | "id": "OHWjBFSQMWZk",
225 | "outputId": "f589564e-5cc3-4031-8481-55b3843d9380"
226 | },
227 | "outputs": [],
228 | "source": [
229 | "import pprint\n",
230 | "print(\"CSV data\")\n",
231 | "pprint.pprint(website_list)"
232 | ]
233 | },
234 | {
235 | "cell_type": "markdown",
236 | "metadata": {
237 | "id": "S1SO6txzTBT_"
238 | },
239 | "source": [
240 | "## Initialize the Firecrawl"
241 | ]
242 | },
243 | {
244 | "cell_type": "code",
245 | "execution_count": null,
246 | "metadata": {
247 | "id": "o8GphTUy7IS_"
248 | },
249 | "outputs": [],
250 | "source": [
251 | "import os\n",
252 | "from firecrawl import FirecrawlApp\n",
253 | "app = FirecrawlApp(api_key=FIRECRAWL_API_KEY)"
254 | ]
255 | },
256 | {
257 | "cell_type": "code",
258 | "execution_count": null,
259 | "metadata": {
260 | "colab": {
261 | "base_uri": "https://localhost:8080/"
262 | },
263 | "id": "8-G8fB6KyGP7",
264 | "outputId": "a06cfbb4-a5c0-4a49-8c8e-254943b34c52"
265 | },
266 | "outputs": [],
267 | "source": [
268 | "import time\n",
269 | "\n",
270 | "# Crawl websites and handle responses\n",
271 | "url_response = {}\n",
272 | "crawl_per_min = 1 # Max crawl per minute\n",
273 | "\n",
274 | "# Track crawls\n",
275 | "crawled_websites = 0\n",
276 | "scraped_pages = 0\n",
277 | "\n",
278 | "for i, website_dict in enumerate(website_list):\n",
279 | " url = website_dict.get('URL')\n",
280 | " print(f\"Crawling: {url}\")\n",
281 | "\n",
282 | " try:\n",
283 | " response = app.crawl_url(\n",
284 | " url,\n",
285 | " params={\n",
286 | " 'limit': 3, # Limit pages to scrape per site.\n",
287 | " 'scrapeOptions': {'formats': ['markdown', 'html']}\n",
288 | " }\n",
289 | " )\n",
290 | " crawled_websites += 1\n",
291 | "\n",
292 | " except Exception as exc:\n",
293 | " print(f\"Failed to fetch {url} -> {exc}\")\n",
294 | " continue\n",
295 | "\n",
296 | " # Store the scraped data and associated info in the response dict\n",
297 | " url_response[url] = {\n",
298 | " \"scraped_data\": response.get(\"data\"),\n",
299 | " \"csv_data\": website_dict\n",
300 | " }\n",
301 | "\n",
302 | " # Pause to comply with crawl per minute limit for free version its 1 crawl per minute\n",
303 | " if i!=len(website_list) and (i + 1) % crawl_per_min == 0:\n",
304 | " print(\"Pausing for 1 minute to comply with crawl limit...\")\n",
305 | " time.sleep(60) # Pause for 1 minute after every crawl\n"
306 | ]
307 | },
308 | {
309 | "cell_type": "markdown",
310 | "metadata": {
311 | "id": "gwu6VIMvTKp1"
312 | },
313 | "source": [
314 | "## Create llamaindex documents from the scraped content"
315 | ]
316 | },
317 | {
318 | "cell_type": "code",
319 | "execution_count": null,
320 | "metadata": {
321 | "id": "fHSEWg7FBdSS"
322 | },
323 | "outputs": [],
324 | "source": [
325 | "from llama_index.core import Document\n",
326 | "documents = []\n",
327 | "\n",
328 | "for _, scraped_content in url_response.items():\n",
329 | " csv_data = scraped_content.get(\"csv_data\")\n",
330 | " scraped_results = scraped_content.get(\"scraped_data\")\n",
331 | "\n",
332 | " for scraped_site_dict in scraped_results:\n",
333 | " for result in scraped_results:\n",
334 | " markdown_content = result.get(\"markdown\")\n",
335 | " title = result.get(\"metadata\").get(\"title\")\n",
336 | " url = result.get(\"metadata\").get(\"sourceURL\")\n",
337 | " documents.append(\n",
338 | " Document(\n",
339 | " text=markdown_content,\n",
340 | " metadata={\n",
341 | " \"title\": title,\n",
342 | " \"url\": url,\n",
343 | " \"description\": csv_data.get(\"Description\"),\n",
344 | " \"category\": csv_data.get(\"Category\")\n",
345 | " }\n",
346 | " )\n",
347 | " )\n"
348 | ]
349 | },
350 | {
351 | "cell_type": "markdown",
352 | "metadata": {
353 | "id": "uwtRhcpQT294"
354 | },
355 | "source": [
356 | "# Create The RAG Pipeline."
357 | ]
358 | },
359 | {
360 | "cell_type": "code",
361 | "execution_count": null,
362 | "metadata": {
363 | "id": "iDOgwesPI-Kq"
364 | },
365 | "outputs": [],
366 | "source": [
367 | "from llama_index.llms.openai import OpenAI\n",
368 | "from llama_index.embeddings.openai import OpenAIEmbedding\n",
369 | "from llama_index.core.node_parser import SentenceSplitter\n",
370 | "\n",
371 | "llm = OpenAI(model=\"gpt-4o-mini\")\n",
372 | "embed_model = OpenAIEmbedding(model=\"text-embedding-3-large\")\n",
373 | "text_splitter = SentenceSplitter(chunk_size=512, chunk_overlap=30)"
374 | ]
375 | },
376 | {
377 | "cell_type": "code",
378 | "execution_count": null,
379 | "metadata": {
380 | "id": "R8Oi4MiJJQii"
381 | },
382 | "outputs": [],
383 | "source": [
384 | "from llama_index.core import Settings\n",
385 | "\n",
386 | "Settings.llm = llm\n",
387 | "Settings.embed_model = embed_model\n",
388 | "Settings.text_splitter = text_splitter"
389 | ]
390 | },
391 | {
392 | "cell_type": "code",
393 | "execution_count": null,
394 | "metadata": {
395 | "id": "61ZLk2GoJ4VH"
396 | },
397 | "outputs": [],
398 | "source": [
399 | "from llama_index.core import VectorStoreIndex\n",
400 | "\n",
401 | "index = VectorStoreIndex.from_documents(documents)\n",
402 | "query_engine = index.as_query_engine()"
403 | ]
404 | },
405 | {
406 | "cell_type": "code",
407 | "execution_count": null,
408 | "metadata": {
409 | "id": "R1ImxmsoUNVo"
410 | },
411 | "outputs": [],
412 | "source": [
413 | "from IPython.display import Markdown, display\n",
414 | "def display_response(response):\n",
415 | " display(Markdown(f\"{response}\"))"
416 | ]
417 | },
418 | {
419 | "cell_type": "code",
420 | "execution_count": null,
421 | "metadata": {
422 | "colab": {
423 | "base_uri": "https://localhost:8080/",
424 | "height": 616
425 | },
426 | "id": "pcFcFgCvJ7Y8",
427 | "outputId": "84449163-47ed-4d15-a080-2a18600e5bc4"
428 | },
429 | "outputs": [],
430 | "source": [
431 | "query = \"Compare the best No sql dbs?\" # Enter your query here, it should be relevant to the crawled websites\n",
432 | "res = query_engine.query(\"I want to use key value store which is the best db?\")\n",
433 | "display_response(res)\n",
434 | "\n",
435 | "print(\"-----------------\")\n",
436 | "# Show the retrieved nodes\n",
437 | "for src in res.source_nodes:\n",
438 | " print(\"Node ID\\t\", src.node_id)\n",
439 | " print(\"Title\\t\", src.metadata['title'])\n",
440 | " print(\"URL\\t\", src.metadata['url'])\n",
441 | " print(\"Score\\t\", src.score)\n",
442 | " print(\"Description\\t\", src.metadata.get(\"description\"))\n",
443 | " print(\"Category\\t\", src.metadata.get(\"category\"))\n",
444 | " print(\"-_\"*20)"
445 | ]
446 | }
447 | ],
448 | "metadata": {
449 | "colab": {
450 | "authorship_tag": "ABX9TyP4qCoBLDDxEkxyJ9OmPq0w",
451 | "include_colab_link": true,
452 | "provenance": []
453 | },
454 | "kernelspec": {
455 | "display_name": "Python 3",
456 | "name": "python3"
457 | },
458 | "language_info": {
459 | "name": "python"
460 | }
461 | },
462 | "nbformat": 4,
463 | "nbformat_minor": 0
464 | }
465 |
--------------------------------------------------------------------------------
/notebooks/Intro_to_Large_Language_Models_(LLMs).ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "[](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/Intro_to_Large_Language_Models_(LLMs).ipynb)"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": null,
13 | "metadata": {
14 | "id": "8RRmESqBDsvK"
15 | },
16 | "outputs": [],
17 | "source": [
18 | "import requests\n",
19 | "\n",
20 | "# Your OpenAI API key\n",
21 | "api_key = \"\"\n",
22 | "\n",
23 | "# from google.colab import userdata\n",
24 | "# api_key = userdata.get('openai_api_key')\n",
25 | "\n",
26 | "# The URL for the OpenAI API (GPT-4 endpoint)\n",
27 | "url = \"https://api.openai.com/v1/chat/completions\""
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": null,
33 | "metadata": {
34 | "id": "x2s6BfWiD2sp"
35 | },
36 | "outputs": [],
37 | "source": [
38 | "# Setting up the headers with your API key for authentication\n",
39 | "headers = {\n",
40 | " \"Authorization\": f\"Bearer {api_key}\",\n",
41 | " \"Content-Type\": \"application/json\",\n",
42 | "}\n",
43 | "\n",
44 | "# The data payload with your prompt and other parameters\n",
45 | "data = {\n",
46 | " \"model\": \"gpt-4o-mini\",\n",
47 | " \"messages\": [\n",
48 | " {\n",
49 | " \"role\": \"system\",\n",
50 | " \"content\": \"You are a helpful assistant that translate from English to French\"\n",
51 | " },\n",
52 | " {\n",
53 | " \"role\": \"user\",\n",
54 | " \"content\": \"Hello, how are you?\"\n",
55 | " }\n",
56 | " ],\n",
57 | " \"temperature\": 0.7,\n",
58 | " \"max_tokens\": 60,\n",
59 | "}"
60 | ]
61 | },
62 | {
63 | "cell_type": "code",
64 | "execution_count": null,
65 | "metadata": {
66 | "colab": {
67 | "base_uri": "https://localhost:8080/"
68 | },
69 | "id": "p8QSMU2OD7e4",
70 | "outputId": "d5ef43e3-f72e-4e8d-bb40-ba5df995d236"
71 | },
72 | "outputs": [
73 | {
74 | "name": "stdout",
75 | "output_type": "stream",
76 | "text": [
77 | "{'id': 'chatcmpl-ALNqUtUWNDuD9ZVZ99F5jLHwR7ZOn', 'object': 'chat.completion', 'created': 1729659914, 'model': 'gpt-4o-mini-2024-07-18', 'choices': [{'index': 0, 'message': {'role': 'assistant', 'content': 'Bonjour, comment ça va ?', 'refusal': None}, 'logprobs': None, 'finish_reason': 'stop'}], 'usage': {'prompt_tokens': 28, 'completion_tokens': 6, 'total_tokens': 34, 'prompt_tokens_details': {'cached_tokens': 0}, 'completion_tokens_details': {'reasoning_tokens': 0}}, 'system_fingerprint': 'fp_482c22a7bc'}\n"
78 | ]
79 | }
80 | ],
81 | "source": [
82 | "import requests\n",
83 | "\n",
84 | "# Sending the POST request to the API\n",
85 | "response = requests.post(url, json=data, headers=headers)\n",
86 | "\n",
87 | "# Checking if the request was successful\n",
88 | "if response.status_code == 200:\n",
89 | " # Print the text part of the response\n",
90 | " print(response.json())\n",
91 | "else:\n",
92 | " print(f\"Failed to fetch data: {response.status_code}, {response.text}\")"
93 | ]
94 | },
95 | {
96 | "cell_type": "code",
97 | "execution_count": null,
98 | "metadata": {
99 | "colab": {
100 | "base_uri": "https://localhost:8080/"
101 | },
102 | "id": "iJNLzDbaD8Bj",
103 | "outputId": "3029c552-c5c1-4090-b697-8b202c9f75b6"
104 | },
105 | "outputs": [
106 | {
107 | "name": "stdout",
108 | "output_type": "stream",
109 | "text": [
110 | "> Response: Bonjour, comment ça va ?\n"
111 | ]
112 | }
113 | ],
114 | "source": [
115 | "print(\"> Response: \", response.json()['choices'][0]['message']['content'])"
116 | ]
117 | },
118 | {
119 | "cell_type": "code",
120 | "execution_count": null,
121 | "metadata": {
122 | "id": "PeXQcWMyw710"
123 | },
124 | "outputs": [],
125 | "source": []
126 | }
127 | ],
128 | "metadata": {
129 | "colab": {
130 | "provenance": []
131 | },
132 | "kernelspec": {
133 | "display_name": "Python 3",
134 | "name": "python3"
135 | },
136 | "language_info": {
137 | "name": "python"
138 | }
139 | },
140 | "nbformat": 4,
141 | "nbformat_minor": 0
142 | }
143 |
--------------------------------------------------------------------------------
/notebooks/Limitations_and_weaknesses_of_LLMs.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "[](https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/Limitations_and_weaknesses_of_LLMs.ipynb)"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": 2,
13 | "metadata": {
14 | "id": "8RRmESqBDsvK"
15 | },
16 | "outputs": [],
17 | "source": [
18 | "import requests\n",
19 | "\n",
20 | "# Your OpenAI API key\n",
21 | "api_key = \"
"
30 | ]
31 | },
32 | {
33 | "cell_type": "code",
34 | "source": [
35 | "!pip install -q llama-index==0.10.37 openai==1.30.1 tiktoken==0.7.0 chromadb==0.5.0 llama-index-vector-stores-chroma==0.1.7 llama-index-readers-wikipedia==0.1.4 wikipedia==1.4.0"
36 | ],
37 | "metadata": {
38 | "colab": {
39 | "base_uri": "https://localhost:8080/"
40 | },
41 | "id": "y_GAV7-zos0Y",
42 | "outputId": "aafcb14a-2caf-4424-c772-1d2bf6246cb2"
43 | },
44 | "execution_count": 1,
45 | "outputs": [
46 | {
47 | "output_type": "stream",
48 | "name": "stdout",
49 | "text": [
50 | " Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
51 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m67.3/67.3 kB\u001b[0m \u001b[31m1.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
52 | "\u001b[?25h Installing build dependencies ... \u001b[?25l\u001b[?25hdone\n",
53 | " Getting requirements to build wheel ... \u001b[?25l\u001b[?25hdone\n",
54 | " Preparing metadata (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n",
55 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m50.4/50.4 kB\u001b[0m \u001b[31m219.0 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
56 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m320.6/320.6 kB\u001b[0m \u001b[31m1.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
57 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.1/1.1 MB\u001b[0m \u001b[31m1.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
58 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m526.8/526.8 kB\u001b[0m \u001b[31m1.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
59 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.4/2.4 MB\u001b[0m \u001b[31m1.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
60 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m273.8/273.8 kB\u001b[0m \u001b[31m1.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
61 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m92.2/92.2 kB\u001b[0m \u001b[31m1.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
62 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m1.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
63 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m77.9/77.9 kB\u001b[0m \u001b[31m1.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
64 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.7/1.7 MB\u001b[0m \u001b[31m2.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
65 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m15.5/15.5 MB\u001b[0m \u001b[31m2.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
66 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.0/2.0 MB\u001b[0m \u001b[31m3.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
67 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m67.6/67.6 kB\u001b[0m \u001b[31m2.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
68 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m6.8/6.8 MB\u001b[0m \u001b[31m3.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
69 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m59.9/59.9 kB\u001b[0m \u001b[31m2.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
70 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m52.5/52.5 kB\u001b[0m \u001b[31m2.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
71 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m130.5/130.5 kB\u001b[0m \u001b[31m4.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
72 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m107.0/107.0 kB\u001b[0m \u001b[31m3.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
73 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m141.1/141.1 kB\u001b[0m \u001b[31m4.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
74 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m41.3/41.3 kB\u001b[0m \u001b[31m2.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
75 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m62.8/62.8 kB\u001b[0m \u001b[31m2.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
76 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m2.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
77 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m341.4/341.4 kB\u001b[0m \u001b[31m4.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
78 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m141.9/141.9 kB\u001b[0m \u001b[31m1.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
79 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m295.8/295.8 kB\u001b[0m \u001b[31m1.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
80 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m71.9/71.9 kB\u001b[0m \u001b[31m3.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
81 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.4/3.4 MB\u001b[0m \u001b[31m4.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
82 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.2/1.2 MB\u001b[0m \u001b[31m5.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
83 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m130.2/130.2 kB\u001b[0m \u001b[31m4.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
84 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m46.0/46.0 kB\u001b[0m \u001b[31m2.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
85 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m307.7/307.7 kB\u001b[0m \u001b[31m3.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
86 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m86.8/86.8 kB\u001b[0m \u001b[31m283.9 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
87 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m49.2/49.2 kB\u001b[0m \u001b[31m3.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
88 | "\u001b[?25h Building wheel for wikipedia (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
89 | " Building wheel for pypika (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n"
90 | ]
91 | }
92 | ]
93 | },
94 | {
95 | "cell_type": "code",
96 | "source": [
97 | "# Allows running asyncio in environments with an existing event loop, like Jupyter notebooks.\n",
98 | "\n",
99 | "import nest_asyncio\n",
100 | "\n",
101 | "nest_asyncio.apply()"
102 | ],
103 | "metadata": {
104 | "id": "Ua0KNwgvyCaj"
105 | },
106 | "execution_count": 2,
107 | "outputs": []
108 | },
109 | {
110 | "cell_type": "code",
111 | "source": [
112 | "import os\n",
113 | "\n",
114 | "os.environ['OPENAI_API_KEY'] = '[OPENAI_API_KEY]'"
115 | ],
116 | "metadata": {
117 | "id": "--Q2zk06wElp"
118 | },
119 | "execution_count": 3,
120 | "outputs": []
121 | },
122 | {
123 | "cell_type": "code",
124 | "source": [
125 | "import logging\n",
126 | "import sys\n",
127 | "\n",
128 | "#You can set the logging level to DEBUG for more verbose output,\n",
129 | "# or use level=logging.INFO for less detailed information.\n",
130 | "logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)\n",
131 | "logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))"
132 | ],
133 | "metadata": {
134 | "id": "tjwZjA8-wITr"
135 | },
136 | "execution_count": 4,
137 | "outputs": []
138 | },
139 | {
140 | "cell_type": "markdown",
141 | "source": [
142 | "# Wikipedia Example"
143 | ],
144 | "metadata": {
145 | "id": "HjI_gRaRutfj"
146 | }
147 | },
148 | {
149 | "cell_type": "markdown",
150 | "source": [
151 | "## LlamaHub Wikipedia Integration"
152 | ],
153 | "metadata": {
154 | "id": "PLUDcXpI41Q_"
155 | }
156 | },
157 | {
158 | "cell_type": "code",
159 | "source": [
160 | "from llama_index.readers.wikipedia import WikipediaReader\n",
161 | "\n",
162 | "# Initialize WikipediaReader\n",
163 | "reader = WikipediaReader()"
164 | ],
165 | "metadata": {
166 | "id": "2gko9Q3hrlMh"
167 | },
168 | "execution_count": 12,
169 | "outputs": []
170 | },
171 | {
172 | "cell_type": "code",
173 | "source": [
174 | "# Load data from Wikipedia\n",
175 | "documents = reader.load_data(pages=['Natural Language Processing', 'Artificial Intelligence'])"
176 | ],
177 | "metadata": {
178 | "id": "Z35ot7P1wIO0"
179 | },
180 | "execution_count": 13,
181 | "outputs": []
182 | },
183 | {
184 | "cell_type": "code",
185 | "source": [
186 | "len( documents )"
187 | ],
188 | "metadata": {
189 | "colab": {
190 | "base_uri": "https://localhost:8080/"
191 | },
192 | "id": "0i9Zp6BJwILk",
193 | "outputId": "a6a1e0a7-98cf-4ba4-d48a-e4f5833b4967"
194 | },
195 | "execution_count": 14,
196 | "outputs": [
197 | {
198 | "output_type": "execute_result",
199 | "data": {
200 | "text/plain": [
201 | "2"
202 | ]
203 | },
204 | "metadata": {},
205 | "execution_count": 14
206 | }
207 | ]
208 | },
209 | {
210 | "cell_type": "markdown",
211 | "source": [
212 | "## Save on DeepLake"
213 | ],
214 | "metadata": {
215 | "id": "03lff4VUTaN9"
216 | }
217 | },
218 | {
219 | "cell_type": "code",
220 | "source": [
221 | "import chromadb\n",
222 | "from llama_index.vector_stores.chroma import ChromaVectorStore\n",
223 | "\n",
224 | "# Load the vector store from the local storage.\n",
225 | "db = chromadb.PersistentClient(path=\"./wikipedia-articles\")\n",
226 | "chroma_collection = db.get_or_create_collection(\"wikipedia-articles\")\n",
227 | "vector_store = ChromaVectorStore(chroma_collection=chroma_collection)"
228 | ],
229 | "metadata": {
230 | "id": "eo8CTHSFTcaR"
231 | },
232 | "execution_count": 15,
233 | "outputs": []
234 | },
235 | {
236 | "cell_type": "markdown",
237 | "source": [
238 | "## Create Nodes"
239 | ],
240 | "metadata": {
241 | "id": "qkKPAnIl44ss"
242 | }
243 | },
244 | {
245 | "cell_type": "code",
246 | "source": [
247 | "from llama_index.core.node_parser import SimpleNodeParser\n",
248 | "\n",
249 | "# Initialize the parser\n",
250 | "parser = SimpleNodeParser.from_defaults(chunk_size=512, chunk_overlap=20)\n",
251 | "\n",
252 | "# Parse documents into nodes\n",
253 | "nodes = parser.get_nodes_from_documents(documents)\n",
254 | "print( len( nodes ) )"
255 | ],
256 | "metadata": {
257 | "colab": {
258 | "base_uri": "https://localhost:8080/"
259 | },
260 | "id": "eB6Rc0U0wII_",
261 | "outputId": "ec338be1-deca-45a7-e6ba-9997e4b7e25a"
262 | },
263 | "execution_count": 20,
264 | "outputs": [
265 | {
266 | "output_type": "stream",
267 | "name": "stdout",
268 | "text": [
269 | "45\n"
270 | ]
271 | }
272 | ]
273 | },
274 | {
275 | "cell_type": "markdown",
276 | "source": [
277 | "## Storage Context"
278 | ],
279 | "metadata": {
280 | "id": "E8tHMS5ZucFE"
281 | }
282 | },
283 | {
284 | "cell_type": "code",
285 | "source": [
286 | "from llama_index.core import StorageContext\n",
287 | "\n",
288 | "storage_context = StorageContext.from_defaults(vector_store=vector_store)"
289 | ],
290 | "metadata": {
291 | "id": "eWFtVpM_TcTQ"
292 | },
293 | "execution_count": 18,
294 | "outputs": []
295 | },
296 | {
297 | "cell_type": "markdown",
298 | "source": [
299 | "## Create index from Documents"
300 | ],
301 | "metadata": {
302 | "id": "kCgdd197CTDt"
303 | }
304 | },
305 | {
306 | "cell_type": "code",
307 | "source": [
308 | "from llama_index.core import VectorStoreIndex\n",
309 | "\n",
310 | "index = VectorStoreIndex(\n",
311 | " nodes=nodes, storage_context=storage_context\n",
312 | ")"
313 | ],
314 | "metadata": {
315 | "id": "g3GCf8LrULIW"
316 | },
317 | "execution_count": 24,
318 | "outputs": []
319 | },
320 | {
321 | "cell_type": "code",
322 | "source": [
323 | "query_engine = index.as_query_engine()\n",
324 | "response = query_engine.query(\"What does NLP stands for?\")\n",
325 | "response.response"
326 | ],
327 | "metadata": {
328 | "colab": {
329 | "base_uri": "https://localhost:8080/",
330 | "height": 35
331 | },
332 | "id": "G7BdNn-Q5AlG",
333 | "outputId": "a311ec41-6cdc-4fe1-fb59-ad338d0b6149"
334 | },
335 | "execution_count": 25,
336 | "outputs": [
337 | {
338 | "output_type": "execute_result",
339 | "data": {
340 | "text/plain": [
341 | "'NLP stands for Natural Language Processing.'"
342 | ],
343 | "application/vnd.google.colaboratory.intrinsic+json": {
344 | "type": "string"
345 | }
346 | },
347 | "metadata": {},
348 | "execution_count": 25
349 | }
350 | ]
351 | },
352 | {
353 | "cell_type": "markdown",
354 | "source": [
355 | "## Store/Load Vector Store"
356 | ],
357 | "metadata": {
358 | "id": "r6cGiUtxu5ga"
359 | }
360 | },
361 | {
362 | "cell_type": "code",
363 | "source": [
364 | "# Index Storage Checks\n",
365 | "import os.path\n",
366 | "from llama_index.core import StorageContext, load_index_from_storage\n",
367 | "\n",
368 | "# Let's see if our index already exists in storage.\n",
369 | "if not os.path.exists(\"./storage\"):\n",
370 | " index.storage_context.persist()\n",
371 | "\n",
372 | "else:\n",
373 | " # If the index already exists, we'll just load it:\n",
374 | " storage_context = StorageContext.from_defaults(persist_dir=\"./storage\")\n",
375 | " index = load_index_from_storage(storage_context)"
376 | ],
377 | "metadata": {
378 | "id": "GHtB0C0mu7f6"
379 | },
380 | "execution_count": 28,
381 | "outputs": []
382 | },
383 | {
384 | "cell_type": "markdown",
385 | "source": [
386 | "# Paul Graham Essay"
387 | ],
388 | "metadata": {
389 | "id": "iF8hwfMKuzst"
390 | }
391 | },
392 | {
393 | "cell_type": "code",
394 | "source": [
395 | "!mkdir -p './paul_graham/'\n",
396 | "!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -O './paul_graham/paul_graham_essay.txt'"
397 | ],
398 | "metadata": {
399 | "colab": {
400 | "base_uri": "https://localhost:8080/"
401 | },
402 | "id": "DrzbBAglwUo0",
403 | "outputId": "5bd17a72-3733-4b8c-e421-5f363f05895f"
404 | },
405 | "execution_count": 6,
406 | "outputs": [
407 | {
408 | "output_type": "stream",
409 | "name": "stdout",
410 | "text": [
411 | "--2024-07-24 18:48:21-- https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt\n",
412 | "Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.109.133, 185.199.108.133, 185.199.111.133, ...\n",
413 | "Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.109.133|:443... connected.\n",
414 | "HTTP request sent, awaiting response... 200 OK\n",
415 | "Length: 75042 (73K) [text/plain]\n",
416 | "Saving to: ‘./paul_graham/paul_graham_essay.txt’\n",
417 | "\n",
418 | "\r ./paul_gr 0%[ ] 0 --.-KB/s \r./paul_graham/paul_ 100%[===================>] 73.28K --.-KB/s in 0.02s \n",
419 | "\n",
420 | "2024-07-24 18:48:21 (2.95 MB/s) - ‘./paul_graham/paul_graham_essay.txt’ saved [75042/75042]\n",
421 | "\n"
422 | ]
423 | }
424 | ]
425 | },
426 | {
427 | "cell_type": "code",
428 | "source": [
429 | "from llama_index.core import SimpleDirectoryReader\n",
430 | "\n",
431 | "# load documents\n",
432 | "documents = SimpleDirectoryReader(\"./paul_graham\").load_data()"
433 | ],
434 | "metadata": {
435 | "id": "S8-QmnkCwIiU"
436 | },
437 | "execution_count": 7,
438 | "outputs": []
439 | },
440 | {
441 | "cell_type": "code",
442 | "source": [
443 | "import chromadb\n",
444 | "from llama_index.vector_stores.chroma import ChromaVectorStore\n",
445 | "\n",
446 | "# Load the vector store from the local storage.\n",
447 | "db = chromadb.PersistentClient(path=\"./paul-graham\")\n",
448 | "chroma_collection = db.get_or_create_collection(\"paul-graham\")\n",
449 | "vector_store = ChromaVectorStore(chroma_collection=chroma_collection)"
450 | ],
451 | "metadata": {
452 | "id": "DfWglp75xc5f"
453 | },
454 | "execution_count": 8,
455 | "outputs": []
456 | },
457 | {
458 | "cell_type": "code",
459 | "source": [
460 | "from llama_index.core import StorageContext\n",
461 | "\n",
462 | "storage_context = StorageContext.from_defaults(vector_store=vector_store)"
463 | ],
464 | "metadata": {
465 | "id": "-EVBlUC-xcj1"
466 | },
467 | "execution_count": 11,
468 | "outputs": []
469 | },
470 | {
471 | "cell_type": "code",
472 | "source": [
473 | "from llama_index.core import VectorStoreIndex\n",
474 | "\n",
475 | "index = VectorStoreIndex.from_documents(\n",
476 | " documents, storage_context=storage_context\n",
477 | ")"
478 | ],
479 | "metadata": {
480 | "id": "8lMa4h9Cwn8b"
481 | },
482 | "execution_count": 12,
483 | "outputs": []
484 | },
485 | {
486 | "cell_type": "code",
487 | "source": [
488 | "query_engine = index.as_query_engine(similarity_top_k=10)"
489 | ],
490 | "metadata": {
491 | "id": "tJsfskjHxj0e"
492 | },
493 | "execution_count": 13,
494 | "outputs": []
495 | },
496 | {
497 | "cell_type": "code",
498 | "source": [
499 | "from llama_index.core.tools import QueryEngineTool, ToolMetadata\n",
500 | "from llama_index.core.query_engine import SubQuestionQueryEngine\n",
501 | "\n",
502 | "query_engine_tools = [\n",
503 | " QueryEngineTool(\n",
504 | " query_engine=query_engine,\n",
505 | " metadata=ToolMetadata(\n",
506 | " name=\"pg_essay\",\n",
507 | " description=\"Paul Graham essay on What I Worked On\",\n",
508 | " ),\n",
509 | " ),\n",
510 | "]\n",
511 | "\n",
512 | "query_engine = SubQuestionQueryEngine.from_defaults(\n",
513 | " query_engine_tools=query_engine_tools,\n",
514 | " use_async=True,\n",
515 | ")"
516 | ],
517 | "metadata": {
518 | "id": "yL9TsFwxxuoA"
519 | },
520 | "execution_count": 14,
521 | "outputs": []
522 | },
523 | {
524 | "cell_type": "code",
525 | "source": [
526 | "response = query_engine.query(\n",
527 | " \"How was Paul Grahams life different before, during, and after YC?\"\n",
528 | ")"
529 | ],
530 | "metadata": {
531 | "colab": {
532 | "base_uri": "https://localhost:8080/"
533 | },
534 | "id": "JWc_n5Lhx2bq",
535 | "outputId": "972fa40f-065b-420a-d5e8-af36c1a2ad2d"
536 | },
537 | "execution_count": 15,
538 | "outputs": [
539 | {
540 | "output_type": "stream",
541 | "name": "stdout",
542 | "text": [
543 | "Generated 3 sub questions.\n",
544 | "\u001b[1;3;38;2;237;90;200m[pg_essay] Q: What did Paul Graham work on before Y Combinator?\n",
545 | "\u001b[0m\u001b[1;3;38;2;90;149;237m[pg_essay] Q: What did Paul Graham work on during Y Combinator?\n",
546 | "\u001b[0m\u001b[1;3;38;2;11;159;203m[pg_essay] Q: What did Paul Graham work on after Y Combinator?\n",
547 | "\u001b[0m\u001b[1;3;38;2;237;90;200m[pg_essay] A: Paul Graham worked on building online stores through a web app called Viaweb before starting Y Combinator.\n",
548 | "\u001b[0m\u001b[1;3;38;2;11;159;203m[pg_essay] A: After Y Combinator, Paul Graham started painting.\n",
549 | "\u001b[0m\u001b[1;3;38;2;90;149;237m[pg_essay] A: Paul Graham worked on various aspects during Y Combinator, including being an angel firm, funding startups in batches, providing seed investments, and offering support to startups similar to what he had received when starting his own company.\n",
550 | "\u001b[0m"
551 | ]
552 | }
553 | ]
554 | },
555 | {
556 | "cell_type": "code",
557 | "source": [
558 | "print( \">>> The final response:\\n\", response )"
559 | ],
560 | "metadata": {
561 | "colab": {
562 | "base_uri": "https://localhost:8080/"
563 | },
564 | "id": "T-ZC66Ltx5Za",
565 | "outputId": "f45ec6bb-ee45-4526-80bd-ca7c30cdbf8f"
566 | },
567 | "execution_count": 16,
568 | "outputs": [
569 | {
570 | "output_type": "stream",
571 | "name": "stdout",
572 | "text": [
573 | ">>> The final response:\n",
574 | " Paul Graham's life involved building online stores through Viaweb before Y Combinator, working on various aspects within Y Combinator such as funding startups and providing support, and then transitioning to painting after Y Combinator.\n"
575 | ]
576 | }
577 | ]
578 | },
579 | {
580 | "cell_type": "code",
581 | "source": [],
582 | "metadata": {
583 | "id": "27fS3JcDyFSj"
584 | },
585 | "execution_count": null,
586 | "outputs": []
587 | }
588 | ]
589 | }
--------------------------------------------------------------------------------
/notebooks/Observablity_And_Tracing.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyOqup5yfCdFa7JfFldUYE42",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "markdown",
31 | "source": [
32 | "## Install Dependencies"
33 | ],
34 | "metadata": {
35 | "id": "AItauLdsp7jj"
36 | }
37 | },
38 | {
39 | "cell_type": "code",
40 | "execution_count": null,
41 | "metadata": {
42 | "id": "pmnledOoOjbn",
43 | "colab": {
44 | "base_uri": "https://localhost:8080/"
45 | },
46 | "outputId": "a423b379-25cc-47f5-cb81-185cf649210a"
47 | },
48 | "outputs": [
49 | {
50 | "output_type": "stream",
51 | "name": "stdout",
52 | "text": [
53 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m50.6/50.6 kB\u001b[0m \u001b[31m1.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
54 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m49.9/49.9 kB\u001b[0m \u001b[31m2.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
55 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.0/1.0 MB\u001b[0m \u001b[31m23.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
56 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m407.7/407.7 kB\u001b[0m \u001b[31m17.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
57 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m296.9/296.9 kB\u001b[0m \u001b[31m14.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
58 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m386.9/386.9 kB\u001b[0m \u001b[31m18.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
59 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.2/1.2 MB\u001b[0m \u001b[31m40.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
60 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m76.4/76.4 kB\u001b[0m \u001b[31m4.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
61 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m78.0/78.0 kB\u001b[0m \u001b[31m4.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
62 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m325.2/325.2 kB\u001b[0m \u001b[31m19.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
63 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m144.5/144.5 kB\u001b[0m \u001b[31m9.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
64 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m54.5/54.5 kB\u001b[0m \u001b[31m3.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
65 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m3.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
66 | "\u001b[?25h"
67 | ]
68 | }
69 | ],
70 | "source": [
71 | "pip install langchain-openai==0.2.3 langchain==0.3.4 --quiet"
72 | ]
73 | },
74 | {
75 | "cell_type": "markdown",
76 | "source": [
77 | "## Setting up env"
78 | ],
79 | "metadata": {
80 | "id": "AgSc92W9qIeF"
81 | }
82 | },
83 | {
84 | "cell_type": "code",
85 | "source": [
86 | "import os\n",
87 | "os.environ[\"OPENAI_API_KEY\"] = \"\"\n",
89 | "os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n",
90 | "os.environ[\"LANGCHAIN_PROJECT\"] = \"\""
91 | ],
92 | "metadata": {
93 | "id": "lXz2mUoyiEuJ"
94 | },
95 | "execution_count": null,
96 | "outputs": []
97 | },
98 | {
99 | "cell_type": "markdown",
100 | "source": [
101 | "## Tracing Langchain calls"
102 | ],
103 | "metadata": {
104 | "id": "LAgKNo9kqOZc"
105 | }
106 | },
107 | {
108 | "cell_type": "code",
109 | "source": [
110 | "from langchain_openai import ChatOpenAI\n",
111 | "from langchain import hub\n",
112 | "\n",
113 | "prompt = hub.pull(\"wikianes/section_writer\")\n",
114 | "llm = ChatOpenAI(model=\"gpt-4o-mini\")\n",
115 | "\n",
116 | "chain = prompt | llm\n",
117 | "output = chain.invoke({\"section_description\": \"Quantum Physics\"})\n",
118 | "print(output.content)"
119 | ],
120 | "metadata": {
121 | "colab": {
122 | "base_uri": "https://localhost:8080/"
123 | },
124 | "id": "okLDL4YQhljS",
125 | "outputId": "872b6cf2-da3f-4c7e-ec1a-7f254a41c7d0"
126 | },
127 | "execution_count": null,
128 | "outputs": [
129 | {
130 | "output_type": "stream",
131 | "name": "stdout",
132 | "text": [
133 | "# Quantum Physics in Anesthesia\n",
134 | "\n",
135 | "## Introduction to Quantum Physics in Medicine\n",
136 | "Quantum physics, the study of matter and energy at the atomic and subatomic levels, has increasingly influenced various fields of medicine, including anesthesiology. Understanding the principles of quantum mechanics can enhance anesthesiologists' comprehension of drug interactions and the mechanisms of action of anesthetic agents, ultimately improving patient outcomes during surgical procedures.\n",
137 | "\n",
138 | "## Quantum Effects on Drug Mechanisms\n",
139 | "Anesthetic agents operate through complex interactions at the molecular level, influenced by quantum phenomena. For example, the binding affinity of anesthetic drugs to their target receptors can be affected by quantum tunneling and superposition. These principles can help explain variations in anesthesia depth and duration, as well as individual patient responses to anesthetic agents. Anesthesiologists should consider these quantum effects when selecting agents, especially in patients with unique metabolic profiles.\n",
140 | "\n",
141 | "## Implications for Anesthetic Practice\n",
142 | "### Preoperative Assessment\n",
143 | "An understanding of quantum effects can inform preoperative assessments, particularly in predicting how patients might metabolize anesthetic drugs. Factors such as age, genetic polymorphisms, and underlying health conditions can alter quantum interactions, necessitating careful evaluation of patient history and potential drug interactions. \n",
144 | "\n",
145 | "### Personalized Anesthesia\n",
146 | "Quantum principles pave the way for personalized anesthesia approaches. By anticipating how individual patient characteristics affect the pharmacodynamics and pharmacokinetics of anesthetics, anesthesiologists can tailor drug dosages and combinations. This customization can minimize adverse effects and enhance the efficacy of anesthesia, particularly in high-risk patients or complex surgical procedures.\n",
147 | "\n",
148 | "### Monitoring and Technology\n",
149 | "Advancements in quantum technology, such as quantum sensors, have the potential to revolutionize patient monitoring during anesthesia. These sensors can provide real-time data on physiological parameters with unprecedented accuracy, allowing for more precise titration of anesthetic agents and improved safety profiles. Anesthesiologists should stay abreast of these technological developments to integrate them into practice effectively.\n",
150 | "\n",
151 | "## Conclusion\n",
152 | "While quantum physics may seem abstract, its principles are increasingly relevant to anesthetic practice. By understanding quantum interactions at the molecular level, anesthesiologists can enhance their approach to drug selection, patient assessment, and monitoring during surgical procedures, leading to improved outcomes and safety for patients. As research in this area continues to evolve, the integration of quantum physics into anesthesiology will likely become more pronounced, necessitating ongoing education and adaptation within the field.\n"
153 | ]
154 | }
155 | ]
156 | },
157 | {
158 | "cell_type": "markdown",
159 | "source": [
160 | "## Tracing OpenAI calls"
161 | ],
162 | "metadata": {
163 | "id": "0-s3VBrSzkXZ"
164 | }
165 | },
166 | {
167 | "cell_type": "code",
168 | "source": [
169 | "from openai import OpenAI\n",
170 | "from langsmith.wrappers import wrap_openai\n",
171 | "\n",
172 | "# wrap_openai from langsmith provides wrapper to\n",
173 | "ai_client = wrap_openai(OpenAI())\n",
174 | "\n",
175 | "\n",
176 | "def retrieve_documents(inquiry: str):\n",
177 | " docs = [\n",
178 | " \"Vector databases enable efficient semantic search for LLMs.\",\n",
179 | " \"Retrieval-Augmented Generation (RAG) improves LLM accuracy with relevant context.\",\n",
180 | " \"Large Language Models (LLMs) have set new benchmarks in NLP tasks.\",\n",
181 | " \"Transformer architectures have revolutionized many areas of machine learning.\",\n",
182 | " \"Embedding models convert text into vector representations for similarity comparisons.\"\n",
183 | " ]\n",
184 | " return docs\n",
185 | "\n",
186 | "def ask_qa(query):\n",
187 | " context = retrieve_documents(query)\n",
188 | " system_prompt = f\"\"\"\n",
189 | " You are an assistant for question-answering tasks.\n",
190 | " Use the following pieces of retrieved context to answer the question.\n",
191 | " Context: {context}\n",
192 | " \"\"\"\n",
193 | "\n",
194 | " return ai_client.chat.completions.create(\n",
195 | " messages=[\n",
196 | " {\"role\": \"system\", \"content\": system_prompt},\n",
197 | " {\"role\": \"user\", \"content\": query},\n",
198 | " ],\n",
199 | " model=\"gpt-4o-mini\",\n",
200 | " )"
201 | ],
202 | "metadata": {
203 | "id": "1sM6lLW2sFT-"
204 | },
205 | "execution_count": null,
206 | "outputs": []
207 | },
208 | {
209 | "cell_type": "code",
210 | "source": [
211 | "ask_qa(\"what is vector db?\")"
212 | ],
213 | "metadata": {
214 | "colab": {
215 | "base_uri": "https://localhost:8080/"
216 | },
217 | "id": "m_sGUKNusXcA",
218 | "outputId": "89ab6371-d654-4ad7-a9a8-92fc82321356"
219 | },
220 | "execution_count": null,
221 | "outputs": [
222 | {
223 | "output_type": "execute_result",
224 | "data": {
225 | "text/plain": [
226 | "ChatCompletion(id='chatcmpl-ALZOOE9lCSqi9VO0o847Qwo3Xc2FI', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='A vector database is a type of database designed to store and manage vector representations of data, particularly for efficient semantic search and similarity comparisons. These vectors are often generated by embedding models, which convert text or other data types into numerical vectors. Vector databases enable quick retrieval of information by allowing for searches based on semantic similarity rather than relying solely on keyword matching. This functionality is particularly useful for applications involving Large Language Models (LLMs), as it facilitates Retrieval-Augmented Generation (RAG) by providing relevant context that enhances model accuracy.', refusal=None, role='assistant', audio=None, function_call=None, tool_calls=None))], created=1729704300, model='gpt-4o-mini-2024-07-18', object='chat.completion', service_tier=None, system_fingerprint='fp_482c22a7bc', usage=CompletionUsage(completion_tokens=105, prompt_tokens=116, total_tokens=221, completion_tokens_details=CompletionTokensDetails(audio_tokens=None, reasoning_tokens=0), prompt_tokens_details=PromptTokensDetails(audio_tokens=None, cached_tokens=0)))"
227 | ]
228 | },
229 | "metadata": {},
230 | "execution_count": 5
231 | }
232 | ]
233 | },
234 | {
235 | "cell_type": "markdown",
236 | "source": [
237 | "## Using Langsmith traceable decorator"
238 | ],
239 | "metadata": {
240 | "id": "-2Inyx_2FPuH"
241 | }
242 | },
243 | {
244 | "cell_type": "code",
245 | "source": [
246 | "from openai import OpenAI\n",
247 | "from langsmith import traceable\n",
248 | "\n",
249 | "openai_client = OpenAI()\n",
250 | "\n",
251 | "@traceable(run_type=\"llm\", name=\"OpenAI call\")\n",
252 | "def call_llm(model, messages):\n",
253 | " result = openai_client.chat.completions.create(\n",
254 | " messages=messages,\n",
255 | " model=model\n",
256 | " )\n",
257 | " return result.choices[0].message.content\n",
258 | "\n",
259 | "\n",
260 | "@traceable(run_type=\"retriever\")\n",
261 | "def retrieve_documents(inquiry: str):\n",
262 | " docs = [\n",
263 | " \"Vector databases enable efficient semantic search for LLMs.\",\n",
264 | " \"Retrieval-Augmented Generation (RAG) improves LLM accuracy with relevant context.\",\n",
265 | " \"Large Language Models (LLMs) have set new benchmarks in NLP tasks.\",\n",
266 | " \"Transformer architectures have revolutionized many areas of machine learning.\",\n",
267 | " \"Embedding models convert text into vector representations for similarity comparisons.\"\n",
268 | " ]\n",
269 | " return docs\n",
270 | "\n",
271 | "@traceable(run_type=\"chain\")\n",
272 | "def ask_qa(query):\n",
273 | " context = retrieve_documents(query)\n",
274 | " system_prompt = f\"\"\"\n",
275 | " You are an assistant for question-answering tasks.\n",
276 | " Use the following pieces of retrieved context to answer the question.\n",
277 | " Context: {context}\n",
278 | " \"\"\"\n",
279 | "\n",
280 | " messages = [\n",
281 | " {\"role\": \"system\", \"content\": system_prompt},\n",
282 | " {\"role\": \"user\", \"content\": query},\n",
283 | " ]\n",
284 | " return call_llm(\"gpt-4o-mini\", messages)"
285 | ],
286 | "metadata": {
287 | "id": "wzkrlI7Hsa54"
288 | },
289 | "execution_count": null,
290 | "outputs": []
291 | },
292 | {
293 | "cell_type": "code",
294 | "source": [
295 | "ask_qa(\"What are large language models\", langsmith_extra={\"metadata\": {\"user\": \"test_user@gmail.com\"}})"
296 | ],
297 | "metadata": {
298 | "colab": {
299 | "base_uri": "https://localhost:8080/",
300 | "height": 90
301 | },
302 | "id": "IvUZtIjFFmyB",
303 | "outputId": "cf498afa-f52d-4c60-e8b9-d59cb5928adf"
304 | },
305 | "execution_count": null,
306 | "outputs": [
307 | {
308 | "output_type": "execute_result",
309 | "data": {
310 | "text/plain": [
311 | "'Large Language Models (LLMs) are advanced AI systems designed to understand and generate human-like text. They have set new benchmarks in various natural language processing (NLP) tasks by utilizing massive datasets and sophisticated architectures, particularly transformer architectures. LLMs leverage techniques such as Retrieval-Augmented Generation (RAG) to enhance their accuracy and effectiveness by incorporating relevant context into their responses. Additionally, embedding models are often used in conjunction with LLMs to convert text into vector representations, facilitating efficient semantic search and similarity comparisons.'"
312 | ],
313 | "application/vnd.google.colaboratory.intrinsic+json": {
314 | "type": "string"
315 | }
316 | },
317 | "metadata": {},
318 | "execution_count": 7
319 | }
320 | ]
321 | },
322 | {
323 | "cell_type": "code",
324 | "source": [],
325 | "metadata": {
326 | "id": "sjEzBXy8aGdy"
327 | },
328 | "execution_count": null,
329 | "outputs": []
330 | }
331 | ]
332 | }
--------------------------------------------------------------------------------
/notebooks/RAG_101.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyNHN2Xow++PNnfnmkcw4iqx",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "markdown",
31 | "source": [
32 | "## RAG 101"
33 | ],
34 | "metadata": {
35 | "id": "_PtvhdlwUmmb"
36 | }
37 | },
38 | {
39 | "cell_type": "code",
40 | "execution_count": null,
41 | "metadata": {
42 | "colab": {
43 | "base_uri": "https://localhost:8080/"
44 | },
45 | "id": "_L_iOECPT-Ki",
46 | "outputId": "d432f4d1-49b1-4500-e686-cab5d82a1e15"
47 | },
48 | "outputs": [
49 | {
50 | "output_type": "stream",
51 | "name": "stdout",
52 | "text": [
53 | "\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/225.4 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m225.4/225.4 kB\u001b[0m \u001b[31m9.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
54 | "\u001b[?25h\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/76.4 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m76.4/76.4 kB\u001b[0m \u001b[31m4.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
55 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m77.9/77.9 kB\u001b[0m \u001b[31m4.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
56 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m3.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
57 | "\u001b[?25h"
58 | ]
59 | }
60 | ],
61 | "source": [
62 | "!pip install -q openai==1.6.0"
63 | ]
64 | },
65 | {
66 | "cell_type": "code",
67 | "source": [
68 | "import os\n",
69 | "from google.colab import userdata\n",
70 | "\n",
71 | "# Set the \"OPENAI_API_KEY\" in the Python environment. Will be used by OpenAI client later.\n",
72 | "os.environ[\"OPENAI_API_KEY\"] = \"[OPENAI_API_KEY]\""
73 | ],
74 | "metadata": {
75 | "id": "nP6_Z0DJUerK"
76 | },
77 | "execution_count": null,
78 | "outputs": []
79 | },
80 | {
81 | "cell_type": "code",
82 | "source": [
83 | "from openai import OpenAI\n",
84 | "\n",
85 | "# Defining the \"client\" object that enables\n",
86 | "# us to connect to OpenAI API endpoints.\n",
87 | "client = OpenAI()"
88 | ],
89 | "metadata": {
90 | "id": "IPmDcnKlUiY5"
91 | },
92 | "execution_count": null,
93 | "outputs": []
94 | },
95 | {
96 | "cell_type": "code",
97 | "source": [
98 | "# Create a user prompt with the user's question\n",
99 | "prompt = f\"How many parameters LLaMA 3 Models have?\"\n",
100 | "\n",
101 | "#Call the OpenAI API\n",
102 | "response = client.chat.completions.create(\n",
103 | " model=\"gpt-4o-mini\",\n",
104 | " temperature=0.0,\n",
105 | " messages=[\n",
106 | " {\"role\": \"system\", \"content\": \"You are a helpful assistant\"},\n",
107 | " {\"role\": \"user\", \"content\": prompt}\n",
108 | " ]\n",
109 | ")\n",
110 | "print(response.choices[0].message.content)"
111 | ],
112 | "metadata": {
113 | "colab": {
114 | "base_uri": "https://localhost:8080/"
115 | },
116 | "id": "yplCUS2pUiU1",
117 | "outputId": "657e2a95-a380-4255-c33e-3964554a115a"
118 | },
119 | "execution_count": null,
120 | "outputs": [
121 | {
122 | "output_type": "stream",
123 | "name": "stdout",
124 | "text": [
125 | "As of my last update in October 2023, the LLaMA 3 models have not been officially released, and specific details about their architecture, including the number of parameters, have not been disclosed. The LLaMA (Large Language Model Meta AI) series from Meta (formerly Facebook) includes LLaMA 1 and LLaMA 2, which have various model sizes ranging from 7 billion to 70 billion parameters.\n",
126 | "\n",
127 | "For the most accurate and up-to-date information regarding LLaMA 3 or any other models, I recommend checking the official announcements from Meta or relevant research publications.\n"
128 | ]
129 | }
130 | ]
131 | },
132 | {
133 | "cell_type": "code",
134 | "source": [
135 | "response.choices[0].message.content.strip()"
136 | ],
137 | "metadata": {
138 | "colab": {
139 | "base_uri": "https://localhost:8080/",
140 | "height": 87
141 | },
142 | "id": "sFM5-GEgUiRR",
143 | "outputId": "0f594cd2-720e-431b-a640-eed28eec0017"
144 | },
145 | "execution_count": null,
146 | "outputs": [
147 | {
148 | "output_type": "execute_result",
149 | "data": {
150 | "text/plain": [
151 | "'As of my last update in October 2023, the LLaMA 3 models have not been officially released, and specific details about their architecture, including the number of parameters, have not been disclosed. The LLaMA (Large Language Model Meta AI) series from Meta (formerly Facebook) includes LLaMA 1 and LLaMA 2, which have various model sizes ranging from 7 billion to 70 billion parameters.\\n\\nFor the most accurate and up-to-date information regarding LLaMA 3 or any other models, I recommend checking the official announcements from Meta or relevant research publications.'"
152 | ],
153 | "application/vnd.google.colaboratory.intrinsic+json": {
154 | "type": "string"
155 | }
156 | },
157 | "metadata": {},
158 | "execution_count": 27
159 | }
160 | ]
161 | },
162 | {
163 | "cell_type": "code",
164 | "source": [
165 | "# An article about the release of LLaMA 3 model.\n",
166 | "# https://towardsai.net/p/artificial-intelligence/some-technical-notes-about-llama-3#.\n",
167 | "ARTICLE = \"\"\"\n",
168 | "Since the debut of the original version, Llama has become one of the foundational blocks of the open source generative AI space. I prefer to use the term “open models,” given that these releases are not completely open source, but that’s just my preference. Last week, the trend in open models became even hotter with the release Llama 3. The release of Llama 3 builds on incredible momentum within the open model ecosystem and brings its own innovations. The 8B and 70B versions of Llama 3 are available, with a 400B version currently being trained. The Llama 3 architecture is based on a decoder-only model and includes a new, highly optimized 128k tokenizer. This is quite notable, given that, with few exceptions, most large language models simply reuse the same tokenizers. The new tokenizer leads to major performance gains. Another area of improvement in the architecture is the grouped query attention, which was already used in Llama 2 but has been enhanced for the larger models. Grouped query attention helps improve inference performance by caching key parameters. Additionally, the context window has also increased.\n",
169 | "Training is one area in which Llama 3 drastically improves over its predecessors. The model was trained on 15 trillion tokens, making the corpus quite large for an 8B parameter model, which speaks to the level of optimization Meta achieved in this release. It’s interesting to note that only 5% of the training corpus consisted of non-English tokens. The training infrastructure utilized 16,000 GPUs, achieving a throughput of 400 TFLOPs, which is nothing short of monumental.\n",
170 | "Architecture Meta AI’s Llama 3 features a standard, decoder-only transformer structure. Llama 3 introduces a tokenizer equipped with a 128K token vocabulary, which enhances language encoding efficiency, significantly boosting model performance. To enhance the inference capabilities, Llama 3 integrates grouped query attention (GQA) across models sized at 8B and 70B. These models are trained with sequences up to 8,192 tokens long, using a masking technique to prevent self-attention across document boundaries.\n",
171 | "1)Tokenizer The latest iteration of Llama 3 showcases an innovative tokenizer. This tokenizer operates with a vocabulary comprising 128K tokens, optimized beyond its predecessors to yield superior inference performance. Notably, the Llama 3–8B model was trained using an impressive 15 trillion tokens, a feat made possible through effective parameter utilization.\n",
172 | "2) GQA Grouped-query attention (GQA) ingeniously combines aspects of multi-head attention (MHA) and multi-query attention (MQA) to form an efficient attention mechanism. By caching keys and values from prior tokens, GQA lessens memory demands as batch sizes or context windows expand, thereby streamlining the decoding process in Transformer models.\n",
173 | "3) RoPE Llama 3 employs Rotary Positional Encoding (RoPE), a sophisticated encoding mechanism that strikes a balance between absolute positional encodings and relative positional encodings. This method not only retains a fixed embedding for each token but also applies a rotational computation to the vectors, enhancing the model’s attention calculations.\n",
174 | "4) KV Cache Key-Value (KV) caching is a technique deployed to speed up the inference in autoregressive models like GPT and Llama. By storing previously computed keys and values, the model reduces repetitive calculations, thus expediting matrix multiplications and enhancing overall efficiency.\n",
175 | "Training Meta AI has pre-trained Llama 3 on over 15 trillion tokens gathered from public sources. The training set is seven times larger than that used for Llama 2 and includes a significantly higher volume of code. With more than 5% of the training data consisting of high-quality, non-English content covering over 30 languages, Llama 3 prepares for multilingual applications, although performance in these languages may not equal that in English.\n",
176 | "In pursuit of the highest data quality, Meta AI developed sophisticated filtering systems, including heuristic and NSFW filters, semantic deduplication, and text classifiers. These systems were refined using insights from previous model generations, particularly Llama 2, which was instrumental in generating training data for Llama 3’s quality-assurance classifiers. For its largest models, Llama 3 utilizes a trio of parallelization strategies: data, model, and pipeline parallelization. Its most effective setup reaches over 400 TFLOPS per GPU, facilitated by training on 16,000 GPUs simultaneously within two custom-built 24,000 GPU clusters. Meta AI has also innovated a new training stack that automates error detection, handling, and maintenance to optimize GPU utilization.\n",
177 | "Llama 3 Instruct In refining its pretrained models for chat applications, Meta AI has employed a hybrid of supervised fine-tuning (SFT), rejection sampling, proximal policy optimization (PPO), and direct preference optimization (DPO). The selection and quality assurance of prompts and preference rankings significantly influence model performance. Moreover, to ensure model safety, these instruction-fine-tuned models undergo rigorous testing, including red-teaming by experts using adversarial prompts to identify and mitigate potential misuse risks.\n",
178 | "The Results Llama 3 achieves top-tier performance across leading industry benchmarks like MMLU and CommonSense QA.\n",
179 | "Additionally, Meta AI has curated a new, high-quality human evaluation set comprising 1,800 prompts spanning 12 critical use cases. Access to this set is restricted even within Meta AI to prevent potential overfitting by the modeling teams.\n",
180 | "An Impressive Model Llama 3 is a very welcome addition to the open model generative AI stack. The initial benchmark results are quite impressive, and the 400B version could rival GPT-4. Distribution is one area where Meta excelled in this release, making Llama 3 available on all major machine learning platforms. It’s been just a few hours, and we are already seeing open source innovations using Llama 3.\"\"\""
181 | ],
182 | "metadata": {
183 | "id": "a1XgODXdUiN3"
184 | },
185 | "execution_count": null,
186 | "outputs": []
187 | },
188 | {
189 | "cell_type": "code",
190 | "source": [
191 | "# Create a user prompt with the user's question\n",
192 | "prompt = f\"Use the following article as the source and answer the question:\\n\\n{ARTICLE}\\n\\nHow many parameters LLaMA 3 Models have?\"\n",
193 | "\n",
194 | "# Call the OpenAI API\n",
195 | "response = client.chat.completions.create(\n",
196 | " model=\"gpt-4o-mini\",\n",
197 | " temperature=0.0,\n",
198 | " messages=[\n",
199 | " {\"role\": \"system\", \"content\": \"You are a helpful assistant\"},\n",
200 | " {\"role\": \"user\", \"content\": prompt}\n",
201 | " ]\n",
202 | " )"
203 | ],
204 | "metadata": {
205 | "id": "KnF52b8IUiEv"
206 | },
207 | "execution_count": null,
208 | "outputs": []
209 | },
210 | {
211 | "cell_type": "code",
212 | "source": [
213 | "print( response.choices[0].message.content.strip() )"
214 | ],
215 | "metadata": {
216 | "colab": {
217 | "base_uri": "https://localhost:8080/"
218 | },
219 | "id": "7gMvLiitVEdq",
220 | "outputId": "0245078b-fb3b-498a-ac9a-81f7af396d81"
221 | },
222 | "execution_count": null,
223 | "outputs": [
224 | {
225 | "output_type": "stream",
226 | "name": "stdout",
227 | "text": [
228 | "Llama 3 has models with 8 billion (8B) and 70 billion (70B) parameters, with a 400 billion (400B) version currently being trained.\n"
229 | ]
230 | }
231 | ]
232 | },
233 | {
234 | "cell_type": "code",
235 | "source": [
236 | "len( ARTICLE.split(\" \") )"
237 | ],
238 | "metadata": {
239 | "colab": {
240 | "base_uri": "https://localhost:8080/"
241 | },
242 | "id": "vyUkCv0yVEZq",
243 | "outputId": "996108d0-253a-4024-e3e2-e45ad160a3f4"
244 | },
245 | "execution_count": null,
246 | "outputs": [
247 | {
248 | "output_type": "execute_result",
249 | "data": {
250 | "text/plain": [
251 | "881"
252 | ]
253 | },
254 | "metadata": {},
255 | "execution_count": 29
256 | }
257 | ]
258 | },
259 | {
260 | "cell_type": "code",
261 | "source": [],
262 | "metadata": {
263 | "id": "nqSwasoUVEXC"
264 | },
265 | "execution_count": null,
266 | "outputs": []
267 | },
268 | {
269 | "cell_type": "code",
270 | "source": [],
271 | "metadata": {
272 | "id": "vRm-3T5NVEUX"
273 | },
274 | "execution_count": null,
275 | "outputs": []
276 | },
277 | {
278 | "cell_type": "code",
279 | "source": [],
280 | "metadata": {
281 | "id": "q1uzBZbAVERn"
282 | },
283 | "execution_count": null,
284 | "outputs": []
285 | }
286 | ]
287 | }
--------------------------------------------------------------------------------
/notebooks/Web_Search_API.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": null,
6 | "metadata": {
7 | "colab": {
8 | "base_uri": "https://localhost:8080/"
9 | },
10 | "id": "JboB5VaCJUrb",
11 | "outputId": "4c658214-e211-40dd-a1b2-1c94572fb844"
12 | },
13 | "outputs": [
14 | {
15 | "output_type": "stream",
16 | "name": "stdout",
17 | "text": [
18 | " Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
19 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m337.0/337.0 kB\u001b[0m \u001b[31m7.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
20 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.1/1.1 MB\u001b[0m \u001b[31m29.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
21 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m296.6/296.6 kB\u001b[0m \u001b[31m16.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
22 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m15.5/15.5 MB\u001b[0m \u001b[31m52.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
23 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m81.3/81.3 kB\u001b[0m \u001b[31m3.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
24 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.2/1.2 MB\u001b[0m \u001b[31m38.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
25 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m104.9/104.9 kB\u001b[0m \u001b[31m4.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
26 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m176.8/176.8 kB\u001b[0m \u001b[31m7.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
27 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m295.8/295.8 kB\u001b[0m \u001b[31m6.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
28 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m49.5/49.5 kB\u001b[0m \u001b[31m1.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
29 | "\u001b[?25h Building wheel for sgmllib3k (setup.py) ... \u001b[?25l\u001b[?25hdone\n"
30 | ]
31 | }
32 | ],
33 | "source": [
34 | "!pip install -q llama-index==0.10.57 openai==1.37.0 tiktoken==0.7.0 llama-index-tools-google==0.1.3 newspaper4k==0.9.3.1 lxml_html_clean"
35 | ]
36 | },
37 | {
38 | "cell_type": "code",
39 | "execution_count": null,
40 | "metadata": {
41 | "id": "1NKAn5scN_g9"
42 | },
43 | "outputs": [],
44 | "source": [
45 | "import os\n",
46 | "\n",
47 | "# Set the following API Keys in the Python environment. Will be used later.\n",
48 | "os.environ[\"OPENAI_API_KEY\"] = \"[OPENAI_API_KEY]\"\n",
49 | "GOOGLE_SEARCH_KEY = \"GOOGLE_SEARCH_KEY\"\n",
50 | "GOOGLE_SEARCH_ENGINE = \"GOOGLE_SEARCH_ENGINE\" # Search Engine ID"
51 | ]
52 | },
53 | {
54 | "cell_type": "markdown",
55 | "source": [
56 | "## LLM and Embedding Model"
57 | ],
58 | "metadata": {
59 | "id": "Yp-tXudCc3Zy"
60 | }
61 | },
62 | {
63 | "cell_type": "code",
64 | "source": [
65 | "from llama_index.embeddings.openai import OpenAIEmbedding\n",
66 | "from llama_index.core import Settings\n",
67 | "from llama_index.llms.openai import OpenAI\n",
68 | "\n",
69 | "Settings.llm = OpenAI(model=\"gpt-4o-mini\", temperature= 0)\n",
70 | "Settings.embed_model = OpenAIEmbedding(model=\"text-embedding-3-small\")"
71 | ],
72 | "metadata": {
73 | "id": "dYmz-uAIc2rb"
74 | },
75 | "execution_count": null,
76 | "outputs": []
77 | },
78 | {
79 | "cell_type": "markdown",
80 | "metadata": {
81 | "id": "ex1gQVHvITMI"
82 | },
83 | "source": [
84 | "# Using Agents/Tools\n"
85 | ]
86 | },
87 | {
88 | "cell_type": "code",
89 | "source": [
90 | "from llama_index.core.tools import FunctionTool\n",
91 | "from llama_index.core.agent import ReActAgent\n",
92 | "\n",
93 | "\n",
94 | "# define sample Tool\n",
95 | "def multiply(a: int, b: int) -> int:\n",
96 | " \"\"\"Multiply two integers and returns the result integer\"\"\"\n",
97 | " return a * b\n",
98 | "\n",
99 | "\n",
100 | "multiply_tool = FunctionTool.from_defaults(fn=multiply)\n",
101 | "\n",
102 | "# initialize ReAct agent\n",
103 | "agent = ReActAgent.from_tools([multiply_tool], verbose=True)"
104 | ],
105 | "metadata": {
106 | "id": "OHd8WbUoZHWi"
107 | },
108 | "execution_count": null,
109 | "outputs": []
110 | },
111 | {
112 | "cell_type": "code",
113 | "source": [
114 | "res = agent.chat(\"What is the multiplication of 43 and 45?\")\n",
115 | "\n",
116 | "# Final response\n",
117 | "res.response"
118 | ],
119 | "metadata": {
120 | "id": "9JGVdS6AZGve",
121 | "outputId": "d4bd7f24-c0c3-490e-ba6e-a3dff39da3bd",
122 | "colab": {
123 | "base_uri": "https://localhost:8080/",
124 | "height": 174
125 | }
126 | },
127 | "execution_count": null,
128 | "outputs": [
129 | {
130 | "output_type": "stream",
131 | "name": "stdout",
132 | "text": [
133 | "> Running step 1d3f5ada-78ed-4e7a-ae7a-c2fc5f152858. Step input: What is the multiplication of 43 and 45?\n",
134 | "\u001b[1;3;38;5;200mThought: The current language of the user is: English. I need to use a tool to help me answer the question.\n",
135 | "Action: multiply\n",
136 | "Action Input: {'a': 43, 'b': 45}\n",
137 | "\u001b[0m\u001b[1;3;34mObservation: 1935\n",
138 | "\u001b[0m> Running step 02f814ea-89fb-4b30-b41b-e7a262415a05. Step input: None\n",
139 | "\u001b[1;3;38;5;200mThought: I can answer without using any more tools. I'll use the user's language to answer.\n",
140 | "Answer: The multiplication of 43 and 45 is 1935.\n",
141 | "\u001b[0m"
142 | ]
143 | },
144 | {
145 | "output_type": "execute_result",
146 | "data": {
147 | "text/plain": [
148 | "'The multiplication of 43 and 45 is 1935.'"
149 | ],
150 | "application/vnd.google.colaboratory.intrinsic+json": {
151 | "type": "string"
152 | }
153 | },
154 | "metadata": {},
155 | "execution_count": 6
156 | }
157 | ]
158 | },
159 | {
160 | "cell_type": "markdown",
161 | "metadata": {
162 | "id": "0LMypoqUyuXq"
163 | },
164 | "source": [
165 | "## Define Google Search Tool\n"
166 | ]
167 | },
168 | {
169 | "cell_type": "code",
170 | "execution_count": null,
171 | "metadata": {
172 | "id": "4Q7sc69nJvWI"
173 | },
174 | "outputs": [],
175 | "source": [
176 | "from llama_index.tools.google import GoogleSearchToolSpec\n",
177 | "\n",
178 | "tool_spec = GoogleSearchToolSpec(key=GOOGLE_SEARCH_KEY, engine=GOOGLE_SEARCH_ENGINE)"
179 | ]
180 | },
181 | {
182 | "cell_type": "code",
183 | "execution_count": null,
184 | "metadata": {
185 | "id": "VrbuIOaMeOIf"
186 | },
187 | "outputs": [],
188 | "source": [
189 | "# Import and initialize our tool spec\n",
190 | "from llama_index.core.tools.tool_spec.load_and_search import LoadAndSearchToolSpec\n",
191 | "\n",
192 | "# Wrap the google search tool to create an index on top of the returned Google search\n",
193 | "wrapped_tool = LoadAndSearchToolSpec.from_defaults(\n",
194 | " tool_spec.to_tool_list()[0],\n",
195 | ").to_tool_list()"
196 | ]
197 | },
198 | {
199 | "cell_type": "markdown",
200 | "metadata": {
201 | "id": "T3ENpLyBy7UL"
202 | },
203 | "source": [
204 | "## Create the Agent\n"
205 | ]
206 | },
207 | {
208 | "cell_type": "code",
209 | "execution_count": null,
210 | "metadata": {
211 | "id": "-_Ab47ppK8b2"
212 | },
213 | "outputs": [],
214 | "source": [
215 | "from llama_index.agent.openai import OpenAIAgent\n",
216 | "\n",
217 | "agent = OpenAIAgent.from_tools(wrapped_tool, verbose=False)"
218 | ]
219 | },
220 | {
221 | "cell_type": "code",
222 | "execution_count": null,
223 | "metadata": {
224 | "id": "YcUyz1-FlCQ8"
225 | },
226 | "outputs": [],
227 | "source": [
228 | "res = agent.chat(\"How many parameters LLaMA 3.2 model has?\")"
229 | ]
230 | },
231 | {
232 | "cell_type": "code",
233 | "execution_count": null,
234 | "metadata": {
235 | "colab": {
236 | "base_uri": "https://localhost:8080/",
237 | "height": 52
238 | },
239 | "id": "w4wK5sY-lOOv",
240 | "outputId": "6e40f9fc-76a9-4006-cd21-705c5e66bc1e"
241 | },
242 | "outputs": [
243 | {
244 | "output_type": "execute_result",
245 | "data": {
246 | "text/plain": [
247 | "'The LLaMA 3.2 model has the following parameters:\\n- Vision LLMs: 11 billion and 90 billion parameters\\n- Lightweight text-only models: 1 billion and 3 billion parameters.'"
248 | ],
249 | "application/vnd.google.colaboratory.intrinsic+json": {
250 | "type": "string"
251 | }
252 | },
253 | "metadata": {},
254 | "execution_count": 11
255 | }
256 | ],
257 | "source": [
258 | "res.response"
259 | ]
260 | },
261 | {
262 | "cell_type": "code",
263 | "execution_count": null,
264 | "metadata": {
265 | "colab": {
266 | "base_uri": "https://localhost:8080/"
267 | },
268 | "id": "TM_cvBA1nTJM",
269 | "outputId": "5a4a5cc9-c630-45bb-9fb1-757e79311261",
270 | "collapsed": true
271 | },
272 | "outputs": [
273 | {
274 | "output_type": "execute_result",
275 | "data": {
276 | "text/plain": [
277 | "[ToolOutput(content='Content loaded! You can now search the information using read_google_search', tool_name='google_search', raw_input={'args': (), 'kwargs': {'query': 'LLaMA 3.2 model parameters'}}, raw_output='Content loaded! You can now search the information using read_google_search', is_error=False),\n",
278 | " ToolOutput(content='The LLaMA 3.2 model features small and medium-sized vision LLMs with 11 billion and 90 billion parameters, as well as lightweight text-only models with 1 billion and 3 billion parameters.', tool_name='read_google_search', raw_input={'args': (), 'kwargs': {'query': 'LLaMA 3.2 model parameters'}}, raw_output='The LLaMA 3.2 model features small and medium-sized vision LLMs with 11 billion and 90 billion parameters, as well as lightweight text-only models with 1 billion and 3 billion parameters.', is_error=False)]"
279 | ]
280 | },
281 | "metadata": {},
282 | "execution_count": 12
283 | }
284 | ],
285 | "source": [
286 | "res.sources"
287 | ]
288 | },
289 | {
290 | "cell_type": "markdown",
291 | "metadata": {
292 | "id": "who-NM4pIhPn"
293 | },
294 | "source": [
295 | "# Using Tools w/ VectorStoreIndex\n"
296 | ]
297 | },
298 | {
299 | "cell_type": "markdown",
300 | "metadata": {
301 | "id": "9g9cTM9GI-19"
302 | },
303 | "source": [
304 | "A limitation of the current agent/tool in LlamaIndex is that it **relies solely on the page description from the retrieved pages** to answer questions. This approach will miss answers that are not visible in the page's description tag. To address this, a possible workaround is to fetch the page results, extract the page content using the newspaper3k library, and then create an index based on the downloaded content. Also, the previous method stacks all retrieved items from the search engine into a single document, making it **difficult to pinpoint the exact source** of the response. However, the following method will enable us to present the sources easily.\n"
305 | ]
306 | },
307 | {
308 | "cell_type": "markdown",
309 | "metadata": {
310 | "id": "31G_fxxJIsbC"
311 | },
312 | "source": [
313 | "## Define Google Search Tool\n"
314 | ]
315 | },
316 | {
317 | "cell_type": "code",
318 | "execution_count": null,
319 | "metadata": {
320 | "id": "lwRmj2odIHxt"
321 | },
322 | "outputs": [],
323 | "source": [
324 | "from llama_index.tools.google import GoogleSearchToolSpec\n",
325 | "\n",
326 | "tool_spec = GoogleSearchToolSpec(key=GOOGLE_SEARCH_KEY, engine=GOOGLE_SEARCH_ENGINE)"
327 | ]
328 | },
329 | {
330 | "cell_type": "code",
331 | "execution_count": null,
332 | "metadata": {
333 | "id": "UVIxdj04Bsf2"
334 | },
335 | "outputs": [],
336 | "source": [
337 | "search_results = tool_spec.google_search(\"LLaMA 3.2 model details\")"
338 | ]
339 | },
340 | {
341 | "cell_type": "code",
342 | "execution_count": null,
343 | "metadata": {
344 | "id": "AlYDNfg2BsdQ"
345 | },
346 | "outputs": [],
347 | "source": [
348 | "import json\n",
349 | "\n",
350 | "search_results = json.loads(search_results[0].text)"
351 | ]
352 | },
353 | {
354 | "cell_type": "markdown",
355 | "metadata": {
356 | "id": "pHALd3uhIxtQ"
357 | },
358 | "source": [
359 | "## Read Each URL Contents\n"
360 | ]
361 | },
362 | {
363 | "cell_type": "code",
364 | "execution_count": null,
365 | "metadata": {
366 | "colab": {
367 | "base_uri": "https://localhost:8080/"
368 | },
369 | "id": "jXz3JFduBsaq",
370 | "outputId": "e814fa12-ad41-4723-a283-dbd9b03419ee"
371 | },
372 | "outputs": [
373 | {
374 | "output_type": "stream",
375 | "name": "stderr",
376 | "text": [
377 | "WARNING:newspaper.network:get_html_status(): bad status code 403 on URL: https://www.reddit.com/r/LocalLLaMA/comments/1fqhjs9/is_llama_32_banned_to_use_in_eu/, html: