python/resources/embeddings/methods/create/index.md +0 −179 deleted
File Deleted View Diff
1## Create embeddings
2
3`embeddings.create(EmbeddingCreateParams**kwargs) -> CreateEmbeddingResponse`
4
5**post** `/embeddings`
6
7Creates an embedding vector representing the input text.
8
9### Parameters
10
11- `input: Union[str, Sequence[str], Iterable[int], Iterable[Iterable[int]]]`
12
13 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.
14
15 - `str`
16
17 The string that will be turned into an embedding.
18
19 - `Sequence[str]`
20
21 The array of strings that will be turned into an embedding.
22
23 - `Iterable[int]`
24
25 The array of integers that will be turned into an embedding.
26
27 - `Iterable[Iterable[int]]`
28
29 The array of arrays containing integers that will be turned into an embedding.
30
31- `model: Union[str, EmbeddingModel]`
32
33 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.
34
35 - `str`
36
37 - `Literal["text-embedding-ada-002", "text-embedding-3-small", "text-embedding-3-large"]`
38
39 - `"text-embedding-ada-002"`
40
41 - `"text-embedding-3-small"`
42
43 - `"text-embedding-3-large"`
44
45- `dimensions: Optional[int]`
46
47 The number of dimensions the resulting output embeddings should have. Only supported in `text-embedding-3` and later models.
48
49- `encoding_format: Optional[Literal["float", "base64"]]`
50
51 The format to return the embeddings in. Can be either `float` or [`base64`](https://pypi.org/project/pybase64/).
52
53 - `"float"`
54
55 - `"base64"`
56
57- `user: Optional[str]`
58
59 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).
60
61### Returns
62
63- `class CreateEmbeddingResponse: …`
64
65 - `data: List[Embedding]`
66
67 The list of embeddings generated by the model.
68
69 - `embedding: List[float]`
70
71 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).
72
73 - `index: int`
74
75 The index of the embedding in the list of embeddings.
76
77 - `object: Literal["embedding"]`
78
79 The object type, which is always "embedding".
80
81 - `"embedding"`
82
83 - `model: str`
84
85 The name of the model used to generate the embedding.
86
87 - `object: Literal["list"]`
88
89 The object type, which is always "list".
90
91 - `"list"`
92
93 - `usage: Usage`
94
95 The usage information for the request.
96
97 - `prompt_tokens: int`
98
99 The number of tokens used by the prompt.
100
101 - `total_tokens: int`
102
103 The total number of tokens used by the request.
104
105### Example
106
107```python
108import os
109from openai import OpenAI
110
111client = OpenAI(
112 api_key=os.environ.get("OPENAI_API_KEY"), # This is the default and can be omitted
113)
114create_embedding_response = client.embeddings.create(
115 input="The quick brown fox jumped over the lazy dog",
116 model="text-embedding-3-small",
117)
118print(create_embedding_response.data)
119```
120
121#### Response
122
123```json
124{
125 "data": [
126 {
127 "embedding": [
128 0
129 ],
130 "index": 0,
131 "object": "embedding"
132 }
133 ],
134 "model": "model",
135 "object": "list",
136 "usage": {
137 "prompt_tokens": 0,
138 "total_tokens": 0
139 }
140}
141```
142
143### Example
144
145```python
146from openai import OpenAI
147client = OpenAI()
148
149client.embeddings.create(
150 model="text-embedding-ada-002",
151 input="The food was delicious and the waiter...",
152 encoding_format="float"
153)
154```
155
156#### Response
157
158```json
159{
160 "object": "list",
161 "data": [
162 {
163 "object": "embedding",
164 "embedding": [
165 0.0023064255,
166 -0.009327292,
167 .... (1536 floats total for ada-002)
168 -0.0028842222,
169 ],
170 "index": 0
171 }
172 ],
173 "model": "text-embedding-ada-002",
174 "usage": {
175 "prompt_tokens": 8,
176 "total_tokens": 8
177 }
178}
179```