Skip to content

Commit

Permalink
Add pinecone vectorstore example
Browse files Browse the repository at this point in the history
  • Loading branch information
hupe1980 committed Sep 8, 2023
1 parent 9f3a3fd commit fa19317
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
84 changes: 84 additions & 0 deletions examples/pinecone_vectorstore/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package main

import (
"context"
"fmt"
"log"
"os"

"github.com/hupe1980/golc/embedding"
"github.com/hupe1980/golc/integration/pinecone"
"github.com/hupe1980/golc/schema"
"github.com/hupe1980/golc/vectorstore"
)

func main() {
openai, err := embedding.NewOpenAI(os.Getenv("OPENAI_API_KEY"))
if err != nil {
log.Fatal(err)
}

client, err := pinecone.NewRestClient(os.Getenv("PINECONE_API_KEY"), pinecone.Endpoint{
IndexName: "golc",
ProjectName: os.Getenv("PINECONE_PROJECT"),
Environment: "us-west1-gcp-free",
})
if err != nil {
log.Fatal(err)
}

vs, err := vectorstore.NewPinecone(client, openai, "textKey", func(po *vectorstore.PineconeOptions) {
po.TopK = 1
})
if err != nil {
log.Fatal(err)
}

if err = vs.AddDocuments(context.Background(), []schema.Document{
{
PageContent: "Pizza is an Italian dish consisting of a flat, round base of dough topped with various ingredients, including tomato sauce, cheese, and various toppings.",
Metadata: map[string]any{
"cousine": "Italian",
},
},
{
PageContent: "Sushi is a Japanese dish consisting of vinegared rice combined with various ingredients, such as seafood, vegetables, and occasionally tropical fruits.",
Metadata: map[string]any{
"cousine": "Japanese",
},
},
{
PageContent: "A burger is a sandwich consisting of a cooked ground meat patty, usually beef, placed in a sliced bread roll or bun.",
Metadata: map[string]any{
"cousine": "American",
},
},
{
PageContent: "Pad Thai is a popular Thai stir-fried noodle dish made with rice noodles, eggs, tofu or shrimp, bean sprouts, peanuts, and lime.",
Metadata: map[string]any{
"cousine": "Thai",
},
},
{
PageContent: "Sashimi is a Japanese delicacy consisting of thinly sliced raw seafood, served with soy sauce and wasabi.",
Metadata: map[string]any{
"cousine": "Japanese",
},
},
{
PageContent: "Lasagna is an Italian pasta dish made with layers of pasta sheets, meat sauce, cheese, and bechamel sauce.",
Metadata: map[string]any{
"cousine": "Italian",
},
},
}); err != nil {
log.Fatal(err)
}

docs, err := vs.SimilaritySearch(context.Background(), "Cheeseburger")
if err != nil {
log.Fatal(err)
}

fmt.Println(docs)
}
9 changes: 9 additions & 0 deletions integration/pinecone/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ func (p *RestClient) Upsert(ctx context.Context, req *UpsertRequest) (*UpsertRes
return nil, err
}

if res.StatusCode != 200 {
errorResponse := ErrorResponse{}
if err := json.Unmarshal(body, &errorResponse); err != nil {
return nil, err
}

return nil, fmt.Errorf("pincoe error: %s", errorResponse.Message)
}

upsertResponse := UpsertResponse{}
if err := json.Unmarshal(body, &upsertResponse); err != nil {
return nil, err
Expand Down
5 changes: 5 additions & 0 deletions integration/pinecone/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,8 @@ type QueryResponse struct {
Matches []*Match `json:"matches"`
Namespace string `json:"namespace"`
}

type ErrorResponse struct {
Code int `json:"code"`
Message string `json:"message"`
}

0 comments on commit fa19317

Please sign in to comment.