Any way add new object into database

Description

I am trying to add a feature that allow user upload good “prompt” and “answer” into set up class. Assume I have only two properties: “Prompt” and “Answer”, and is any option that allow me directly add new object “new prompt” and “new answer” into my class.

I know there is an alternative way is updating my dataset then reset the class, but that takes long time and not ‘smoothly’.

Server Setup Information

  • Weaviate Server Version: v3
  • Deployment Method: Weaviate Cloud
  • Client Language and Version: Python and v3.26.2

Hi @Zhenzhao_Tu !

You mean adding new properties?

If that’s the case, yes, you can add new properties.

Not sure I understood your issue :frowning:

Sorry about the confusion,

I got only two properties in my scheme class: “prompt”, “answer”. Now I want to add one new objects (“new prompt”, “new answer”) into my existing class. Does that possible?

Or in the other words, it’s like add a new row into dataset then reset the weavite database so we can add new objects in the db.

I am not sure it’s making sense

Hi!

You can add new objects (or rows) in you collection (schema/class/“table”).

Consider the following code that creates the collection you described:

import weaviate
from weaviate import classes as wvc
client = weaviate.connect_to_local()

collection = client.collections.create(
    name="MyCollection",
    vectorizer_config=wvc.config.Configure.Vectorizer.text2vec_openai(),
    properties=[
        wvc.config.Property(
            name="prompt",
            data_type=wvc.config.DataType.TEXT
        ),
        wvc.config.Property(
            name="answer",
            data_type=wvc.config.DataType.TEXT
        )
    ]
)

Now you can get that collection, and add objects to it:

collection = client.collections.get("MyCollection")
collection.data.insert({"prompt": "New Prompt", "answer": "New Answer"})

You can now retrieve those objects

query = collection.query.fetch_objects()
print(
    [ item.properties for item in query.objects ]
)

Let me know if this is what you want :slight_smile:

Exactly what I need! Thanks!

@DudaNogueira

I am also tried a way to add objects it looks like working which is different from your, can you check mine see if they are similar work thanks!

from client_setup import get_client
from weaviate.util import generate_uuid5

client = get_client()

# Create a new object
new_object = {
        "Prompt": "What is the capital of France?",
        "Answer": "Paris",
}

# define the add function
def add_object(new_object):
    new_data_uuid = generate_uuid5(new_object)

    # Add the object to Weaviate
    client.data_object.create(
            data_object=new_object,
            class_name="DSCode",
            uuid=new_data_uuid
    )


print(client.data_object.get_by_id(new_data_uuid, with_vector=False))
print(client.query.aggregate("DSCode").with_meta_count().do())

Oh, I see.

This is because you are using the python v3 syntax.

When you install the version 4.4.4 weaviate-client python package, you have access to both v3 and v4 syntax.

this is how you would initiate a python v3 client (in deprecation):

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

and this is how you initiate the new python v4 client:

client = weaviate.connect_to_local()

We strongely recommend to use the new python v4 client, as it delivers a lot of improvements.

Let me know if this helps!