diff --git a/main.go b/main.go index 10f7d0d..ccd3c52 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,10 @@ import ( ) func main() { + + // create Weaviate schema + weaviate.CreateSchema() + router := gin.Default() router.GET("/weaviate/schema", func(c *gin.Context) { diff --git a/weaviate/weaviate.go b/weaviate/weaviate.go index 521c1c6..7d717b7 100644 --- a/weaviate/weaviate.go +++ b/weaviate/weaviate.go @@ -3,10 +3,12 @@ package weaviate import ( "context" "encoding/json" + "fmt" "os" "github.com/weaviate/weaviate-go-client/v4/weaviate" "github.com/weaviate/weaviate-go-client/v4/weaviate/auth" + "github.com/weaviate/weaviate/entities/models" ) func RetrieveSchema() ([]byte, error) { @@ -35,3 +37,45 @@ func RetrieveSchema() ([]byte, error) { return jsonSchema, nil } + +func CreateSchema() { + + cfg := weaviate.Config{ + Host: os.Getenv("WEAVIATE_HOST"), // Replace with your endpoint + Scheme: "http", + AuthConfig: auth.ApiKey{Value: os.Getenv("WEAVIATE_KEY")}, + } + + client, err := weaviate.NewClient(cfg) + if err != nil { + panic(err) + } + + className := "Prompt" + + exists, err := client.Schema().ClassExistenceChecker().WithClassName(className).Do(context.Background()) + + if !exists { + classObj := &models.Class{ + Class: "Prompt", + Description: "Prompts generated by users", + Vectorizer: "none", + Properties: []*models.Property{ + { + DataType: []string{"string"}, + Description: "content of the prompt", + Name: "prompt", + }, + }, + } + + err = client.Schema().ClassCreator().WithClass(classObj).Do(context.Background()) + if err != nil { + panic(err) + } + fmt.Println("created class") + } else { + fmt.Println("class already exists") + } + +}