{/* Auto-generated by scripts/mdx_snippets_gen.py. Do not edit manually. */} export const PyBasicAddColumns = "table.add_columns(\n {\n \"power\": \"cast(((stats.strength + stats.courage + stats.magic + stats.wisdom) / 4.0) as float)\"\n }\n)\n"; export const PyBasicAddData = "magical_characters = [\n {\n \"id\": 9,\n \"name\": \"Morgan le Fay\",\n \"role\": \"Sorceress\",\n \"description\": \"A powerful enchantress, Arthur's half-sister, and a complex figure who oscillates between aiding and opposing Camelot.\",\n \"vector\": [0.10, 0.84, 0.25, 0.70],\n \"stats\": { \"strength\": 2, \"courage\": 3, \"magic\": 5, \"wisdom\": 4 }\n },\n {\n \"id\": 10,\n \"name\": \"The Lady of the Lake\",\n \"role\": \"Mystical Guardian\",\n \"description\": \"A mysterious supernatural figure associated with Avalon, known for giving Arthur the sword Excalibur.\",\n \"vector\": [0.00, 0.90, 0.58, 0.88],\n \"stats\": { \"strength\": 2, \"courage\": 3, \"magic\": 5, \"wisdom\": 5 }\n }\n]\ntable.add(magical_characters)\n"; export const PyBasicAsyncApi = "import lancedb\n\nasync_db = await lancedb.connect_async(uri)\nasync_table = await async_db.create_table(\n \"camelot_async\",\n data=data,\n mode=\"overwrite\",\n)\n\nquery_vector = [0.03, 0.85, 0.61, 0.90]\nasync_results = await (\n await async_table.search(query_vector)\n).limit(5).select([\"name\", \"role\", \"description\"]).to_polars()\nprint(async_results)\n"; export const PyBasicCreateEmptyTable = "schema = pa.schema(\n [\n pa.field(\"id\", pa.uint16()),\n pa.field(\"name\", pa.string()),\n pa.field(\"role\", pa.string()),\n pa.field(\"description\", pa.string()),\n pa.field(\"vector\", pa.list_(pa.float32(), 4)),\n pa.field(\n \"stats\",\n pa.struct(\n [\n pa.field(\"strength\", pa.int8()),\n pa.field(\"courage\", pa.int8()),\n pa.field(\"magic\", pa.int8()),\n pa.field(\"wisdom\", pa.int8()),\n ]\n ),\n ),\n ]\n)\ndb.create_table(\"camelot_pa\", schema=schema, mode=\"overwrite\")\n"; export const PyBasicCreateTable = "table = db.create_table(\"camelot\", data=data, mode=\"overwrite\")\n"; export const PyBasicCreateTablePandas = "pandas_df = pd.DataFrame(data)\ntable_pd = db.create_table(\"camelot_pd\", data=pandas_df, mode=\"overwrite\")\n"; export const PyBasicCreateTablePolars = "polars_df = pl.DataFrame(data)\ntable_pl = db.create_table(\"camelot_pl\", data=polars_df, mode=\"overwrite\")\n"; export const PyBasicDeleteRows = "table.delete('role = \"Traitor Knight\"')\n"; export const PyBasicDropColumns = "table.drop_columns([\"power\"])\n"; export const PyBasicDropTable = "db.drop_table(\"camelot\")\n"; export const PyBasicImports = "import json\n\nimport lancedb\nimport pandas as pd\nimport polars as pl\nimport pyarrow as pa\n"; export const PyBasicOpenTable = "table = db.open_table(\"camelot\")\n"; export const PyBasicSortPolars = "# Sort Polars DataFrame by power in descending order\nprint(r4.sort(\"power\", descending=True).limit(5))\n"; export const PyBasicVectorSearch = "query_vector = [0.03, 0.85, 0.61, 0.90]\nresult = table.search(query_vector).limit(5).to_polars()\nprint(result)\n"; export const PyBasicVectorSearchQ1 = "# Who are the characters similar to \"wizard\"?\nquery_vector_1 = [0.03, 0.85, 0.61, 0.90]\nr1 = (\n table.search(query_vector_1)\n .limit(5)\n .select([\"name\", \"role\", \"description\"])\n .to_polars()\n)\nprint(r1)\n"; export const PyBasicVectorSearchQ2 = "# Who are the characters with high magic stats?\nquery_vector_2 = [0.03, 0.85, 0.61, 0.90]\nr2 = (\n table.search(query_vector_2)\n .where(\"stats.magic > 3\")\n .select([\"name\", \"role\", \"description\"])\n .limit(5)\n .to_polars()\n)\nprint(r2)\n"; export const PyBasicVectorSearchQ3 = "# Who are the strongest characters?\nr3 = (\n table.search()\n .where(\"stats.strength > 3\")\n .select([\"name\", \"role\", \"description\"])\n .limit(5)\n .to_polars()\n)\nprint(r3)\n"; export const PyBasicVectorSearchQ4 = "# Who are the strongest characters?\nr4 = (\n table.search()\n .select([\"name\", \"role\", \"description\", \"power\"])\n .to_polars()\n)\nprint(r4)\n"; export const PyDataLoad = "with open(data_path, \"r\") as f:\n data = json.load(f)\n"; export const TsBasicAddColumns = "await table.addColumns([\n {\n name: \"power\",\n valueSql:\n \"cast(((stats.strength + stats.courage + stats.magic + stats.wisdom) / 4.0) as float)\",\n },\n]);\n"; export const TsBasicAddData = "const magicalCharacters = [\n {\n id: 9,\n name: \"Morgan le Fay\",\n role: \"Sorceress\",\n description:\n \"A powerful enchantress, Arthur's half-sister, and a complex figure who oscillates between aiding and opposing Camelot.\",\n vector: [0.1, 0.84, 0.25, 0.7],\n stats: { strength: 2, courage: 3, magic: 5, wisdom: 4 },\n },\n {\n id: 10,\n name: \"The Lady of the Lake\",\n role: \"Mystical Guardian\",\n description:\n \"A mysterious supernatural figure associated with Avalon, known for giving Arthur the sword Excalibur.\",\n vector: [0.0, 0.9, 0.58, 0.88],\n stats: { strength: 2, courage: 3, magic: 5, wisdom: 5 },\n },\n];\nawait table.add(magicalCharacters);\n"; export const TsBasicCreateEmptyTable = "const schema = new arrow.Schema([\n new arrow.Field(\"id\", new arrow.Int16()),\n new arrow.Field(\"name\", new arrow.Utf8()),\n new arrow.Field(\"role\", new arrow.Utf8()),\n new arrow.Field(\"description\", new arrow.Utf8()),\n new arrow.Field(\n \"vector\",\n new arrow.FixedSizeList(\n 4,\n new arrow.Field(\"item\", new arrow.Float32(), true),\n ),\n ),\n new arrow.Field(\n \"stats\",\n new arrow.Struct([\n new arrow.Field(\"strength\", new arrow.Int8()),\n new arrow.Field(\"courage\", new arrow.Int8()),\n new arrow.Field(\"magic\", new arrow.Int8()),\n new arrow.Field(\"wisdom\", new arrow.Int8()),\n ]),\n ),\n]);\nawait db.createEmptyTable(\"camelot_empty\", schema, { mode: \"overwrite\" });\n"; export const TsBasicCreateTable = "let table = await db.createTable(\"camelot\", data, {\n mode: \"overwrite\",\n});\n"; export const TsBasicDeleteRows = "await table.delete('role = \"Traitor Knight\"');\n"; export const TsBasicDropColumns = "await table.dropColumns([\"power\"]);\n"; export const TsBasicDropTable = "await db.dropTable(\"camelot\");\n"; export const TsBasicImports = "import * as lancedb from \"@lancedb/lancedb\";\nimport * as arrow from \"apache-arrow\";\n"; export const TsBasicOpenTable = "table = await db.openTable(\"camelot\");\n"; export const TsBasicVectorSearch = "const queryVector = [0.03, 0.85, 0.61, 0.9];\nconst result = await table.search(queryVector).limit(5).toArray();\nconsole.log(result);\n"; export const TsBasicVectorSearchQ1 = "// Who are the characters similar to \"wizard\"?\nconst queryVector1 = [0.03, 0.85, 0.61, 0.9];\nconst r1 = await table\n .search(queryVector1)\n .limit(5)\n .select([\"name\", \"role\", \"description\"])\n .toArray();\nconsole.log(r1);\n"; export const TsBasicVectorSearchQ2 = "// Who are the characters similar to \"wizard\" with high magic stats?\nconst queryVector2 = [0.03, 0.85, 0.61, 0.9];\nconst r2 = await table\n .search(queryVector2)\n .where(\"stats.magic > 3\")\n .select([\"name\", \"role\", \"description\"])\n .limit(5)\n .toArray();\nconsole.log(r2);\n"; export const TsBasicVectorSearchQ3 = "// Who are the strongest characters?\nconst r3 = await table\n .query()\n .where(\"stats.strength > 3\")\n .select([\"name\", \"role\", \"description\"])\n .limit(5)\n .toArray();\nconsole.log(r3);\n"; export const TsBasicVectorSearchQ4 = "// Who are the strongest characters?\nconst r4 = await table\n .query()\n .select([\"name\", \"role\", \"description\", \"power\"])\n .toArray();\nconsole.log(r4);\n"; export const TsDataLoad = "const data = JSON.parse(fs.readFileSync(dataPath, \"utf-8\"));\n"; export const RsBasicAddColumns = "table\n .add_columns(\n NewColumnTransform::SqlExpressions(vec![(\n \"power\".to_string(),\n \"cast(((stats.strength + stats.courage + stats.magic + stats.wisdom) / 4.0) as float)\"\n .to_string(),\n )]),\n None,\n )\n .await\n .unwrap();\n"; export const RsBasicAddData = "let magical_characters = vec![\n Character {\n id: 9,\n name: \"Morgan le Fay\".to_string(),\n role: \"Sorceress\".to_string(),\n description: \"A powerful enchantress, Arthur's half-sister, and a complex figure who oscillates between aiding and opposing Camelot.\".to_string(),\n vector: [0.10, 0.84, 0.25, 0.70],\n stats: Stats {\n strength: 2,\n courage: 3,\n magic: 5,\n wisdom: 4,\n },\n },\n Character {\n id: 10,\n name: \"The Lady of the Lake\".to_string(),\n role: \"Mystical Guardian\".to_string(),\n description: \"A mysterious supernatural figure associated with Avalon, known for giving Arthur the sword Excalibur.\".to_string(),\n vector: [0.00, 0.90, 0.58, 0.88],\n stats: Stats {\n strength: 2,\n courage: 3,\n magic: 5,\n wisdom: 5,\n },\n },\n];\ntable\n .add(characters_to_reader(camelot_schema(), &magical_characters))\n .execute()\n .await\n .unwrap();\n"; export const RsBasicCreateEmptyTable = "let schema = Arc::new(Schema::new(vec![\n Field::new(\"id\", DataType::Int16, false),\n Field::new(\"name\", DataType::Utf8, false),\n Field::new(\"role\", DataType::Utf8, false),\n Field::new(\"description\", DataType::Utf8, false),\n Field::new(\n \"vector\",\n DataType::FixedSizeList(Arc::new(Field::new(\"item\", DataType::Float32, true)), 4),\n false,\n ),\n Field::new(\n \"stats\",\n DataType::Struct(arrow_schema::Fields::from(vec![\n Arc::new(Field::new(\"strength\", DataType::Int8, false)),\n Arc::new(Field::new(\"courage\", DataType::Int8, false)),\n Arc::new(Field::new(\"magic\", DataType::Int8, false)),\n Arc::new(Field::new(\"wisdom\", DataType::Int8, false)),\n ])),\n false,\n ),\n]));\ndb.create_empty_table(\"camelot_empty\", schema)\n .mode(CreateTableMode::Overwrite)\n .execute()\n .await\n .unwrap();\n"; export const RsBasicCreateTable = "let mut table = db\n .create_table(\"camelot\", characters_to_reader(schema.clone(), &data))\n .mode(CreateTableMode::Overwrite)\n .execute()\n .await\n .unwrap();\n"; export const RsBasicDeleteRows = "table.delete(\"role = 'Traitor Knight'\").await.unwrap();\n"; export const RsBasicDropColumns = "table.drop_columns(&[\"power\"]).await.unwrap();\n"; export const RsBasicDropTable = "db.drop_table(\"camelot\", &[]).await.unwrap();\n"; export const RsBasicImports = "use arrow_array::types::Float32Type;\nuse arrow_array::{\n FixedSizeListArray, Int8Array, Int16Array, RecordBatch, RecordBatchIterator, StringArray,\n StructArray,\n};\nuse arrow_schema::{DataType, Field, FieldRef, Schema};\nuse futures_util::TryStreamExt;\nuse lancedb::database::CreateTableMode;\nuse lancedb::query::{ExecutableQuery, QueryBase, Select};\nuse lancedb::{connect, table::NewColumnTransform};\n"; export const RsBasicOpenTable = "table = db.open_table(\"camelot\").execute().await.unwrap();\n"; export const RsBasicVectorSearch = "let query_vector = [0.03, 0.85, 0.61, 0.90];\nlet result = table\n .query()\n .nearest_to(&query_vector)\n .unwrap()\n .limit(5)\n .execute()\n .await\n .unwrap()\n .try_collect::>()\n .await\n .unwrap();\nprintln!(\"{result:?}\");\n"; export const RsBasicVectorSearchQ1 = "// Who are the characters similar to \"wizard\"?\nlet query_vector_1 = [0.03, 0.85, 0.61, 0.90];\nlet r1 = table\n .query()\n .nearest_to(&query_vector_1)\n .unwrap()\n .limit(5)\n .select(Select::Columns(vec![\n \"name\".to_string(),\n \"role\".to_string(),\n \"description\".to_string(),\n ]))\n .execute()\n .await\n .unwrap()\n .try_collect::>()\n .await\n .unwrap();\nprintln!(\"{r1:?}\");\n"; export const RsBasicVectorSearchQ2 = "// Who are the characters similar to \"wizard\" with high magic stats?\nlet query_vector_2 = [0.03, 0.85, 0.61, 0.90];\nlet r2 = table\n .query()\n .nearest_to(&query_vector_2)\n .unwrap()\n .only_if(\"stats.magic > 3\")\n .select(Select::Columns(vec![\n \"name\".to_string(),\n \"role\".to_string(),\n \"description\".to_string(),\n ]))\n .limit(5)\n .execute()\n .await\n .unwrap()\n .try_collect::>()\n .await\n .unwrap();\nprintln!(\"{r2:?}\");\n"; export const RsBasicVectorSearchQ3 = "// Who are the strongest characters?\nlet r3 = table\n .query()\n .only_if(\"stats.strength > 3\")\n .select(Select::Columns(vec![\n \"name\".to_string(),\n \"role\".to_string(),\n \"description\".to_string(),\n ]))\n .limit(5)\n .execute()\n .await\n .unwrap()\n .try_collect::>()\n .await\n .unwrap();\nprintln!(\"{r3:?}\");\n"; export const RsBasicVectorSearchQ4 = "// Who are the strongest characters?\nlet r4 = table\n .query()\n .select(Select::Columns(vec![\n \"name\".to_string(),\n \"role\".to_string(),\n \"description\".to_string(),\n \"power\".to_string(),\n ]))\n .execute()\n .await\n .unwrap()\n .try_collect::>()\n .await\n .unwrap();\nprintln!(\"{r4:?}\");\n"; export const RsDataLoad = "let data: Vec =\n serde_json::from_str(&fs::read_to_string(camelot_json_path()).unwrap()).unwrap();\n";