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.
This application utilizes the following Azure resources:
- Azure Spring Apps to host the application
- Azure OpenAI for ChatGPT
- Azure Cosmos DB as the vector store database.
Here's a high level architecture diagram that illustrates these components.
- Indexing flow (CLI)
- Load private documents from your local disk.
- Split the text into chunks.
- Convert text chunks into embeddings
- Save the embeddings into Cosmos DB Vector Store
- Query flow (Web API)
- Convert the user's query text to an embedding.
- Query Top-K nearest text chunks from the Cosmos DB vector store (by cosine similarity).
- Populate the prompt template with the chunks.
- Call to OpenAI text completion API.
The following prerequisites are required to use this application. Please ensure that you have them all installed locally.
- Git.
- Java 17 or later
- Azure Cosmos DB Mongo vCore account
- An Azure OpenAI account (see more here)
-
git clone this repo.
-
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>
-
Build the application:
mvn clean package
-
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 inprivate-data
folder, and load those documents into Cosmos DB instead. -
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
-
Open your browser and navigate to
http://localhost:8080/
. You should see the below page. Test it out by typing in a question and clickingSend
.Screenshot of the deployed chatgpt app