🔄Update: Final
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package cn.hezhaohui.backend.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@Configuration
|
||||
public class RestTmplateConfig {
|
||||
@Bean
|
||||
public RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package cn.hezhaohui.backend.controller;
|
||||
|
||||
import cn.hezhaohui.backend.entity.ChatRequest;
|
||||
import cn.hezhaohui.backend.entity.ChatResponse;
|
||||
import cn.hezhaohui.backend.service.ChatService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@@ -14,7 +15,7 @@ public class ChatController {
|
||||
private ChatService chatService;
|
||||
|
||||
@PostMapping("/api/chat")
|
||||
public String chat(@RequestBody ChatRequest request) {
|
||||
public ChatResponse chat(@RequestBody ChatRequest request) {
|
||||
return chatService.chat(request);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package cn.hezhaohui.backend.entity;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class ChatResponse {
|
||||
private String response;
|
||||
}
|
||||
@@ -1,13 +1,33 @@
|
||||
package cn.hezhaohui.backend.service;
|
||||
|
||||
import cn.hezhaohui.backend.entity.ChatRequest;
|
||||
import cn.hezhaohui.backend.entity.ChatResponse;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@Service
|
||||
public class ChatService {
|
||||
|
||||
public String chat(ChatRequest request) {
|
||||
System.out.println(request);
|
||||
return " ";
|
||||
@Resource
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
|
||||
|
||||
public ChatResponse chat(ChatRequest req) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("Authorization", "Bearer " + req.getApiKey());
|
||||
headers.set("Content-Type", "application/json");
|
||||
String requestBody = String.format(
|
||||
"{\"model\": \"Qwen/Qwen3-30B-A3B\", \"messages\": [{\"role\": \"user\", \"content\": \"%s\"}]}",
|
||||
req.getMessage()
|
||||
);
|
||||
HttpEntity<String> stringHttpEntity = new HttpEntity<>(requestBody, headers);
|
||||
ResponseEntity<String> response = restTemplate.postForEntity(req.getEndpoint(), stringHttpEntity, String.class);
|
||||
System.out.println(response.getBody());
|
||||
return ChatResponse.builder().response(response.getBody()).build();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user