python/resources/embeddings/index.md +0 −257 deleted
File Deleted View Diff
1# Embeddings
2
3## Create embeddings
4
5`embeddings.create(EmbeddingCreateParams**kwargs) -> CreateEmbeddingResponse`
6
7**post** `/embeddings`
8
9Creates an embedding vector representing the input text.
10
11### Parameters
12
13- `input: Union[str, Sequence[str], Iterable[int], Iterable[Iterable[int]]]`
14
15 Input text to embed, encoded as a string or array of tokens. To embed multiple inputs in a single request, pass an array of strings or array of token arrays. The input must not exceed the max input tokens for the model (8192 tokens for all embedding models), cannot be an empty string, and any array must be 2048 dimensions or less. [Example Python code](https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken) for counting tokens. In addition to the per-input token limit, all embedding models enforce a maximum of 300,000 tokens summed across all inputs in a single request.
16
17 - `str`
18
19 The string that will be turned into an embedding.
20
21 - `Sequence[str]`
22
23 The array of strings that will be turned into an embedding.
24
25 - `Iterable[int]`
26
27 The array of integers that will be turned into an embedding.
28
29 - `Iterable[Iterable[int]]`
30
31 The array of arrays containing integers that will be turned into an embedding.
32
33- `model: Union[str, EmbeddingModel]`
34
35 ID of the model to use. You can use the [List models](https://platform.openai.com/docs/api-reference/models/list) API to see all of your available models, or see our [Model overview](https://platform.openai.com/docs/models) for descriptions of them.
36
37 - `str`
38
39 - `Literal["text-embedding-ada-002", "text-embedding-3-small", "text-embedding-3-large"]`
40
41 - `"text-embedding-ada-002"`
42
43 - `"text-embedding-3-small"`
44
45 - `"text-embedding-3-large"`
46
47- `dimensions: Optional[int]`
48
49 The number of dimensions the resulting output embeddings should have. Only supported in `text-embedding-3` and later models.
50
51- `encoding_format: Optional[Literal["float", "base64"]]`
52
53 The format to return the embeddings in. Can be either `float` or [`base64`](https://pypi.org/project/pybase64/).
54
55 - `"float"`
56
57 - `"base64"`
58
59- `user: Optional[str]`
60
61 A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
62
63### Returns
64
65- `class CreateEmbeddingResponse: …`
66
67 - `data: List[Embedding]`
68
69 The list of embeddings generated by the model.
70
71 - `embedding: List[float]`
72
73 The embedding vector, which is a list of floats. The length of vector depends on the model as listed in the [embedding guide](https://platform.openai.com/docs/guides/embeddings).
74
75 - `index: int`
76
77 The index of the embedding in the list of embeddings.
78
79 - `object: Literal["embedding"]`
80
81 The object type, which is always "embedding".
82
83 - `"embedding"`
84
85 - `model: str`
86
87 The name of the model used to generate the embedding.
88
89 - `object: Literal["list"]`
90
91 The object type, which is always "list".
92
93 - `"list"`
94
95 - `usage: Usage`
96
97 The usage information for the request.
98
99 - `prompt_tokens: int`
100
101 The number of tokens used by the prompt.
102
103 - `total_tokens: int`
104
105 The total number of tokens used by the request.
106
107### Example
108
109```python
110import os
111from openai import OpenAI
112
113client = OpenAI(
114 api_key=os.environ.get("OPENAI_API_KEY"), # This is the default and can be omitted
115)
116create_embedding_response = client.embeddings.create(
117 input="The quick brown fox jumped over the lazy dog",
118 model="text-embedding-3-small",
119)
120print(create_embedding_response.data)
121```
122
123#### Response
124
125```json
126{
127 "data": [
128 {
129 "embedding": [
130 0
131 ],
132 "index": 0,
133 "object": "embedding"
134 }
135 ],
136 "model": "model",
137 "object": "list",
138 "usage": {
139 "prompt_tokens": 0,
140 "total_tokens": 0
141 }
142}
143```
144
145### Example
146
147```python
148from openai import OpenAI
149client = OpenAI()
150
151client.embeddings.create(
152 model="text-embedding-ada-002",
153 input="The food was delicious and the waiter...",
154 encoding_format="float"
155)
156```
157
158#### Response
159
160```json
161{
162 "object": "list",
163 "data": [
164 {
165 "object": "embedding",
166 "embedding": [
167 0.0023064255,
168 -0.009327292,
169 .... (1536 floats total for ada-002)
170 -0.0028842222,
171 ],
172 "index": 0
173 }
174 ],
175 "model": "text-embedding-ada-002",
176 "usage": {
177 "prompt_tokens": 8,
178 "total_tokens": 8
179 }
180}
181```
182
183## Domain Types
184
185### Create Embedding Response
186
187- `class CreateEmbeddingResponse: …`
188
189 - `data: List[Embedding]`
190
191 The list of embeddings generated by the model.
192
193 - `embedding: List[float]`
194
195 The embedding vector, which is a list of floats. The length of vector depends on the model as listed in the [embedding guide](https://platform.openai.com/docs/guides/embeddings).
196
197 - `index: int`
198
199 The index of the embedding in the list of embeddings.
200
201 - `object: Literal["embedding"]`
202
203 The object type, which is always "embedding".
204
205 - `"embedding"`
206
207 - `model: str`
208
209 The name of the model used to generate the embedding.
210
211 - `object: Literal["list"]`
212
213 The object type, which is always "list".
214
215 - `"list"`
216
217 - `usage: Usage`
218
219 The usage information for the request.
220
221 - `prompt_tokens: int`
222
223 The number of tokens used by the prompt.
224
225 - `total_tokens: int`
226
227 The total number of tokens used by the request.
228
229### Embedding
230
231- `class Embedding: …`
232
233 Represents an embedding vector returned by embedding endpoint.
234
235 - `embedding: List[float]`
236
237 The embedding vector, which is a list of floats. The length of vector depends on the model as listed in the [embedding guide](https://platform.openai.com/docs/guides/embeddings).
238
239 - `index: int`
240
241 The index of the embedding in the list of embeddings.
242
243 - `object: Literal["embedding"]`
244
245 The object type, which is always "embedding".
246
247 - `"embedding"`
248
249### Embedding Model
250
251- `Literal["text-embedding-ada-002", "text-embedding-3-small", "text-embedding-3-large"]`
252
253 - `"text-embedding-ada-002"`
254
255 - `"text-embedding-3-small"`
256
257 - `"text-embedding-3-large"`