-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
536 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
services: | ||
libraries: | ||
image: nginx:latest | ||
ports: | ||
- "8000:80" | ||
volumes: | ||
- ./src/test/resources/libraries:/usr/share/nginx/html:ro | ||
- ./nginx_lib_dev.conf:/etc/nginx/conf.d/default.conf:ro | ||
restart: always | ||
ai: | ||
build: ./src/test/resources/ai | ||
ports: | ||
- "8001:80" | ||
volumes: | ||
- ./src/test/resources/ai:/var/www/html | ||
restart: always | ||
db: | ||
image: cockroachdb/cockroach:v23.1.15 | ||
command: start-single-node --certs-dir=/certs --advertise-addr=db | ||
volumes: | ||
- ./init-db.sql:/docker-entrypoint-initdb.d/init-db.sql | ||
- ./cockroach-certs:/certs | ||
ports: | ||
- "26257:26257" | ||
- "8081:8080" | ||
restart: always | ||
api: | ||
restart: always | ||
image: leto-modelizer-api:latest | ||
environment: | ||
DATABASE_HOST: db:26257 | ||
AI_HOST: http://ai/api/ | ||
GITHUB_CLIENT_ID: ${GITHUB_CLIENT_ID} | ||
GITHUB_CLIENT_SECRET: ${GITHUB_CLIENT_SECRET} | ||
LIBRARY_HOST_WHITELIST: http://libraries/ | ||
SUPER_ADMINISTRATOR_LOGIN: ${SUPER_ADMINISTRATOR_LOGIN} | ||
POSTGRES_DB: ${POSTGRES_DB:-leto_db} | ||
POSTGRES_USER: ${POSTGRES_USER:-leto_admin} | ||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-password} | ||
DATABASE_NAME: ${DATABASE_NAME:-leto_db} | ||
DATABASE_USER: ${DATABASE_USER:-leto_admin} | ||
DATABASE_PASSWORD: ${DATABASE_PASSWORD:-password} | ||
SSL_KEY_STORE: ${SSL_KEY_STORE:-classpath:keystore.jks} | ||
SSL_KEY_STORE_PASSWORD: ${SSL_KEY_STORE_PASSWORD:-password} | ||
SSL_KEY_PASSWORD: ${SSL_KEY_PASSWORD:-password} | ||
LETO_MODELIZER_URL: ${LETO_MODELIZER_URL:-http://localhost:8080/} | ||
LETO_ADMIN_URL: ${LETO_ADMIN_URL:-http://localhost:9000/} | ||
CSRF_TOKEN_TIMEOUT: ${CSRF_TOKEN_TIMEOUT:-3600} | ||
USER_SESSION_TIMEOUT: ${USER_SESSION_TIMEOUT:-3600} | ||
|
||
ports: | ||
- "8443:8443" | ||
depends_on: | ||
- db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# Nginx configuration to simulate storage server for libraries. | ||
server { | ||
listen 80; | ||
server_name localhost; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/main/java/com/ditrit/letomodelizerapi/controller/AIController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.ditrit.letomodelizerapi.controller; | ||
|
||
import com.ditrit.letomodelizerapi.model.ai.AIRequestRecord; | ||
import com.ditrit.letomodelizerapi.persistence.model.User; | ||
import com.ditrit.letomodelizerapi.service.AIService; | ||
import com.ditrit.letomodelizerapi.service.UserService; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpSession; | ||
import jakarta.validation.Valid; | ||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.core.Context; | ||
import jakarta.ws.rs.core.HttpHeaders; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
import lombok.AllArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Controller; | ||
|
||
/** | ||
* Controller to manage ai endpoint. | ||
*/ | ||
@Path("/ai") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Controller | ||
@Slf4j | ||
@AllArgsConstructor(onConstructor = @__(@Autowired)) | ||
public class AIController { | ||
|
||
/** | ||
* Service to manage user. | ||
*/ | ||
private UserService userService; | ||
|
||
/** | ||
* Service to manage ai request. | ||
*/ | ||
private AIService aiService; | ||
|
||
/** | ||
* Handles a POST request to initiate an interaction with an Artificial Intelligence (AI) based on the provided | ||
* request details. | ||
* This endpoint accepts an AI request record, which includes the parameters necessary for the AI interaction. | ||
* The method retrieves the user from the session, logs the request details, and forwards the request to the AI | ||
* service. It then constructs and returns a response containing the AI's output. | ||
* | ||
* <p>The method uses the AI service to process the request by the user, generating a JSON response that is returned | ||
* to the client. | ||
* This process allows for dynamic interactions with the AI, facilitating use cases such as querying for | ||
* information, executing commands, or initiating workflows within the application. | ||
* | ||
* @param request the HttpServletRequest, used to access the user's session. | ||
* @param aiRequestRecord the request details for the AI, validated to ensure it meets the expected format. | ||
* @return a Response object containing the AI's response in JSON format, with a status of OK (200). | ||
*/ | ||
|
||
@POST | ||
public Response requestAI(final @Context HttpServletRequest request, | ||
final @Valid AIRequestRecord aiRequestRecord) throws InterruptedException { | ||
HttpSession session = request.getSession(); | ||
User user = userService.getFromSession(session); | ||
|
||
log.info("[{}] Received POST request to request AI with {}", user.getLogin(), aiRequestRecord); | ||
|
||
String json = aiService.sendRequest(aiRequestRecord); | ||
|
||
return Response.status(HttpStatus.CREATED.value()) | ||
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON) | ||
.entity(json) | ||
.build(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/ditrit/letomodelizerapi/model/ai/AIRequestRecord.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.ditrit.letomodelizerapi.model.ai; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.Pattern; | ||
|
||
/** | ||
* A record representing a request to an Artificial Intelligence (AI) within the application. | ||
* This record includes details about the request such as the plugin involved, the type of request, and | ||
* a description of the request. It is used as a data transfer object to encapsulate the information needed | ||
* by the AI to process user requests. | ||
* | ||
* @param plugin the name of the plugin related to the AI request. Must not be blank. | ||
* @param type the type of the AI request, constrained to specific values such as "diagram" by a pattern | ||
* to ensure that only valid request types are considered. | ||
* @param description a description of the AI request, providing context or additional information. Must not be blank. | ||
*/ | ||
public record AIRequestRecord( | ||
@NotBlank String plugin, | ||
@Pattern(regexp = "diagram") String type, | ||
@NotBlank String description | ||
) { | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/ditrit/letomodelizerapi/model/ai/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* Package that contains ai models. | ||
*/ | ||
package com.ditrit.letomodelizerapi.model.ai; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/com/ditrit/letomodelizerapi/service/AIService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.ditrit.letomodelizerapi.service; | ||
|
||
import com.ditrit.letomodelizerapi.model.ai.AIRequestRecord; | ||
|
||
/** | ||
* Service implementation for interacting with an Artificial Intelligence (AI). | ||
* This class provides concrete implementation for sending requests to an AI, based on user input and specific | ||
* request details encapsulated in an AIRequestRecord. | ||
*/ | ||
public interface AIService { | ||
|
||
/** | ||
* Sends a request to the Artificial Intelligence (AI) based on the provided user and request record. | ||
* This method constructs the request, sends it to the AI system, and returns the AI response as a String. | ||
* | ||
* @param aiRequest the AIRequestRecord containing details about the request to be sent to the AI. | ||
* @return the response from the AI as a String. | ||
*/ | ||
String sendRequest(AIRequestRecord aiRequest); | ||
} |
Oops, something went wrong.