mirror of
https://github.com/vxcontrol/langchaingo.git
synced 2026-07-21 08:55:25 -04:00
6c81282073
* examples: update examples, clean up module declarations - Fix and relax lint rules (for now) - Clean up and fix module declarations - Add .gitattributes to mark go.sum as binary * ci: separate and enhance CI workflows - Split example builds into dedicated workflow - Add comprehensive test coverage reporting - Improve CI structure with matrix testing - Add race condition testing - Add automated PR coverage comments * test: improve test reliability and agent message handling - Simplify MRKL agent test to use basic math calculation - Update OpenAI functions agent to use ToolChatMessage - Add proper environment checks for Zep integration tests * agents: fix issue in tool call handling for openai function agent
128 lines
2.7 KiB
Go
128 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/tmc/langchaingo/embeddings"
|
|
"github.com/tmc/langchaingo/llms/openai"
|
|
"github.com/tmc/langchaingo/schema"
|
|
"github.com/tmc/langchaingo/vectorstores"
|
|
"github.com/tmc/langchaingo/vectorstores/pinecone"
|
|
)
|
|
|
|
func main() {
|
|
// Create an embeddings client using the OpenAI API. Requires environment variable OPENAI_API_KEY to be set.
|
|
|
|
llm, err := openai.New(openai.WithEmbeddingModel("text-embedding-3-small")) // Specify your preferred embedding model
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
e, err := embeddings.NewEmbedder(llm)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
// Create a new Pinecone vector store.
|
|
store, err := pinecone.New(
|
|
pinecone.WithHost("https://api.pinecone.io"),
|
|
pinecone.WithEmbedder(e),
|
|
pinecone.WithAPIKey("YOUR_API_KEY"),
|
|
pinecone.WithNameSpace(uuid.New().String()),
|
|
)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Add documents to the Pinecone vector store.
|
|
_, err = store.AddDocuments(context.Background(), []schema.Document{
|
|
{
|
|
PageContent: "Tokyo",
|
|
Metadata: map[string]any{
|
|
"population": 38,
|
|
"area": 2190,
|
|
},
|
|
},
|
|
{
|
|
PageContent: "Paris",
|
|
Metadata: map[string]any{
|
|
"population": 11,
|
|
"area": 105,
|
|
},
|
|
},
|
|
{
|
|
PageContent: "London",
|
|
Metadata: map[string]any{
|
|
"population": 9.5,
|
|
"area": 1572,
|
|
},
|
|
},
|
|
{
|
|
PageContent: "Santiago",
|
|
Metadata: map[string]any{
|
|
"population": 6.9,
|
|
"area": 641,
|
|
},
|
|
},
|
|
{
|
|
PageContent: "Buenos Aires",
|
|
Metadata: map[string]any{
|
|
"population": 15.5,
|
|
"area": 203,
|
|
},
|
|
},
|
|
{
|
|
PageContent: "Rio de Janeiro",
|
|
Metadata: map[string]any{
|
|
"population": 13.7,
|
|
"area": 1200,
|
|
},
|
|
},
|
|
{
|
|
PageContent: "Sao Paulo",
|
|
Metadata: map[string]any{
|
|
"population": 22.6,
|
|
"area": 1523,
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Search for similar documents.
|
|
docs, err := store.SimilaritySearch(ctx, "japan", 1)
|
|
fmt.Println(docs)
|
|
|
|
// Search for similar documents using score threshold.
|
|
docs, err = store.SimilaritySearch(ctx, "only cities in south america", 10, vectorstores.WithScoreThreshold(0.80))
|
|
fmt.Println(docs)
|
|
|
|
// Search for similar documents using score threshold and metadata filter.
|
|
filter := map[string]interface{}{
|
|
"$and": []map[string]interface{}{
|
|
{
|
|
"area": map[string]interface{}{
|
|
"$gte": 1000,
|
|
},
|
|
},
|
|
{
|
|
"population": map[string]interface{}{
|
|
"$gte": 15.5,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
docs, err = store.SimilaritySearch(ctx, "only cities in south america",
|
|
10,
|
|
vectorstores.WithScoreThreshold(0.80),
|
|
vectorstores.WithFilters(filter))
|
|
fmt.Println(docs)
|
|
}
|