Skip to content

Commit cb90bc8

Browse files
committed
Updated examples for Zig 0.16.0
1 parent be832f9 commit cb90bc8

8 files changed

Lines changed: 56 additions & 60 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/.zig-cache
22
/zig-out
3+
/zig-pkg

‎build.zig‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ pub fn build(b: *std.Build) void {
1717
.root_module = b.createModule(.{
1818
.root_source_file = b.path("examples/libpq.zig"),
1919
.target = b.graph.host,
20+
.link_libc = true,
2021
}),
2122
});
22-
libpqExe.linkSystemLibrary("pq");
23-
libpqExe.linkLibC();
23+
libpqExe.root_module.linkSystemLibrary("pq", .{});
2424
b.installArtifact(libpqExe);
2525

2626
const openaiExe = b.addExecutable(.{

‎build.zig.zon‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
.minimum_zig_version = "0.14.0",
66
.dependencies = .{
77
.pg = .{
8-
.url = "git+https://github.com/karlseguin/pg.zig?ref=master#f930e90b735ebadf28244c7a31e9c442b264b544",
9-
.hash = "pg-0.0.0-Wp_7gfoJBgDfkQBaWRTwjYcTdNyS8UHRDmMTs4NuSmxY",
8+
.url = "git+https://github.com/karlseguin/pg.zig?ref=master#aef360fef7feb7a65a2d4b6347f413e988845dd6",
9+
.hash = "pg-0.0.0-Wp_7gUN_BgC96OS2Q1oxXDjSOkdaSFBP_mYrmOzIw0m3",
1010
},
1111
},
1212
.paths = .{

‎examples/cohere.zig‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ const Embeddings = struct {
2222
}
2323
};
2424

25-
fn embed(allocator: std.mem.Allocator, texts: []const []const u8, inputType: []const u8, apiKey: []const u8) !Embeddings {
26-
var client = std.http.Client{ .allocator = allocator };
25+
fn embed(io: std.Io, allocator: std.mem.Allocator, texts: []const []const u8, inputType: []const u8, apiKey: []const u8) !Embeddings {
26+
var client = std.http.Client{ .allocator = allocator, .io = io };
2727
defer client.deinit();
2828

2929
const url = "https://api.cohere.com/v2/embed";
@@ -64,23 +64,22 @@ fn embed(allocator: std.mem.Allocator, texts: []const []const u8, inputType: []c
6464
fn bitString(allocator: std.mem.Allocator, data: []const u8) !std.array_list.Managed(u8) {
6565
var buf = std.array_list.Managed(u8).init(allocator);
6666
for (data) |v| {
67-
try buf.writer().print("{b:08}", .{v});
67+
try buf.print("{b:08}", .{v});
6868
}
6969
return buf;
7070
}
7171

72-
pub fn main() !void {
73-
const apiKey = std.posix.getenv("CO_API_KEY") orelse {
72+
pub fn main(init: std.process.Init) !void {
73+
const apiKey = init.environ_map.get("CO_API_KEY") orelse {
7474
std.debug.print("Set CO_API_KEY\n", .{});
7575
std.process.exit(1);
7676
};
7777

78-
var gpa = std.heap.GeneralPurposeAllocator(.{}).init;
79-
defer std.debug.assert(gpa.deinit() == .ok);
80-
const allocator = gpa.allocator();
78+
const allocator = init.gpa;
79+
const io = init.io;
8180

82-
var pool = try pg.Pool.init(allocator, .{ .auth = .{
83-
.username = std.posix.getenv("USER").?,
81+
var pool = try pg.Pool.init(io, allocator, .{ .auth = .{
82+
.username = init.environ_map.get("USER").?,
8483
.database = "pgvector_example",
8584
} });
8685
defer pool.deinit();
@@ -93,7 +92,7 @@ pub fn main() !void {
9392
_ = try conn.exec("CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding bit(1536))", .{});
9493

9594
const documents = [_][]const u8{ "The dog is barking", "The cat is purring", "The bear is growling" };
96-
var documentEmbeddings = try embed(allocator, &documents, "search_document", apiKey);
95+
var documentEmbeddings = try embed(io, allocator, &documents, "search_document", apiKey);
9796
defer documentEmbeddings.deinit();
9897
for (&documents, 0..) |content, i| {
9998
var bit = try bitString(allocator, documentEmbeddings.get(i).?);
@@ -103,14 +102,14 @@ pub fn main() !void {
103102
}
104103

105104
const query = "forest";
106-
var queryEmbeddings = try embed(allocator, &[_][]const u8{query}, "search_query", apiKey);
105+
var queryEmbeddings = try embed(io, allocator, &[_][]const u8{query}, "search_query", apiKey);
107106
defer queryEmbeddings.deinit();
108107
var queryBit = try bitString(allocator, queryEmbeddings.get(0).?);
109108
defer queryBit.deinit();
110109
var result = try conn.query("SELECT content FROM documents ORDER BY embedding <~> $1 LIMIT 5", .{queryBit.items});
111110
defer result.deinit();
112111
while (try result.next()) |row| {
113-
const content = row.get([]const u8, 0);
112+
const content = try row.get([]const u8, 0);
114113
std.debug.print("{s}\n", .{content});
115114
}
116115
}

‎examples/hybrid.zig‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ const Embeddings = struct {
1818
}
1919
};
2020

21-
fn embed(allocator: std.mem.Allocator, input: []const []const u8, _: []const u8) !Embeddings {
22-
var client = std.http.Client{ .allocator = allocator };
21+
fn embed(io: std.Io, allocator: std.mem.Allocator, input: []const []const u8, _: []const u8) !Embeddings {
22+
var client = std.http.Client{ .allocator = allocator, .io = io };
2323
defer client.deinit();
2424

2525
// TODO nomic-embed-text uses a task prefix
@@ -52,13 +52,12 @@ fn embed(allocator: std.mem.Allocator, input: []const []const u8, _: []const u8)
5252
return Embeddings{ .parsed = parsed };
5353
}
5454

55-
pub fn main() !void {
56-
var gpa = std.heap.GeneralPurposeAllocator(.{}).init;
57-
defer std.debug.assert(gpa.deinit() == .ok);
58-
const allocator = gpa.allocator();
55+
pub fn main(init: std.process.Init) !void {
56+
const allocator = init.gpa;
57+
const io = init.io;
5958

60-
var pool = try pg.Pool.init(allocator, .{ .auth = .{
61-
.username = std.posix.getenv("USER").?,
59+
var pool = try pg.Pool.init(io, allocator, .{ .auth = .{
60+
.username = init.environ_map.get("USER").?,
6261
.database = "pgvector_example",
6362
} });
6463
defer pool.deinit();
@@ -72,7 +71,7 @@ pub fn main() !void {
7271
_ = try conn.exec("CREATE INDEX ON documents USING GIN (to_tsvector('english', content))", .{});
7372

7473
const documents = [_][]const u8{ "The dog is barking", "The cat is purring", "The bear is growling" };
75-
var documentEmbeddings = try embed(allocator, &documents, "search_document");
74+
var documentEmbeddings = try embed(io, allocator, &documents, "search_document");
7675
defer documentEmbeddings.deinit();
7776
for (&documents, 0..) |content, i| {
7877
const params = .{ content, documentEmbeddings.get(i) };
@@ -104,14 +103,14 @@ pub fn main() !void {
104103
;
105104

106105
const query = "growling bear";
107-
var queryEmbeddings = try embed(allocator, &[_][]const u8{query}, "search_query");
106+
var queryEmbeddings = try embed(io, allocator, &[_][]const u8{query}, "search_query");
108107
const k = 60;
109108
defer queryEmbeddings.deinit();
110109
var result = try conn.query(sql, .{ query, queryEmbeddings.get(0), k });
111110
defer result.deinit();
112111
while (try result.next()) |row| {
113-
const id = row.get(i64, 0);
114-
const score = row.get(f64, 1);
112+
const id = try row.get(i64, 0);
113+
const score = try row.get(f64, 1);
115114
std.debug.print("document: {d} | RRF score: {d}\n", .{ id, score });
116115
}
117116
}

‎examples/openai.zig‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ const Embeddings = struct {
2222
}
2323
};
2424

25-
fn embed(allocator: std.mem.Allocator, input: []const []const u8, apiKey: []const u8) !Embeddings {
26-
var client = std.http.Client{ .allocator = allocator };
25+
fn embed(io: std.Io, allocator: std.mem.Allocator, input: []const []const u8, apiKey: []const u8) !Embeddings {
26+
var client = std.http.Client{ .allocator = allocator, .io = io };
2727
defer client.deinit();
2828

2929
const url = "https://api.openai.com/v1/embeddings";
@@ -59,18 +59,17 @@ fn embed(allocator: std.mem.Allocator, input: []const []const u8, apiKey: []cons
5959
return Embeddings{ .parsed = parsed };
6060
}
6161

62-
pub fn main() !void {
63-
const apiKey = std.posix.getenv("OPENAI_API_KEY") orelse {
62+
pub fn main(init: std.process.Init) !void {
63+
const apiKey = init.environ_map.get("OPENAI_API_KEY") orelse {
6464
std.debug.print("Set OPENAI_API_KEY\n", .{});
6565
std.process.exit(1);
6666
};
6767

68-
var gpa = std.heap.GeneralPurposeAllocator(.{}).init;
69-
defer std.debug.assert(gpa.deinit() == .ok);
70-
const allocator = gpa.allocator();
68+
const allocator = init.gpa;
69+
const io = init.io;
7170

72-
var pool = try pg.Pool.init(allocator, .{ .auth = .{
73-
.username = std.posix.getenv("USER").?,
71+
var pool = try pg.Pool.init(io, allocator, .{ .auth = .{
72+
.username = init.environ_map.get("USER").?,
7473
.database = "pgvector_example",
7574
} });
7675
defer pool.deinit();
@@ -83,20 +82,20 @@ pub fn main() !void {
8382
_ = try conn.exec("CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding vector(1536))", .{});
8483

8584
const documents = [_][]const u8{ "The dog is barking", "The cat is purring", "The bear is growling" };
86-
var documentEmbeddings = try embed(allocator, &documents, apiKey);
85+
var documentEmbeddings = try embed(io, allocator, &documents, apiKey);
8786
defer documentEmbeddings.deinit();
8887
for (&documents, 0..) |content, i| {
8988
const params = .{ content, documentEmbeddings.get(i) };
9089
_ = try conn.exec("INSERT INTO documents (content, embedding) VALUES ($1, $2::float4[])", params);
9190
}
9291

9392
const query = "forest";
94-
var queryEmbeddings = try embed(allocator, &[_][]const u8{query}, apiKey);
93+
var queryEmbeddings = try embed(io, allocator, &[_][]const u8{query}, apiKey);
9594
defer queryEmbeddings.deinit();
9695
var result = try conn.query("SELECT content FROM documents ORDER BY embedding <=> $1::float4[]::vector LIMIT 5", .{queryEmbeddings.get(0)});
9796
defer result.deinit();
9897
while (try result.next()) |row| {
99-
const content = row.get([]const u8, 0);
98+
const content = try row.get([]const u8, 0);
10099
std.debug.print("{s}\n", .{content});
101100
}
102101
}

‎examples/pg.zig‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ const Vector = struct {
1414
}
1515
};
1616

17-
pub fn main() !void {
18-
var gpa = std.heap.GeneralPurposeAllocator(.{}).init;
19-
defer std.debug.assert(gpa.deinit() == .ok);
20-
const allocator = gpa.allocator();
17+
pub fn main(init: std.process.Init) !void {
18+
const allocator = init.gpa;
19+
const io = init.io;
2120

22-
var pool = try pg.Pool.init(allocator, .{ .auth = .{
23-
.username = std.posix.getenv("USER").?,
21+
var pool = try pg.Pool.init(io, allocator, .{ .auth = .{
22+
.username = init.environ_map.get("USER").?,
2423
.database = "pgvector_zig_test",
2524
} });
2625
defer pool.deinit();
@@ -41,8 +40,8 @@ pub fn main() !void {
4140
var result = try conn.query("SELECT * FROM pg_items ORDER BY embedding <-> $1::float4[]::vector LIMIT 5", queryParams);
4241
defer result.deinit();
4342
while (try result.next()) |row| {
44-
const id = row.get(i64, 0);
45-
var embedding = try Vector.decode(allocator, row.get([]const u8, 1));
43+
const id = try row.get(i64, 0);
44+
var embedding = try Vector.decode(allocator, try row.get([]const u8, 1));
4645
defer embedding.deinit();
4746
std.debug.print("{d}: {any}\n", .{ id, embedding.items });
4847
}

‎examples/sparse.zig‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ const Embeddings = struct {
2121
}
2222
};
2323

24-
fn embed(allocator: std.mem.Allocator, inputs: []const []const u8) !Embeddings {
25-
var client = std.http.Client{ .allocator = allocator };
24+
fn embed(io: std.Io, allocator: std.mem.Allocator, inputs: []const []const u8) !Embeddings {
25+
var client = std.http.Client{ .allocator = allocator, .io = io };
2626
defer client.deinit();
2727

2828
const url = "http://localhost:3000/embed";
@@ -52,13 +52,12 @@ fn embed(allocator: std.mem.Allocator, inputs: []const []const u8) !Embeddings {
5252
return Embeddings{ .parsed = parsed };
5353
}
5454

55-
pub fn main() !void {
56-
var gpa = std.heap.GeneralPurposeAllocator(.{}).init;
57-
defer std.debug.assert(gpa.deinit() == .ok);
58-
const allocator = gpa.allocator();
55+
pub fn main(init: std.process.Init) !void {
56+
const allocator = init.gpa;
57+
const io = init.io;
5958

60-
var pool = try pg.Pool.init(allocator, .{ .auth = .{
61-
.username = std.posix.getenv("USER").?,
59+
var pool = try pg.Pool.init(io, allocator, .{ .auth = .{
60+
.username = init.environ_map.get("USER").?,
6261
.database = "pgvector_example",
6362
} });
6463
defer pool.deinit();
@@ -71,20 +70,20 @@ pub fn main() !void {
7170
_ = try conn.exec("CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding sparsevec(30522))", .{});
7271

7372
const documents = [_][]const u8{ "The dog is barking", "The cat is purring", "The bear is growling" };
74-
var documentEmbeddings = try embed(allocator, &documents);
73+
var documentEmbeddings = try embed(io, allocator, &documents);
7574
defer documentEmbeddings.deinit();
7675
for (&documents, 0..) |content, i| {
7776
const params = .{ content, documentEmbeddings.get(i) };
7877
_ = try conn.exec("INSERT INTO documents (content, embedding) VALUES ($1, $2::float4[])", params);
7978
}
8079

8180
const query = "forest";
82-
var queryEmbeddings = try embed(allocator, &[_][]const u8{query});
81+
var queryEmbeddings = try embed(io, allocator, &[_][]const u8{query});
8382
defer queryEmbeddings.deinit();
8483
var result = try conn.query("SELECT content FROM documents ORDER BY embedding <#> $1::float4[]::sparsevec LIMIT 5", .{queryEmbeddings.get(0)});
8584
defer result.deinit();
8685
while (try result.next()) |row| {
87-
const content = row.get([]const u8, 0);
86+
const content = try row.get([]const u8, 0);
8887
std.debug.print("{s}\n", .{content});
8988
}
9089
}

0 commit comments

Comments
 (0)