BagelDB is a Python library for interacting with the BagelDB API.
Project description
Here's a step by step guide on how to use BagelDB python client.
-
Import the
BagelDB
clientStart by importing the
BagelDB
client into your Python script.from betabageldb import BagelDB
-
Initialize the BagelDB client
Initialize a new instance of the
BagelDB
client. You need to specify the index you want to work with at this stage. This index will be used for all subsequent insertions and searches. Make sure you have your OpenAI API key set in your environment variables asOPENAI_API_KEY
.index = "myIndex" db = BagelDB(index)
-
Ping the BagelDB server
To check the connection to the BagelDB server, you can use the
ping()
method. This method will return a response from the BagelDB API in JSON format.response = db.ping() print(response)
-
Get OpenAI embeddings
Use the
getOpenAIEmbedding(inputText, model='text-embedding-ada-002')
method to get embeddings from OpenAI. TheinputText
is the text for which embeddings are required.model
is optional and defaults to 'text-embedding-ada-002'.input_text = "Some text" embeddings = db.getOpenAIEmbedding(input_text) print(embeddings)
-
Insert vectors into BagelDB
To insert vectors into BagelDB, use the
insert(vectors)
method.vectors
is a list of vectors to be inserted. The index is already set during the initialization of the BagelDB client and is not required here.vectors = [{'id': 'vec1', 'values': [0.1, 0.2, 0.3], 'metadata': {'key': 'value'}}] insert_response = db.insert(vectors) print(insert_response)
-
Search for a vector in BagelDB
To search for a vector in BagelDB, use the
search(vector)
method.vector
is the vector for which the search is to be performed. The index is already set during the initialization of the BagelDB client and is not required here.vector = [0.1, 0.2, 0.3] search_response = db.search(vector) print(search_response)
Remember to handle exceptions in your application. The methods in the BagelDB
client can raise exceptions if a network error occurs or if the response from the server indicates a failed HTTP status code.
-
Insert vectors from texts
The
insertFromTexts(texts, model='text-embedding-ada-002')
method can be used to convert a list of texts to their respective embeddings and insert these as vectors into BagelDB. Thetexts
parameter is a list of strings for which embeddings are required, andmodel
is optional and defaults to 'text-embedding-ada-002'. Each vector will be assigned an id incrementally starting from 0 and metadata will contain original text.texts = ["Some text 1", "Some text 2"] insert_response = db.insertFromTexts(texts) print(insert_response)
-
Search for a vector from text
To search for a vector derived from a given text in BagelDB, you can use the
searchFromText(text, model='text-embedding-ada-002')
method.text
is the string for which the corresponding embedding is to be found and searched, andmodel
is optional and defaults to 'text-embedding-ada-002'.text = "Some text" search_response = db.searchFromText(text) print(search_response)
These new methods, insertFromTexts
and searchFromText
, simplify the process of converting text to embeddings and performing operations on BagelDB, providing a higher level of abstraction for users to work with.
Remember to handle exceptions in your application. The methods in the BagelDB
client can raise exceptions if a network error occurs or if the response from the server indicates a failed HTTP status code.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for betabageldb-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88312de690d23beca73b62acb40281670c756205ac9e793df82026d35e5bd76d |
|
MD5 | b4cfcdec8a1a4cabe789a260f8289fd9 |
|
BLAKE2b-256 | 3f21fa7b39d9eda7b82ade69c59270f7fdec1159ac75a02e788b98a2673cb884 |