"vscode:/vscode.git/clone" did not exist on "c7c79b16cd34cc47d9a49f4bcdd1c535131ab7b3"
openai_api_embeddings.ipynb 4.98 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
    "## Launch A Server\n",
    "\n",
23
    "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
24
25
26
27
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
28
   "execution_count": null,
29
   "metadata": {},
Chayenne's avatar
Chayenne committed
30
   "outputs": [],
Chayenne's avatar
Chayenne committed
31
   "source": [
32
33
34
35
36
37
    "from sglang.utils import (\n",
    "    execute_shell_command,\n",
    "    wait_for_server,\n",
    "    terminate_process,\n",
    "    print_highlight,\n",
    ")\n",
38
    "\n",
Chayenne's avatar
Chayenne committed
39
40
    "embedding_process = execute_shell_command(\n",
    "    \"\"\"\n",
41
    "python -m sglang.launch_server --model-path Alibaba-NLP/gte-Qwen2-7B-instruct \\\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
42
    "    --port 30000 --host 0.0.0.0 --is-embedding\n",
Chayenne's avatar
Chayenne committed
43
44
    "\"\"\"\n",
    ")\n",
Chayenne's avatar
Chayenne committed
45
    "\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
46
    "wait_for_server(\"http://localhost:30000\")"
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",
Lianmin Zheng's avatar
Lianmin Zheng committed
66
    "curl_text = f\"\"\"curl -s http://localhost:30000/v1/embeddings \\\n",
Chayenne's avatar
Chayenne committed
67
68
69
70
71
72
    "  -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",
73
    "print_highlight(f\"Text embedding (first 10): {text_embedding[:10]}\")"
Chayenne's avatar
Chayenne committed
74
75
76
77
78
79
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Lianmin Zheng's avatar
Lianmin Zheng committed
80
    "## Using Python Requests"
Chayenne's avatar
Chayenne committed
81
82
83
84
85
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
86
   "metadata": {},
Chayenne's avatar
Chayenne committed
87
88
89
90
91
92
93
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "text = \"Once upon a time\"\n",
    "\n",
    "response = requests.post(\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
94
    "    \"http://localhost:30000/v1/embeddings\",\n",
Chayenne's avatar
Chayenne committed
95
    "    json={\"model\": \"Alibaba-NLP/gte-Qwen2-7B-instruct\", \"input\": text},\n",
Chayenne's avatar
Chayenne committed
96
97
98
99
100
101
102
103
104
105
106
107
    ")\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
108
109
110
111
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
112
   "execution_count": null,
113
   "metadata": {},
Chayenne's avatar
Chayenne committed
114
   "outputs": [],
Chayenne's avatar
Chayenne committed
115
116
117
   "source": [
    "import openai\n",
    "\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
118
    "client = openai.Client(base_url=\"http://127.0.0.1:30000/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",
127
    "print_highlight(f\"Text embedding (first 10): {embedding}\")"
Chayenne's avatar
Chayenne committed
128
129
130
131
132
133
134
135
136
137
138
139
140
   ]
  },
  {
   "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
141
   "execution_count": null,
142
   "metadata": {},
Chayenne's avatar
Chayenne committed
143
   "outputs": [],
Chayenne's avatar
Chayenne committed
144
145
146
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",
    "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
154
    "curl_ids = f\"\"\"curl -s http://localhost:30000/v1/embeddings \\\n",
Chayenne's avatar
Chayenne committed
155
156
157
158
159
160
    "  -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",
161
    "print_highlight(f\"Input IDs embedding (first 10): {input_ids_embedding[:10]}\")"
Chayenne's avatar
Chayenne committed
162
   ]
163
164
165
  },
  {
   "cell_type": "code",
166
167
   "execution_count": null,
   "metadata": {},
Chayenne's avatar
Chayenne committed
168
   "outputs": [],
169
170
171
   "source": [
    "terminate_process(embedding_process)"
   ]
Chayenne's avatar
Chayenne committed
172
173
174
175
176
177
178
179
180
181
182
183
  }
 ],
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
184
   "pygments_lexer": "ipython3"
Chayenne's avatar
Chayenne committed
185
186
187
188
189
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}