go/resources/embeddings/methods/create/index.md +0 −155 deleted
File Deleted View Diff
1## Create embeddings
2
3`client.Embeddings.New(ctx, body) (*CreateEmbeddingResponse, error)`
4
5**post** `/embeddings`
6
7Creates an embedding vector representing the input text.
8
9### Parameters
10
11- `body EmbeddingNewParams`
12
13 - `Input param.Field[EmbeddingNewParamsInputUnion]`
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 - `string`
18
19 - `type EmbeddingNewParamsInputArrayOfStrings []string`
20
21 The array of strings that will be turned into an embedding.
22
23 - `type EmbeddingNewParamsInputArrayOfTokens []int64`
24
25 The array of integers that will be turned into an embedding.
26
27 - `type EmbeddingNewParamsInputArrayOfTokenArrays [][]int64`
28
29 The array of arrays containing integers that will be turned into an embedding.
30
31 - `Model param.Field[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 - `string`
36
37 - `type EmbeddingModel string`
38
39 - `const EmbeddingModelTextEmbeddingAda002 EmbeddingModel = "text-embedding-ada-002"`
40
41 - `const EmbeddingModelTextEmbedding3Small EmbeddingModel = "text-embedding-3-small"`
42
43 - `const EmbeddingModelTextEmbedding3Large EmbeddingModel = "text-embedding-3-large"`
44
45 - `Dimensions param.Field[int64]`
46
47 The number of dimensions the resulting output embeddings should have. Only supported in `text-embedding-3` and later models.
48
49 - `EncodingFormat param.Field[EmbeddingNewParamsEncodingFormat]`
50
51 The format to return the embeddings in. Can be either `float` or [`base64`](https://pypi.org/project/pybase64/).
52
53 - `const EmbeddingNewParamsEncodingFormatFloat EmbeddingNewParamsEncodingFormat = "float"`
54
55 - `const EmbeddingNewParamsEncodingFormatBase64 EmbeddingNewParamsEncodingFormat = "base64"`
56
57 - `User param.Field[string]`
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- `type CreateEmbeddingResponse struct{…}`
64
65 - `Data []Embedding`
66
67 The list of embeddings generated by the model.
68
69 - `Embedding []float64`
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 int64`
74
75 The index of the embedding in the list of embeddings.
76
77 - `Object Embedding`
78
79 The object type, which is always "embedding".
80
81 - `const EmbeddingEmbedding Embedding = "embedding"`
82
83 - `Model string`
84
85 The name of the model used to generate the embedding.
86
87 - `Object List`
88
89 The object type, which is always "list".
90
91 - `const ListList List = "list"`
92
93 - `Usage CreateEmbeddingResponseUsage`
94
95 The usage information for the request.
96
97 - `PromptTokens int64`
98
99 The number of tokens used by the prompt.
100
101 - `TotalTokens int64`
102
103 The total number of tokens used by the request.
104
105### Example
106
107```go
108package main
109
110import (
111 "context"
112 "fmt"
113
114 "github.com/openai/openai-go"
115 "github.com/openai/openai-go/option"
116)
117
118func main() {
119 client := openai.NewClient(
120 option.WithAPIKey("My API Key"),
121 )
122 createEmbeddingResponse, err := client.Embeddings.New(context.TODO(), openai.EmbeddingNewParams{
123 Input: openai.EmbeddingNewParamsInputUnion{
124 OfString: openai.String("The quick brown fox jumped over the lazy dog"),
125 },
126 Model: openai.EmbeddingModelTextEmbedding3Small,
127 })
128 if err != nil {
129 panic(err.Error())
130 }
131 fmt.Printf("%+v\n", createEmbeddingResponse.Data)
132}
133```
134
135#### Response
136
137```json
138{
139 "data": [
140 {
141 "embedding": [
142 0
143 ],
144 "index": 0,
145 "object": "embedding"
146 }
147 ],
148 "model": "model",
149 "object": "list",
150 "usage": {
151 "prompt_tokens": 0,
152 "total_tokens": 0
153 }
154}
155```