@@ -8,23 +8,23 @@ Pinecone <- R6::R6Class(
88 pinecone_api_key <- Sys.getenv(" PINECONE_API_KEY" )
99
1010 url <- paste0(" https://api.pinecone.io/indexes/" , private $ .index )
11-
12- httr2 :: request(url ) | >
13- httr2 :: req_headers(" Api-Key" = pinecone_api_key ) | >
14- httr2 :: req_perform() | >
11+
12+ httr2 :: request(url ) | >
13+ httr2 :: req_headers(" Api-Key" = pinecone_api_key ) | >
14+ httr2 :: req_perform() | >
1515 httr2 :: resp_body_json()
16- },
17-
16+ },
17+
1818 write_record = function (id , text , metadata = list ()) {
19-
20- pinecone_api_key <- Sys.getenv(" PINECONE_API_KEY" )
21-
22- url <- paste0(" https://" , private $ .index_host )
23-
19+
20+ pinecone_api_key <- Sys.getenv(" PINECONE_API_KEY" )
21+
22+ url <- paste0(" https://" , private $ .index_host )
23+
2424 embeddings <- private $ .get_embeddings(text = text )
25-
25+
2626 metadata $ text <- text
27-
27+
2828 body <- list (
2929 namespace = private $ .namespace ,
3030 vectors = list (
@@ -33,28 +33,28 @@ Pinecone <- R6::R6Class(
3333 metadata = metadata
3434 )
3535 )
36-
37- request <- httr2 :: request(url ) | >
38- httr2 :: req_url_path_append(" vectors/upsert" ) | >
36+
37+ request <- httr2 :: request(url ) | >
38+ httr2 :: req_url_path_append(" vectors/upsert" ) | >
3939 httr2 :: req_headers(
4040 " Api-Key" = pinecone_api_key ,
4141 " X-Pinecone-API-Version" = " 2024-10"
42- ) | >
43- httr2 :: req_body_json(body )
44-
45- response <- request | >
42+ ) | >
43+ httr2 :: req_body_json(body )
44+
45+ response <- request | >
4646 httr2 :: req_perform()
47-
47+
4848 response_body <- httr2 :: resp_body_json(response )
4949 response_body
5050 },
5151
5252 read_record = function (id ) {
5353
5454 pinecone_api_key <- Sys.getenv(" PINECONE_API_KEY" )
55-
55+
5656 url <- paste0(" https://" , private $ .index_host )
57-
57+
5858 request <- httr2 :: request(url ) | >
5959 httr2 :: req_url_path_append(" vectors" ) | >
6060 httr2 :: req_url_path_append(" fetch" ) | >
@@ -65,58 +65,115 @@ Pinecone <- R6::R6Class(
6565 httr2 :: req_headers(
6666 " Api-Key" = pinecone_api_key ,
6767 " X-Pinecone-API-Version" = " 2024-10"
68- )
69-
70- response <- request | >
68+ )
69+
70+ response <- request | >
7171 httr2 :: req_perform()
72-
72+
7373 response_body <- httr2 :: resp_body_json(response )
7474 results <- response_body $ vectors
75-
76- results
75+
76+ results
7777 },
7878
79-
79+
8080 find_records = function (query , top_k = 1 ) {
81-
81+
8282 embeddings <- private $ .get_embeddings(query )
83-
83+
8484 pinecone_api_key <- Sys.getenv(" PINECONE_API_KEY" )
85-
85+
8686 url <- paste0(" https://" , private $ .index_host )
87-
87+
8888 body <- list (
8989 namespace = private $ .namespace ,
9090 vector = embeddings ,
9191 topK = top_k ,
9292 includeValues = FALSE ,
9393 includeMetadata = TRUE
9494 )
95-
95+
9696 request <- httr2 :: request(url ) | >
9797 httr2 :: req_url_path_append(" query" ) | >
9898 httr2 :: req_headers(
9999 " Api-Key" = pinecone_api_key ,
100100 " X-Pinecone-API-Version" = " 2024-10"
101101 ) | >
102102 httr2 :: req_body_json(body )
103-
104- response <- request | >
103+
104+ response <- request | >
105105 httr2 :: req_perform()
106-
106+
107107 response_body <- httr2 :: resp_body_json(response )
108108 results <- response_body $ matches
109-
110- results | >
109+
110+ results | >
111111 purrr :: map(function (result ) {
112112 result $ values <- NULL
113113 result
114114 })
115+ },
116+
117+ list_record_IDs = function () {
118+
119+ pinecone_api_key <- Sys.getenv(" PINECONE_API_KEY" )
120+
121+ url <- paste0(" https://" , private $ .index_host )
122+
123+ response_body <- NULL
124+ has_next_page <- TRUE
125+ record_ids <- c()
126+
127+ while (has_next_page ) {
128+
129+ request <- httr2 :: request(url ) | >
130+ httr2 :: req_url_path_append(" vectors" ) | >
131+ httr2 :: req_url_path_append(" list" ) | >
132+ httr2 :: req_url_query(
133+ namespace = private $ .namespace ,
134+ paginationToken = response_body $ pagination $ `next`
135+ ) | >
136+ httr2 :: req_headers(
137+ " Api-Key" = pinecone_api_key ,
138+ " X-Pinecone-API-Version" = " 2024-10"
139+ )
140+
141+ response <- request | >
142+ httr2 :: req_perform()
143+
144+ response_body <- httr2 :: resp_body_json(response )
145+ record_ids <- c(record_ids ,
146+ purrr :: map_vec(response_body $ vectors , ~ . $ id ))
147+ has_next_page <- " pagination" %in% names(response_body )
148+ }
149+
150+ return (record_ids )
151+ },
152+
153+ purge_records = function (ids ) {
154+ pinecone_api_key <- Sys.getenv(" PINECONE_API_KEY" )
155+
156+ url <- paste0(" https://" , private $ .index_host )
157+
158+ body <- list (
159+ ids = ids ,
160+ namespace = private $ .namespace
161+ )
162+
163+ httr2 :: request(url ) | >
164+ httr2 :: req_url_path_append(" vectors" ) | >
165+ httr2 :: req_url_path_append(" delete" ) | >
166+ httr2 :: req_headers(
167+ " Api-Key" = pinecone_api_key ,
168+ " X-Pinecone-API-Version" = " 2024-10"
169+ ) | >
170+ httr2 :: req_body_json(body ) | >
171+ httr2 :: req_perform()
115172 }
116173 ),
117174
118175 active = list (
119-
176+
120177 namespace = function (value ) {
121178 if (missing(value )) return (private $ .namespace )
122179 private $ .namespace <- value
@@ -127,14 +184,14 @@ Pinecone <- R6::R6Class(
127184 private $ .index <- value
128185 }
129186 ),
130-
187+
131188 private = list (
132-
189+
133190 .project_id = NULL ,
134191 .index = NULL ,
135192 .namespace = NULL ,
136193 .index_host = NULL ,
137-
194+
138195 .initialize = function (index , namespace ) {
139196
140197 private $ .index <- index
@@ -143,37 +200,37 @@ Pinecone <- R6::R6Class(
143200 },
144201
145202 .get_embeddings = function (text ) {
146-
147- pinecone_api_key <- Sys.getenv(" PINECONE_API_KEY" )
148-
203+
204+ pinecone_api_key <- Sys.getenv(" PINECONE_API_KEY" )
205+
149206 url <- " https://api.pinecone.io"
150-
207+
151208 body <- list (
152209 model = " multilingual-e5-large" ,
153210 parameters = list (
154211 input_type = " passage" ,
155212 truncate = " END"
156- ),
213+ ),
157214 inputs = list (
158215 list (text = text )
159- )
216+ )
160217 )
161218
162- request <- httr2 :: request(url ) | >
163- httr2 :: req_url_path_append(" embed" ) | >
219+ request <- httr2 :: request(url ) | >
220+ httr2 :: req_url_path_append(" embed" ) | >
164221 httr2 :: req_headers(
165222 " Api-Key" = pinecone_api_key ,
166223 " X-Pinecone-API-Version" = " 2024-10"
167- ) | >
168- httr2 :: req_body_json(body )
169-
170- response <- request | >
224+ ) | >
225+ httr2 :: req_body_json(body )
226+
227+ response <- request | >
171228 httr2 :: req_perform()
172-
229+
173230 response_body <- httr2 :: resp_body_json(response )
174-
231+
175232 response_body $ data [[1 ]]$ values | > unlist()
176-
233+
177234 }
178235 )
179236)
0 commit comments