Files
lancedb/docs/web/snippets/openai.mdx
T
Jack Ye b21fb2a4dc docs: bring the open-source documentation into this repository
The pages describing this code lived in lancedb/docs, a repository with ten
stars that neither contributing guide mentioned. A user who hit a wrong page
while reading the source had to find a separate, near-invisible repository to
fix it. These 102 pages now sit beside the code, and a documentation fix is a
pull request in the repository the reader already had open.

`docs/web` is a complete Mintlify site: `cd docs/web && mint dev` serves it with
no other checkout. Enterprise pages stay private and are merged in when the
published site is assembled, so links into them do not resolve here — every
open-source page does.

`docs/web-tests` holds the tests the code examples are extracted from. They ran
against a published wheel before; here they can run against this repository's
own SDK, so an example that stops compiling fails the pull request that broke
it rather than being noticed later.

Both directories sit beside `docs/src` and are invisible to it: mkdocs reads
`docs_dir: src`, so the SDK reference builds and deploys exactly as before.
2026-09-02 21:09:30 -07:00

7 lines
1.9 KiB
Plaintext

{/* Auto-generated by scripts/mdx_snippets_gen.py. Do not edit manually. */}
export const RsImports = "\nuse std::{iter::once, sync::Arc};\n\nuse arrow_array::{Float64Array, Int32Array, RecordBatch, RecordBatchIterator, StringArray};\nuse arrow_schema::{DataType, Field, Schema};\nuse futures::StreamExt;\nuse lancedb::{\n arrow::IntoArrow,\n connect,\n embeddings::{openai::OpenAIEmbeddingFunction, EmbeddingDefinition, EmbeddingFunction},\n query::{ExecutableQuery, QueryBase},\n Result,\n};\n";
export const RsOpenaiEmbeddings = "#[tokio::main]\nasync fn main() -> Result<()> {\n let tempdir = tempfile::tempdir().unwrap();\n let tempdir = tempdir.path().to_str().unwrap();\n let api_key = std::env::var(\"OPENAI_API_KEY\").expect(\"OPENAI_API_KEY is not set\");\n let embedding = Arc::new(OpenAIEmbeddingFunction::new_with_model(\n api_key,\n \"text-embedding-3-large\",\n )?);\n\n let db = connect(tempdir).execute().await?;\n db.embedding_registry()\n .register(\"openai\", embedding.clone())?;\n\n let table = db\n .create_table(\"vectors\", make_data())\n .add_embedding(EmbeddingDefinition::new(\n \"text\",\n \"openai\",\n Some(\"embeddings\"),\n ))?\n .execute()\n .await?;\n\n let query = Arc::new(StringArray::from_iter_values(once(\"something warm\")));\n let query_vector = embedding.compute_query_embeddings(query)?;\n let mut results = table\n .vector_search(query_vector)?\n .limit(1)\n .execute()\n .await?;\n\n let rb = results.next().await.unwrap()?;\n let out = rb\n .column_by_name(\"text\")\n .unwrap()\n .as_any()\n .downcast_ref::<StringArray>()\n .unwrap();\n let text = out.iter().next().unwrap().unwrap();\n println!(\"Closest match: {}\", text);\n Ok(())\n}\n";