!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}(),document.documentElement.setAttribute("data-announcement-bar-initially-dismissed",function(){try{return"true"===localStorage.getItem("docusaurus.announcement.dismiss")}catch(t){}return!1}())

[Skip to main content](#__docusaurus_skipToContent_fallback)

LangChain v0.2 is coming soon! Preview the new docs [here](/v0.2/docs/introduction/).

[

![🦜️🔗 Langchain](/v0.1/img/brand/wordmark.png)![🦜️🔗 Langchain](/v0.1/img/brand/wordmark-dark.png)

](/v0.1/)[Docs](/v0.1/docs/get_started/introduction/)[Use cases](/v0.1/docs/use_cases/)[Integrations](/v0.1/docs/integrations/platforms/)[API Reference](https://api.js.langchain.com)

[More](#)

*   [People](/v0.1/docs/people/)
*   [Community](/v0.1/docs/community/)
*   [Tutorials](/v0.1/docs/additional_resources/tutorials/)
*   [Contributing](/v0.1/docs/contributing/)

[v0.1](#)

*   [v0.2](https://js.langchain.com/v0.2/docs/introduction)
*   [v0.1](/v0.1/docs/get_started/introduction/)

[🦜🔗](#)

*   [LangSmith](https://smith.langchain.com)
*   [LangSmith Docs](https://docs.smith.langchain.com)
*   [LangChain Hub](https://smith.langchain.com/hub)
*   [LangServe](https://github.com/langchain-ai/langserve)
*   [Python Docs](https://python.langchain.com/)

[Chat](https://chatjs.langchain.com)[](https://github.com/langchain-ai/langchainjs)

Search

*   [Providers](/v0.1/docs/integrations/platforms/)
    
    *   [Providers](/v0.1/docs/integrations/platforms/)
    *   [Anthropic](/v0.1/docs/integrations/platforms/anthropic/)
    *   [AWS](/v0.1/docs/integrations/platforms/aws/)
    *   [Google](/v0.1/docs/integrations/platforms/google/)
    *   [Microsoft](/v0.1/docs/integrations/platforms/microsoft/)
    *   [OpenAI](/v0.1/docs/integrations/platforms/openai/)
*   [Components](/v0.1/docs/integrations/components/)
    
    *   [LLMs](/v0.1/docs/integrations/llms/)
        
    *   [Chat models](/v0.1/docs/integrations/chat/)
        
    *   [Document loaders](/v0.1/docs/integrations/document_loaders/)
        
    *   [Document transformers](/v0.1/docs/integrations/document_transformers/)
        
    *   [Document compressors](/v0.1/docs/integrations/document_compressors/)
        
    *   [Text embedding models](/v0.1/docs/integrations/text_embedding/)
        
    *   [Vector stores](/v0.1/docs/integrations/vectorstores/)
        
        *   [Memory](/v0.1/docs/integrations/vectorstores/memory/)
        *   [AnalyticDB](/v0.1/docs/integrations/vectorstores/analyticdb/)
        *   [Astra DB](/v0.1/docs/integrations/vectorstores/astradb/)
        *   [Azure AI Search](/v0.1/docs/integrations/vectorstores/azure_aisearch/)
        *   [Azure Cosmos DB](/v0.1/docs/integrations/vectorstores/azure_cosmosdb/)
        *   [Cassandra](/v0.1/docs/integrations/vectorstores/cassandra/)
        *   [Chroma](/v0.1/docs/integrations/vectorstores/chroma/)
        *   [ClickHouse](/v0.1/docs/integrations/vectorstores/clickhouse/)
        *   [CloseVector](/v0.1/docs/integrations/vectorstores/closevector/)
        *   [Cloudflare Vectorize](/v0.1/docs/integrations/vectorstores/cloudflare_vectorize/)
        *   [Convex](/v0.1/docs/integrations/vectorstores/convex/)
        *   [Couchbase](/v0.1/docs/integrations/vectorstores/couchbase/)
        *   [Elasticsearch](/v0.1/docs/integrations/vectorstores/elasticsearch/)
        *   [Faiss](/v0.1/docs/integrations/vectorstores/faiss/)
        *   [Google Vertex AI Matching Engine](/v0.1/docs/integrations/vectorstores/googlevertexai/)
        *   [SAP HANA Cloud Vector Engine](/v0.1/docs/integrations/vectorstores/hanavector/)
        *   [HNSWLib](/v0.1/docs/integrations/vectorstores/hnswlib/)
        *   [LanceDB](/v0.1/docs/integrations/vectorstores/lancedb/)
        *   [Milvus](/v0.1/docs/integrations/vectorstores/milvus/)
        *   [Momento Vector Index (MVI)](/v0.1/docs/integrations/vectorstores/momento_vector_index/)
        *   [MongoDB Atlas](/v0.1/docs/integrations/vectorstores/mongodb_atlas/)
        *   [MyScale](/v0.1/docs/integrations/vectorstores/myscale/)
        *   [Neo4j Vector Index](/v0.1/docs/integrations/vectorstores/neo4jvector/)
        *   [Neon Postgres](/v0.1/docs/integrations/vectorstores/neon/)
        *   [OpenSearch](/v0.1/docs/integrations/vectorstores/opensearch/)
        *   [PGVector](/v0.1/docs/integrations/vectorstores/pgvector/)
        *   [Pinecone](/v0.1/docs/integrations/vectorstores/pinecone/)
        *   [Prisma](/v0.1/docs/integrations/vectorstores/prisma/)
        *   [Qdrant](/v0.1/docs/integrations/vectorstores/qdrant/)
        *   [Redis](/v0.1/docs/integrations/vectorstores/redis/)
        *   [Rockset](/v0.1/docs/integrations/vectorstores/rockset/)
        *   [SingleStore](/v0.1/docs/integrations/vectorstores/singlestore/)
        *   [Supabase](/v0.1/docs/integrations/vectorstores/supabase/)
        *   [Tigris](/v0.1/docs/integrations/vectorstores/tigris/)
        *   [Turbopuffer](/v0.1/docs/integrations/vectorstores/turbopuffer/)
        *   [TypeORM](/v0.1/docs/integrations/vectorstores/typeorm/)
        *   [Typesense](/v0.1/docs/integrations/vectorstores/typesense/)
        *   [Upstash Vector](/v0.1/docs/integrations/vectorstores/upstash/)
        *   [USearch](/v0.1/docs/integrations/vectorstores/usearch/)
        *   [Vectara](/v0.1/docs/integrations/vectorstores/vectara/)
        *   [Vercel Postgres](/v0.1/docs/integrations/vectorstores/vercel_postgres/)
        *   [Voy](/v0.1/docs/integrations/vectorstores/voy/)
        *   [Weaviate](/v0.1/docs/integrations/vectorstores/weaviate/)
        *   [Xata](/v0.1/docs/integrations/vectorstores/xata/)
        *   [Zep](/v0.1/docs/integrations/vectorstores/zep/)
    *   [Retrievers](/v0.1/docs/integrations/retrievers/)
        
    *   [Tools](/v0.1/docs/integrations/tools/)
        
    *   [Agents and toolkits](/v0.1/docs/integrations/toolkits/)
        
    *   [Chat Memory](/v0.1/docs/integrations/chat_memory/)
        
    *   [Stores](/v0.1/docs/integrations/stores/)
        

*   [](/v0.1/)
*   [Components](/v0.1/docs/integrations/components/)
*   [Vector stores](/v0.1/docs/integrations/vectorstores/)
*   Xata

On this page

Xata
====

[Xata](https://xata.io) is a serverless data platform, based on PostgreSQL. It provides a type-safe TypeScript/JavaScript SDK for interacting with your database, and a UI for managing your data.

Xata has a native vector type, which can be added to any table, and supports similarity search. LangChain inserts vectors directly to Xata, and queries it for the nearest neighbors of a given vector, so that you can use all the LangChain Embeddings integrations with Xata.

Setup[​](#setup "Direct link to Setup")
---------------------------------------

### Install the Xata CLI[​](#install-the-xata-cli "Direct link to Install the Xata CLI")

    npm install @xata.io/cli -g

### Create a database to be used as a vector store[​](#create-a-database-to-be-used-as-a-vector-store "Direct link to Create a database to be used as a vector store")

In the [Xata UI](https://app.xata.io) create a new database. You can name it whatever you want, but for this example we'll use `langchain`. Create a table, again you can name it anything, but we will use `vectors`. Add the following columns via the UI:

*   `content` of type "Text". This is used to store the `Document.pageContent` values.
*   `embedding` of type "Vector". Use the dimension used by the model you plan to use (1536 for OpenAI).
*   any other columns you want to use as metadata. They are populated from the `Document.metadata` object. For example, if in the `Document.metadata` object you have a `title` property, you can create a `title` column in the table and it will be populated.

### Initialize the project[​](#initialize-the-project "Direct link to Initialize the project")

In your project, run:

    xata init

and then choose the database you created above. This will also generate a `xata.ts` or `xata.js` file that defines the client you can use to interact with the database. See the [Xata getting started docs](https://xata.io/docs/getting-started/installation) for more details on using the Xata JavaScript/TypeScript SDK.

Usage[​](#usage "Direct link to Usage")
---------------------------------------

tip

See [this section for general instructions on installing integration packages](/v0.1/docs/get_started/installation/#installing-integration-packages).

*   npm
*   Yarn
*   pnpm

    npm install @langchain/openai @langchain/community

    yarn add @langchain/openai @langchain/community

    pnpm add @langchain/openai @langchain/community

### Example: Q&A chatbot using OpenAI and Xata as vector store[​](#example-qa-chatbot-using-openai-and-xata-as-vector-store "Direct link to Example: Q&A chatbot using OpenAI and Xata as vector store")

This example uses the `VectorDBQAChain` to search the documents stored in Xata and then pass them as context to the OpenAI model, in order to answer the question asked by the user.

    import { XataVectorSearch } from "@langchain/community/vectorstores/xata";import { OpenAIEmbeddings, OpenAI } from "@langchain/openai";import { BaseClient } from "@xata.io/client";import { VectorDBQAChain } from "langchain/chains";import { Document } from "@langchain/core/documents";// First, follow set-up instructions at// https://js.langchain.com/docs/modules/data_connection/vectorstores/integrations/xata// if you use the generated client, you don't need this function.// Just import getXataClient from the generated xata.ts instead.const getXataClient = () => {  if (!process.env.XATA_API_KEY) {    throw new Error("XATA_API_KEY not set");  }  if (!process.env.XATA_DB_URL) {    throw new Error("XATA_DB_URL not set");  }  const xata = new BaseClient({    databaseURL: process.env.XATA_DB_URL,    apiKey: process.env.XATA_API_KEY,    branch: process.env.XATA_BRANCH || "main",  });  return xata;};export async function run() {  const client = getXataClient();  const table = "vectors";  const embeddings = new OpenAIEmbeddings();  const store = new XataVectorSearch(embeddings, { client, table });  // Add documents  const docs = [    new Document({      pageContent: "Xata is a Serverless Data platform based on PostgreSQL",    }),    new Document({      pageContent:        "Xata offers a built-in vector type that can be used to store and query vectors",    }),    new Document({      pageContent: "Xata includes similarity search",    }),  ];  const ids = await store.addDocuments(docs);  // eslint-disable-next-line no-promise-executor-return  await new Promise((r) => setTimeout(r, 2000));  const model = new OpenAI();  const chain = VectorDBQAChain.fromLLM(model, store, {    k: 1,    returnSourceDocuments: true,  });  const response = await chain.invoke({ query: "What is Xata?" });  console.log(JSON.stringify(response, null, 2));  await store.delete({ ids });}

#### API Reference:

*   [XataVectorSearch](https://api.js.langchain.com/classes/langchain_community_vectorstores_xata.XataVectorSearch.html) from `@langchain/community/vectorstores/xata`
*   [OpenAIEmbeddings](https://api.js.langchain.com/classes/langchain_openai.OpenAIEmbeddings.html) from `@langchain/openai`
*   [OpenAI](https://api.js.langchain.com/classes/langchain_openai.OpenAI.html) from `@langchain/openai`
*   [VectorDBQAChain](https://api.js.langchain.com/classes/langchain_chains.VectorDBQAChain.html) from `langchain/chains`
*   [Document](https://api.js.langchain.com/classes/langchain_core_documents.Document.html) from `@langchain/core/documents`

### Example: Similarity search with a metadata filter[​](#example-similarity-search-with-a-metadata-filter "Direct link to Example: Similarity search with a metadata filter")

This example shows how to implement semantic search using LangChain.js and Xata. Before running it, make sure to add an `author` column of type String to the `vectors` table in Xata.

    import { XataVectorSearch } from "@langchain/community/vectorstores/xata";import { OpenAIEmbeddings } from "@langchain/openai";import { BaseClient } from "@xata.io/client";import { Document } from "@langchain/core/documents";// First, follow set-up instructions at// https://js.langchain.com/docs/modules/data_connection/vectorstores/integrations/xata// Also, add a column named "author" to the "vectors" table.// if you use the generated client, you don't need this function.// Just import getXataClient from the generated xata.ts instead.const getXataClient = () => {  if (!process.env.XATA_API_KEY) {    throw new Error("XATA_API_KEY not set");  }  if (!process.env.XATA_DB_URL) {    throw new Error("XATA_DB_URL not set");  }  const xata = new BaseClient({    databaseURL: process.env.XATA_DB_URL,    apiKey: process.env.XATA_API_KEY,    branch: process.env.XATA_BRANCH || "main",  });  return xata;};export async function run() {  const client = getXataClient();  const table = "vectors";  const embeddings = new OpenAIEmbeddings();  const store = new XataVectorSearch(embeddings, { client, table });  // Add documents  const docs = [    new Document({      pageContent: "Xata works great with Langchain.js",      metadata: { author: "Xata" },    }),    new Document({      pageContent: "Xata works great with Langchain",      metadata: { author: "Langchain" },    }),    new Document({      pageContent: "Xata includes similarity search",      metadata: { author: "Xata" },    }),  ];  const ids = await store.addDocuments(docs);  // eslint-disable-next-line no-promise-executor-return  await new Promise((r) => setTimeout(r, 2000));  // author is applied as pre-filter to the similarity search  const results = await store.similaritySearchWithScore("xata works great", 6, {    author: "Langchain",  });  console.log(JSON.stringify(results, null, 2));  await store.delete({ ids });}

#### API Reference:

*   [XataVectorSearch](https://api.js.langchain.com/classes/langchain_community_vectorstores_xata.XataVectorSearch.html) from `@langchain/community/vectorstores/xata`
*   [OpenAIEmbeddings](https://api.js.langchain.com/classes/langchain_openai.OpenAIEmbeddings.html) from `@langchain/openai`
*   [Document](https://api.js.langchain.com/classes/langchain_core_documents.Document.html) from `@langchain/core/documents`

* * *

#### Help us out by providing feedback on this documentation page:

[

Previous

Weaviate

](/v0.1/docs/integrations/vectorstores/weaviate/)[

Next

Zep

](/v0.1/docs/integrations/vectorstores/zep/)

*   [Setup](#setup)
    *   [Install the Xata CLI](#install-the-xata-cli)
    *   [Create a database to be used as a vector store](#create-a-database-to-be-used-as-a-vector-store)
    *   [Initialize the project](#initialize-the-project)
*   [Usage](#usage)
    *   [Example: Q&A chatbot using OpenAI and Xata as vector store](#example-qa-chatbot-using-openai-and-xata-as-vector-store)
    *   [Example: Similarity search with a metadata filter](#example-similarity-search-with-a-metadata-filter)

Community

*   [Discord](https://discord.gg/cU2adEyC7w)
*   [Twitter](https://twitter.com/LangChainAI)

GitHub

*   [Python](https://github.com/langchain-ai/langchain)
*   [JS/TS](https://github.com/langchain-ai/langchainjs)

More

*   [Homepage](https://langchain.com)
*   [Blog](https://blog.langchain.dev)

Copyright © 2024 LangChain, Inc.