@@ -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
6464fn 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}
0 commit comments