Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package main

// START CreateCollection
import (
"context"
"fmt"
"os"

"github.com/weaviate/weaviate-go-client/v5/weaviate"
"github.com/weaviate/weaviate-go-client/v5/weaviate/auth"
"github.com/weaviate/weaviate/entities/models"
)

func main() {
// Best practice: store your credentials in environment variables
weaviateURL := os.Getenv("WEAVIATE_HOST")
weaviateAPIKey := os.Getenv("WEAVIATE_API_KEY")

// Step 1.1: Connect to your Weaviate Cloud instance
cfg := weaviate.Config{
Host: weaviateURL,
Scheme: "https",
AuthConfig: auth.ApiKey{Value: weaviateAPIKey},
}
client, err := weaviate.NewClient(cfg)
if err != nil {
panic(err)
}

// END CreateCollection

// NOT SHOWN TO THE USER - DELETE EXISTING COLLECTION
client.Schema().ClassDeleter().WithClassName("Movie").Do(context.Background())

// START CreateCollection
// Step 1.2: Create a collection
// highlight-start
classObj := &models.Class{
Class: "Movie",
Vectorizer: "text2vec-weaviate",
ModuleConfig: map[string]interface{}{
"generative-anthropic": map[string]interface{}{
"model": "claude-3-5-haiku-latest",
},
},
}

err = client.Schema().ClassCreator().WithClass(classObj).Do(context.Background())
if err != nil {
panic(err)
}
// highlight-end

// END CreateCollection

// START CreateCollection
// Step 1.3: Import three objects
dataObjects := []map[string]interface{}{
{
"title": "The Matrix",
"description": "A computer hacker learns about the true nature of reality and his role in the war against its controllers.",
"genre": "Science Fiction",
},
{
"title": "Spirited Away",
"description": "A young girl becomes trapped in a mysterious world of spirits and must find a way to save her parents and return home.",
"genre": "Animation",
},
{
"title": "The Lord of the Rings: The Fellowship of the Ring",
"description": "A meek Hobbit and his companions set out on a perilous journey to destroy a powerful ring and save Middle-earth.",
"genre": "Fantasy",
},
}

// END CreateCollection

// START CreateCollection
// Insert objects
objects := make([]*models.Object, len(dataObjects))
for i, obj := range dataObjects {
objects[i] = &models.Object{
Class: "Movie",
Properties: obj,
}
}

_, err = client.Batch().ObjectsBatcher().WithObjects(objects...).Do(context.Background())
if err != nil {
panic(err)
}

fmt.Printf("Imported & vectorized %d objects into the Movie collection\n", len(dataObjects))
// END CreateCollection
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package main

// START NearText
import (
"context"
"encoding/json"
"fmt"
"os"

"github.com/weaviate/weaviate-go-client/v5/weaviate"
"github.com/weaviate/weaviate-go-client/v5/weaviate/auth"
"github.com/weaviate/weaviate-go-client/v5/weaviate/graphql"
)

func main() {
// Best practice: store your credentials in environment variables
weaviateURL := os.Getenv("WEAVIATE_HOST")
weaviateAPIKey := os.Getenv("WEAVIATE_API_KEY")

// Step 1.1: Connect to your Weaviate Cloud instance
cfg := weaviate.Config{
Host: weaviateURL,
Scheme: "https",
AuthConfig: auth.ApiKey{Value: weaviateAPIKey},
}
client, err := weaviate.NewClient(cfg)
if err != nil {
panic(err)
}

// Step 2.2: Perform a semantic search with NearText
// highlight-start
// END NearText

// START NearText
title := graphql.Field{Name: "title"}
description := graphql.Field{Name: "description"}
genre := graphql.Field{Name: "genre"}

nearText := client.GraphQL().NearTextArgBuilder().
WithConcepts([]string{"sci-fi"})

result, err := client.GraphQL().Get().
WithClassName("Movie").
WithNearText(nearText).
WithLimit(2).
WithFields(title, description, genre).
Do(context.Background())
// END NearText

// START NearText
// highlight-end

if err != nil {
panic(err)
}

// Inspect the results
if result.Errors != nil {
fmt.Printf("Error: %v\n", result.Errors)
return
}

data := result.Data["Get"].(map[string]interface{})
movies := data["Movie"].([]interface{})

for _, movie := range movies {
jsonData, err := json.MarshalIndent(movie, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(jsonData))
}
// END NearText
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

// START RAG
import (
"context"
"fmt"
"os"

"github.com/weaviate/weaviate-go-client/v5/weaviate"
"github.com/weaviate/weaviate-go-client/v5/weaviate/auth"
"github.com/weaviate/weaviate-go-client/v5/weaviate/graphql"
)

func main() {
// Best practice: store your credentials in environment variables
weaviateURL := os.Getenv("WEAVIATE_URL")
weaviateAPIKey := os.Getenv("WEAVIATE_API_KEY")
anthropicAPIKey := os.Getenv("ANTHROPIC_API_KEY")

// Step 2.1: Connect to your Weaviate Cloud instance
// highlight-start
headers := map[string]string{
"X-Anthropic-Api-Key": anthropicAPIKey,
}

cfg := weaviate.Config{
Host: weaviateURL,
Scheme: "https",
AuthConfig: auth.ApiKey{Value: weaviateAPIKey},
Headers: headers,
}
client, err := weaviate.NewClient(cfg)
if err != nil {
panic(err)
}
// highlight-end

// Step 2.2: Perform RAG with NearText results
// highlight-start
title := graphql.Field{Name: "title"}
description := graphql.Field{Name: "description"}
genre := graphql.Field{Name: "genre"}

nearText := client.GraphQL().NearTextArgBuilder().
WithConcepts([]string{"sci-fi"})

generate := graphql.NewGenerativeSearch().GroupedResult("Write a tweet with emojis about this movie.")

result, err := client.GraphQL().Get().
WithClassName("Movie").
WithNearText(nearText).
WithLimit(1).
WithFields(title, description, genre).
WithGenerativeSearch(generate).
Do(context.Background())
// highlight-end

if err != nil {
panic(err)
}

// Inspect the results
if result.Errors != nil {
fmt.Printf("Error: %v\n", result.Errors)
return
}

fmt.Printf("%v", result)
// END RAG
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package main

// START CreateCollection
import (
"context"
"fmt"

"github.com/weaviate/weaviate-go-client/v5/weaviate"
"github.com/weaviate/weaviate/entities/models"
)

func main() {
// Step 1.1: Connect to your local Weaviate instance
cfg := weaviate.Config{
Host: "localhost:8080",
Scheme: "http",
}
client, err := weaviate.NewClient(cfg)
if err != nil {
panic(err)
}

// END CreateCollection

// NOT SHOWN TO THE USER - DELETE EXISTING COLLECTION
client.Schema().ClassDeleter().WithClassName("Movie").Do(context.Background())

// START CreateCollection
// Step 1.2: Create a collection
// highlight-start
classObj := &models.Class{
Class: "Movie",
Vectorizer: "text2vec-ollama",
ModuleConfig: map[string]interface{}{
"text2vec-ollama": map[string]interface{}{ // Configure the Ollama embedding integration
"apiEndpoint": "http://ollama:11434", // If using Docker you might need: http://host.docker.internal:11434
"model": "nomic-embed-text", // The model to use
},
"generative-ollama": map[string]interface{}{ // Configure the Ollama generative integration
"apiEndpoint": "http://ollama:11434", // If using Docker you might need: http://host.docker.internal:11434
"model": "llama3.2", // The model to use
},
},
}

err = client.Schema().ClassCreator().WithClass(classObj).Do(context.Background())
if err != nil {
panic(err)
}
// highlight-end

// Step 1.3: Import three objects
dataObjects := []map[string]interface{}{
{
"title": "The Matrix",
"description": "A computer hacker learns about the true nature of reality and his role in the war against its controllers.",
"genre": "Science Fiction",
},
{
"title": "Spirited Away",
"description": "A young girl becomes trapped in a mysterious world of spirits and must find a way to save her parents and return home.",
"genre": "Animation",
},
{
"title": "The Lord of the Rings: The Fellowship of the Ring",
"description": "A meek Hobbit and his companions set out on a perilous journey to destroy a powerful ring and save Middle-earth.",
"genre": "Fantasy",
},
}

// Insert objects
objects := make([]*models.Object, len(dataObjects))
for i, obj := range dataObjects {
objects[i] = &models.Object{
Class: "Movie",
Properties: obj,
}
}

_, err = client.Batch().ObjectsBatcher().WithObjects(objects...).Do(context.Background())
if err != nil {
panic(err)
}

fmt.Printf("Imported & vectorized %d objects into the Movie collection\n", len(dataObjects))
// END CreateCollection
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

// START NearText
import (
"context"
"encoding/json"
"fmt"

"github.com/weaviate/weaviate-go-client/v5/weaviate"
"github.com/weaviate/weaviate-go-client/v5/weaviate/graphql"
)

func main() {
// Step 1.1: Connect to your local Weaviate instance
cfg := weaviate.Config{
Host: "localhost:8080",
Scheme: "http",
}
client, err := weaviate.NewClient(cfg)
if err != nil {
panic(err)
}

// Step 2.2: Perform a semantic search with NearText
// highlight-start
title := graphql.Field{Name: "title"}
description := graphql.Field{Name: "description"}
genre := graphql.Field{Name: "genre"}

nearText := client.GraphQL().NearTextArgBuilder().
WithConcepts([]string{"sci-fi"})

result, err := client.GraphQL().Get().
WithClassName("Movie").
WithNearText(nearText).
WithLimit(2).
WithFields(title, description, genre).
Do(context.Background())
// highlight-end

if err != nil {
panic(err)
}

// Inspect the results
if result.Errors != nil {
fmt.Printf("Error: %v\n", result.Errors)
return
}

data := result.Data["Get"].(map[string]interface{})
movies := data["Movie"].([]interface{})

for _, movie := range movies {
jsonData, err := json.MarshalIndent(movie, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(jsonData))
}
// END NearText
}
Loading
Loading