Skip to content

Commit

Permalink
BIGTOP-4189: Mock Data Implementation for AI Chat API (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
lhpqaq authored Aug 19, 2024
1 parent 2c90eaa commit 48e788d
Show file tree
Hide file tree
Showing 15 changed files with 815 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.bigtop.manager.server.controller;

import org.apache.bigtop.manager.server.enums.ResponseStatus;
import org.apache.bigtop.manager.server.model.converter.PlatformConverter;
import org.apache.bigtop.manager.server.model.dto.PlatformDTO;
import org.apache.bigtop.manager.server.model.req.PlatformReq;
import org.apache.bigtop.manager.server.model.vo.ChatMessageVO;
import org.apache.bigtop.manager.server.model.vo.ChatThreadVO;
import org.apache.bigtop.manager.server.model.vo.PlatformAuthCredentialVO;
import org.apache.bigtop.manager.server.model.vo.PlatformAuthorizedVO;
import org.apache.bigtop.manager.server.model.vo.PlatformVO;
import org.apache.bigtop.manager.server.service.AIChatService;
import org.apache.bigtop.manager.server.utils.ResponseEntity;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import jakarta.annotation.Resource;
import java.util.List;

@Tag(name = "AI Chat Controller")
@RestController
@RequestMapping("/ai/chat/")
public class AIChatController {

@Resource
private AIChatService chatService;

@Operation(summary = "platforms", description = "Get all platforms")
@GetMapping("/platforms")
public ResponseEntity<List<PlatformVO>> platforms() {
return ResponseEntity.success(chatService.platforms());
}

@Operation(summary = "platforms", description = "Get authorized platforms")
@GetMapping("/platforms/authorized")
public ResponseEntity<List<PlatformAuthorizedVO>> authorizedPlatforms() {
return ResponseEntity.success(chatService.authorizedPlatforms());
}

@Operation(summary = "platforms", description = "Get authorized platforms")
@GetMapping("/platforms/{platformId}/auth/credential")
public ResponseEntity<List<PlatformAuthCredentialVO>> platformsAuthCredential(@PathVariable Long platformId) {
return ResponseEntity.success(chatService.platformsAuthCredential(platformId));
}

@Operation(summary = "platforms", description = "Add authorized platforms")
@PutMapping("/platforms")
public ResponseEntity<PlatformVO> addAuthorizedPlatform(@RequestBody PlatformReq platformReq) {
PlatformDTO platformDTO = PlatformConverter.INSTANCE.fromReq2DTO(platformReq);
return ResponseEntity.success(chatService.addAuthorizedPlatform(platformDTO));
}

@Operation(summary = "platforms", description = "Delete authorized platforms")
@DeleteMapping("/platforms/{platformId}")
public ResponseEntity<Boolean> deleteAuthorizedPlatform(@PathVariable Long platformId) {
int code = chatService.deleteAuthorizedPlatform(platformId);
if (code != 0) {
return ResponseEntity.error(ResponseStatus.PARAMETER_ERROR, "Permission denied");
}
return ResponseEntity.success(true);
}

@Operation(summary = "new threads", description = "Create a chat threads")
@PutMapping("/platforms/{platformId}/threads")
public ResponseEntity<ChatThreadVO> createChatThreads(@PathVariable Long platformId, @RequestParam String model) {
return ResponseEntity.success(chatService.createChatThreads(platformId, model));
}

@Operation(summary = "delete threads", description = "Delete a chat threads")
@DeleteMapping("platforms/{platformId}/threads/{threadId}")
public ResponseEntity<Boolean> deleteChatThreads(@PathVariable Long platformId, @PathVariable Long threadId) {
int code = chatService.deleteChatThreads(platformId, threadId);
if (code != 0) {
return ResponseEntity.error(ResponseStatus.PARAMETER_ERROR, "No Content");
}
return ResponseEntity.success(true);
}

@Operation(summary = "get", description = "Get all threads of a platform")
@GetMapping("platforms/{platformId}/threads")
public ResponseEntity<List<ChatThreadVO>> getAllChatThreads(
@PathVariable Long platformId, @RequestParam String model) {
return ResponseEntity.success(chatService.getAllChatThreads(platformId, model));
}

@Operation(summary = "talk", description = "Talk with AI")
@PostMapping("platforms/{platformId}/threads/{threadId}/talk")
public SseEmitter talk(@PathVariable Long platformId, @PathVariable Long threadId, @RequestParam String message) {
return chatService.talk(platformId, threadId, message);
}

@Operation(summary = "history", description = "Get chat records")
@GetMapping("platforms/{platformId}/threads/{threadId}/history")
public ResponseEntity<List<ChatMessageVO>> history(@PathVariable Long platformId, @PathVariable Long threadId) {
return ResponseEntity.success(chatService.history(platformId, threadId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.bigtop.manager.server.model.converter;

import org.apache.bigtop.manager.server.config.MapStructSharedConfig;
import org.apache.bigtop.manager.server.model.dto.PropertyDTO;
import org.apache.bigtop.manager.server.model.vo.PropertyVO;

import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.List;

@Mapper(config = MapStructSharedConfig.class)
public interface AuthCredentialConverter {

AuthCredentialConverter INSTANCE = Mappers.getMapper(AuthCredentialConverter.class);

PropertyVO fromDTO2VO(PropertyDTO propertyDTO);

List<PropertyVO> fromDTO2VO(List<PropertyDTO> propertyDTOList);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.bigtop.manager.server.model.converter;

import org.apache.bigtop.manager.server.config.MapStructSharedConfig;
import org.apache.bigtop.manager.server.model.dto.PlatformDTO;
import org.apache.bigtop.manager.server.model.req.PlatformReq;

import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

@Mapper(config = MapStructSharedConfig.class)
public interface PlatformConverter {
PlatformConverter INSTANCE = Mappers.getMapper(PlatformConverter.class);

PlatformDTO fromReq2DTO(PlatformReq platformReq);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.bigtop.manager.server.model.dto;

import lombok.Data;

import java.io.Serializable;

@Data
public class AuthCredentialDTO implements Serializable {

private String key;

private String value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.bigtop.manager.server.model.dto;

import lombok.Data;

import java.util.List;

@Data
public class PlatformDTO {
private Long platformId;
private List<AuthCredentialDTO> authCredentials;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.bigtop.manager.server.model.req;

import lombok.Data;

import jakarta.validation.constraints.NotBlank;

@Data
public class AuthCredentialReq {

@NotBlank
private String key;

private String value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.bigtop.manager.server.model.req;

import lombok.Data;

import jakarta.validation.constraints.NotEmpty;
import java.util.List;

@Data
public class PlatformReq {
@NotEmpty
private Long platformId;

@NotEmpty
private List<AuthCredentialReq> authCredentials;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.bigtop.manager.server.model.vo;

import lombok.Data;

@Data
public class ChatMessageVO {
private String sender;

private String message;

private String createTime;

public ChatMessageVO(String sender, String messageText, String createTime) {
this.sender = sender;
this.message = messageText;
this.createTime = createTime;
}
}
Loading

0 comments on commit 48e788d

Please sign in to comment.