AI

Weaviate Python

Weaviate is an open-source, GraphQL, and RESTful API-based knowledge graph that allows us to store, search, analyze, and relate the data with the speed and accuracy that AI algorithms need.

This tutorial guides 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 that you have the following:

  • A working Python 3.7+ 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 that we have the Weaviate library installed for Python. We can do this using the “pip” command as follows:

$ pip 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 use the Weaviate Python client. We can use the following configuration to connect to the server.

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

The previous code 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 it is running on a different hostname or port.

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

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 the 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 the “Person” information as shown in the following example code:

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

client.schema.create(schema)

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

Adding a Sample Data

Once we define the schema on the server, we can proceed and add a sample data as shown in the following example code:

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

We learned how to use the Weaviate Python client to connect to a Weaviate server, create a schema, and add a sample data. You can check the docs for more information.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list