openai_api_embeddings.ipynb 6.61 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
    "\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
9
10
    "SGLang provides OpenAI-compatible APIs to enable a smooth transition from OpenAI services to self-hosted local models.\n",
    "A complete reference for the API is available in the [OpenAI API Reference](https://platform.openai.com/docs/guides/embeddings).\n",
Chayenne's avatar
Chayenne committed
11
    "\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
12
13
14
    "This tutorial covers the embedding APIs for embedding models, such as  \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
15
16
17
18
19
20
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Chayenne's avatar
Chayenne committed
21
22
23
    "## Launch A Server\n",
    "\n",
    "The following code is equivalent to running this in the shell:\n",
Chayenne's avatar
Chayenne committed
24
    "\n",
Chayenne's avatar
Chayenne committed
25
26
    "```bash\n",
    "python -m sglang.launch_server --model-path Alibaba-NLP/gte-Qwen2-7B-instruct \\\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
27
    "    --port 30000 --host 0.0.0.0 --is-embedding\n",
Chayenne's avatar
Chayenne committed
28
29
30
    "```\n",
    "\n",
    "Remember to add `--is-embedding` to the command."
Chayenne's avatar
Chayenne committed
31
32
33
34
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
35
   "execution_count": null,
Chayenne's avatar
Chayenne committed
36
37
38
39
40
41
42
43
   "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
44
   "outputs": [],
Chayenne's avatar
Chayenne committed
45
   "source": [
46
47
48
49
50
51
    "from sglang.utils import (\n",
    "    execute_shell_command,\n",
    "    wait_for_server,\n",
    "    terminate_process,\n",
    "    print_highlight,\n",
    ")\n",
52
    "\n",
Chayenne's avatar
Chayenne committed
53
54
    "embedding_process = execute_shell_command(\n",
    "    \"\"\"\n",
55
    "python -m sglang.launch_server --model-path Alibaba-NLP/gte-Qwen2-7B-instruct \\\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
56
    "    --port 30000 --host 0.0.0.0 --is-embedding\n",
Chayenne's avatar
Chayenne committed
57
58
    "\"\"\"\n",
    ")\n",
Chayenne's avatar
Chayenne committed
59
    "\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
60
    "wait_for_server(\"http://localhost:30000\")"
Chayenne's avatar
Chayenne committed
61
62
63
64
65
66
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Chayenne's avatar
Chayenne committed
67
    "## Using cURL"
Chayenne's avatar
Chayenne committed
68
69
70
71
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
72
   "execution_count": null,
Chayenne's avatar
Chayenne committed
73
74
75
76
77
78
79
80
   "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
81
   "outputs": [],
Chayenne's avatar
Chayenne committed
82
   "source": [
Chayenne's avatar
Chayenne committed
83
84
85
    "import subprocess, json\n",
    "\n",
    "text = \"Once upon a time\"\n",
Chayenne's avatar
Chayenne committed
86
    "\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
87
    "curl_text = f\"\"\"curl -s http://localhost:30000/v1/embeddings \\\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
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Lianmin Zheng's avatar
Lianmin Zheng committed
101
    "## Using Python Requests"
Chayenne's avatar
Chayenne committed
102
103
104
105
106
107
108
109
110
111
112
113
114
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "text = \"Once upon a time\"\n",
    "\n",
    "response = requests.post(\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
115
    "    \"http://localhost:30000/v1/embeddings\",\n",
Chayenne's avatar
Chayenne committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
    "    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
132
133
134
135
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
136
   "execution_count": null,
Chayenne's avatar
Chayenne committed
137
138
139
140
141
142
143
144
   "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
145
   "outputs": [],
Chayenne's avatar
Chayenne committed
146
147
148
   "source": [
    "import openai\n",
    "\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
149
    "client = openai.Client(base_url=\"http://127.0.0.1:30000/v1\", api_key=\"None\")\n",
Chayenne's avatar
Chayenne committed
150
151
152
153
    "\n",
    "# Text embedding example\n",
    "response = client.embeddings.create(\n",
    "    model=\"Alibaba-NLP/gte-Qwen2-7B-instruct\",\n",
Chayenne's avatar
Chayenne committed
154
    "    input=text,\n",
Chayenne's avatar
Chayenne committed
155
156
157
    ")\n",
    "\n",
    "embedding = response.data[0].embedding[:10]\n",
158
    "print_highlight(f\"Text embedding (first 10): {embedding}\")"
Chayenne's avatar
Chayenne committed
159
160
161
162
163
164
165
166
167
168
169
170
171
   ]
  },
  {
   "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
172
   "execution_count": null,
Chayenne's avatar
Chayenne committed
173
174
175
176
177
178
179
180
   "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
181
   "outputs": [],
Chayenne's avatar
Chayenne committed
182
183
184
185
186
187
188
189
190
191
   "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",
Lianmin Zheng's avatar
Lianmin Zheng committed
192
    "curl_ids = f\"\"\"curl -s http://localhost:30000/v1/embeddings \\\n",
Chayenne's avatar
Chayenne committed
193
194
195
196
197
198
    "  -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",
199
    "print_highlight(f\"Input IDs embedding (first 10): {input_ids_embedding[:10]}\")"
Chayenne's avatar
Chayenne committed
200
   ]
201
202
203
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
204
   "execution_count": 6,
Chayenne's avatar
Chayenne committed
205
206
207
208
209
210
   "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"
211
    }
Chayenne's avatar
Chayenne committed
212
213
   },
   "outputs": [],
214
215
216
   "source": [
    "terminate_process(embedding_process)"
   ]
Chayenne's avatar
Chayenne committed
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
  }
 ],
 "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
}