Skip to content

Commit

Permalink
chore: update example
Browse files Browse the repository at this point in the history
  • Loading branch information
neon-sunset committed Jul 6, 2023
1 parent 5c95f72 commit 1225f14
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 143 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/playground/bin/Debug/net8.0/Playground.dll",
"program": "${workspaceFolder}/example/bin/Debug/net8.0/Example.dll",
"args": [],
"cwd": "${workspaceFolder}/playground",
"cwd": "${workspaceFolder}/example",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
Expand Down
8 changes: 4 additions & 4 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"type": "process",
"args": [
"build",
"${workspaceFolder}/playground/Playground.csproj",
"${workspaceFolder}/example/Example.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
Expand All @@ -19,7 +19,7 @@
"type": "process",
"args": [
"publish",
"${workspaceFolder}/playground/Playground.csproj",
"${workspaceFolder}/example/Example.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
Expand All @@ -33,9 +33,9 @@
"watch",
"run",
"--project",
"${workspaceFolder}/playground/Playground.csproj"
"${workspaceFolder}/example/Example.csproj"
],
"problemMatcher": "$msCompile"
}
]
}
}
2 changes: 1 addition & 1 deletion Pinecone.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Playground", "playground\Playground.csproj", "{919B9AF0-F429-4F3B-8AD9-71E96F228BBD}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example", "example\Example.csproj", "{919B9AF0-F429-4F3B-8AD9-71E96F228BBD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
5 changes: 2 additions & 3 deletions playground/Playground.csproj → example/Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<StripSymbols>true</StripSymbols>
<WarningsAsErrors>nullable</WarningsAsErrors>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand All @@ -14,8 +14,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.5" />
<PackageReference Include="Microsoft.SemanticKernel" Version="0.13.277.1-preview" />
<PackageReference Include="Azure.AI.OpenAI" Version="1.0.0-beta.5" />
</ItemGroup>

</Project>
69 changes: 69 additions & 0 deletions example/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using Azure.AI.OpenAI;
using Pinecone;

const string indexName = "your-index";

var openAIKey = "your-key";
var pineconeKey = "your-key";
var pineconeEnv = "your-env";

using var pinecone = new PineconeClient(pineconeKey, pineconeEnv);

// 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
if (!(await pinecone.ListIndexes()).Contains(indexName))
{
await pinecone.CreateIndex(indexName, 1536, Metric.Cosine);
}

// Create an OpenAI Azure client and declare a helper method to embed our text
var openAI = new OpenAIClient(openAIKey);
async Task<float[]> Embed(string text)
{
var request = new EmbeddingsOptions(text);
var response = await openAI.GetEmbeddingsAsync("text-embedding-ada-002", request);
return response.Value.Data[0].Embedding.ToArray(); // IReadOnlyList<float>, really Microsoft?
}

// Get our Pinecone index (uses gRPC by default)
using var index = await pinecone.GetIndex(indexName);

var first = new Vector
{
Id = "first",
Values = await Embed("Hello world!"),
Metadata = new() { ["price"] = 50 }
};

var second = new Vector
{
Id = "second",
Values = await Embed("Hello world!"),
Metadata = new() { ["price"] = 100 }
};

// Upsert vectors into the index
await index.Upsert(new[] { first, second });

// Specify metadata filter to query the index with
var priceRange = new MetadataMap
{
["price"] = new MetadataMap
{
["$gte"] = 75,
["$lte"] = 125
}
};

// Query the index by embedding and metadata filter
var results = await index.Query(
await Embed("Hello world!"),
topK: 3,
filter: priceRange,
includeMetadata: true);

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

// Remove the example vectors we just added
await index.Delete(new[] { "first", "second" });
40 changes: 0 additions & 40 deletions playground/Benchmark.cs

This file was deleted.

93 changes: 0 additions & 93 deletions playground/Program.cs

This file was deleted.

0 comments on commit 1225f14

Please sign in to comment.