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