Skip to content

Commit f99073b

Browse files
committed
📦 NEW: Embed
1 parent be835dc commit f99073b

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

examples/embed/embed.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Experimental upcoming beta AI primitve.
2+
# Please refer to the documentation for more information: https://langbase.com/docs for more information.
3+
import os
4+
from dotenv import load_dotenv
5+
from langbase import Langbase
6+
7+
load_dotenv()
8+
9+
# Cconfigure the Langbase client with your API key
10+
langbase = Langbase(api_key=os.environ.get("LANGBASE_API_KEY"))
11+
12+
def main():
13+
"""
14+
Generates embeddings for the given text chunks.
15+
"""
16+
response = langbase.embed(
17+
chunks=[
18+
"Langbase is the most powerful serverless platform for building AI agents with memory. Build, scale, and evaluate AI agents with semantic memory (RAG) and world-class developer experience. We process billions of AI messages/tokens daily. Built for every developer, not just AI/ML experts."
19+
],
20+
embedding_model="openai:text-embedding-3-large",
21+
)
22+
print(response)
23+
24+
if __name__ == "__main__":
25+
main()

langbase/types.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,14 @@ class ToolWebSearchOptions(TypedDict, total=False):
339339
domains: List[str]
340340
api_key: str
341341

342+
class EmbedOptions(TypedDict, total=False):
343+
"""Options for embedding generation."""
344+
chunks: List[str]
345+
embedding_model: EmbeddingModel
346+
347+
348+
EmbedResponse = List[List[float]]
349+
342350

343351
class ToolWebSearchResponse(TypedDict):
344352
"""Response from web search."""

tests/test_langbase.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,17 +249,33 @@ def test_threads_messages_list(self, mock_get):
249249
mock_get.assert_called_once_with("/v1/threads/thread_123/messages")
250250
self.assertEqual(result, [{"id": "msg_123", "content": "Hello"}])
251251

252-
253-
254252
@patch("langbase.request.Request.post")
255253
def test_embed(self, mock_post):
256254
"""Test embed method."""
257255
mock_post.return_value = [[0.1, 0.2, 0.3]]
258-
result = self.lb.embed(
256+
257+
# Test with embedding model
258+
result_with_model = self.lb.embed(
259+
chunks=["Test text"],
260+
embedding_model="test-model"
261+
)
262+
263+
mock_post.assert_called_with(
264+
"/v1/embed",
265+
{"chunks": ["Test text"], "embeddingModel": "test-model"}
266+
)
267+
self.assertEqual(result_with_model, [[0.1, 0.2, 0.3]])
268+
269+
# Test without embedding model
270+
result_without_model = self.lb.embed(
259271
chunks=["Test text"]
260272
)
261-
mock_post.assert_called_once()
262-
self.assertEqual(result, [[0.1, 0.2, 0.3]])
273+
274+
mock_post.assert_called_with(
275+
"/v1/embed",
276+
{"chunks": ["Test text"]}
277+
)
278+
self.assertEqual(result_without_model, [[0.1, 0.2, 0.3]])
263279

264280
@patch("langbase.request.Request.post")
265281
def test_chunker(self, mock_post):

0 commit comments

Comments
 (0)