embedding_model.ipynb 5.87 KB
Newer Older
Chayenne's avatar
Chayenne committed
1
2
3
4
5
6
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Chayenne's avatar
Chayenne committed
7
8
9
10
11
12
    "# Embedding Model\n",
    "\n",
    "SGLang supports embedding models in the same way as completion models. Here are some example models:\n",
    "\n",
    "- [intfloat/e5-mistral-7b-instruct](https://huggingface.co/intfloat/e5-mistral-7b-instruct)\n",
    "- [Alibaba-NLP/gte-Qwen2-7B-instruct](https://huggingface.co/Alibaba-NLP/gte-Qwen2-7B-instruct)\n"
Chayenne's avatar
Chayenne committed
13
14
15
16
17
18
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Chayenne's avatar
Chayenne committed
19
20
21
    "## Launch A Server\n",
    "\n",
    "The following code is equivalent to running this in the shell:\n",
Chayenne's avatar
Chayenne committed
22
    "\n",
Chayenne's avatar
Chayenne committed
23
24
    "```bash\n",
    "python -m sglang.launch_server --model-path Alibaba-NLP/gte-Qwen2-7B-instruct \\\n",
25
    "    --port 30010 --host 0.0.0.0 --is-embedding\n",
Chayenne's avatar
Chayenne committed
26
27
28
    "```\n",
    "\n",
    "Remember to add `--is-embedding` to the command."
Chayenne's avatar
Chayenne committed
29
30
31
32
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
33
   "execution_count": null,
Chayenne's avatar
Chayenne committed
34
35
36
37
38
39
40
41
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-01T02:47:32.337369Z",
     "iopub.status.busy": "2024-11-01T02:47:32.337032Z",
     "iopub.status.idle": "2024-11-01T02:47:59.540926Z",
     "shell.execute_reply": "2024-11-01T02:47:59.539861Z"
    }
   },
Chayenne's avatar
Chayenne committed
42
   "outputs": [],
Chayenne's avatar
Chayenne committed
43
   "source": [
44
45
46
47
48
49
    "from sglang.utils import (\n",
    "    execute_shell_command,\n",
    "    wait_for_server,\n",
    "    terminate_process,\n",
    "    print_highlight,\n",
    ")\n",
50
    "\n",
Chayenne's avatar
Chayenne committed
51
52
    "embedding_process = execute_shell_command(\n",
    "    \"\"\"\n",
53
    "python -m sglang.launch_server --model-path Alibaba-NLP/gte-Qwen2-7B-instruct \\\n",
54
    "    --port 30010 --host 0.0.0.0 --is-embedding\n",
Chayenne's avatar
Chayenne committed
55
56
    "\"\"\"\n",
    ")\n",
Chayenne's avatar
Chayenne committed
57
    "\n",
58
    "wait_for_server(\"http://localhost:30010\")"
Chayenne's avatar
Chayenne committed
59
60
61
62
63
64
65
66
67
68
69
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Use Curl"
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
70
   "execution_count": null,
Chayenne's avatar
Chayenne committed
71
72
73
74
75
76
77
78
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-01T02:47:59.543958Z",
     "iopub.status.busy": "2024-11-01T02:47:59.543670Z",
     "iopub.status.idle": "2024-11-01T02:47:59.591699Z",
     "shell.execute_reply": "2024-11-01T02:47:59.590809Z"
    }
   },
Chayenne's avatar
Chayenne committed
79
   "outputs": [],
Chayenne's avatar
Chayenne committed
80
   "source": [
Chayenne's avatar
Chayenne committed
81
82
83
    "import subprocess, json\n",
    "\n",
    "text = \"Once upon a time\"\n",
Chayenne's avatar
Chayenne committed
84
    "\n",
Chayenne's avatar
Chayenne committed
85
    "curl_text = f\"\"\"curl -s http://localhost:30010/v1/embeddings \\\n",
Chayenne's avatar
Chayenne committed
86
87
    "  -H \"Content-Type: application/json\" \\\n",
    "  -H \"Authorization: Bearer None\" \\\n",
Chayenne's avatar
Chayenne committed
88
89
90
91
92
93
    "  -d '{{\"model\": \"Alibaba-NLP/gte-Qwen2-7B-instruct\", \"input\": \"{text}\"}}'\"\"\"\n",
    "\n",
    "text_embedding = json.loads(subprocess.check_output(curl_text, shell=True))[\"data\"][0][\n",
    "    \"embedding\"\n",
    "]\n",
    "\n",
94
    "print_highlight(f\"Text embedding (first 10): {text_embedding[:10]}\")"
Chayenne's avatar
Chayenne committed
95
96
97
98
99
100
101
102
103
104
105
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using OpenAI Compatible API"
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
106
   "execution_count": null,
Chayenne's avatar
Chayenne committed
107
108
109
110
111
112
113
114
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-01T02:47:59.594229Z",
     "iopub.status.busy": "2024-11-01T02:47:59.594049Z",
     "iopub.status.idle": "2024-11-01T02:48:00.006233Z",
     "shell.execute_reply": "2024-11-01T02:48:00.005255Z"
    }
   },
Chayenne's avatar
Chayenne committed
115
   "outputs": [],
Chayenne's avatar
Chayenne committed
116
117
118
   "source": [
    "import openai\n",
    "\n",
Chayenne's avatar
Chayenne committed
119
    "client = openai.Client(base_url=\"http://127.0.0.1:30010/v1\", api_key=\"None\")\n",
Chayenne's avatar
Chayenne committed
120
121
122
123
    "\n",
    "# Text embedding example\n",
    "response = client.embeddings.create(\n",
    "    model=\"Alibaba-NLP/gte-Qwen2-7B-instruct\",\n",
Chayenne's avatar
Chayenne committed
124
    "    input=text,\n",
Chayenne's avatar
Chayenne committed
125
126
127
    ")\n",
    "\n",
    "embedding = response.data[0].embedding[:10]\n",
128
    "print_highlight(f\"Text embedding (first 10): {embedding}\")"
Chayenne's avatar
Chayenne committed
129
130
131
132
133
134
135
136
137
138
139
140
141
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using Input IDs\n",
    "\n",
    "SGLang also supports `input_ids` as input to get the embedding."
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
142
   "execution_count": null,
Chayenne's avatar
Chayenne committed
143
144
145
146
147
148
149
150
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-01T02:48:00.008858Z",
     "iopub.status.busy": "2024-11-01T02:48:00.008689Z",
     "iopub.status.idle": "2024-11-01T02:48:01.872542Z",
     "shell.execute_reply": "2024-11-01T02:48:01.871573Z"
    }
   },
Chayenne's avatar
Chayenne committed
151
   "outputs": [],
Chayenne's avatar
Chayenne committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
   "source": [
    "import json\n",
    "import os\n",
    "from transformers import AutoTokenizer\n",
    "\n",
    "os.environ[\"TOKENIZERS_PARALLELISM\"] = \"false\"\n",
    "\n",
    "tokenizer = AutoTokenizer.from_pretrained(\"Alibaba-NLP/gte-Qwen2-7B-instruct\")\n",
    "input_ids = tokenizer.encode(text)\n",
    "\n",
    "curl_ids = f\"\"\"curl -s http://localhost:30010/v1/embeddings \\\n",
    "  -H \"Content-Type: application/json\" \\\n",
    "  -H \"Authorization: Bearer None\" \\\n",
    "  -d '{{\"model\": \"Alibaba-NLP/gte-Qwen2-7B-instruct\", \"input\": {json.dumps(input_ids)}}}'\"\"\"\n",
    "\n",
    "input_ids_embedding = json.loads(subprocess.check_output(curl_ids, shell=True))[\"data\"][\n",
    "    0\n",
    "][\"embedding\"]\n",
    "\n",
171
    "print_highlight(f\"Input IDs embedding (first 10): {input_ids_embedding[:10]}\")"
Chayenne's avatar
Chayenne committed
172
   ]
173
174
175
  },
  {
   "cell_type": "code",
176
   "execution_count": 5,
Chayenne's avatar
Chayenne committed
177
178
179
180
181
182
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-01T02:48:01.875204Z",
     "iopub.status.busy": "2024-11-01T02:48:01.874915Z",
     "iopub.status.idle": "2024-11-01T02:48:02.193734Z",
     "shell.execute_reply": "2024-11-01T02:48:02.192158Z"
183
    }
Chayenne's avatar
Chayenne committed
184
185
   },
   "outputs": [],
186
187
188
   "source": [
    "terminate_process(embedding_process)"
   ]
Chayenne's avatar
Chayenne committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "AlphaMeemory",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}