embedding_model.ipynb 5.77 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
22
23
24
25
26
27
    "## Launch A Server\n",
    "\n",
    "The following code is equivalent to running this in the shell:\n",
    "```bash\n",
    "python -m sglang.launch_server --model-path Alibaba-NLP/gte-Qwen2-7B-instruct \\\n",
    "    --port 30010 --host 0.0.0.0 --is-embedding --log-level error\n",
    "```\n",
    "\n",
    "Remember to add `--is-embedding` to the command."
Chayenne's avatar
Chayenne committed
28
29
30
31
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
32
   "execution_count": 7,
Chayenne's avatar
Chayenne committed
33
34
35
36
37
38
39
40
41
42
43
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Embedding server is ready. Proceeding with the next steps.\n"
     ]
    }
   ],
   "source": [
44
45
    "from sglang.utils import execute_shell_command, wait_for_server, terminate_process\n",
    "\n",
Chayenne's avatar
Chayenne committed
46
47
    "embedding_process = execute_shell_command(\n",
    "    \"\"\"\n",
48
49
    "python -m sglang.launch_server --model-path Alibaba-NLP/gte-Qwen2-7B-instruct \\\n",
    "    --port 30010 --host 0.0.0.0 --is-embedding --log-level error\n",
Chayenne's avatar
Chayenne committed
50
51
    "\"\"\"\n",
    ")\n",
Chayenne's avatar
Chayenne committed
52
    "\n",
53
    "wait_for_server(\"http://localhost:30010\")\n",
Chayenne's avatar
Chayenne committed
54
55
56
57
58
59
60
61
62
63
64
65
66
    "\n",
    "print(\"Embedding server is ready. Proceeding with the next steps.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Use Curl"
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
67
   "execution_count": 8,
Chayenne's avatar
Chayenne committed
68
69
70
71
72
73
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
Chayenne's avatar
Chayenne committed
74
      "Text embedding (first 10): [0.0083160400390625, 0.0006804466247558594, -0.00809478759765625, -0.0006995201110839844, 0.0143890380859375, -0.0090179443359375, 0.01238250732421875, 0.00209808349609375, 0.0062103271484375, -0.003047943115234375]\n"
Chayenne's avatar
Chayenne committed
75
76
77
78
     ]
    }
   ],
   "source": [
Chayenne's avatar
Chayenne committed
79
80
81
    "import subprocess, json\n",
    "\n",
    "text = \"Once upon a time\"\n",
Chayenne's avatar
Chayenne committed
82
    "\n",
Chayenne's avatar
Chayenne committed
83
    "curl_text = f\"\"\"curl -s http://localhost:30010/v1/embeddings \\\n",
Chayenne's avatar
Chayenne committed
84
85
    "  -H \"Content-Type: application/json\" \\\n",
    "  -H \"Authorization: Bearer None\" \\\n",
Chayenne's avatar
Chayenne committed
86
87
88
89
90
91
92
    "  -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",
    "print(f\"Text embedding (first 10): {text_embedding[:10]}\")"
Chayenne's avatar
Chayenne committed
93
94
95
96
97
98
99
100
101
102
103
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using OpenAI Compatible API"
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
104
   "execution_count": 9,
Chayenne's avatar
Chayenne committed
105
106
107
108
109
110
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
Chayenne's avatar
Chayenne committed
111
      "Text embedding (first 10): [0.00829315185546875, 0.0007004737854003906, -0.00809478759765625, -0.0006799697875976562, 0.01438140869140625, -0.00897979736328125, 0.0123748779296875, 0.0020923614501953125, 0.006195068359375, -0.0030498504638671875]\n"
Chayenne's avatar
Chayenne committed
112
113
114
115
116
117
     ]
    }
   ],
   "source": [
    "import openai\n",
    "\n",
Chayenne's avatar
Chayenne committed
118
    "client = openai.Client(base_url=\"http://127.0.0.1:30010/v1\", api_key=\"None\")\n",
Chayenne's avatar
Chayenne committed
119
120
121
122
    "\n",
    "# Text embedding example\n",
    "response = client.embeddings.create(\n",
    "    model=\"Alibaba-NLP/gte-Qwen2-7B-instruct\",\n",
Chayenne's avatar
Chayenne committed
123
    "    input=text,\n",
Chayenne's avatar
Chayenne committed
124
125
126
    ")\n",
    "\n",
    "embedding = response.data[0].embedding[:10]\n",
Chayenne's avatar
Chayenne committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
    "print(f\"Text embedding (first 10): {embedding}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using Input IDs\n",
    "\n",
    "SGLang also supports `input_ids` as input to get the embedding."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Input IDs embedding (first 10): [0.00829315185546875, 0.0007004737854003906, -0.00809478759765625, -0.0006799697875976562, 0.01438140869140625, -0.00897979736328125, 0.0123748779296875, 0.0020923614501953125, 0.006195068359375, -0.0030498504638671875]\n"
     ]
    }
   ],
   "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",
    "print(f\"Input IDs embedding (first 10): {input_ids_embedding[:10]}\")"
Chayenne's avatar
Chayenne committed
172
   ]
173
174
175
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
176
   "execution_count": 11,
177
178
179
180
181
   "metadata": {},
   "outputs": [],
   "source": [
    "terminate_process(embedding_process)"
   ]
Chayenne's avatar
Chayenne committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
  }
 ],
 "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
}