openai_api_embeddings.ipynb 5.38 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",
12
    "This tutorial covers the embedding APIs for embedding models. For a list of the supported models see the [corresponding overview page](https://docs.sglang.ai/supported_models/embedding_models.html)\n"
Chayenne's avatar
Chayenne committed
13
14
15
16
17
18
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Chayenne's avatar
Chayenne committed
19
20
    "## Launch A Server\n",
    "\n",
21
    "Launch the server in your terminal and wait for it to initialize. Remember to add `--is-embedding` to the command."
Chayenne's avatar
Chayenne committed
22
23
24
25
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
26
   "execution_count": null,
27
   "metadata": {},
Chayenne's avatar
Chayenne committed
28
   "outputs": [],
Chayenne's avatar
Chayenne committed
29
   "source": [
30
31
32
33
34
35
36
37
    "from sglang.test.test_utils import is_in_ci\n",
    "\n",
    "if is_in_ci():\n",
    "    from patch import launch_server_cmd\n",
    "else:\n",
    "    from sglang.utils import launch_server_cmd\n",
    "\n",
    "from sglang.utils import wait_for_server, print_highlight, terminate_process\n",
38
    "\n",
39
    "embedding_process, port = launch_server_cmd(\n",
Chayenne's avatar
Chayenne committed
40
    "    \"\"\"\n",
41
    "python3 -m sglang.launch_server --model-path Alibaba-NLP/gte-Qwen2-1.5B-instruct \\\n",
42
    "    --host 0.0.0.0 --is-embedding\n",
Chayenne's avatar
Chayenne committed
43
44
    "\"\"\"\n",
    ")\n",
Chayenne's avatar
Chayenne committed
45
    "\n",
46
    "wait_for_server(f\"http://localhost:{port}\")"
Chayenne's avatar
Chayenne committed
47
48
49
50
51
52
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Chayenne's avatar
Chayenne committed
53
    "## Using cURL"
Chayenne's avatar
Chayenne committed
54
55
56
57
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
58
   "execution_count": null,
59
   "metadata": {},
Chayenne's avatar
Chayenne committed
60
   "outputs": [],
Chayenne's avatar
Chayenne committed
61
   "source": [
Chayenne's avatar
Chayenne committed
62
63
64
    "import subprocess, json\n",
    "\n",
    "text = \"Once upon a time\"\n",
Chayenne's avatar
Chayenne committed
65
    "\n",
66
    "curl_text = f\"\"\"curl -s http://localhost:{port}/v1/embeddings \\\n",
67
    "  -H \"Content-Type: application/json\" \\\n",
68
    "  -d '{{\"model\": \"Alibaba-NLP/gte-Qwen2-1.5B-instruct\", \"input\": \"{text}\"}}'\"\"\"\n",
Chayenne's avatar
Chayenne committed
69
    "\n",
70
71
72
73
74
    "result = subprocess.check_output(curl_text, shell=True)\n",
    "\n",
    "print(result)\n",
    "\n",
    "text_embedding = json.loads(result)[\"data\"][0][\"embedding\"]\n",
Chayenne's avatar
Chayenne committed
75
    "\n",
76
    "print_highlight(f\"Text embedding (first 10): {text_embedding[:10]}\")"
Chayenne's avatar
Chayenne committed
77
78
79
80
81
82
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Lianmin Zheng's avatar
Lianmin Zheng committed
83
    "## Using Python Requests"
Chayenne's avatar
Chayenne committed
84
85
86
87
88
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
89
   "metadata": {},
Chayenne's avatar
Chayenne committed
90
91
92
93
94
95
96
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "text = \"Once upon a time\"\n",
    "\n",
    "response = requests.post(\n",
97
    "    f\"http://localhost:{port}/v1/embeddings\",\n",
98
    "    json={\"model\": \"Alibaba-NLP/gte-Qwen2-1.5B-instruct\", \"input\": text},\n",
Chayenne's avatar
Chayenne committed
99
100
101
102
103
104
105
106
107
108
109
110
    ")\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
111
112
113
114
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
115
   "execution_count": null,
116
   "metadata": {},
Chayenne's avatar
Chayenne committed
117
   "outputs": [],
Chayenne's avatar
Chayenne committed
118
119
120
   "source": [
    "import openai\n",
    "\n",
121
    "client = openai.Client(base_url=f\"http://127.0.0.1:{port}/v1\", api_key=\"None\")\n",
Chayenne's avatar
Chayenne committed
122
123
124
    "\n",
    "# Text embedding example\n",
    "response = client.embeddings.create(\n",
125
    "    model=\"Alibaba-NLP/gte-Qwen2-1.5B-instruct\",\n",
Chayenne's avatar
Chayenne committed
126
    "    input=text,\n",
Chayenne's avatar
Chayenne committed
127
128
129
    ")\n",
    "\n",
    "embedding = response.data[0].embedding[:10]\n",
130
    "print_highlight(f\"Text embedding (first 10): {embedding}\")"
Chayenne's avatar
Chayenne committed
131
132
133
134
135
136
137
138
139
140
141
142
143
   ]
  },
  {
   "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
144
   "execution_count": null,
145
   "metadata": {},
Chayenne's avatar
Chayenne committed
146
   "outputs": [],
Chayenne's avatar
Chayenne committed
147
148
149
150
151
152
153
   "source": [
    "import json\n",
    "import os\n",
    "from transformers import AutoTokenizer\n",
    "\n",
    "os.environ[\"TOKENIZERS_PARALLELISM\"] = \"false\"\n",
    "\n",
154
    "tokenizer = AutoTokenizer.from_pretrained(\"Alibaba-NLP/gte-Qwen2-1.5B-instruct\")\n",
Chayenne's avatar
Chayenne committed
155
156
    "input_ids = tokenizer.encode(text)\n",
    "\n",
157
    "curl_ids = f\"\"\"curl -s http://localhost:{port}/v1/embeddings \\\n",
158
    "  -H \"Content-Type: application/json\" \\\n",
159
    "  -d '{{\"model\": \"Alibaba-NLP/gte-Qwen2-1.5B-instruct\", \"input\": {json.dumps(input_ids)}}}'\"\"\"\n",
Chayenne's avatar
Chayenne committed
160
161
162
163
164
    "\n",
    "input_ids_embedding = json.loads(subprocess.check_output(curl_ids, shell=True))[\"data\"][\n",
    "    0\n",
    "][\"embedding\"]\n",
    "\n",
165
    "print_highlight(f\"Input IDs embedding (first 10): {input_ids_embedding[:10]}\")"
Chayenne's avatar
Chayenne committed
166
   ]
167
168
169
  },
  {
   "cell_type": "code",
170
171
   "execution_count": null,
   "metadata": {},
Chayenne's avatar
Chayenne committed
172
   "outputs": [],
173
   "source": [
174
    "terminate_process(embedding_process)"
175
   ]
176
177
178
179
180
181
182
183
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Multi-Modal Embedding Model\n",
    "Please refer to [Multi-Modal Embedding Model](../supported_models/embedding_models.md)"
   ]
Chayenne's avatar
Chayenne committed
184
185
186
187
188
189
190
191
192
193
194
195
  }
 ],
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
196
   "pygments_lexer": "ipython3"
Chayenne's avatar
Chayenne committed
197
198
199
200
201
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}