{"id":27370150,"url":"https://github.com/datastaxdevs/demo-generativeai-with-java","last_synced_at":"2025-04-13T08:48:09.541Z","repository":{"id":194279280,"uuid":"690464741","full_name":"datastaxdevs/demo-generativeai-with-java","owner":"datastaxdevs","description":"Show how to build a project to do generative Ai with Java","archived":false,"fork":false,"pushed_at":"2023-11-10T12:21:15.000Z","size":1755,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":5,"default_branch":"main","last_synced_at":"2023-11-10T14:47:19.891Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/datastaxdevs.png","metadata":{"files":{"readme":"README.MD","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-09-12T08:46:14.000Z","updated_at":"2023-09-12T09:36:42.000Z","dependencies_parsed_at":null,"dependency_job_id":"8610d019-3c01-4395-9063-3921477fa08e","html_url":"https://github.com/datastaxdevs/demo-generativeai-with-java","commit_stats":null,"previous_names":["datastaxdevs/demo-generativeai-with-java"],"tags_count":0,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/datastaxdevs%2Fdemo-generativeai-with-java","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/datastaxdevs%2Fdemo-generativeai-with-java/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/datastaxdevs%2Fdemo-generativeai-with-java/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/datastaxdevs%2Fdemo-generativeai-with-java/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/datastaxdevs","download_url":"https://codeload.github.com/datastaxdevs/demo-generativeai-with-java/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248688190,"owners_count":21145762,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-04-13T08:48:07.545Z","updated_at":"2025-04-13T08:48:09.533Z","avatar_url":"https://github.com/datastaxdevs.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# Demo of Generative AI with Java\n\n[![Gitpod ready-to-code](https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/datastaxdevs/demo-generativeai-with-java)\n[![License Apache2](https://img.shields.io/hexpm/l/plug.svg)](http://www.apache.org/licenses/LICENSE-2.0)\n[![Discord](https://img.shields.io/discord/685554030159593522)](https://discord.com/widget?id=685554030159593522\u0026theme=dark)\n\n## 📋 Table of content\n\n### Week 1 \n- [01. Create Astra Account](#-1---create-your-datastax-astra-account)\n- [02. Create Astra Token](#-2---create-an-astra-token)\n- [03. Copy the token](#-3---copy-the-token-value-in-your-clipboard)\n- [04. Open Gitpod](#-4---open-gitpod)\n- [05. Setup CLI](#-5---set-up-the-cli-with-your-token)\n- [06. Create Database](#-6---create-destination-database-and-a-keyspace)\n- [07. Setup env variables](#-7---setup-env-variables)\n- [08. Register to OpenAI](#-8---register-to-openai)\n- [09. Setup Project](#-9---setup-project)\n- [10. Vector Search](#-10---vector-search)\n- [11. Retrieve Augmented Generation](#-11---rag-for-retrieve-augmented-generation)\n\n### Week 2\n\n- [Slides](./genai-with-java.pdf)\n- [12. Setup project](#-12---setup-project)\n- [13. Ingest document](#-13---ingest-document)\n- [14. Chap Completion](#-14---chat-completion)\n\n## WEEK1\n\n![](./img/01-set-the-scene.png)\n\n#### ✅ `1` - Create your DataStax Astra account\n\n\u003e ℹ️ Account creation tutorial is available in [awesome astra](https://awesome-astra.github.io/docs/pages/astra/create-account/)\n\n\n_click the image below or go to [https://astra.datastax./com](bit.ly/3QxhO6t)_\n\n\u003ca href=\"bit.ly/3QxhO6t\"\u003e\n\u003cimg src=\"https://awesome-astra.github.io/docs/img/astra/astra-signin-github-0.png\" /\u003e\n\u003c/a\u003e\n\u003cbr/\u003e\n\n\n#### ✅ `2` - Create an Astra Token\n\n\u003e ℹ️ Token creation tutorial is available in [awesome astra](https://awesome-astra.github.io/docs/pages/astra/create-token/#c-procedure)\n\n- `Locate `Settings` (#1) in the menu on the left, then `Token Management` (#2)\n\n- Select the role `Organization Administrator` before clicking `[Generate Token]`\n\n![](https://github.com/DataStax-Academy/cassandra-for-data-engineers/blob/main/images/setup-astra-2.png?raw=true)\n\nThe Token is in fact three separate strings: a `Client ID`, a `Client Secret` and the `token` proper. You will need some of these strings to access the database, depending on the type of access you plan. Although the Client ID, strictly speaking, is not a secret, you should regard this whole object as a secret and make sure not to share it inadvertently (e.g. committing it to a Git repository) as it grants access to your databases.\n\n```json\n{\n  \"ClientId\": \"ROkiiDZdvPOvHRSgoZtyAapp\",\n  \"ClientSecret\": \"fakedfaked\",\n  \"Token\":\"AstraCS:fake\"\n}\n```\n\n#### ✅ `3` - Copy the token value in your clipboard\n\nYou can also leave the windo open to copy the value in a second.\n\n#### ✅ `4` - Open Gitpod\n\n\u003e\n\u003e ↗️ _Right Click and select open as a new Tab..._\n\u003e\n\u003e [![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/datastaxdevs/https://gitpod.io/#https://github.com/datastaxdevs/demo-generativeai-with-java)\n\u003e\n\n![](./img/gitpod.png)\n\n\n#### ✅ `5` - Set up the CLI with your token\n\n_In gitpod, in a terminal window:_\n\n- Login\n\n```bash\nastra login --token AstraCS:fake\n```\n\n- Validate your are setup\n\n```bash\nastra org\n```\n\n\u003e **Output**\n\u003e ```\n\u003e gitpod /workspace/workshop-beam (main) $ astra org\n\u003e +----------------+-----------------------------------------+\n\u003e | Attribute      | Value                                   |\n\u003e +----------------+-----------------------------------------+\n\u003e | Name           | cedrick.lunven@datastax.com             |\n\u003e | id             | f9460f14-9879-4ebe-83f2-48d3f3dce13c    |\n\u003e +----------------+-----------------------------------------+\n\u003e ```\n\n\n#### ✅ `6` - Create destination Database and a keyspace\n\n\u003e ℹ️ You can notice we enabled the Vector Search capability\n\n- Create db `workshop_beam` and wait for the DB to become active\n\n```\nastra db create demo-genai -k genai --vector --if-not-exists\n```\n\n\u003e 💻 Output\n\u003e\n\u003e ```console\n\u003e [INFO]  Database 'demo-genai' does not exist. Creating database 'demo-genai' with keyspace 'genai'\n\u003e [INFO]  Enabling vector search for database demo-genai\n\u003e [INFO]  Database 'demo-genai' and keyspace 'genai' are being created.\n\u003e [INFO]  Database 'demo-genai' has status 'PENDING' waiting to be 'ACTIVE' ...\n\u003e [INFO]  Database 'demo-genai' has status 'ACTIVE' (took 112341 millis)\n\u003e [OK]    Database 'demo-genai' is ready.\n\u003e ```\n\n- List databases\n\n```\nastra db list\n```\n\n\u003e 💻 Output\n\u003e\n\u003e ```\n\u003e +--------------------------+--------------------------------------+-----------+-------+---+-----------+\n\u003e | Name                     | id                                   | Regions   | Cloud | V | Status    |\n\u003e +--------------------------+--------------------------------------+-----------+-------+---+-----------+\n\u003e | demo-genai               | 9e54ff00-57e2-47ed-8699-f94d5dd11b6f | us-east1  | gcp   | ■ | ACTIVE    |\n\u003e +--------------------------+--------------------------------------+-----------+-------+---+-----------+\n\u003e ```\n\n- Describe your db\n\n```\nastra db describe demo-genai\n```\n\n\u003e 💻 Output\n\u003e \n\u003e ```console\n\u003e +------------------+-----------------------------------------+\n\u003e | Attribute        | Value                                   |\n\u003e +------------------+-----------------------------------------+\n\u003e | Name             | demo-genai                              |\n\u003e | id               | 9e54ff00-57e2-47ed-8699-f94d5dd11b6f    |\n\u003e | Status           | ACTIVE                                  |\n\u003e | Cloud            | GCP                                     |\n\u003e | Regions          | us-east1                                |\n\u003e | Default Keyspace | genai                                   |\n\u003e | Creation Time    | 2023-09-12T08:55:36Z                    |\n\u003e |                  |                                         |\n\u003e | Keyspaces        | [0] genai                               |\n\u003e |                  |                                         |\n\u003e |                  |                                         |\n\u003e | Regions          | [0] us-east1                            |\n\u003e |                  |                                         |\n\u003e +------------------+-----------------------------------------+\n\u003e ```\n\n\n#### ✅ `7` - Setup env variables\n\n- Create `.env` file with variables\n\n```bash\nastra db create-dotenv demo-genai \n```\n\n- Display the file\n\n```bash\ncat .env\n```\n\n- Load env variables\n\n```\nset -a\nsource .env\nset +a\nenv | grep ASTRA\n```\n\n#### ✅ `8` - Register to OpenAI\n\n- Access to [OpenAI platform](https://platform.openai.com/) and register.\n\n![](./img/openai-home.png)\n\n- In your profile, go to `View API KEYS`, create a new key and copy the value in your clipboard. \nYou have a free trial for a month of so.\n\n![](./img/openai-key.png)\n\n```java\nEXPORT OPENAI_API_KEY=\u003ckey\u003e\n```\n\n#### ✅ `9` - Setup project\n\nThis command will allows to validate that Java ,\nmaven and lombok are working as expected and you can connect.\n\n\u003e Note:\n\u003e To create the project i simply when with the astra sdk arachetype as follow\n\u003e ```\n\u003e mvn archetype:generate \\\n\u003e -DarchetypeGroupId=com.datastax.astra \\\n\u003e -DarchetypeArtifactId=spring-boot-3x-archetype \\\n\u003e -DarchetypeVersion=0.6.9 \\\n\u003e -DinteractiveMode=false \\\n\u003e -DgroupId=com.datastax.demo \\\n\u003e -DartifactId=genai-demo \\\n\u003e -Dversion=1.0-SNAPSHOT\n\u003e ```\n\u003e and added the vector dependency:\n\u003e ```xml\n\u003e \u003cdependency\u003e\n\u003e   \u003cgroupId\u003ecom.datastax.astra\u003c/groupId\u003e\n\u003e   \u003cartifactId\u003eastra-sdk-vector\u003c/artifactId\u003e\n\u003e   \u003cversion\u003e${astra-sdk-starter.version}\u003c/version\u003e\n\u003e \u003c/dependency\u003e\n\u003e ```\n\n- Run connection test:\n\n```\nmvn test -Dtest=ConnectionTest#shouldBeConnectedTest\n```\n\n- Run OpenAI Test:\n\n![](./img/02-compute-embeddings.png)\n\n```\nmvn test -Dtest=OpenAiTest#shouldTestOpenAICreateEmbeddings\n```\n\n#### ✅ `10` - Vector Search\n\n- Ingest data\n\n![](./img/03-ingests-data.png)\n\n```\nmvn test -Dtest=GenerativeAITest#shouldIngestDocuments\n```\n\n- Open a cqlsh (in a new terminal)\n\n```\nastra db cqlsh genai-demo -k genai\nselect row_id, metadata_s, blob_text, vector from philosophers\n```\n\n- Similarity Search\n\n```\nmvn test -Dtest=GenerativeAITest#shouldSimilaritySearchQuotes\n```\n\n- Similarity Search + MetaData (by Author)\n\n```\nmvn test -Dtest=GenerativeAITest#shouldSimilaritySearchQuotesFilteredByAuthor\n```\n\n- Similarity Search + MetaData (by Tags)\n\n```\nmvn test -Dtest=GenerativeAITest#shouldSimilaritySearchQuotesFilteredByTags\n```\n\n- Similarity Search with a threshold\n\n```\nmvn test -Dtest=GenerativeAITest#shouldSimilaritySearchQuotesWithThreshold\n```\n\n#### ✅ `11` - RAG for Retrieve Augmented Generation\n\nThe Full Monty.....\n\n```\nmvn test -Dtest=GenerativeAITest#shouldGenerateQuotesWithRag\n```\n\n## WEEK 2\n\n#### ✅ `12` - Setup Project\n\n- Check list of running db\n\n```console\nastra db list\n```\n\n- Resume Db if needed (or create a new once)\n\n```json\nastra db resume langchain4j\nastra db create langchain4j --if-not-exists\n```\n\n- Make sure you setup the env variables (`$ASTRA_APPLICATION_TOKEN`)\n\n```bash\nastra db create-dotenv langchain4j\nset -a\nsource .env\nset +a\nenv | grep ASTRA\n```\n\nGo the `application.yaml` and check values are correct for your\n\n\n```yaml\nastra:\n  database:\n    name: langchain4j\n    keyspace: langchain4j\n    table: langchain4j\n```\n\n#### ✅ `13` - Ingest Document\n\n\n```java\n@Test\n@DisplayName(\"02. Should Ingest a document\")\n@EnabledIfEnvironmentVariable(named = \"ASTRA_DB_APPLICATION_TOKEN\", matches = \"Astra.*\")\n@EnabledIfEnvironmentVariable(named = \"OPENAI_API_KEY\", matches = \"sk.*\")\nvoid should_Ingest_Document() {\n\n  Document document = FileSystemDocumentLoader.loadDocument(path, DocumentType.TXT);\n  DocumentSplitter splitter = DocumentSplitters\n        .recursive(100, 10,\n        new OpenAiTokenizer(GPT_3_5_TURBO));\n  \n  EmbeddingStoreIngestor.builder()\n     .documentSplitter(splitter)\n     .embeddingModel(embeddingModel)\n     .embeddingStore(embeddingStore)\n     .build().ingest(document);\n}\n```\n\n#### ✅ `14` - Chat Completion\n\n```java\n  @Test\n@DisplayName(\"03. Should Chat Completion\")\n@EnabledIfEnvironmentVariable(named = \"ASTRA_DB_APPLICATION_TOKEN\", matches = \"Astra.*\")\n@EnabledIfEnvironmentVariable(named = \"OPENAI_API_KEY\", matches = \"sk.*\")\nvoid should_chat_completion(){\n        .. //check code in the class\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdatastaxdevs%2Fdemo-generativeai-with-java","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdatastaxdevs%2Fdemo-generativeai-with-java","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdatastaxdevs%2Fdemo-generativeai-with-java/lists"}