"vscode:/vscode.git/clone" did not exist on "4057ea82c9a11f4f2379189c390f4a4f88f73854"
openai_embedding_api.ipynb 6.4 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
    "# OpenAI APIs - Embedding\n",
Chayenne's avatar
Chayenne committed
8
9
10
11
12
    "\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
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Chayenne's avatar
Chayenne committed
65
    "## Using cURL"
Chayenne's avatar
Chayenne committed
66
67
68
69
   ]
  },
  {
   "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
86
87
88
89
90
91
    "curl_text = f\"\"\"curl -s http://localhost:30010/v1/embeddings \\\n",
    "  -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",
92
    "print_highlight(f\"Text embedding (first 10): {text_embedding[:10]}\")"
Chayenne's avatar
Chayenne committed
93
94
95
96
97
98
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Chayenne's avatar
Chayenne committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
    "## Using OpenAI Compatible API w/ Requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "text = \"Once upon a time\"\n",
    "\n",
    "response = requests.post(\n",
    "    \"http://localhost:30010/v1/embeddings\",\n",
    "    json={\n",
    "        \"model\": \"Alibaba-NLP/gte-Qwen2-7B-instruct\",\n",
    "        \"input\": text\n",
    "    }\n",
    ")\n",
    "\n",
    "text_embedding = response.json()[\"data\"][0][\"embedding\"]\n",
    "\n",
    "print_highlight(f\"Text embedding (first 10): {text_embedding[:10]}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using OpenAI Python Client"
Chayenne's avatar
Chayenne committed
130
131
132
133
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
134
   "execution_count": null,
Chayenne's avatar
Chayenne committed
135
136
137
138
139
140
141
142
   "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
143
   "outputs": [],
Chayenne's avatar
Chayenne committed
144
145
146
   "source": [
    "import openai\n",
    "\n",
Chayenne's avatar
Chayenne committed
147
    "client = openai.Client(base_url=\"http://127.0.0.1:30010/v1\", api_key=\"None\")\n",
Chayenne's avatar
Chayenne committed
148
149
150
151
    "\n",
    "# Text embedding example\n",
    "response = client.embeddings.create(\n",
    "    model=\"Alibaba-NLP/gte-Qwen2-7B-instruct\",\n",
Chayenne's avatar
Chayenne committed
152
    "    input=text,\n",
Chayenne's avatar
Chayenne committed
153
154
155
    ")\n",
    "\n",
    "embedding = response.data[0].embedding[:10]\n",
156
    "print_highlight(f\"Text embedding (first 10): {embedding}\")"
Chayenne's avatar
Chayenne committed
157
158
159
160
161
162
163
164
165
166
167
168
169
   ]
  },
  {
   "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
170
   "execution_count": null,
Chayenne's avatar
Chayenne committed
171
172
173
174
175
176
177
178
   "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
179
   "outputs": [],
Chayenne's avatar
Chayenne committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
   "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",
    "  -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",
197
    "print_highlight(f\"Input IDs embedding (first 10): {input_ids_embedding[:10]}\")"
Chayenne's avatar
Chayenne committed
198
   ]
199
200
201
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
202
   "execution_count": null,
Chayenne's avatar
Chayenne committed
203
204
205
206
207
208
   "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"
209
    }
Chayenne's avatar
Chayenne committed
210
211
   },
   "outputs": [],
212
213
214
   "source": [
    "terminate_process(embedding_process)"
   ]
Chayenne's avatar
Chayenne committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
  }
 ],
 "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
}