たとえば、SELECT * FROM item WHERE complaints LIKE
"%wrong color%" などの基本的な SQL クエリでは、complaints フィールドに The picture shows a blue one, but the one I received was red のみが含まれる行は返されません。
[[["わかりやすい","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-08-08 UTC。"],[],[],null,["# Understand an example of an embedding workflow\n\n\u003cbr /\u003e\n\n[MySQL](/sql/docs/mysql/understand-example-embedding-workflow \"View this page for the MySQL database engine\") \\| PostgreSQL \\| SQL Server\n\n\u003cbr /\u003e\n\nThis page provides an example of a workflow that demonstrates how the\n[`embedding()`](/sql/docs/postgres/work-with-vectors#generate-an-embedding)\nfunction works\nwith the data that's stored in your tables and the\n[`pgvector` query functionality](/sql/docs/postgres/work-with-vectors#query).\nThe example\nuses plain-text input to fetch a result from a database that relies on large\nlanguage model (LLM)-driven semantic parsing of the text's meaning.\n\nAn example scenario of an embedding workflow\n--------------------------------------------\n\nImagine a database running on Cloud SQL for PostgreSQL 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 column stores\n buyer complaints that are logged about each item as plain text.\n\n- The database integrates with the [Vertex AI\n Model Garden](/model-garden), giving it access to the `textembedding-gecko` LLM.\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 matches only\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 where the `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 where\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 scenario described\nearlier.\n\nPrepare the table\n-----------------\n\nBefore you run LLM-based queries on the content of the `items` table, 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 to store embeddings.\n`sql\nALTER TABLE items ADD COLUMN complaint_embedding vector(768);`\n\n\u003cbr /\u003e\n\nThis example specifies `768` as an argument because that's how many dimensions the `textembedding-gecko` LLM supports. For more information, see\n[Generate an embedding](/sql/docs/postgres/work-with-vectors#generate-an-embedding).\n\nThe example applies the `vector` data type to the column to simplify using `pgvector` functions and operators with the column's values.\n\n### Populate the new column\n\nUse the `embedding()`function to populate this new column with embeddings based\non the value of each row's text that appears in the `complaints` column. In this example,\nCloud SQL generates the embeddings using the LLM with the ID of\n`textembedding-gecko`, version `004`. \n\n UPDATE items SET complaint_embedding = embedding('text-embedding-005', complaints);\n\nThis example casts the `real[]` return value of `embedding()` into a `vector` value implicitly to store the value in the `vector` column that you created in [Create a column to store embeddings](#create-column).\n\n\u003cbr /\u003e\n\n### Create an index\n\nTo improve performance, add an index to the `items` table. \n\n CREATE INDEX complaint_embed_idx ON items\n USING hnsw (complaint_embedding vector_cosine_ops);\n\nFor more information on creating this type of index, see [Create a nearest-neighbor index](/sql/docs/postgres/work-with-vectors#index). Also, for more information on tuning the index by setting parameters, see [Query and index embeddings using `pgvector`](/sql/docs/postgres/work-with-vectors).\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 that `pgvector`\nprovides\nto complete the following actions:\n\n- Sort the table's rows on semantic proximity to the text of `It was the wrong color`.\n- Return the top ten complaints.\n\nThe query displays the `id` and `name` values of the first sorted row. \n\n SELECT id, name FROM items\n ORDER BY complaint_embedding\n \u003c-\u003e embedding('text-embedding-005', 'It was the wrong color')::vector LIMIT 10;\n\nWhat's next\n-----------\n\n- [Build generative AI applications using Cloud SQL](/sql/docs/postgres/ai-overview)\n\n- [Integrate Cloud SQL with Vertex AI](/sql/docs/postgres/integrate-cloud-sql-with-vertex-ai)\n\n- [Work with vector embeddings](/sql/docs/postgres/work-with-vectors)\n\n- [Invoke online predictions from Cloud SQL instances](/sql/docs/postgres/invoke-online-predictions)\n\n- [Build LLM-powered applications using LangChain](/sql/docs/postgres/langchain)"]]