Development

How to Use Python to Connect with Weaviate DB

Weaviate is an open-source, GraphQL, and RESTful API-based knowledge graph that allows us to store, search, analyze, and relate data with the speed and accuracy that AI algorithms need.
Captain Salem 2 min read
How to Use Python to Connect with Weaviate DB

This tutorial will guide you through setting up Weaviate and using it with Python. We will use the official Weaviate Python client for demonstration.

Prerequisites

To follow along with this post, ensure you have the following:

  • A working Python 3.10+ environment.
  • A running Weaviate instance. You can create one using Docker or Weaviate Cloud as a managed option.

Installing the Weaviate Python Library

The next step is to ensure we have the Weaviate library for Python installed. We can do this using the pip command as shown:

pip3 install weaviate-client

This should download the latest stable version of the Weaviate Client library onto your system.

Setting Up the Python Client

To interact with Weaviate, we will use the Weaviate Python client. We can use the configuration shown below to connect to the server.

>>> import weaviate
>>> client = weaviate.Client("http://localhost:8080 ")
>>> client.schema.get()

The code above starts by importing the weaviate client and creating a client connecting to the Weaviate server on localhost:8080.

You can replace the address to the Weaviate server if running on a different hostname or port.

If your server is configured to use API-Key authentication, you can define the configuration as shown below:

>>> import weaviate

>>> auth_config = weaviate.AuthApiKey(api_key="YOUR-WEAVIATE-API-KEY")

# Instantiate the client with the auth config
>>> client = weaviate.Client(url="http://localhost:8080",auth_client_secret=auth_config)

Be sure to replace the URL, and api_key parameter with your correct values.

Creating a Schema

We need to create a schema before we can add data to Weaviate. A schema consists of class definitions. A class definition contains the class name and its properties.

We can create a schema containing Person information as shown in the example code below:

schema = {
    "classes": [
        {
            "class": "Person",
            "properties": [
                {
                    "name": "name",
                    "dataType": ["string"]
                },
                {
                    "name": "age",
                    "dataType": ["int"]
                }
            ]
        }
    ]
}

This should create a schema for a Person with the defined properties such as name and age.

Adding Sample Data

Once we have defined the schema on the server, we can proceed and add sample data as shown in the example code below:

person = {
    "class": "Person",
    "id": "person-1",
    "properties": {
        "name": "John Doe",
        "age": 30
    }
}

client.data_object.create(person)

This code should be a single entry to the Person schema with the values of the defined name and age properties.

Conclusion

In this tutorial, we learned how to use the Weaviate Python client to connect to a Weaviate server, create a schema, and add sample data.

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.