Skip to content

Commit 7c7e5fc

Browse files
committed
add upsert records
1 parent 3c43c45 commit 7c7e5fc

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

src/pinecone/data.rs

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ use crate::models::{
1313
DescribeIndexStatsResponse, FetchResponse, ListResponse, Metadata, Namespace, QueryResponse,
1414
SparseValues, UpdateResponse, UpsertResponse, Vector,
1515
};
16-
use crate::openapi::apis::vector_operations_api;
16+
use crate::openapi::apis::vector_operations_api::UpsertRecordsNamespaceError;
17+
use crate::openapi::apis::ResponseContent;
1718
use crate::protos;
1819

1920
#[derive(Debug, Clone)]
@@ -97,6 +98,73 @@ impl Index {
9798
Ok(response)
9899
}
99100

101+
pub async fn upsert_records(
102+
&mut self,
103+
namespace: &str,
104+
records: Vec<serde_json::Value>,
105+
) -> Result<(), PineconeError> {
106+
let configuration = self.client.openapi_config.clone();
107+
108+
let client = self.client.openapi_config.client.clone();
109+
110+
let uri_str = format!(
111+
"{}/records/namespaces/{namespace}/upsert",
112+
configuration.base_path,
113+
namespace = crate::openapi::apis::urlencode(namespace)
114+
);
115+
let mut req_builder = client.request(reqwest::Method::POST, uri_str.as_str());
116+
117+
if let Some(ref user_agent) = configuration.user_agent {
118+
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
119+
}
120+
if let Some(ref apikey) = configuration.api_key {
121+
let key = apikey.key.clone();
122+
let value = match apikey.prefix {
123+
Some(ref prefix) => format!("{} {}", prefix, key),
124+
None => key,
125+
};
126+
req_builder = req_builder.header("Api-Key", value);
127+
};
128+
req_builder = req_builder.header(reqwest::header::CONTENT_TYPE, "application/x-ndjson");
129+
130+
let ndjson = records
131+
.iter()
132+
.map(|r| r.to_string())
133+
.collect::<Vec<String>>()
134+
.join("\n");
135+
136+
req_builder = req_builder.json(&ndjson);
137+
138+
let req = req_builder
139+
.build()
140+
.map_err(|e| PineconeError::ReqwestError {
141+
source: anyhow::Error::from(e),
142+
})?;
143+
let resp = client
144+
.execute(req)
145+
.await
146+
.map_err(|e| PineconeError::ReqwestError {
147+
source: anyhow::Error::from(e),
148+
})?;
149+
150+
let status = resp.status();
151+
let content = resp.text().await.map_err(|e| PineconeError::ReqwestError {
152+
source: anyhow::Error::from(e),
153+
})?;
154+
155+
if !status.is_client_error() && !status.is_server_error() {
156+
Ok(())
157+
} else {
158+
let entity: Option<UpsertRecordsNamespaceError> = serde_json::from_str(&content).ok();
159+
Err(ResponseContent {
160+
status,
161+
content,
162+
entity,
163+
}
164+
.into())
165+
}
166+
}
167+
100168
/// The list operation lists the IDs of vectors in a single namespace of a serverless index. An optional prefix can be passed to limit the results to IDs with a common prefix.
101169
///
102170
/// ### Arguments

0 commit comments

Comments
 (0)