Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BIGTOP-4189: Mock Data Implementation for AI Chat API in Bigtop-Manager #46

Merged
merged 6 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
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.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.*;

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

import jakarta.annotation.Resource;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

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 = "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<Integer> deleteAuthorizedPlatform(@PathVariable Long platformId) {
int code = chatService.deleteAuthorizedPlatform(platformId);
if (code != 0) {
return ResponseEntity.error(ResponseStatus.PARAMETER_ERROR,"权限不足");
}
return ResponseEntity.success(0);
}

@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<Integer> deleteChatThreads(@PathVariable Long platformId, @PathVariable Long threadId) {
int code = chatService.deleteChatThreads(platformId, threadId);
if (code != 0) {
return ResponseEntity.error(ResponseStatus.PARAMETER_ERROR, "无内容");
}
return ResponseEntity.success(0);
}

@Operation(summary = "get", description = "Get all threads of a platform")
@GetMapping("platforms/{platformId}/threads")
public ResponseEntity<List<ChatThreadVO>> getAllChatThreads(
@PathVariable Long platformId, @RequestParam(value = "model", required = false) 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,16 @@
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.dto.UserDTO;
import org.apache.bigtop.manager.server.model.req.PlatformReq;
import org.apache.bigtop.manager.server.model.req.UserReq;
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,10 @@
package org.apache.bigtop.manager.server.model.dto;

import jakarta.validation.constraints.NotEmpty;
import lombok.Data;

@Data
public class PlatformDTO {
private Long platformId;
private String apiKey;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.apache.bigtop.manager.server.model.req;

import jakarta.validation.constraints.NotEmpty;
import lombok.Data;

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

@NotEmpty
private String apiKey;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.apache.bigtop.manager.server.model.vo;

import lombok.Data;

@Data
public class ChatThreadVO {
private Long threadId;

private Long platformId;

private String model;

private String createTime;

private String updateTime;

public ChatThreadVO(Long tid, Long pid, String model, String createTime) {
this.threadId = tid;
this.platformId = pid;
this.model = model;
this.createTime = createTime;
this.updateTime = createTime;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.apache.bigtop.manager.server.model.vo;

import lombok.Data;

@Data
public class PlatformAuthorizedVO {
private Long platformId;

private String platformName;

private String apiKey;

private String supportModels;

public PlatformAuthorizedVO(long l, String name, String key, String models) {
this.platformId = l;
this.platformName = name;
this.supportModels = models;
this.apiKey = key;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.apache.bigtop.manager.server.model.vo;

import lombok.Data;

@Data
public class PlatformVO {
private Long id;

private String name;

private String supportModels;

public PlatformVO(Long i, String name, String models) {
this.id = i;
this.name = name;
this.supportModels = models;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.apache.bigtop.manager.server.service;

import org.apache.bigtop.manager.server.model.dto.PlatformDTO;
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.PlatformAuthorizedVO;
import org.apache.bigtop.manager.server.model.vo.PlatformVO;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.List;

public interface AIChatService {
List<PlatformVO> platforms();

List<PlatformAuthorizedVO> authorizedPlatforms();

PlatformVO addAuthorizedPlatform(PlatformDTO platformDTO);

int deleteAuthorizedPlatform(Long platformId);

ChatThreadVO createChatThreads(Long platformId, String model);

int deleteChatThreads(Long platformId, Long threadId);

List<ChatThreadVO> getAllChatThreads(Long platformId, String model);

SseEmitter talk(Long platformId, Long threadId, String message);

List<ChatMessageVO> history(Long platformId, Long threadId);
}
Loading
Loading