Skip to content

Commit a33c4a6

Browse files
committed
Updated example [skip ci]
1 parent 4a95ca0 commit a33c4a6

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

examples/openai/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ publish = false
88
pgvector = { path = "../..", features = ["postgres"] }
99
postgres = "0.19"
1010
serde_json = "1"
11-
ureq = { version = "2", features = ["json"] }
11+
ureq = { version = "3", features = ["json"] }

examples/openai/src/main.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ fn main() -> Result<(), Box<dyn Error>> {
1212

1313
client.execute("CREATE EXTENSION IF NOT EXISTS vector", &[])?;
1414
client.execute("DROP TABLE IF EXISTS documents", &[])?;
15-
client.execute("CREATE TABLE documents (id serial PRIMARY KEY, content text, embedding vector(1536))", &[])?;
15+
client.execute(
16+
"CREATE TABLE documents (id serial PRIMARY KEY, content text, embedding vector(1536))",
17+
&[],
18+
)?;
1619

1720
let input = [
1821
"The dog is barking",
@@ -23,7 +26,10 @@ fn main() -> Result<(), Box<dyn Error>> {
2326

2427
for (content, embedding) in input.iter().zip(embeddings) {
2528
let embedding = Vector::from(embedding);
26-
client.execute("INSERT INTO documents (content, embedding) VALUES ($1, $2)", &[&content, &embedding])?;
29+
client.execute(
30+
"INSERT INTO documents (content, embedding) VALUES ($1, $2)",
31+
&[&content, &embedding],
32+
)?;
2733
}
2834

2935
let document_id = 2;
@@ -39,12 +45,13 @@ fn fetch_embeddings(input: &[&str]) -> Result<Vec<Vec<f32>>, Box<dyn Error>> {
3945
let api_key = std::env::var("OPENAI_API_KEY").or(Err("Set OPENAI_API_KEY"))?;
4046

4147
let response: Value = ureq::post("https://api.openai.com/v1/embeddings")
42-
.set("Authorization", &format!("Bearer {}", api_key))
43-
.send_json(ureq::json!({
48+
.header("Authorization", &format!("Bearer {}", api_key))
49+
.send_json(serde_json::json!({
4450
"input": input,
4551
"model": "text-embedding-3-small",
4652
}))?
47-
.into_json()?;
53+
.body_mut()
54+
.read_json()?;
4855

4956
let embeddings = response["data"]
5057
.as_array()

0 commit comments

Comments
 (0)