/spring-chatgpt-cosmosdb-mongo-vcore

A ChatGPT like sample using Spring / Java with Azure Cosmos DB Mongo vCore for vector search.

Primary LanguageJavaMIT LicenseMIT

Spring ChatGPT Sample with Azure Cosmos DB

This sample shows how to build a ChatGPT like application in Spring and run on Azure Spring Apps with Azure Cosmos DB. The vector store in Azure Cosmos DB enables ChatGPT to use your private data to answer the questions.

Application Architecture

This application utilizes the following Azure resources:

Here's a high level architecture diagram that illustrates these components.

"Application architecture diagram"

How it works

Workflow

  1. Indexing flow (CLI)
    1. Load private documents from your local disk.
    2. Split the text into chunks.
    3. Convert text chunks into embeddings
    4. Save the embeddings into Cosmos DB Vector Store
  2. Query flow (Web API)
    1. Convert the user's query text to an embedding.
    2. Query Top-K nearest text chunks from the Cosmos DB vector store (by cosine similarity).
    3. Populate the prompt template with the chunks.
    4. Call to OpenAI text completion API.

Getting Started

Prerequisites

The following prerequisites are required to use this application. Please ensure that you have them all installed locally.

Quickstart

  1. git clone this repo.

  2. Create the following environment variables with the appropriate values:

    set AZURE_OPENAI_EMBEDDINGDEPLOYMENTID=<Your OpenAI embedding deployment id>
    set AZURE_OPENAI_CHATDEPLOYMENTID=<Your Azure OpenAI chat deployment id>
    set AZURE_OPENAI_ENDPOINT=<Your Azure OpenAI endpoint>
    set AZURE_OPENAI_APIKEY=<Your Azure OpenAI API key>
    set COSMOSDB_DATABASE=<Choose any database name>
    set COSMOSDB_URI=<Cosmos DB Mongo vCore connection string>
    set COSMOSDB_USERNAME=<Username you created for your Cosmos DB Mongo vCore cluster>
    set COSMOSDB_PASSWORD=<Password you created for your Cosmos DB Mongo vCore cluster>
  3. Build the application:

    mvn clean package
  4. The following command will read and process your own private text documents, create a Cosmos DB Mongo vCore collection with vector index, and load the processed documents into it:

       java -jar spring-chatgpt-sample-cli/target/spring-chatgpt-sample-cli-0.0.1-SNAPSHOT.jar --from=C:/<path you your private text docs>
    

    Note: if you don't run the above to process your own documents, at first startup the application will read a pre-provided and pre-processed vector-store.json file in private-data folder, and load those documents into Cosmos DB instead.

  5. Run the following command to build and run the application:

    java -jar spring-chatgpt-sample-webapi/target/spring-chatgpt-sample-webapi-0.0.1-SNAPSHOT.jar
  6. Open your browser and navigate to http://localhost:8080/. You should see the below page. Test it out by typing in a question and clicking Send.

    "Screenshot of deployed chatgpt app"

    Screenshot of the deployed chatgpt app