mirror of
https://github.com/YuWanTingbb/unofficial-gpt4.git
synced 2025-10-13 21:51:23 +00:00
新增对于消息报错提醒,减少对于用户的困扰
This commit is contained in:
54
.github/workflows/jar-image.yml
vendored
54
.github/workflows/jar-image.yml
vendored
@@ -2,7 +2,7 @@ name: Build and Push Docker Image -jar
|
||||
|
||||
on:
|
||||
release:
|
||||
types: [created]
|
||||
types: [ created ]
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
tag:
|
||||
@@ -14,33 +14,33 @@ jobs:
|
||||
build-and-push:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v1
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v1
|
||||
|
||||
- name: Login to Docker Hub
|
||||
uses: docker/login-action@v1
|
||||
with:
|
||||
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||
password: ${{ secrets.DOCKERHUB_PASSWORD }}
|
||||
- name: Login to Docker Hub
|
||||
uses: docker/login-action@v1
|
||||
with:
|
||||
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||
password: ${{ secrets.DOCKERHUB_PASSWORD }}
|
||||
|
||||
- name: Set tag name
|
||||
id: tag_name
|
||||
run: |
|
||||
if [ "${{ github.event_name }}" = "release" ]; then
|
||||
echo "::set-output name=tag::${GITHUB_REF#refs/tags/}"
|
||||
elif [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
|
||||
echo "::set-output name=tag::${{ github.event.inputs.tag }}"
|
||||
fi
|
||||
- name: Set tag name
|
||||
id: tag_name
|
||||
run: |
|
||||
if [ "${{ github.event_name }}" = "release" ]; then
|
||||
echo "::set-output name=tag::${GITHUB_REF#refs/tags/}"
|
||||
elif [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
|
||||
echo "::set-output name=tag::${{ github.event.inputs.tag }}"
|
||||
fi
|
||||
|
||||
- name: Build and push Docker image with Release tag
|
||||
uses: docker/build-push-action@v2
|
||||
with:
|
||||
context: .
|
||||
file: ./Dockerfiles/Dockerfile.jar
|
||||
push: true
|
||||
tags: |
|
||||
yangclivia/gpt4-copilot-java:${{ steps.tag_name.outputs.tag }}-jar
|
||||
yangclivia/gpt4-copilot-java:latest-jar
|
||||
platforms: linux/amd64,linux/arm64/v8
|
||||
- name: Build and push Docker image with Release tag
|
||||
uses: docker/build-push-action@v2
|
||||
with:
|
||||
context: .
|
||||
file: ./Dockerfiles/Dockerfile.jar
|
||||
push: true
|
||||
tags: |
|
||||
yangclivia/gpt4-copilot-java:${{ steps.tag_name.outputs.tag }}-jar
|
||||
yangclivia/gpt4-copilot-java:latest-jar
|
||||
platforms: linux/amd64,linux/arm64/v8
|
||||
|
192
.github/workflows/maven-publish.yml
vendored
192
.github/workflows/maven-publish.yml
vendored
@@ -1,7 +1,7 @@
|
||||
name: Build and Upload Spring Boot Native Image with Maven
|
||||
on:
|
||||
release:
|
||||
types: [created]
|
||||
types: [ created ]
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
tag:
|
||||
@@ -13,100 +13,100 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest, macos-latest, windows-latest]
|
||||
os: [ ubuntu-latest, macos-latest, windows-latest ]
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Set tag name
|
||||
id: tag_name
|
||||
run: |
|
||||
if [ "${{ github.event_name }}" = "release" ]; then
|
||||
echo "::set-output name=tag::${GITHUB_REF#refs/tags/}"
|
||||
elif [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
|
||||
echo "::set-output name=tag::${{ github.event.inputs.tag }}"
|
||||
fi
|
||||
shell: bash
|
||||
- name: Set up JDK 17
|
||||
uses: actions/setup-java@v2
|
||||
with:
|
||||
java-version: '17'
|
||||
distribution: 'temurin'
|
||||
- name: Cache Maven packages
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: ~/.m2
|
||||
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
|
||||
restore-keys: ${{ runner.os }}-m2
|
||||
- uses: graalvm/setup-graalvm@v1
|
||||
with:
|
||||
java-version: '21'
|
||||
distribution: 'graalvm'
|
||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
native-image-job-reports: 'true'
|
||||
- name: Set execute permissions for mvnw
|
||||
run: chmod +x ./mvnw
|
||||
- name: Set UTF-8 Encoding
|
||||
run: echo "JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8" >> $GITHUB_ENV
|
||||
- name: Build native image with Maven
|
||||
run: |
|
||||
./mvnw native:compile -Pnative
|
||||
if [ "${{ matrix.os }}" = "windows-latest" ]; then
|
||||
mv ./target/gpt-4-copilot.exe ./target/gpt-4-copilot-${{ steps.tag_name.outputs.tag }}-${{ matrix.os }}.exe
|
||||
else
|
||||
mv ./target/gpt-4-copilot ./target/gpt-4-copilot-${{ steps.tag_name.outputs.tag }}-${{ matrix.os }}
|
||||
fi
|
||||
shell: bash
|
||||
- name: Create directory for packaging
|
||||
run: |
|
||||
mkdir packaging
|
||||
if [ "${{ matrix.os }}" = "windows-latest" ]; then
|
||||
cp ./target/gpt-4-copilot-${{ steps.tag_name.outputs.tag }}-${{ matrix.os }}.exe ./packaging/gpt-4-copilot.exe
|
||||
else
|
||||
cp ./target/gpt-4-copilot-${{ steps.tag_name.outputs.tag }}-${{ matrix.os }} ./packaging/gpt-4-copilot
|
||||
fi
|
||||
cp ./config.json ./packaging/config.json
|
||||
shell: bash
|
||||
- name: Create ZIP (Unix)
|
||||
if: runner.os != 'Windows'
|
||||
run: |
|
||||
cd packaging
|
||||
zip -r ../target/gpt-4-copilot-${{ steps.tag_name.outputs.tag }}-${{ matrix.os }}.zip .
|
||||
shell: bash
|
||||
- name: Create ZIP (Windows)
|
||||
if: runner.os == 'Windows'
|
||||
run: |
|
||||
Compress-Archive -Path packaging/* -DestinationPath ./target/gpt-4-copilot-${{ steps.tag_name.outputs.tag }}-${{ matrix.os }}.zip
|
||||
shell: powershell
|
||||
- name: Check if release exists
|
||||
id: check_release
|
||||
run: |
|
||||
RELEASE=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
|
||||
https://api.github.com/repos/${{ github.repository }}/releases/tags/${{ steps.tag_name.outputs.tag }}) || true
|
||||
if echo "$RELEASE" | grep -q '"id":'; then
|
||||
echo "::set-output name=exists::true"
|
||||
UPLOAD_URL=$(echo "$RELEASE" | jq -r .upload_url)
|
||||
echo "::set-output name=upload_url::${UPLOAD_URL}"
|
||||
else
|
||||
echo "::set-output name=exists::false"
|
||||
fi
|
||||
shell: bash
|
||||
- name: Create Release if not exists
|
||||
if: steps.check_release.outputs.exists == 'false'
|
||||
id: create_release
|
||||
uses: actions/create-release@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
tag_name: ${{ steps.tag_name.outputs.tag }}
|
||||
release_name: Release ${{ steps.tag_name.outputs.tag }}
|
||||
draft: false
|
||||
prerelease: false
|
||||
- name: Upload ZIP to Release
|
||||
uses: actions/upload-release-asset@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
upload_url: ${{ steps.check_release.outputs.exists == 'true' && steps.check_release.outputs.upload_url || steps.create_release.outputs.upload_url }}
|
||||
asset_path: ./target/gpt-4-copilot-${{ steps.tag_name.outputs.tag }}-${{ matrix.os }}.zip
|
||||
asset_name: gpt-4-copilot-${{ steps.tag_name.outputs.tag }}-${{ matrix.os }}.zip
|
||||
asset_content_type: application/zip
|
||||
- name: Set tag name
|
||||
id: tag_name
|
||||
run: |
|
||||
if [ "${{ github.event_name }}" = "release" ]; then
|
||||
echo "::set-output name=tag::${GITHUB_REF#refs/tags/}"
|
||||
elif [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
|
||||
echo "::set-output name=tag::${{ github.event.inputs.tag }}"
|
||||
fi
|
||||
shell: bash
|
||||
- name: Set up JDK 17
|
||||
uses: actions/setup-java@v2
|
||||
with:
|
||||
java-version: '17'
|
||||
distribution: 'temurin'
|
||||
- name: Cache Maven packages
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: ~/.m2
|
||||
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
|
||||
restore-keys: ${{ runner.os }}-m2
|
||||
- uses: graalvm/setup-graalvm@v1
|
||||
with:
|
||||
java-version: '21'
|
||||
distribution: 'graalvm'
|
||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
native-image-job-reports: 'true'
|
||||
- name: Set execute permissions for mvnw
|
||||
run: chmod +x ./mvnw
|
||||
- name: Set UTF-8 Encoding
|
||||
run: echo "JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8" >> $GITHUB_ENV
|
||||
- name: Build native image with Maven
|
||||
run: |
|
||||
./mvnw native:compile -Pnative
|
||||
if [ "${{ matrix.os }}" = "windows-latest" ]; then
|
||||
mv ./target/gpt-4-copilot.exe ./target/gpt-4-copilot-${{ steps.tag_name.outputs.tag }}-${{ matrix.os }}.exe
|
||||
else
|
||||
mv ./target/gpt-4-copilot ./target/gpt-4-copilot-${{ steps.tag_name.outputs.tag }}-${{ matrix.os }}
|
||||
fi
|
||||
shell: bash
|
||||
- name: Create directory for packaging
|
||||
run: |
|
||||
mkdir packaging
|
||||
if [ "${{ matrix.os }}" = "windows-latest" ]; then
|
||||
cp ./target/gpt-4-copilot-${{ steps.tag_name.outputs.tag }}-${{ matrix.os }}.exe ./packaging/gpt-4-copilot.exe
|
||||
else
|
||||
cp ./target/gpt-4-copilot-${{ steps.tag_name.outputs.tag }}-${{ matrix.os }} ./packaging/gpt-4-copilot
|
||||
fi
|
||||
cp ./config.json ./packaging/config.json
|
||||
shell: bash
|
||||
- name: Create ZIP (Unix)
|
||||
if: runner.os != 'Windows'
|
||||
run: |
|
||||
cd packaging
|
||||
zip -r ../target/gpt-4-copilot-${{ steps.tag_name.outputs.tag }}-${{ matrix.os }}.zip .
|
||||
shell: bash
|
||||
- name: Create ZIP (Windows)
|
||||
if: runner.os == 'Windows'
|
||||
run: |
|
||||
Compress-Archive -Path packaging/* -DestinationPath ./target/gpt-4-copilot-${{ steps.tag_name.outputs.tag }}-${{ matrix.os }}.zip
|
||||
shell: powershell
|
||||
- name: Check if release exists
|
||||
id: check_release
|
||||
run: |
|
||||
RELEASE=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
|
||||
https://api.github.com/repos/${{ github.repository }}/releases/tags/${{ steps.tag_name.outputs.tag }}) || true
|
||||
if echo "$RELEASE" | grep -q '"id":'; then
|
||||
echo "::set-output name=exists::true"
|
||||
UPLOAD_URL=$(echo "$RELEASE" | jq -r .upload_url)
|
||||
echo "::set-output name=upload_url::${UPLOAD_URL}"
|
||||
else
|
||||
echo "::set-output name=exists::false"
|
||||
fi
|
||||
shell: bash
|
||||
- name: Create Release if not exists
|
||||
if: steps.check_release.outputs.exists == 'false'
|
||||
id: create_release
|
||||
uses: actions/create-release@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
tag_name: ${{ steps.tag_name.outputs.tag }}
|
||||
release_name: Release ${{ steps.tag_name.outputs.tag }}
|
||||
draft: false
|
||||
prerelease: false
|
||||
- name: Upload ZIP to Release
|
||||
uses: actions/upload-release-asset@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
upload_url: ${{ steps.check_release.outputs.exists == 'true' && steps.check_release.outputs.upload_url || steps.create_release.outputs.upload_url }}
|
||||
asset_path: ./target/gpt-4-copilot-${{ steps.tag_name.outputs.tag }}-${{ matrix.os }}.zip
|
||||
asset_name: gpt-4-copilot-${{ steps.tag_name.outputs.tag }}-${{ matrix.os }}.zip
|
||||
asset_content_type: application/zip
|
||||
|
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +1,4 @@
|
||||
|
||||
e -i 66fe458e9ff3b7072252b455b08ac5c3076591bc^
|
||||
*.json
|
||||
*.json
|
||||
|
26
config.json
26
config.json
@@ -1,15 +1,15 @@
|
||||
{
|
||||
"password":"920052ec-14fc-4215-974d-d6fd3c89dd91",
|
||||
"gpt4_prompt":true,
|
||||
"gpt3_sleepTime":0,
|
||||
"gpt4_sleepTime":100,
|
||||
"maxPoolSize":300,
|
||||
"vscode_version":"1.88.0",
|
||||
"copilot_chat_version":"0.15.2024041001",
|
||||
"get_token_url":"https://api.cocopilot.org/copilot_internal/v2/token",
|
||||
"one_copilot_limit":30,
|
||||
"one_coCopilot_limit":30,
|
||||
"one_selfCopilot_limit":30,
|
||||
"serverPort":8080,
|
||||
"prefix":"/"
|
||||
"password": "920052ec-14fc-4215-974d-d6fd3c89dd91",
|
||||
"gpt4_prompt": true,
|
||||
"gpt3_sleepTime": 0,
|
||||
"gpt4_sleepTime": 100,
|
||||
"maxPoolSize": 300,
|
||||
"vscode_version": "1.88.0",
|
||||
"copilot_chat_version": "0.15.2024041001",
|
||||
"get_token_url": "https://api.cocopilot.org/copilot_internal/v2/token",
|
||||
"one_copilot_limit": 30,
|
||||
"one_coCopilot_limit": 30,
|
||||
"one_selfCopilot_limit": 30,
|
||||
"serverPort": 8080,
|
||||
"prefix": "/"
|
||||
}
|
2
pom.xml
2
pom.xml
@@ -10,7 +10,7 @@
|
||||
</parent>
|
||||
<groupId>com.gpt4.copilot</groupId>
|
||||
<artifactId>gpt-4-copilot</artifactId>
|
||||
<version>0.1.3</version>
|
||||
<version>0.2.0</version>
|
||||
<name>native</name>
|
||||
<description>Demo project for Spring Boot with GraalVM Native Image</description>
|
||||
<properties>
|
||||
|
@@ -24,6 +24,7 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -157,7 +158,7 @@ public class ChatController {
|
||||
File jsonFile = new File(parent);
|
||||
Path jsonFilePath = Paths.get(parent);
|
||||
// 如果 JSON 文件不存在,创建一个新的 JSON 对象
|
||||
if (!jsonFile.exists() || jsonFile.length() == 0){
|
||||
if (!jsonFile.exists() || jsonFile.length() == 0) {
|
||||
try {
|
||||
if (!jsonFile.exists()) {
|
||||
// 创建文件machineIdList.json
|
||||
@@ -476,15 +477,7 @@ public class ChatController {
|
||||
// 异步处理
|
||||
CompletableFuture<ResponseEntity<Object>> future = CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
if (conversation == null) {
|
||||
return new ResponseEntity<>(Result.error("Request body is missing or not in JSON format"), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
String apiKey;
|
||||
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
|
||||
apiKey = authorizationHeader.substring(7);
|
||||
} else {
|
||||
return new ResponseEntity<>(Result.error("Authorization header is missing"), HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
String apiKey = getRequestApikey(authorizationHeader, conversation);
|
||||
if (!copilotTokenList.containsKey(apiKey)) {
|
||||
String token = getCopilotToken(apiKey);
|
||||
if (token == null) {
|
||||
@@ -512,6 +505,8 @@ public class ChatController {
|
||||
if (!resp.isSuccessful()) {
|
||||
if (resp.code() == 429) {
|
||||
return new ResponseEntity<>(Result.error("rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
||||
} else if (resp.code() == 400) {
|
||||
return new ResponseEntity<>(Result.error("messages is none or too long and over 32K"), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
} else {
|
||||
String token = getCopilotToken(apiKey);
|
||||
if (token == null) {
|
||||
@@ -526,8 +521,8 @@ public class ChatController {
|
||||
outPutChat(response, resp, conversation, model);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
return null;
|
||||
}, executor);
|
||||
@@ -535,14 +530,26 @@ public class ChatController {
|
||||
return getObjectResponseEntity(response, future);
|
||||
}
|
||||
|
||||
private String getRequestApikey(String authorizationHeader, @org.springframework.web.bind.annotation.RequestBody Object conversation) {
|
||||
if (conversation == null) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Request body is missing or not in JSON format");
|
||||
}
|
||||
String apiKey;
|
||||
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
|
||||
apiKey = authorizationHeader.substring(7);
|
||||
} else {
|
||||
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "Authorization header is missing");
|
||||
}
|
||||
return apiKey;
|
||||
}
|
||||
|
||||
private String modelAdjust(Conversation conversation) {
|
||||
String model = conversation.getModel();
|
||||
if(model == null){
|
||||
if (model == null) {
|
||||
conversation.setModel("gpt-3.5-turbo");
|
||||
return "gpt-3.5-turbo";
|
||||
}
|
||||
else{
|
||||
if(model.startsWith("gpt-4")) {
|
||||
} else {
|
||||
if (model.startsWith("gpt-4")) {
|
||||
conversation.setModel("gpt-4");
|
||||
}
|
||||
return model;
|
||||
@@ -571,15 +578,7 @@ public class ChatController {
|
||||
// 异步处理
|
||||
CompletableFuture<ResponseEntity<Object>> future = CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
if (conversation == null) {
|
||||
return new ResponseEntity<>(Result.error("Request body is missing or not in JSON format"), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
String apiKey;
|
||||
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
|
||||
apiKey = authorizationHeader.substring(7);
|
||||
} else {
|
||||
return new ResponseEntity<>(Result.error("Authorization header is missing"), HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
String apiKey = getRequestApikey(authorizationHeader, conversation);
|
||||
if (!coCopilotTokenList.containsKey(apiKey)) {
|
||||
String token = getCoCoToken(apiKey);
|
||||
if (token == null) {
|
||||
@@ -607,6 +606,8 @@ public class ChatController {
|
||||
if (!resp.isSuccessful()) {
|
||||
if (resp.code() == 429) {
|
||||
return new ResponseEntity<>(Result.error("rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
||||
} else if (resp.code() == 400) {
|
||||
return new ResponseEntity<>(Result.error("messages is none or too long and over 32K"), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
} else {
|
||||
String token = getCoCoToken(apiKey);
|
||||
if (token == null) {
|
||||
@@ -621,8 +622,8 @@ public class ChatController {
|
||||
outPutChat(response, resp, conversation, model);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
return null;
|
||||
}, executor);
|
||||
@@ -661,7 +662,7 @@ public class ChatController {
|
||||
*/
|
||||
private String[] extractApiKeyAndRequestUrl(String authorizationHeader, Object conversation) throws IllegalArgumentException {
|
||||
if (conversation == null) {
|
||||
throw new IllegalArgumentException("Request body is missing or not in JSON format");
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Request body is missing or not in JSON format");
|
||||
}
|
||||
String apiKey = null;
|
||||
String requestUrl = null;
|
||||
@@ -732,6 +733,8 @@ public class ChatController {
|
||||
if (!resp.isSuccessful()) {
|
||||
if (resp.code() == 429) {
|
||||
return new ResponseEntity<>(Result.error("rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
||||
} else if (resp.code() == 400) {
|
||||
return new ResponseEntity<>(Result.error("messages is none or too long and over 32K"), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
} else {
|
||||
String token = getSelfToken(apiKey, requestUrl);
|
||||
if (token == null) {
|
||||
@@ -746,8 +749,8 @@ public class ChatController {
|
||||
outPutChat(response, resp, conversation, model);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
return null;
|
||||
}, executor);
|
||||
@@ -807,7 +810,7 @@ public class ChatController {
|
||||
}
|
||||
|
||||
private Request getEmdPrompt(Object conversation,
|
||||
Map<String, String> headersMap) {
|
||||
Map<String, String> headersMap) {
|
||||
try {
|
||||
String json = com.alibaba.fastjson2.JSON.toJSONString(conversation);
|
||||
RequestBody requestBody = RequestBody.create(json, JSON);
|
||||
@@ -821,7 +824,6 @@ public class ChatController {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* ghu/gho 请求
|
||||
* 请求体不是json 会报Request body is missing or not in JSON format
|
||||
@@ -844,15 +846,7 @@ public class ChatController {
|
||||
// 异步处理
|
||||
CompletableFuture<ResponseEntity<Object>> future = CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
if (conversation == null) {
|
||||
return new ResponseEntity<>(Result.error("Request body is missing or not in JSON format"), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
String apiKey;
|
||||
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
|
||||
apiKey = authorizationHeader.substring(7);
|
||||
} else {
|
||||
return new ResponseEntity<>(Result.error("Authorization header is missing"), HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
String apiKey = getRequestApikey(authorizationHeader, conversation);
|
||||
if (!copilotTokenList.containsKey(apiKey)) {
|
||||
String token = getCopilotToken(apiKey);
|
||||
if (token == null) {
|
||||
@@ -879,6 +873,8 @@ public class ChatController {
|
||||
if (!resp.isSuccessful()) {
|
||||
if (resp.code() == 429) {
|
||||
return new ResponseEntity<>(Result.error("rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
||||
} else if (resp.code() == 400) {
|
||||
return new ResponseEntity<>(Result.error("messages is none or too long and over 32K"), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
} else {
|
||||
String token = getCopilotToken(apiKey);
|
||||
if (token == null) {
|
||||
@@ -894,8 +890,8 @@ public class ChatController {
|
||||
}
|
||||
}
|
||||
return null;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
}, executor);
|
||||
|
||||
@@ -940,15 +936,7 @@ public class ChatController {
|
||||
// 异步处理
|
||||
CompletableFuture<ResponseEntity<Object>> future = CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
if (conversation == null) {
|
||||
return new ResponseEntity<>(Result.error("Request body is missing or not in JSON format"), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
String apiKey;
|
||||
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
|
||||
apiKey = authorizationHeader.substring(7);
|
||||
} else {
|
||||
return new ResponseEntity<>(Result.error("Authorization header is missing"), HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
String apiKey = getRequestApikey(authorizationHeader, conversation);
|
||||
if (!coCopilotTokenList.containsKey(apiKey)) {
|
||||
String token = getCoCoToken(apiKey);
|
||||
if (token == null) {
|
||||
@@ -975,6 +963,8 @@ public class ChatController {
|
||||
if (!resp.isSuccessful()) {
|
||||
if (resp.code() == 429) {
|
||||
return new ResponseEntity<>(Result.error("rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
||||
} else if (resp.code() == 400) {
|
||||
return new ResponseEntity<>(Result.error("messages is none or too long and over 32K"), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
} else {
|
||||
String token = getCoCoToken(apiKey);
|
||||
if (token == null) {
|
||||
@@ -990,8 +980,8 @@ public class ChatController {
|
||||
}
|
||||
}
|
||||
return null;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
}, executor);
|
||||
|
||||
@@ -1049,6 +1039,8 @@ public class ChatController {
|
||||
if (!resp.isSuccessful()) {
|
||||
if (resp.code() == 429) {
|
||||
return new ResponseEntity<>(Result.error("rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
||||
} else if (resp.code() == 400) {
|
||||
return new ResponseEntity<>(Result.error("messages is none or too long and over 32K"), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
} else {
|
||||
String token = getSelfToken(apiKey, requestUrl);
|
||||
if (token == null) {
|
||||
@@ -1064,8 +1056,8 @@ public class ChatController {
|
||||
}
|
||||
}
|
||||
return null;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
}, executor);
|
||||
return getObjectResponseEntity(future);
|
||||
@@ -1094,7 +1086,7 @@ public class ChatController {
|
||||
}
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1301,7 +1293,7 @@ public class ChatController {
|
||||
*/
|
||||
private String saveMadchineId(String apiKey) {
|
||||
try {
|
||||
if(machineIdList.containsKey(apiKey)){
|
||||
if (machineIdList.containsKey(apiKey)) {
|
||||
String machineId = machineIdList.get(apiKey);
|
||||
log.info("机械码读取成功!对应的机械码为:" + machineId);
|
||||
return machineId;
|
||||
|
@@ -25,7 +25,7 @@ public class CustomErrorController implements ErrorController {
|
||||
" <title>Document</title>\n" +
|
||||
"</head>\n" +
|
||||
"<body>\n" +
|
||||
" <p>Thanks you use gpt4-copilot-java-0.1.3</p>\n" +
|
||||
" <p>Thanks you use gpt4-copilot-java-0.2.0</p>\n" +
|
||||
" <p><a href=\"https://apifox.com/apidoc/shared-4301e565-a8df-48a0-85a5-bda2c4c3965a\">详细使用文档</a></p>\n" +
|
||||
" <p><a href=\"https://github.com/Yanyutin753/gpt4-copilot-java-sh\">项目地址</a></p>\n" +
|
||||
"</body>\n" +
|
||||
|
@@ -37,7 +37,7 @@ public class copilotApplication {
|
||||
private static final String VS_CODE_API_URL = "https://api.github.com/repos/microsoft/vscode/releases/latest";
|
||||
private static final String VS_CODE_CHAT_URL = "https://marketplace.visualstudio.com/_apis/public/gallery/extensionquery";
|
||||
|
||||
public static void main(String[] args){
|
||||
public static void main(String[] args) {
|
||||
String configFilePath = System.getProperty("user.dir") + File.separator + "config.json";
|
||||
SystemSetting config = loadConfig(configFilePath);
|
||||
setSystemProperties(config);
|
||||
@@ -48,7 +48,7 @@ public class copilotApplication {
|
||||
private static SystemSetting loadConfig(String configFilePath) {
|
||||
File jsonFile = new File(configFilePath);
|
||||
Path jsonFilePath = Paths.get(configFilePath);
|
||||
if (!jsonFile.exists() || jsonFile.length() == 0){
|
||||
if (!jsonFile.exists() || jsonFile.length() == 0) {
|
||||
try {
|
||||
if (!jsonFile.exists()) {
|
||||
// 创建文件config.json
|
||||
@@ -71,7 +71,7 @@ public class copilotApplication {
|
||||
String jsonContent = new String(Files.readAllBytes(Paths.get(configFilePath)));
|
||||
// 将 JSON 字符串解析为 JSONObject
|
||||
JSONObject jsonObject = com.alibaba.fastjson2.JSON.parseObject(jsonContent);
|
||||
if(jsonObject == null){
|
||||
if (jsonObject == null) {
|
||||
jsonObject = new JSONObject();
|
||||
}
|
||||
String password = getValueOrDefault(jsonObject, "password", UUID.randomUUID().toString(), "config.json没有新增password参数,现已增加!");
|
||||
@@ -115,8 +115,7 @@ public class copilotApplication {
|
||||
T value;
|
||||
if (jsonObject == null) {
|
||||
value = null;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
try {
|
||||
value = (T) jsonObject.get(key);
|
||||
} catch (JSONException e) {
|
||||
@@ -297,11 +296,12 @@ public class copilotApplication {
|
||||
System.out.println("one_selfCopilot_limit:" + ChatController.getSystemSetting().getOne_selfCopilot_limit());
|
||||
System.out.println("gpt4-copilot-java 初始化接口成功!");
|
||||
System.out.println("======================================================");
|
||||
System.out.println("******原神gpt4-copilot-java-native v0.1.3启动成功******");
|
||||
System.out.println("******原神gpt4-copilot-java-native v0.2.0启动成功******");
|
||||
System.out.println("* 对chat接口的模型进行重定向,减少潜在的风险");
|
||||
System.out.println("* 使用ConcurrentHashMap,粗略的对于每个密钥按每分钟进行限速");
|
||||
System.out.println("* 新增环境变量用于对gpt-4*等模型进行系统prompt提示");
|
||||
System.out.println("* 新增url|apikey形式传入/self/*接口,用于自定义地址和密钥");
|
||||
System.out.println("* 新增对于消息报错提醒,减少对于用户的困扰");
|
||||
System.out.println("* 修复部分bug,优化读取config.json代码,提升稳定性");
|
||||
System.out.println("* 新增每个密钥对于特定的机器码,且保存在文件中,一秘钥一机器码,减小被查询异常");
|
||||
System.out.println("URL地址:http://0.0.0.0:" + config.getServerPort() + config.getPrefix() + "");
|
||||
|
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.
Binary file not shown.
Binary file not shown.
BIN
target/gpt-4-copilot-0.2.0.jar.original
Normal file
BIN
target/gpt-4-copilot-0.2.0.jar.original
Normal file
Binary file not shown.
@@ -1,3 +1,3 @@
|
||||
artifactId=gpt-4-copilot
|
||||
groupId=com.gpt4.copilot
|
||||
version=0.1.3
|
||||
version=0.2.0
|
||||
|
Reference in New Issue
Block a user