Skip to content

Commit b264c4a

Browse files
committed
Basic .NET warpper prototype
1 parent 55a9b86 commit b264c4a

File tree

5 files changed

+612
-7
lines changed

5 files changed

+612
-7
lines changed

.gitignore

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
.vs/
33
/x64/Debug
44
/x64/Release
5-
/build
65
/dist
76
embeddings.egg-info/
8-
/test.db
9-
/examples/dotnet/bin/Debug
10-
/examples/dotnet/obj/Debug
7+
/src/embeddings.vcxproj.user
118
/embeddings/embeddings.exp
129
/embeddings/embeddings.lib
1310
/embeddings/embeddings.pdb
1411
/embeddings/embeddings.pyd
15-
/examples/dotnet
16-
/src/embeddings.cs
17-
/src/embeddings.vcxproj.user
12+
/examples/dotnet/bin/Debug
13+
/examples/dotnet/obj/Debug
14+
/examples/dotnet/obj/Release
15+
/examples/dotnet/bin/Release

examples/dotnet/App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
5+
</startup>
6+
</configuration>

examples/dotnet/dotnet.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using embeddings;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace dotnet {
9+
10+
class Demo {
11+
static unsafe void Main() {
12+
13+
IntPtr db = Embeddings.AllocDb();
14+
15+
// OPEN (like mode "a+"): GENERIC_READ | FILE_APPEND_DATA, OPEN_ALWAYS
16+
const uint GENERIC_READ = 0x80000000;
17+
const uint FILE_APPEND_DATA = 0x00000004;
18+
const uint OPEN_ALWAYS = 4;
19+
20+
bool ok = Embeddings.Open(
21+
db,
22+
"test.db",
23+
GENERIC_READ | FILE_APPEND_DATA,
24+
OPEN_ALWAYS,
25+
dim: 128 /* floats per vector */
26+
);
27+
if (!ok) throw new Exception("Open failed");
28+
29+
// APPEND
30+
float[] vec = new float[128];
31+
vec[0] = 1.0f;
32+
Uiid id = Embeddings.GuidToUiid(Guid.NewGuid());
33+
fixed (float* pVec = vec) {
34+
bool appended = Embeddings.Append(
35+
db,
36+
ref id,
37+
pVec,
38+
(uint)(vec.Length * sizeof(float)),
39+
flush: false
40+
);
41+
if (!appended) throw new Exception("Append failed");
42+
}
43+
44+
// SEARCH top-5
45+
fixed (float* pQuery = vec) {
46+
Score[] scores;
47+
int count = Embeddings.Search(
48+
db,
49+
pQuery,
50+
(uint)vec.Length,
51+
topk: 5,
52+
threshold: 0.0f,
53+
out scores
54+
);
55+
56+
Console.WriteLine("count = " + count);
57+
for (int i = 0; i < count; i++) {
58+
Guid gid = Embeddings.UiidToGuid(ref scores[i].id);
59+
Console.WriteLine("{0} {1}", gid, scores[i].score);
60+
}
61+
}
62+
63+
// CURSOR scan (no extra copies per row)
64+
IntPtr cur = Embeddings.CursorOpen(db);
65+
uint err;
66+
embeddings.RecordView rec;
67+
while (Embeddings.CursorRead(cur, out rec, out err)) {
68+
Guid gid = rec.ToGuid();
69+
Console.WriteLine("Row guid = {0}, firstByteOfBlob = {1}",
70+
gid,
71+
rec.Blob[0]);
72+
}
73+
// err == ERROR_HANDLE_EOF when done.
74+
Embeddings.CursorClose(cur);
75+
76+
// CLEANUP
77+
Embeddings.Close(db);
78+
Embeddings.FreeDb(db);
79+
}
80+
}
81+
82+
}

examples/dotnet/dotnet.csproj

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{3A34259E-4063-4A4D-AADC-50AE19555C4A}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>dotnet</RootNamespace>
10+
<AssemblyName>dotnet</AssemblyName>
11+
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>x64</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
26+
<Prefer32Bit>false</Prefer32Bit>
27+
</PropertyGroup>
28+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
29+
<PlatformTarget>x86</PlatformTarget>
30+
<DebugType>pdbonly</DebugType>
31+
<Optimize>true</Optimize>
32+
<OutputPath>bin\Release\</OutputPath>
33+
<DefineConstants>TRACE</DefineConstants>
34+
<ErrorReport>prompt</ErrorReport>
35+
<WarningLevel>4</WarningLevel>
36+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
37+
</PropertyGroup>
38+
<ItemGroup>
39+
<Reference Include="System" />
40+
<Reference Include="System.Core" />
41+
<Reference Include="System.Xml.Linq" />
42+
<Reference Include="System.Data.DataSetExtensions" />
43+
<Reference Include="Microsoft.CSharp" />
44+
<Reference Include="System.Data" />
45+
<Reference Include="System.Net.Http" />
46+
<Reference Include="System.Xml" />
47+
</ItemGroup>
48+
<ItemGroup>
49+
<Compile Include="..\..\src\embeddings.cs">
50+
<Link>embeddings.cs</Link>
51+
</Compile>
52+
<Compile Include="dotnet.cs" />
53+
</ItemGroup>
54+
<ItemGroup>
55+
<None Include="App.config" />
56+
</ItemGroup>
57+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
58+
</Project>

0 commit comments

Comments
 (0)