Skip to content

Commit

Permalink
Add F# sample (#82)
Browse files Browse the repository at this point in the history
* Add F# sample

* Address PR suggestions from @Lanayx
  • Loading branch information
kant2002 authored Dec 12, 2023
1 parent 2afb55e commit 5c78d5a
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 7 deletions.
19 changes: 14 additions & 5 deletions Pinecone.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pinecone", "src\Pinecone.csproj", "{BD39ECA6-DEE9-4A5B-BC97-E4899F0E4EF6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pinecone", "src\Pinecone.csproj", "{BD39ECA6-DEE9-4A5B-BC97-E4899F0E4EF6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example", "example\Example.csproj", "{919B9AF0-F429-4F3B-8AD9-71E96F228BBD}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.CSharp", "example\Example.CSharp\Example.CSharp.csproj", "{919B9AF0-F429-4F3B-8AD9-71E96F228BBD}"
EndProject
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Example.FSharp", "example\Example.FSharp\Example.FSharp.fsproj", "{48198DC6-2D75-417F-8096-9FF9AC657511}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BD39ECA6-DEE9-4A5B-BC97-E4899F0E4EF6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BD39ECA6-DEE9-4A5B-BC97-E4899F0E4EF6}.Debug|Any CPU.Build.0 = Debug|Any CPU
Expand All @@ -24,5 +23,15 @@ Global
{919B9AF0-F429-4F3B-8AD9-71E96F228BBD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{919B9AF0-F429-4F3B-8AD9-71E96F228BBD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{919B9AF0-F429-4F3B-8AD9-71E96F228BBD}.Release|Any CPU.Build.0 = Release|Any CPU
{48198DC6-2D75-417F-8096-9FF9AC657511}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{48198DC6-2D75-417F-8096-9FF9AC657511}.Debug|Any CPU.Build.0 = Debug|Any CPU
{48198DC6-2D75-417F-8096-9FF9AC657511}.Release|Any CPU.ActiveCfg = Release|Any CPU
{48198DC6-2D75-417F-8096-9FF9AC657511}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {891CE120-06FE-4217-AAD2-C7D53DF53E48}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\src\Pinecone.csproj" />
<ProjectReference Include="..\..\src\Pinecone.csproj" />
</ItemGroup>

</Project>
File renamed without changes.
17 changes: 17 additions & 0 deletions example/Example.FSharp/Example.FSharp.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Pinecone.csproj" />
</ItemGroup>

</Project>
49 changes: 49 additions & 0 deletions example/Example.FSharp/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
open Pinecone
open System.Collections.Generic

let createMetadata x =
MetadataMap(x |> Seq.map (fun (k, m) -> KeyValuePair(k,m) ))

let main = task {
use pinecone = new PineconeClient("[api-key]", "[pinecone-env]")

// Check if the index exists and create it if it doesn't
// Depending on the storage type and infrastructure state this may take a while
// Free tier is limited to 1 index only
let indexName = "test-index"
let! indexList = pinecone.ListIndexes()
if indexList |> Array.contains indexName |> not then
do! pinecone.CreateIndex(indexName, 1536u, Metric.Cosine)

// Get the Pinecone index by name (uses gRPC by default).
// The index client is thread-safe, consider caching and/or
// injecting it as a singleton into your DI container.
use! index = pinecone.GetIndex(indexName)

let tags = [|"tag1" ; "tag2"|]
let first = Vector(Id = "first", Values = Array.zeroCreate 1536, Metadata = createMetadata["new", true; "price", 50; "tags", tags])
let second = Vector(Id = "second", Values = Array.zeroCreate 1536, Metadata = createMetadata["price", 50])

// Upsert vectors into the index
let! _ = index.Upsert [|first; second|]

// Specify metadata filter to query the index with
let priceRange = createMetadata["price", createMetadata["$gte", 75; "$lte", 125]]

// Partially update a vector (allows to update dense/sparse/metadata properties only)
do! index.Update("second", metadata = createMetadata["price", 99])

// Query the index by embedding and metadata filter
let! results = index.Query((Array.zeroCreate 1536), 3u, filter = priceRange, includeMetadata = true)
let metadata =
results
|> Seq.collect _.Metadata
|> Seq.map string
|> String.concat "\n"
printfn "%s" metadata

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

main.Wait()
6 changes: 5 additions & 1 deletion src/Types/VectorTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ public record ScoredVector
public MetadataMap? Metadata { get; init; }
}

public sealed class MetadataMap : Dictionary<string, MetadataValue> { }
public sealed class MetadataMap : Dictionary<string, MetadataValue>
{
public MetadataMap() : base() { }
public MetadataMap(IEnumerable<KeyValuePair<string, MetadataValue>> collection) : base(collection) { }
}

[JsonConverter(typeof(MetadataValueConverter))]
public readonly record struct MetadataValue
Expand Down

0 comments on commit 5c78d5a

Please sign in to comment.