update doc with llama_index (#1180)

This commit is contained in:
Yunlin Mao
2025-01-13 21:03:59 +08:00
committed by GitHub
parent 845a0bd5fe
commit bceb7196d1
2 changed files with 422 additions and 46 deletions

File diff suppressed because one or more lines are too long

View File

@@ -2,6 +2,10 @@
"cells": [ "cells": [
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "f4abc589d9bfffca",
"metadata": {
"collapsed": false
},
"source": [ "source": [
"# Usage\n", "# Usage\n",
"\n", "\n",
@@ -34,27 +38,29 @@
"```\n", "```\n",
"\n", "\n",
"## 3. Go!" "## 3. Go!"
], ]
"metadata": {
"collapsed": false
},
"id": "f4abc589d9bfffca"
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"id": "c32122833dd7b8c8",
"metadata": {
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"!pip install modelscope\n", "!pip install modelscope\n",
"!pip install transformers -U\n", "!pip install transformers -U\n",
"!pip install llama-index llama-index-llms-huggingface ipywidgets " "!pip install llama-index llama-index-llms-huggingface ipywidgets "
], ]
"metadata": {
"collapsed": false
},
"id": "c32122833dd7b8c8"
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"id": "63704e2b21a9ba52",
"metadata": {
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"!wget https://modelscope.oss-cn-beijing.aliyuncs.com/resource/rag/punkt.zip\n", "!wget https://modelscope.oss-cn-beijing.aliyuncs.com/resource/rag/punkt.zip\n",
@@ -74,15 +80,90 @@
"!mv /mnt/workspace/xianjiaoda.md /mnt/workspace/custom_data\n", "!mv /mnt/workspace/xianjiaoda.md /mnt/workspace/custom_data\n",
"\n", "\n",
"!cd /mnt/workspace" "!cd /mnt/workspace"
], ]
"metadata": {
"collapsed": false
},
"id": "63704e2b21a9ba52"
}, },
{ {
"cell_type": "code", "cell_type": "code",
"outputs": [], "execution_count": 2,
"id": "eef67659e94045c5",
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Downloading Model to directory: /mnt/workspace/.cache/modelscope/qwen/Qwen1.5-4B-Chat\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"2025-01-13 15:52:53,260 - modelscope - INFO - Model revision not specified, using default: [master] version.\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"2025-01-13 15:52:53,637 - modelscope - INFO - Creating symbolic link [/mnt/workspace/.cache/modelscope/qwen/Qwen1.5-4B-Chat].\n",
"2025-01-13 15:52:53,638 - modelscope - WARNING - Failed to create symbolic link /mnt/workspace/.cache/modelscope/qwen/Qwen1.5-4B-Chat for /mnt/workspace/.cache/modelscope/qwen/Qwen1___5-4B-Chat.\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "4523c5dd31ba411d95cc0ce9e5da8ded",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Loading checkpoint shards: 0%| | 0/2 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"llm created\n",
"Downloading Model to directory: /mnt/workspace/.cache/modelscope/damo/nlp_gte_sentence-embedding_chinese-base\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"2025-01-13 15:53:01,651 - modelscope - INFO - Model revision not specified, using default: [master] version.\n",
"2025-01-13 15:53:01,894 - modelscope - INFO - initiate model from /mnt/workspace/.cache/modelscope/hub/damo/nlp_gte_sentence-embedding_chinese-base\n",
"2025-01-13 15:53:01,895 - modelscope - INFO - initiate model from location /mnt/workspace/.cache/modelscope/hub/damo/nlp_gte_sentence-embedding_chinese-base.\n",
"2025-01-13 15:53:01,898 - modelscope - INFO - initialize model from /mnt/workspace/.cache/modelscope/hub/damo/nlp_gte_sentence-embedding_chinese-base\n",
"2025-01-13 15:53:02,532 - modelscope - WARNING - No preprocessor field found in cfg.\n",
"2025-01-13 15:53:02,533 - modelscope - WARNING - No val key and type key found in preprocessor domain of configuration.json file.\n",
"2025-01-13 15:53:02,533 - modelscope - WARNING - Cannot find available config to build preprocessor at mode inference, current config: {'model_dir': '/mnt/workspace/.cache/modelscope/hub/damo/nlp_gte_sentence-embedding_chinese-base'}. trying to build by task and model information.\n",
"2025-01-13 15:53:02,588 - modelscope - WARNING - No preprocessor field found in cfg.\n",
"2025-01-13 15:53:02,588 - modelscope - WARNING - No val key and type key found in preprocessor domain of configuration.json file.\n",
"2025-01-13 15:53:02,589 - modelscope - WARNING - Cannot find available config to build preprocessor at mode inference, current config: {'model_dir': '/mnt/workspace/.cache/modelscope/hub/damo/nlp_gte_sentence-embedding_chinese-base', 'sequence_length': 128}. trying to build by task and model information.\n",
"/root/miniconda3/envs/modelscope_1.21/lib/python3.9/site-packages/transformers/modeling_utils.py:1044: FutureWarning: The `device` argument is deprecated and will be removed in v5 of Transformers.\n",
" warnings.warn(\n",
"/root/miniconda3/envs/modelscope_1.21/lib/python3.9/site-packages/transformers/generation/configuration_utils.py:628: UserWarning: `do_sample` is set to `False`. However, `temperature` is set to `0.0` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `temperature`.\n",
" warnings.warn(\n",
"/root/miniconda3/envs/modelscope_1.21/lib/python3.9/site-packages/transformers/generation/configuration_utils.py:633: UserWarning: `do_sample` is set to `False`. However, `top_p` is set to `0.8` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `top_p`.\n",
" warnings.warn(\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"作为一所历史悠久的综合性研究型大学,西安交通大学有着丰富的校训文化。其中,\"厚德博学,求是创新\"是其最为人所知的校训之一。这句校训不仅体现了学校的教育理念,也反映了学校对学生的期望和要求。此外,西安交通大学还有一句著名的校训:\"明德尚志,自强不息\",这也是学校对学生的一种激励和引导。这两句校训都强调了教育的重要性,以及学生应该具备的道德品质和自我提升的精神。\n"
]
}
],
"source": [ "source": [
"import logging\n", "import logging\n",
"import sys\n", "import sys\n",
@@ -93,9 +174,7 @@
"from llama_index.core import (\n", "from llama_index.core import (\n",
" SimpleDirectoryReader,\n", " SimpleDirectoryReader,\n",
" VectorStoreIndex,\n", " VectorStoreIndex,\n",
" Settings,\n", " Settings\n",
" ServiceContext,\n",
" set_global_service_context,\n",
")\n", ")\n",
"from llama_index.core.base.embeddings.base import BaseEmbedding, Embedding\n", "from llama_index.core.base.embeddings.base import BaseEmbedding, Embedding\n",
"from llama_index.core.prompts import PromptTemplate\n", "from llama_index.core.prompts import PromptTemplate\n",
@@ -176,9 +255,8 @@
"\n", "\n",
"embedding_model = \"damo/nlp_gte_sentence-embedding_chinese-base\"\n", "embedding_model = \"damo/nlp_gte_sentence-embedding_chinese-base\"\n",
"embeddings = ModelScopeEmbeddings4LlamaIndex(model_id=embedding_model)\n", "embeddings = ModelScopeEmbeddings4LlamaIndex(model_id=embedding_model)\n",
"service_context = ServiceContext.from_defaults(embed_model=embeddings, llm=llm)\n",
"set_global_service_context(service_context)\n",
"Settings.embed_model = embeddings\n", "Settings.embed_model = embeddings\n",
"Settings.llm = llm\n",
"\n", "\n",
"# load example documents\n", "# load example documents\n",
"documents = SimpleDirectoryReader(\"/mnt/workspace/custom_data/\").load_data()\n", "documents = SimpleDirectoryReader(\"/mnt/workspace/custom_data/\").load_data()\n",
@@ -192,11 +270,7 @@
"# do query\n", "# do query\n",
"response = query_engine.query(\"西安较大的校训是什么\")\n", "response = query_engine.query(\"西安较大的校训是什么\")\n",
"print(response)\n" "print(response)\n"
], ]
"metadata": {
"collapsed": false
},
"id": "eef67659e94045c5"
} }
], ],
"metadata": { "metadata": {