Compare commits
2 Commits
master
...
lizihan_br
| Author | SHA1 | Date |
|---|---|---|
|
|
ffea86e25f | 1 year ago |
|
|
e21812e1f8 | 1 year ago |
@ -0,0 +1,38 @@
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
!**/src/test/**/target/
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea/modules.xml
|
||||
.idea/jarRepositories.xml
|
||||
.idea/compiler.xml
|
||||
.idea/libraries/
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### Eclipse ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
|
||||
### Mac OS ###
|
||||
.DS_Store
|
||||
@ -0,0 +1,8 @@
|
||||
# 默认忽略的文件
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# 基于编辑器的 HTTP 客户端请求
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding">
|
||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
||||
@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.example</groupId>
|
||||
<artifactId>chatgpt-java</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.15.3</version> <!-- 确保版本存在 -->
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@ -0,0 +1,117 @@
|
||||
package org.example;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
|
||||
public class ChatGPTMultiTurn {
|
||||
|
||||
private static final String API_KEY = "sk-ZoGHErXVBPukGFAW0f4f88C51cD84717AfEaBd70A9B7E49d"; //API 密钥
|
||||
private static final String API_URL = "https://openkey.cloud/v1/chat/completions"; //URL
|
||||
private static final ObjectMapper mapper = new ObjectMapper();
|
||||
private static final HttpClient client = HttpClient.newHttpClient();
|
||||
private static final List<ObjectNode> messages = new ArrayList<>();
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
try {
|
||||
// 初始化对话,系统提示
|
||||
ObjectNode systemMessage = mapper.createObjectNode();
|
||||
systemMessage.put("role", "system");
|
||||
systemMessage.put("content", "你是一个乐于助人的助手。");
|
||||
messages.add(systemMessage);
|
||||
|
||||
System.out.println("与 ChatGPT 对话。输入 'exit' 以结束对话。");
|
||||
|
||||
while (true) {
|
||||
System.out.print("你: ");
|
||||
String userInput = scanner.nextLine();
|
||||
|
||||
if (userInput.equalsIgnoreCase("exit")) {
|
||||
System.out.println("对话结束。再见!");
|
||||
break;
|
||||
}
|
||||
|
||||
addUserMessage(userInput);
|
||||
String response = getResponse();
|
||||
System.out.println("ChatGPT 的回复: " + response);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
scanner.close();
|
||||
}
|
||||
}
|
||||
|
||||
private static void addUserMessage(String content) {
|
||||
ObjectNode userMessage = mapper.createObjectNode();
|
||||
userMessage.put("role", "user");
|
||||
userMessage.put("content", content);
|
||||
messages.add(userMessage);
|
||||
}
|
||||
|
||||
private static String getResponse() throws Exception {
|
||||
//JSON 请求体
|
||||
ObjectNode requestBody = mapper.createObjectNode();
|
||||
requestBody.put("model", "gpt-3.5-turbo");
|
||||
requestBody.put("temperature", 0.7);
|
||||
requestBody.set("messages", mapper.valueToTree(messages));
|
||||
|
||||
String requestBodyString = mapper.writeValueAsString(requestBody);
|
||||
|
||||
// 创建 HTTP 请求
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(API_URL))
|
||||
.header("Content-Type", "application/json")
|
||||
.header("Authorization", "Bearer " + API_KEY)
|
||||
.POST(HttpRequest.BodyPublishers.ofString(requestBodyString, StandardCharsets.UTF_8))
|
||||
.build();
|
||||
|
||||
// 发送请求并获取响应
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
// 调试
|
||||
|
||||
/*
|
||||
System.out.println("请求方法: " + request.method());
|
||||
System.out.println("请求URI: " + request.uri());
|
||||
System.out.println("请求体: " + requestBodyString);
|
||||
System.out.println("响应状态码: " + response.statusCode());
|
||||
System.out.println("响应体: " + response.body());
|
||||
*/
|
||||
|
||||
// 检查响应状态码
|
||||
if (response.statusCode() != 200) {
|
||||
throw new RuntimeException("请求失败,状态码:" + response.statusCode() + ",响应体:" + response.body());
|
||||
}
|
||||
|
||||
// 解析响应 JSON
|
||||
JsonNode jsonResponse = mapper.readTree(response.body());
|
||||
JsonNode choices = jsonResponse.get("choices");
|
||||
if (choices != null && choices.isArray() && choices.size() > 0) {
|
||||
JsonNode firstChoice = choices.get(0);
|
||||
JsonNode messageNode = firstChoice.get("message");
|
||||
if (messageNode != null) {
|
||||
// 将助手的回复添加到消息列表中
|
||||
ObjectNode assistantMessage = mapper.createObjectNode();
|
||||
assistantMessage.put("role", "assistant");
|
||||
assistantMessage.put("content", messageNode.get("content").asText());
|
||||
messages.add(assistantMessage);
|
||||
|
||||
return messageNode.get("content").asText();
|
||||
}
|
||||
}
|
||||
|
||||
throw new RuntimeException("无法解析响应内容");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
package org.example;
|
||||
|
||||
//TIP 要<b>运行</b>代码,请按 <shortcut actionId="Run"/> 或
|
||||
// 点击装订区域中的 <icon src="AllIcons.Actions.Execute"/> 图标。
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
//TIP 当文本光标位于高亮显示的文本处时按 <shortcut actionId="ShowIntentionActions"/>
|
||||
// 查看 IntelliJ IDEA 建议如何修正。
|
||||
System.out.printf("Hello and welcome!");
|
||||
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
//TIP 按 <shortcut actionId="Debug"/> 开始调试代码。我们已经设置了一个 <icon src="AllIcons.Debugger.Db_set_breakpoint"/> 断点
|
||||
// 但您始终可以通过按 <shortcut actionId="ToggleLineBreakpoint"/> 添加更多断点。
|
||||
System.out.println("i = " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue