### ObjectBox Python Setup Source: https://github.com/objectbox/objectbox-python/blob/main/example/README.md Steps to set up a Python virtual environment and install the ObjectBox library for use with the provided examples. ```shell cd example python3 -m venv venv source venv/bin/activate pip install objectbox ``` -------------------------------- ### Install ObjectBox and Ollama Python Bindings Source: https://github.com/objectbox/objectbox-python/blob/main/example/ollama/README.md Installs the necessary Python libraries for ObjectBox and Ollama using pip. This is a prerequisite for running the example. ```shell pip install ollama pip install objectbox ``` ```shell pip install -r requirements.txt ``` -------------------------------- ### ObjectBox Python Setup and Development Commands Source: https://github.com/objectbox/objectbox-python/blob/main/CONTRIBUTING.md This section details the essential shell commands for setting up the ObjectBox Python project, fetching dependencies, building, testing, and running benchmarks. These commands are crucial for contributors to establish their development environment and maintain code quality. ```shell make depend # Initializes virtualenv and fetches the objectbox-c shared library. ``` ```shell make # Builds and tests the project. ``` ```shell make help # Displays all available make options. ``` ```shell make test # Runs the test suite to ensure changes work as expected. ``` ```shell make benchmark # Measures CRUD performance, useful for performance-related changes. ``` -------------------------------- ### Install ObjectBox Python Source: https://github.com/objectbox/objectbox-python/blob/main/example/vectorsearch-cities-notebook/Vector-Search-City.ipynb Installs or upgrades the ObjectBox Python library to the latest version. This is a prerequisite for using ObjectBox in Python projects. ```Python !pip install --upgrade objectbox ``` -------------------------------- ### Install Matplotlib for Visualization Source: https://github.com/objectbox/objectbox-python/blob/main/example/vectorsearch-cities-notebook/Vector-Search-City.ipynb A command to install the matplotlib library, which is a popular plotting library used for creating visualizations of data, including the nearest neighbor search results. ```Shell !pip install matplotlib ``` -------------------------------- ### Run ObjectBox Ollama Example Source: https://github.com/objectbox/objectbox-python/blob/main/example/ollama/README.md Executes the main Python script to demonstrate ObjectBox integration with Ollama embeddings. It outputs text generated by the model. ```shell $ python main.py Llamas are members of the camel family, which includes other large, even-toed ungulates such as camels, dromedaries, and Bactrian camels. Llamas are most closely related to alpacas, which are also native to South America and share many similarities in terms of their physical characteristics and behavior. Both llamas and alpacas belong to the family Camelidae, and are classified as ruminants due to their unique digestive system that allows them to break down cellulose in plant material. ``` -------------------------------- ### Tasks CLI Example Source: https://github.com/objectbox/objectbox-python/blob/main/example/README.md Demonstrates basic CRUD operations (create, list, mark as done) using the ObjectBox Python API in a command-line interface. It showcases adding tasks, listing them, and marking them as completed. ```shell cd tasks $ python main.py Welcome to the ObjectBox tasks-list app example. Type help or ? for a list of commands. > new buy oat > new buy yeast > new bake bread > ls ID Created Finished Text 1 Mon Apr 22 11:02:27 2024 buy oat 2 Mon Apr 22 11:02:30 2024 buy yeast 3 Mon Apr 22 11:02:34 2024 bake bread > done 1 > done 2 > ls > ls ID Created Finished Text 1 Mon Apr 22 11:02:27 2024 Mon Apr 22 11:03:02 2024 buy oat 2 Mon Apr 22 11:02:30 2024 Mon Apr 22 11:03:18 2024 buy yeast 3 Mon Apr 22 11:02:34 2024 bake bread > exit ``` -------------------------------- ### Query Cities by Name Prefix Source: https://github.com/objectbox/objectbox-python/blob/main/example/vectorsearch-cities-notebook/Vector-Search-City.ipynb Demonstrates how to query the ObjectBox database for objects where a specific string property starts with a given prefix. It shows building a query using the `starts_with` operator and finding matching results. ```Python query = box.query(City.name.starts_with("Be")).build() results = query.find() print(f"Found {len(results)} objects") for city in results: print(f"{city.name:>10s} {city.location}") ``` -------------------------------- ### Install ObjectBox Python Source: https://github.com/objectbox/objectbox-python/blob/main/README.md Provides the command to install or upgrade the ObjectBox Python library using pip. This is the standard method for adding ObjectBox to your Python project. ```Bash pip install --upgrade objectbox ``` -------------------------------- ### Create ObjectBox Store and Box Source: https://github.com/objectbox/objectbox-python/blob/main/example/vectorsearch-cities-notebook/Vector-Search-City.ipynb Initializes the ObjectBox `Store` (database instance) and obtains a `Box` for the `City` entity to perform data operations. The `Store` is a database instance. ```Python store = Store() box = store.box(City) ``` -------------------------------- ### Vector-Search Cities CLI Example Source: https://github.com/objectbox/objectbox-python/blob/main/example/README.md Demonstrates vector search capabilities by finding nearest neighbors based on city names or coordinates, and adding new locations. It uses a pre-defined dataset of cities with geo coordinates. ```shell cd vector-search-cities $ python main.py Welcome to the ObjectBox vectorsearch-cities example. Type help or ? for a list of commands. > ls ID Name Latitude Longitude 1 Abuja 9.08 7.40 2 Accra 5.60 -0.19 [..] 212 Yerevan 40.19 44.52 213 Zagreb 45.81 15.98 > ls Ber ID Name Latitude Longitude 28 Berlin 52.52 13.40 29 Bern 46.95 7.45 > city_neighbors Berlin ID Name Latitude Longitude Score 147 Prague 50.08 14.44 7.04 49 Copenhagen 55.68 12.57 10.66 200 Vienna 48.21 16.37 27.41 34 Bratislava 48.15 17.11 32.82 89 Ljubljana 46.06 14.51 42.98 > neighbors 6,52.52,13.405 ID Name Latitude Longitude Score 28 Berlin 52.52 13.40 0.00 147 Prague 50.08 14.44 7.04 49 Copenhagen 55.68 12.57 10.66 200 Vienna 48.21 16.37 27.41 34 Bratislava 48.15 17.11 32.82 89 Ljubljana 46.06 14.51 42.98 > add Area51, 37.23, -115.81 > city_neighbors Area51 ID Name Latitude Longitude Score 107 Mexico City 19.43 -99.13 594.86 27 Belmopan 17.25 -88.76 1130.92 64 Guatemala City 14.63 -90.51 1150.79 164 San Salvador 13.69 -89.22 1261.12 67 Havana 23.11 -82.37 1317.73 ``` -------------------------------- ### Close ObjectBox Store Source: https://github.com/objectbox/objectbox-python/blob/main/example/vectorsearch-cities-notebook/Vector-Search-City.ipynb Provides an example of how to close the ObjectBox store connection. This is typically optional but can be useful in environments like Jupyter notebooks to reset the store state. ```Python # Uncomment this to close the store, if you want to open it again: # store.close() ``` -------------------------------- ### Pull Ollama Models Source: https://github.com/objectbox/objectbox-python/blob/main/example/ollama/README.md Downloads specified embedding models from Ollama. These models are required for generating text embeddings. ```shell ollama pull llama3 ollama pull mxbai-embed-large ``` -------------------------------- ### Define City Object and List in Python Source: https://github.com/objectbox/objectbox-python/blob/main/example/vectorsearch-cities-notebook/Vector-Search-City.ipynb This Python code defines a helper function `city` to create `City` objects, each containing a name and location (latitude, longitude). It then populates a list named `cities` with various capital cities from around the world. ```python def city(name, lat, lon): return City(name=name, location=[lat,lon]) cities = [ city("Abuja", 9.0765, 7.3986), city("Accra", 5.6037, -0.1870), city("Addis Ababa", 9.0084, 38.7813), city("Algiers", 36.7529, 3.0420), city("Amman", 31.9632, 35.9306), city("Amsterdam", 52.3667, 4.8945), city("Ankara", 39.9334, 32.8597), city("Antananarivo", -18.8792, 47.5079), city("Apia", -13.8330, -171.7667), city("Ashgabat", 37.9601, 58.3261), city("Asmara", 15.3229, 38.9251), city("Astana", 51.1796, 71.4475), city("Asunción", -25.2637, -57.5759), city("Athens", 37.9795, 23.7162), city("Avarua", -21.2079, -159.7750), city("Baghdad", 33.3152, 44.3661), city("Baku", 40.4093, 49.8671), city("Bamako", 12.6530, -7.9864), city("Bandar Seri Begawan", 4.9031, 114.9398), city("Bangkok", 13.7563, 100.5018), city("Bangui", 4.3947, 18.5582), city("Banjul", 13.4549, -16.5790), city("Basseterre", 17.3026, -62.7177), city("Beijing", 39.9042, 116.4074), city("Beirut", 33.8889, 35.4944), city("Belgrade", 44.7866, 20.4489), city("Belmopan", 17.2510, -88.7590), city("Berlin", 52.5200, 13.4050), city("Bern", 46.9480, 7.4474), city("Bishkek", 42.8746, 74.5698), city("Bissau", 11.8636, -15.5842), city("Bogotá", 4.7109, -74.0721), city("Brasília", -15.8267, -47.9218), city("Bratislava", 48.1486, 17.1077), city("Brazzaville", -4.2634, 15.2429), city("Bridgetown", 13.1132, -59.5988), city("Brussels", 50.8503, 4.3517), city("Bucharest", 44.4268, 26.1025), city("Budapest", 47.4979, 19.0402), city("Buenos Aires", -34.6037, -58.3816), city("Bujumbura", -3.3818, 29.3622), city("Cairo", 30.0444, 31.2357), city("Canberra", -35.2809, 149.1300), city("Caracas", 10.4806, -66.9036), city("Castries", 14.0101, -60.9874), city("Chisinau", 47.0105, 28.8638), city("Colombo", 6.9271, 79.8612), city("Conakry", 9.6412, -13.5784), city("Copenhagen", 55.6761, 12.5683), city("Dakar", 14.7167, -17.4677), city("Damascus", 33.5131, 36.2919), city("Dhaka", 23.8103, 90.4125), city("Dili", -8.5569, 125.5603), city("Djibouti", 11.5890, 43.1456), city("Dodoma", -6.1748, 35.7469), city("Doha", 25.2854, 51.5310), city("Dublin", 53.3498, -6.2603), city("Dushanbe", 38.5868, 68.7841), city("Freetown", 8.4840, -13.2299), city("Funafuti", -8.5210, 179.1962), city("Gaborone", -24.6282, 25.9231), city("Georgetown", 6.8013, -58.1550), city("Gibraltar", 36.1408, -5.3536), city("Guatemala City", 14.6349, -90.5069), city("Hanoi", 21.0278, 105.8342), city("Harare", -17.8252, 31.0335), city("Havana", 23.1136, -82.3666), city("Helsinki", 60.1699, 24.9384), city("Honiara", -9.4376, 159.9720), city("Islamabad", 33.6844, 73.0479), city("Jakarta", -6.2088, 106.8456), city("Juba", 4.8594, 31.5713), city("Kabul", 34.5553, 69.2075), city("Kampala", 0.3476, 32.5825), city("Kathmandu", 27.7172, 85.3240), city("Khartoum", 15.5007, 32.5599), city("Kiev", 50.4501, 30.5234), city("Kigali", -1.9441, 30.0619), city("Kingston", 17.9710, -76.7924), city("Kingstown", 13.1467, -61.2121), city("Kinshasa", -4.4419, 15.2663), city("Kuala Lumpur", 3.1390, 101.6869), city("Kuwait City", 29.3759, 47.9774), city("La Paz", -16.4897, -68.1193), city("Libreville", 0.4162, 9.4673), city("Lilongwe", -13.9626, 33.7741), city("Lima", -12.0464, -77.0428), city("Lisbon", 38.7223, -9.1393), city("Ljubljana", 46.0569, 14.5058), city("Lomé", 6.1319, 1.2228), city("London", 51.5072, -0.1276), city("Luanda", -8.8399, 13.2894), city("Lusaka", -15.3875, 28.3228), city("Luxembourg City", 49.6116, 6.1319), city("Madrid", 40.4168, -3.7038), city("Majuro", 7.1164, 171.1859), city("Malabo", 3.7508, 8.7839), city("Male", 4.1755, 73.5093), city("Mamoudzou", -12.7871, 45.2750), city("Managua", 12.1364, -86.2514), city("Manama", 26.2285, 50.5860), city("Manila", 14.5995, 120.9842), city("Maputo", -25.8918, 32.6051), city("Maseru", -29.2976, 27.4854), city("Mbabane", -26.3054, 31.1367), city("Melekeok", 7.4874, 134.6265), city("Mexico City", 19.4326, -99.1332), city("Minsk", 53.9045, 27.5615), city("Mogadishu", 2.0469, 45.3182), city("Monaco", 43.7325, 7.4189), city("Monrovia", 6.3005, -10.7974), city("Montevideo", -34.9011, -56.1645), city("Moroni", -11.7022, 43.2551), city("Moscow", 55.7558, 37.6173), city("Muscat", 23.5859, 58.4059), city("Nairobi", -1.2921, 36.8219), city("Nassau", 25.0478, -77.3554), city("Naypyidaw", 19.7633, 96.0785), city("New Delhi", 28.6139, 77.2090), city("Ngerulmud", 7.5004, 134.6249), city("Niamey", 13.5122, 2.1254), city("Nicosia", 35.1725, 33.3650) ] ``` -------------------------------- ### Nearest-Neighbor Vector Search Source: https://github.com/objectbox/objectbox-python/blob/main/example/vectorsearch-cities-notebook/Vector-Search-City.ipynb Illustrates performing a nearest-neighbor search using vector data stored in ObjectBox. It shows how to query for the closest matches to a given location using the `nearest_neighbor` operator and retrieve results along with their similarity scores. ```Python query_location = [51.0, 12.0] # Somewhere in Germany, south-west of Berlin query = box.query(City.location.nearest_neighbor(query_location, 15)).build() results = query.find_with_scores() print(f"Found {len(results)} objects") for i, result in enumerate(results): capital, score = result print(f"{i + 1}. Capital: {capital.name}, Score: {score}") ``` -------------------------------- ### Visualize Nearest Neighbors with Matplotlib Source: https://github.com/objectbox/objectbox-python/blob/main/example/vectorsearch-cities-notebook/Vector-Search-City.ipynb Visualizes the results of a nearest-neighbor search using matplotlib. It plots the found cities as blue dots and the query location as a red dot, annotating each city with its name. ```Python import matplotlib.pyplot as plt for result in results: city = result[0] lat = city.location[0] lon = city.location[1] plt.scatter(lon, lat, color='blue', s=50, alpha=0.8) plt.annotate(city.name, (lon, lat), textcoords="offset points", xytext=(0, -10), ha='center', fontsize=8) plt.scatter(query_location[1], query_location[0], color='red', s=50, alpha=0.8) plt.xlabel('Longitude') plt.ylabel('Latitude') plt.title('Nearest Capitals') plt.show() ``` -------------------------------- ### Define ObjectBox City Data Model Source: https://github.com/objectbox/objectbox-python/blob/main/example/vectorsearch-cities-notebook/Vector-Search-City.ipynb Defines the `City` entity for ObjectBox, including its properties like `id`, `name`, and a `location` vector indexed with HNSW for efficient vector search. The `HnswIndex` is a special index for vectors that enables high-performance vector search. ```Python from objectbox import Entity, Float32Vector, HnswIndex, Id, Store, String @Entity() class City: id = Id() name = String() location = Float32Vector(index=HnswIndex(dimensions=2)) ``` -------------------------------- ### Store City Objects in Objectbox Database Source: https://github.com/objectbox/objectbox-python/blob/main/example/vectorsearch-cities-notebook/Vector-Search-City.ipynb This snippet shows how to clear existing data and insert a collection of City objects into the Objectbox database. It uses `box.remove_all()` to ensure a clean state and `box.put()` to add the data. The `box.count()` method is used to verify the number of items stored. ```python box.remove_all() # Remove previous data (if any) to avoid duplicates box.put(*cities) print("Cities in the database:", box.count()) ``` -------------------------------- ### Python Project Dependencies Source: https://github.com/objectbox/objectbox-python/blob/main/requirements.txt This snippet details the essential Python packages required for building and testing the objectbox-python project. It includes build system tools like pip, setuptools, and wheel, a core data serialization library (flatbuffers), a testing framework (pytest), a package publishing tool (twine), and a numerical computation library (numpy). ```python-requirements pip setuptools wheel flatbuffers==24.3.25 pytest>=4.4.1 twine numpy ``` -------------------------------- ### Python ObjectBox CRUD Operations Source: https://github.com/objectbox/objectbox-python/blob/main/README.md Demonstrates the basic Create, Read, Update, and Delete (CRUD) operations using ObjectBox in Python. It shows how to define an entity, interact with the database store, and perform common data manipulation tasks. ```Python from objectbox import Entity, Id, Store, String @Entity() class Person: id = Id name = String # The ObjectBox Store represents a database; keep it around... store = Store() # Get a box for the "Person" entity; a Box is the main interaction point with objects and the database. box = store.box(Person) person = Person(name = "Joe Green") id = box.put(person) # Create person = box.get(id) # Read person.name = "Joe Black" box.put(person) # Update box.remove(person) # Delete ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.