docs
0
# Embeddings
1
2
Text embedding generation for converting text into numerical vectors for semantic similarity, search, and machine learning applications. Supports multiple providers through Portkey's unified interface.
3
4
## Capabilities
5
6
### Embedding Creation
7
8
Creates vector embeddings from input text using the specified model. Supports various embedding models from different providers with consistent interface.
9
10
```python { .api }
11
class Embeddings:
12
def create(
13
self,
14
*,
15
input: str,
16
model: Optional[str] = "portkey-default",
17
dimensions: Union[int, NotGiven] = NOT_GIVEN,
18
encoding_format: Union[str, NotGiven] = NOT_GIVEN,
19
user: Union[str, NotGiven] = NOT_GIVEN,
20
**kwargs
21
) -> CreateEmbeddingResponse:
22
"""
23
Create embeddings for the given input text.
24
25
Args:
26
input: Input text to create embeddings for
27
model: Embedding model to use (defaults to "portkey-default")
28
dimensions: Number of dimensions for the embedding vectors (model-dependent)
29
encoding_format: Format for encoding the embeddings (e.g., "float", "base64")
30
user: User identifier for tracking and analytics
31
**kwargs: Additional provider-specific parameters
32
33
Returns:
34
CreateEmbeddingResponse: Response containing embedding vectors and metadata
35
"""
36
37
class AsyncEmbeddings:
38
async def create(
39
self,
40
*,
41
input: str,
42
model: Optional[str] = "portkey-default",
43
dimensions: Union[int, NotGiven] = NOT_GIVEN,
44
encoding_format: Union[str, NotGiven] = NOT_GIVEN,
45
user: Union[str, NotGiven] = NOT_GIVEN,
46
**kwargs
47
) -> CreateEmbeddingResponse:
48
"""Async version of create method."""
49
```
50
51
### Usage Examples
52
53
```python
54
from portkey_ai import Portkey
55
56
# Initialize client
57
portkey = Portkey(
58
api_key="PORTKEY_API_KEY",
59
virtual_key="VIRTUAL_KEY"
60
)
61
62
# Create embeddings for single text
63
response = portkey.embeddings.create(
64
input="Hello, world!",
65
model="text-embedding-ada-002"
66
)
67
68
# Access the embedding vector
69
embedding = response.data[0].embedding
70
print(f"Embedding dimensions: {len(embedding)}")
71
72
# Create embeddings with specific dimensions
73
response = portkey.embeddings.create(
74
input="Text to embed",
75
model="text-embedding-3-small",
76
dimensions=512
77
)
78
79
# Batch embedding creation (provider-dependent)
80
texts = [
81
"First text to embed",
82
"Second text to embed",
83
"Third text to embed"
84
]
85
86
for text in texts:
87
response = portkey.embeddings.create(
88
input=text,
89
model="text-embedding-ada-002",
90
user="user-123"
91
)
92
print(f"Embedding for '{text}': {len(response.data[0].embedding)} dimensions")
93
```
94
95
### Async Usage
96
97
```python
98
import asyncio
99
from portkey_ai import AsyncPortkey
100
101
async def create_embeddings():
102
portkey = AsyncPortkey(
103
api_key="PORTKEY_API_KEY",
104
virtual_key="VIRTUAL_KEY"
105
)
106
107
response = await portkey.embeddings.create(
108
input="Async embedding creation",
109
model="text-embedding-ada-002"
110
)
111
112
return response.data[0].embedding
113
114
# Run async function
115
embedding = asyncio.run(create_embeddings())
116
```
117
118
## Types
119
120
```python { .api }
121
class CreateEmbeddingResponse:
122
"""Response from embedding creation request"""
123
object: str # "list"
124
data: List[EmbeddingObject]
125
model: str
126
usage: EmbeddingUsage
127
_headers: Optional[dict] # Response headers for debugging
128
129
class EmbeddingObject:
130
"""Individual embedding object"""
131
object: str # "embedding"
132
embedding: List[float] # The embedding vector
133
index: int # Index in the input list
134
135
class EmbeddingUsage:
136
"""Token usage information"""
137
prompt_tokens: int
138
total_tokens: int
139
```