舉例來說,如果 complaints 欄位只包含 The picture shows a blue one, but the one I received was red,則 SELECT * FROM item WHERE complaints LIKE
"%wrong color%" 等基本 SQL 查詢不會傳回資料列。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[[["\u003cp\u003eThis page demonstrates using the \u003ccode\u003eembedding()\u003c/code\u003e function with table-stored data and the \u003ccode\u003epgvector\u003c/code\u003e feature for LLM-driven semantic text analysis.\u003c/p\u003e\n"],["\u003cp\u003eThe example scenario involves a database with an \u003ccode\u003eitems\u003c/code\u003e table and a \u003ccode\u003ecomplaints\u003c/code\u003e column, leveraging the \u003ccode\u003etextembedding-gecko\u003c/code\u003e model for processing.\u003c/p\u003e\n"],["\u003cp\u003eTo enable LLM-powered queries, the \u003ccode\u003egoogle_ml_integration\u003c/code\u003e and \u003ccode\u003evector\u003c/code\u003e extensions need to be installed, and the database must be integrated with Vertex AI.\u003c/p\u003e\n"],["\u003cp\u003eA \u003ccode\u003ecomplaint_embedding\u003c/code\u003e column of type \u003ccode\u003evector\u003c/code\u003e is added to the \u003ccode\u003eitems\u003c/code\u003e table, and populated with embeddings generated by the \u003ccode\u003etextembedding-gecko\u003c/code\u003e model, with the use of an index to improve performance.\u003c/p\u003e\n"],["\u003cp\u003eSemantic nearest-neighbor queries can be executed using the \u003ccode\u003e<->\u003c/code\u003e operator provided by \u003ccode\u003epgvector\u003c/code\u003e, allowing for sorting rows based on semantic proximity to a given text.\u003c/p\u003e\n"]]],[],null,["1\n\n\nAn example embedding workflow\n\n\nSelect a documentation version:\n\n\n15.5.5keyboard_arrow_down\n\n- [15.5.5](/alloydb/omni/15.5.5/docs/example-embeddings)\n\n\u003cbr /\u003e\n\nThis page provides an example workflow that demonstrates how [the `embedding()`\nfunction](/alloydb/omni/15.5.5/docs/work-with-embeddings) works together with your table-stored data and `pgvector` feature. The example uses plain-text input to fetch a result from a database that relies on large language model (LLM)-driven semantic parsing of the text's meaning.\n\n\u003cbr /\u003e\n\nAn example scenario\n-------------------\n\nImagine a database running on AlloyDB with the following aspects:\n\n- The database contains a table, `items`. Each row in this table describes an\n item that your business sells.\n\n- The `items` table contains a column, `complaints`. This `TEXT` column stores\n buyer complaints logged about each item.\n\n- The database integrates with the Vertex AI\n Model Garden, giving it access to the `textembedding-gecko` English\n models.\n\nEven though this database stores complaints about items, these complaints are\nstored as plain text, making it challenging to query. For example, if you want\nto see which items have the most complaints from customers who received the\nwrong color of merchandise, then you can perform ordinary SQL queries on the\ntable, looking for various keyword matches. However, this approach only matches\nrows that contain those exact keywords.\n\nFor example, a basic SQL query such as `SELECT * FROM item WHERE complaints LIKE\n\"%wrong color%\"` doesn't return a row whose `complaints` field contains only\n`The picture shows a blue one, but the one I received was red`.\n\nSQL queries using LLM-powered embeddings can help bridge this gap. By\napplying embeddings, you can query the table in this example for items whose\ncomplaints have semantic similarity to a given text prompt, such as \"It was the\nwrong color\".\n\nThe following steps show how to enable this in the example setup described\nearlier.\n\nBefore you begin\n----------------\n\nMake sure that you meet the following requirements:\n\n#### Required database extension\n\n- Ensure that the following extensions are installed on your\n AlloyDB database.\n\n - `google_ml_integration` extension\n\n - `vector` extension, version `0.5.0.google-1` or later\n\nFor more information about installing and managing extensions, see [Configure\ndatabase\nextensions](/alloydb/docs/reference/extensions).\n\n- Set the `google_ml_integration.enable_model_support` database flag to `off`.\n\n#### Set up model access\n\nBefore you can generate embeddings from an AlloyDB database, you\nmust configure AlloyDB to work with a text embedding model.\n\nTo work with the cloud-based `textembeddings-gecko` model, you need to\n[integrate your database with\nwith Vertex AI](/alloydb/omni/15.5.5/docs/install-with-alloydb-ai).\n\nPrepare the table\n-----------------\n\nBefore you run LLM-based queries on the `items` table's content, you must\nprepare the table to store and index embeddings based on your existing\ndata.\n\n### Create a column to store embeddings\n\nAdd a column to the table for storing embeddings. \n\n ALTER TABLE items ADD COLUMN complaint_embedding vector(768);\n\nThis example specifies `768` as an argument, because that is the number of\ndimensions supported by the `textembedding-gecko` English models. For more information, see\n[Generate an embedding](/alloydb/omni/15.5.5/docs/work-with-embeddings).\n\nThe example applies the `vector` data type to the column for ease of using\n`pgvector` functions and operators with its values.\n\n### Populate the new column\n\nIf you already have embeddings in `CSV` format, follow the\nsteps in [Store vector embeddings](/alloydb/omni/15.5.5/docs/store-embeddings) to store your embeddings.\n\nOptionally, use the `embedding()` function to populate this new column with\nembeddings in case you have text stored in the`complaints` column. In this\nexample setup,\nAlloyDB generates the embeddings using the\n`textembedding-gecko` model, version `003`. \n\n UPDATE items SET complaint_embedding = embedding('textembedding-gecko@003', complaints);\n\nThis example implicitly casts the `real[]` return value of `embedding()` into a `vector` value,\nin order to store the value into the `vector` column created earlier.\n\n### Create an index\n\nTo improve performance, add an index to `items` that uses the\nscalar-quantization techniques. \n\n CREATE INDEX complaint_embed_idx ON items\n USING ivf (complaint_embedding vector_l2_ops)\n WITH (lists = 20, quantizer = 'SQ8');\n\nFor more information on creating approximate nearest-neighbor indexes, see\n[Indexing](https://github.com/pgvector/pgvector#indexing).\n\nRun LLM-powered queries with provided text\n------------------------------------------\n\nYou can now make semantic nearest-neighbor queries on the `items` table. The\nfollowing query uses the `\u003c-\u003e` operator provided by `pgvector` to sort the\ntable's rows on semantic proximity to the text `It was the wrong color` and return the top ten complaints. The\nquery displays the `id` and `name` values of the first sorted row. \n\n SELECT id, name FROM items\n ORDER BY complaint_embedding::vector\n \u003c-\u003e embedding('textembedding-gecko@003', 'It was the wrong color') LIMIT 10;\n\nWhat's next\n-----------\n\n- For another example workflow involving AlloyDB and `pgvector`, see [Building AI-powered apps on Google Cloud databases using\n pgvector, LLMs and\n LangChain](https://cloud.google.com/blog/products/databases/using-pgvector-llms-and-langchain-with-google-cloud-databases)."]]