Created
February 13, 2025 14:14
-
-
Save ryogrid/36c354411793a329c6f16ee34c601fa5 to your computer and use it in GitHub Desktop.
LLM with deepseek-r1:14b on Ollama
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/github/ryogrid/ryogridJupyterNotebooks/blob/master/Ollama_deepseek-r1_14b_hybrid_RAG_for_web_url.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "vM7HIRlKe7i-" | |
}, | |
"source": [ | |
"# **See full article on [Meduim](https://medium.com/@tharindumadhusanka99/llama3-rag-on-google-colab-73c43aa53281)**" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"Instance is T4 GPU of Free plan" | |
], | |
"metadata": { | |
"id": "1qv_XhXhaLYO" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "B99T0uX5N5Cd", | |
"outputId": "feea050d-5f27-48ab-de89-30b2995c8bd2" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Collecting colab-xterm\n", | |
" Downloading colab_xterm-0.2.0-py3-none-any.whl.metadata (1.2 kB)\n", | |
"Requirement already satisfied: ptyprocess~=0.7.0 in /usr/local/lib/python3.10/dist-packages (from colab-xterm) (0.7.0)\n", | |
"Requirement already satisfied: tornado>5.1 in /usr/local/lib/python3.10/dist-packages (from colab-xterm) (6.3.3)\n", | |
"Downloading colab_xterm-0.2.0-py3-none-any.whl (115 kB)\n", | |
"\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/115.6 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m115.6/115.6 kB\u001b[0m \u001b[31m5.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[?25hInstalling collected packages: colab-xterm\n", | |
"Successfully installed colab-xterm-0.2.0\n" | |
] | |
} | |
], | |
"source": [ | |
"!pip install colab-xterm\n", | |
"%load_ext colabxterm" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "Bm-pQgz3dgQr" | |
}, | |
"outputs": [], | |
"source": [ | |
"# type these codes in below terminal after run the cell (%xterm)\n", | |
"# curl -fsSL https://ollama.com/install.sh | sh\n", | |
"# ollama serve & ollama pull schroneko/gemma-2-2b-jpn-it & ollama pull deepseek-r1:14b" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 839 | |
}, | |
"id": "S3Pw_eekOpuW", | |
"outputId": "fd494f5a-a05a-4952-e82a-81b68be1d31f" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": [ | |
"Launching Xterm..." | |
] | |
}, | |
"metadata": {} | |
}, | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": [ | |
"<IPython.core.display.Javascript object>" | |
], | |
"application/javascript": [ | |
"\n", | |
" (async () => {\n", | |
" const url = new URL(await google.colab.kernel.proxyPort(10001, {'cache': true}));\n", | |
" const iframe = document.createElement('iframe');\n", | |
" iframe.src = url;\n", | |
" iframe.setAttribute('width', '100%');\n", | |
" iframe.setAttribute('height', '800');\n", | |
" iframe.setAttribute('frameborder', 0);\n", | |
" document.body.appendChild(iframe);\n", | |
" })();\n", | |
" " | |
] | |
}, | |
"metadata": {} | |
} | |
], | |
"source": [ | |
"%xterm" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"id": "ovlpFAQbPpYK", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"outputId": "a3007d22-3c61-4d68-de6d-c70863de20de" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.5/2.5 MB\u001b[0m \u001b[31m10.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.0/1.0 MB\u001b[0m \u001b[31m10.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m413.2/413.2 kB\u001b[0m \u001b[31m10.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m50.9/50.9 kB\u001b[0m \u001b[31m6.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m67.3/67.3 kB\u001b[0m \u001b[31m5.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[?25h Installing build dependencies ... \u001b[?25l\u001b[?25hdone\n", | |
" Getting requirements to build wheel ... \u001b[?25l\u001b[?25hdone\n", | |
" Preparing metadata (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m611.1/611.1 kB\u001b[0m \u001b[31m10.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.4/2.4 MB\u001b[0m \u001b[31m6.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m278.6/278.6 kB\u001b[0m \u001b[31m2.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m94.8/94.8 kB\u001b[0m \u001b[31m2.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.0/2.0 MB\u001b[0m \u001b[31m1.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m99.2/99.2 kB\u001b[0m \u001b[31m2.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m13.3/13.3 MB\u001b[0m \u001b[31m9.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m55.9/55.9 kB\u001b[0m \u001b[31m7.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m177.4/177.4 kB\u001b[0m \u001b[31m8.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m65.0/65.0 kB\u001b[0m \u001b[31m7.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m118.7/118.7 kB\u001b[0m \u001b[31m8.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m73.0/73.0 kB\u001b[0m \u001b[31m8.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m62.3/62.3 kB\u001b[0m \u001b[31m7.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m442.1/442.1 kB\u001b[0m \u001b[31m10.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m319.7/319.7 kB\u001b[0m \u001b[31m10.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m71.5/71.5 kB\u001b[0m \u001b[31m7.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.8/3.8 MB\u001b[0m \u001b[31m11.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m452.9/452.9 kB\u001b[0m \u001b[31m10.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m46.0/46.0 kB\u001b[0m \u001b[31m6.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m86.8/86.8 kB\u001b[0m \u001b[31m8.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[?25h Building wheel for pypika (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n", | |
"\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n", | |
"tensorflow 2.17.1 requires protobuf!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<5.0.0dev,>=3.20.3, but you have protobuf 5.29.3 which is incompatible.\n", | |
"tensorflow-metadata 1.13.1 requires protobuf<5,>=3.20.3, but you have protobuf 5.29.3 which is incompatible.\u001b[0m\u001b[31m\n", | |
"\u001b[0m" | |
] | |
} | |
], | |
"source": [ | |
"!pip -qq install langchain\n", | |
"!pip -qq install langchain-core\n", | |
"!pip -qq install langchain-community\n", | |
"!pip -qq install langchain_ollama\n", | |
"!pip -qq install \"langchain-chroma>=0.1.2\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 348 | |
}, | |
"id": "q7gKz7iUPJ6y", | |
"outputId": "196ec7cc-b102-4fe8-af0f-9d72b576ddd2" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"'<think>\\n嗯,为什么天空是蓝色的呢?这个问题听起来很简单,但其实背后涉及很多科学知识。我记得以前学过跟光有关的知识,可能和光线的颜色有关系吧。\\n\\n首先,我想知道太阳发出的光是什么颜色。太阳看起来是黄色的,但实际上它发出的是各种颜色的光混合在一起,也就是白光。白光是由红、橙、黄、绿、蓝、靛、紫这些颜色组成的。\\n\\n那为什么天空会显示蓝色呢?难道光线在传播过程中发生了什么变化吗?我记得有听说过“散射”这个词,可能和这个有关。散射是指光线被空气中的分子或小颗粒分散开来,导致不同方向的光被散射。\\n\\n具体来说,空气中有很多气体分子,比如氮气和氧气。这些分子比可见光的波长要小很多,所以当太阳光进入大气层时,这些分子会把光向各个方向散射。这就是所谓的“瑞利散射”,是英国物理学家瑞利勋爵发现的。\\n\\n不同的颜色光被散射的程度不同,因为它们的波长不同。蓝色和紫色的光波长较短,所以它们更容易被空气中的分子散射。而红色和橙色的光波长较长,散射的效果就弱一些。\\n\\n既然如此,为什么我们看到的是蓝色而不是紫色呢?可能是因为人眼对紫色的敏感度较低,或者大气中存在其他因素影响。比如,空气中可能有微小的颗粒或杂质,这些也会改变散射的情况,使得蓝色成为主导颜色。\\n\\n另外,日出和日落时,太阳的位置比较低,阳光需要穿过更厚的大气层,这时较长波长的红色光更容易被散射,所以天空在那个时候会呈现橙色或红色。这进一步证明了短波长的蓝光容易被散射,而红光则较少。\\n\\n还有,我想到过下雨的时候,天空有时会变成灰蒙蒙的,这是因为空气中的水滴或者其他颗粒增加了,导致更多的散射,使得蓝色看起来更明显或者变得模糊,这可能与散射的程度有关。\\n\\n再想想,晴朗的天气下,天空特别蓝,而阴天则显得灰暗。这是因为晴天时空气中颗粒较少,瑞利散射效果更好,蓝色更为明显;而阴天时空气中的水滴或其他颗粒增多,导致米氏散射(由较大的颗粒引起)增加,使天空看起来更灰。\\n\\n总结一下,天空呈现蓝色主要是因为太阳光中的蓝光被大气中的分子大量散射,而其他颜色的光散射较少。瑞利散射对波长较短的光更为有效,因此我们看到的是蓝色的天空。不过,在某些情况下,如日出、日落或天气变化时,光线经过更厚的大气层或者有更多颗粒物存在,就会呈现出不同的颜色。\\n</think>\\n\\n天之所以呈现蓝色,主要是由于太阳辐射的白光在进入地球大气层后发生散射的结果。具体来说,阳光中的不同颜色(波长)在空气中被分子和微粒散射的程度不同,其中蓝光因波长短而散射较多,使天空呈现蓝色。\\n\\n**详细解释:**\\n\\n1. **太阳辐射与色散** \\n 太阳发出的白光是由多种颜色组成的。当这些光线进入大气层时,它们会被空气中的分子(主要为氮气和氧气)以及悬浮颗粒物散射。这种散射被称为瑞利散射,由英国物理学家瑞利勋爵首先描述。\\n\\n2. **波长与散射关系** \\n 不同颜色的光具有不同的波长,其中蓝紫光波长短,红光波长长。根据瑞利散射定律,散射的程度与波长的四次方成反比(即波长越短,散射越多)。因此,波长短的蓝紫光被大量散射到各个方向。\\n\\n3. **人眼感知的影响** \\n 尽管蓝紫光散射较多,但由于人眼对紫色不敏感,我们看到的是蓝色。此外,太阳辐射中紫光成分较少,也影响了天空呈现的颜色偏向蓝色。\\n\\n4. **大气因素与天气状况** \\n 在晴朗干燥的天气,空气中的颗粒较少,瑞利散射占主导,天空呈现清晰的蓝色。而在日出或日落时,阳光需穿过更厚的大气层,蓝紫光被大量散射和吸收,红橙色光得以穿透,形成红色或橙色的天空。\\n\\n5. **其他因素的影响** \\n 空气中的水滴、尘埃等较大颗粒会引发米氏散射,这种散射对各波长的光影响相似,导致天空在阴天时显得灰蒙。此外,大气污染和自然现象(如火山灰)也能改变天空的颜色。\\n\\n综上所述,天空呈现蓝色是由于太阳辐射中的蓝紫光在大气中被大量散射,而人眼的感知偏好以及大气条件共同作用的结果。这一现象不仅展示了物理学的基本原理,也体现了地球大气层对光线传播的独特影响。'" | |
], | |
"application/vnd.google.colaboratory.intrinsic+json": { | |
"type": "string" | |
} | |
}, | |
"metadata": {}, | |
"execution_count": 10 | |
} | |
], | |
"source": [ | |
"# from langchain_community.llms import Ollama\n", | |
"from langchain_ollama import OllamaLLM\n", | |
"#llm = Ollama(model = \"schroneko/gemma-2-2b-jpn-it\")\n", | |
"llm = OllamaLLM(model = \"deepseek-r1:14b\")\n", | |
"llm.invoke(\"空はなぜ青いのですか?\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "fLNJWzd0Ul9V", | |
"outputId": "caf2e677-9f99-43a7-f2a9-db7ad13610f0" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m62.2/62.2 MB\u001b[0m \u001b[31m10.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m321.9/321.9 kB\u001b[0m \u001b[31m10.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m464.1/464.1 kB\u001b[0m \u001b[31m10.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m12.5/12.5 MB\u001b[0m \u001b[31m11.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[?25hCollecting rank_bm25\n", | |
" Downloading rank_bm25-0.2.2-py3-none-any.whl.metadata (3.2 kB)\n", | |
"Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from rank_bm25) (1.26.4)\n", | |
"Downloading rank_bm25-0.2.2-py3-none-any.whl (8.6 kB)\n", | |
"Installing collected packages: rank_bm25\n", | |
"Successfully installed rank_bm25-0.2.2\n", | |
"Collecting sudachipy\n", | |
" Downloading SudachiPy-0.6.10-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (12 kB)\n", | |
"Collecting sudachidict_full\n", | |
" Downloading SudachiDict-full-20250129.tar.gz (9.0 kB)\n", | |
" Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", | |
"Downloading SudachiPy-0.6.10-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.7 MB)\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.7/1.7 MB\u001b[0m \u001b[31m10.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[?25hBuilding wheels for collected packages: sudachidict_full\n", | |
" Building wheel for sudachidict_full (setup.py) ... \u001b[?25l\u001b[?25hdone\n", | |
" Created wheel for sudachidict_full: filename=SudachiDict_full-20250129-py3-none-any.whl size=126565548 sha256=4c5a551dcaf48093c8b67dff9fe7b0fd5aa649501d1c5881272c4b8cbb0ab86e\n", | |
" Stored in directory: /root/.cache/pip/wheels/f7/ec/ee/72546d5ca007ed729a120526283ab27cfd8f6fce306f5bfb8b\n", | |
"Successfully built sudachidict_full\n", | |
"Installing collected packages: sudachipy, sudachidict_full\n", | |
"Successfully installed sudachidict_full-20250129 sudachipy-0.6.10\n" | |
] | |
} | |
], | |
"source": [ | |
"!pip install ollama langchain beautifulsoup4 chromadb gradio -q\n", | |
"!pip install rank_bm25\n", | |
"!pip install sudachipy sudachidict_full" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [], | |
"metadata": { | |
"id": "eWLQjCOeDVvJ" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 646 | |
}, | |
"id": "DlJzBxznmQrw", | |
"outputId": "588ca618-17e2-4f48-8e8c-2e9e9c650d70" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Running Gradio in a Colab notebook requires sharing enabled. Automatically setting `share=True` (you can turn this off by setting `share=False` in `launch()` explicitly).\n", | |
"\n", | |
"Colab notebook detected. To show errors in colab notebook, set debug=True in launch()\n", | |
"* Running on public URL: https://892f7b8b70c563ef72.gradio.live\n", | |
"\n", | |
"This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from the terminal in the working directory to deploy to Hugging Face Spaces (https://huggingface.co/spaces)\n" | |
] | |
}, | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": [ | |
"<IPython.core.display.HTML object>" | |
], | |
"text/html": [ | |
"<div><iframe src=\"https://892f7b8b70c563ef72.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>" | |
] | |
}, | |
"metadata": {} | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [] | |
}, | |
"metadata": {}, | |
"execution_count": 13 | |
} | |
], | |
"source": [ | |
"import gradio as gr\n", | |
"import ollama\n", | |
"from bs4 import BeautifulSoup as bs\n", | |
"from langchain.text_splitter import RecursiveCharacterTextSplitter\n", | |
"from langchain_community.document_loaders import WebBaseLoader\n", | |
"from langchain_chroma import Chroma\n", | |
"from langchain_ollama import OllamaEmbeddings\n", | |
"from uuid import uuid4\n", | |
"from langchain_core.documents import Document\n", | |
"\n", | |
"from langchain.retrievers import BM25Retriever, EnsembleRetriever\n", | |
"from typing import List\n", | |
"from sudachipy import tokenizer\n", | |
"from sudachipy import dictionary\n", | |
"\n", | |
"# Load the data from the web URL\n", | |
"url = 'https://ja.wikipedia.org/wiki/%E3%81%BC%E3%81%A3%E3%81%A1%E3%83%BB%E3%81%96%E3%83%BB%E3%82%8D%E3%81%A3%E3%81%8F!'\n", | |
"loader = WebBaseLoader(url)\n", | |
"docs = loader.load()\n", | |
"\n", | |
"# Split the loaded documents into chunks\n", | |
"text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)\n", | |
"splits = text_splitter.split_documents(docs)\n", | |
"\n", | |
"# Create Ollama embeddings and vector store\n", | |
"embeddings = OllamaEmbeddings(model=\"yxchia/multilingual-e5-base\")\n", | |
"vectorstore = Chroma(\n", | |
" collection_name=\"example_collection\",\n", | |
" embedding_function=embeddings,\n", | |
" persist_directory=\"./chroma_langchain_db\", # Where to save data locally, remove if not necessary\n", | |
")\n", | |
"#vectorstore = Chroma.from_documents(documents=splits, embedding=embeddings)\n", | |
"\n", | |
"#documents = enumerate(splits )\n", | |
"#documents = [Document(page_content=text, metadata={'source': 'test'}, id=idx) for idx, text in enumerate(splits)]\n", | |
"\n", | |
"uuids = [str(uuid4()) for _ in range(len(splits))]\n", | |
"vectorstore.add_documents(documents=splits, ids=uuids)\n", | |
"\n", | |
"# for keyword search\n", | |
"def preprocess_func(text: str) -> List[str]:\n", | |
" tokenizer_obj = dictionary.Dictionary(dict=\"full\").create()\n", | |
" mode = tokenizer.Tokenizer.SplitMode.A\n", | |
" tokens = tokenizer_obj.tokenize(text ,mode)\n", | |
" words = [token.surface() for token in tokens]\n", | |
" words = list(set(words)) # 重複削除\n", | |
" return words\n", | |
"\n", | |
"texts = [doc.page_content for doc in splits]\n", | |
"# キーワード検索をするRetrieverの準備\n", | |
"bm25_retriever = BM25Retriever.from_texts(\n", | |
" texts,\n", | |
" preprocess_func=preprocess_func,\n", | |
" k=4,\n", | |
")\n", | |
"\n", | |
"# Define the RAG setup\n", | |
"#retriever = vectorstore.as_retriever()\n", | |
"vec_retriever = vectorstore.as_retriever(\n", | |
" search_type=\"mmr\",\n", | |
" search_kwargs={\"k\": 4, \"fetch_k\": 20},\n", | |
")\n", | |
"\n", | |
"# ベクトル検索で得たチャンクとキーワード検索で得たチャンクをよしなにアンサンブルするRetriever\n", | |
"ensemble_retriever = EnsembleRetriever(\n", | |
" retrievers=[bm25_retriever, vec_retriever],\n", | |
" weights=[0.5, 0.5],\n", | |
")\n", | |
"\n", | |
"# Define the function to call the Ollama schroneko/gemma-2-2b-jpn-it model\n", | |
"def ollama_llm(question, context):\n", | |
" formatted_prompt = f\"#Context: {context}\\n\\n#Question: {question}\"\n", | |
"\n", | |
" response = ollama.chat(model='deepseek-r1:14b', messages=[{'role': 'user', 'content': formatted_prompt}])\n", | |
" return response['message']['content']\n", | |
"\n", | |
"def rag_chain(question):\n", | |
" #retrieved_docs = retriever.invoke(question)\n", | |
" retrieved_docs = ensemble_retriever.invoke(question)\n", | |
" #retrieved_docs = bm25_retriever.invoke(question)\n", | |
" #retrieved_docs = vec_retriever.invoke(question)\n", | |
" formatted_context = \"\\n\\n\".join(doc.page_content for doc in retrieved_docs)\n", | |
" return ollama_llm(question, formatted_context) + \"\\n\\nContext: \" + formatted_context\n", | |
"\n", | |
"# Define the Gradio interface\n", | |
"def get_important_facts(question):\n", | |
" return rag_chain(question)\n", | |
"\n", | |
"# Create a Gradio app interface\n", | |
"iface = gr.Interface(\n", | |
" fn=get_important_facts,\n", | |
" inputs=gr.Textbox(lines=2, placeholder=\"Enter your question here...\"),\n", | |
" outputs=\"text\",\n", | |
" title=\"RAG with multilingual-e5-base and deepseek:14b on Ollama\",\n", | |
" description=\"Ask questions about the proveded context\",\n", | |
")\n", | |
"\n", | |
"# Launch the Gradio app\n", | |
"iface.launch()\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [], | |
"metadata": { | |
"id": "D-8iGNRkYyq6" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"gpuType": "T4", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"display_name": "Python 3", | |
"name": "python3" | |
}, | |
"language_info": { | |
"name": "python" | |
}, | |
"accelerator": "GPU" | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment