Skip to content

Commit

Permalink
docs: update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
neon-sunset committed Sep 26, 2024
1 parent 8cd8b48 commit edfb41d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
24 changes: 20 additions & 4 deletions example/Example.CSharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@
// injecting it as a singleton into your DI container.
using var index = await pinecone.GetIndex(indexName);

// Define a helper method to generate random vectors,
// Pinecone disallows vectors with all zeros.
static float[] GetRandomVector(int dimension) =>
Enumerable
.Range(0, dimension)
.Select(_ => Random.Shared.NextSingle())
.ToArray();

var first = new Vector
{
Id = "first",
// Zeroed-out placeholder vector, this is where you put the embeddings unless using sparse vectors
Values = new float[1536],
Values = GetRandomVector(1536),
Metadata = new()
{
["new"] = true,
Expand All @@ -35,7 +42,7 @@
var second = new Vector
{
Id = "second",
Values = new float[1536],
Values = GetRandomVector(1536),
Metadata = new() { ["price"] = 100 }
};

Expand All @@ -55,14 +62,23 @@
}
};

// Wait a bit for the index to update
await Task.Delay(1000);

// Query the index by embedding and metadata filter
var results = await index.Query(
new float[1536],
GetRandomVector(1536),
topK: 3,
filter: priceRange,
includeMetadata: true);

Console.WriteLine(string.Join('\n', results.SelectMany(v => v.Metadata!)));

// List all vectors IDs in the index
await foreach (var id in index.List())
{
Console.WriteLine(id);
}

// Remove the example vectors we just added
await index.Delete(["first", "second"]);
6 changes: 5 additions & 1 deletion example/Example.FSharp/Example.FSharp.fsproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand All @@ -14,4 +14,8 @@
<ProjectReference Include="..\..\src\Pinecone.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FSharp.Control.TaskSeq" Version="0.4.0" />
</ItemGroup>

</Project>
10 changes: 9 additions & 1 deletion example/Example.FSharp/Program.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#nowarn "3391"
#nowarn "3391"
open System
open System.Collections.Generic
open System.Threading.Tasks
open FSharp.Control
open Pinecone

let createMetadata x =
Expand Down Expand Up @@ -39,6 +41,9 @@ let main = task {
// Specify metadata filter to query the index with
let priceRange = createMetadata ["price", createMetadata ["$gte", 75; "$lte", 125]]

// Wait a bit for the index to update
do! Task.Delay(1000)

// Query the index by embedding and metadata filter
let! results = index.Query(getRandomVector 1536, 3u, filter = priceRange, includeMetadata = true)
let metadata =
Expand All @@ -48,6 +53,9 @@ let main = task {
|> String.concat "\n"
printfn "%s" metadata

// List all vectors in the index
do! index.List() |> TaskSeq.iter (printfn "%s")

// Remove the example vectors we just added
do! index.Delete ["first"; "second"]
}
Expand Down

0 comments on commit edfb41d

Please sign in to comment.