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

Update SimpleApp.java #293

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
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
20 changes: 19 additions & 1 deletion FlySpring/edgechain-app/chat/SimpleApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ public static void main(String[] args) {
System.setProperty("server.port", "8080");
SpringApplication.run(SimpleApp.class, args);
}
/* Added new methods for it
@SpringBootApplication
public class SimpleApp {

private static final Logger logger = LoggerFactory.getLogger(SimpleApp.class);

@Value("${openai.api.key}")
private String openaiApiKey;

public static void main(String[] args) {
System.setProperty("server.port", "8080");
SpringApplication.run(SimpleApp.class, args);
}
*/

@RestController
@RequestMapping("/v1/examples")
Expand All @@ -47,6 +61,7 @@ public Conversation() {

@PostMapping("/gpt/ask")
public ResponseEntity<String> ask(@RequestBody String prompt) {
try { // Added try-catch exception
updateMessageList("user", prompt);
String model = "gpt-3.5-turbo";
ChatCompletionRequest chatCompletionRequest =
Expand All @@ -70,7 +85,10 @@ public ResponseEntity<String> ask(@RequestBody String prompt) {
System.out.println(response);
updateMessageList("assistant", response);
return new ResponseEntity<>(response, HttpStatus.OK);
}
} catch (Exception e) { // Added try-catch exception
logger.error("An error occurred while processing the request.", e);
return new ResponseEntity<>("An error occurred: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}

private void updateMessageList(String role, String content) {
messages.add(new ChatMessage(role, content));
Expand Down
Loading