sanbo
commited on
Commit
·
39ea0b7
1
Parent(s):
9827e6c
update sth. at 2024-12-19 16:45:47
Browse files- .gitignore +67 -0
- Dockerfile +27 -0
- README.md +55 -5
- go.mod +34 -0
- go.sum +79 -0
- main.go +532 -0
- web/avatar.png +0 -0
- web/icon.png +0 -0
- web/index.html +0 -0
.gitignore
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.externalNativeBuild
|
2 |
+
import-summary.txt
|
3 |
+
|
4 |
+
#java files
|
5 |
+
*.class
|
6 |
+
*.dex
|
7 |
+
.sync/
|
8 |
+
|
9 |
+
#for idea temp file
|
10 |
+
*.iws
|
11 |
+
*.ipr
|
12 |
+
*.iml
|
13 |
+
target/
|
14 |
+
.idea/
|
15 |
+
.gradle/
|
16 |
+
release/
|
17 |
+
build/
|
18 |
+
spoon/
|
19 |
+
releasebak/
|
20 |
+
|
21 |
+
#mac temp file
|
22 |
+
__MACOSX
|
23 |
+
.DS_Store
|
24 |
+
._.DS_Store
|
25 |
+
|
26 |
+
#for eclipse
|
27 |
+
.settings/
|
28 |
+
local.properties
|
29 |
+
*gen/
|
30 |
+
*.classpath
|
31 |
+
*/bin/
|
32 |
+
bin/
|
33 |
+
.project
|
34 |
+
|
35 |
+
#temp file
|
36 |
+
*.bak
|
37 |
+
|
38 |
+
*.pmd
|
39 |
+
sh.exe.stackdump
|
40 |
+
|
41 |
+
.vs/
|
42 |
+
.vscode/
|
43 |
+
|
44 |
+
*.log
|
45 |
+
*.ctxt
|
46 |
+
.mtj.tmp/
|
47 |
+
|
48 |
+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
49 |
+
hs_err_pid*
|
50 |
+
|
51 |
+
# Package Files #
|
52 |
+
# *.jar
|
53 |
+
*.war
|
54 |
+
*.nar
|
55 |
+
*.ear
|
56 |
+
*.zip
|
57 |
+
*.tar.gz
|
58 |
+
*.rar
|
59 |
+
*.cxx
|
60 |
+
*.cfg
|
61 |
+
# for nodejs
|
62 |
+
node_modules/
|
63 |
+
# for python
|
64 |
+
package-lock.json
|
65 |
+
.$*
|
66 |
+
*.drawio.bkp
|
67 |
+
|
Dockerfile
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM golang:1.22 AS builder
|
2 |
+
|
3 |
+
ENV CGO_ENABLED=0
|
4 |
+
|
5 |
+
WORKDIR /app
|
6 |
+
|
7 |
+
COPY go.mod go.sum ./
|
8 |
+
RUN go mod download
|
9 |
+
|
10 |
+
COPY . .
|
11 |
+
RUN CGO_ENABLED=0 GOARCH=amd64 go build -o ddg .
|
12 |
+
|
13 |
+
FROM alpine:latest
|
14 |
+
|
15 |
+
WORKDIR /app
|
16 |
+
|
17 |
+
COPY --from=builder /app/ddg .
|
18 |
+
|
19 |
+
ENV MAX_RETRY_COUNT="3"
|
20 |
+
ENV RETRY_DELAY="5000"
|
21 |
+
ENV PORT="7860"
|
22 |
+
|
23 |
+
EXPOSE 7860
|
24 |
+
|
25 |
+
RUN chmod +x ddg
|
26 |
+
|
27 |
+
CMD ["./ddg"]
|
README.md
CHANGED
@@ -1,10 +1,60 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: docker
|
7 |
-
pinned:
|
|
|
|
|
8 |
---
|
9 |
|
10 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: d
|
3 |
+
emoji: 👀
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: yellow
|
6 |
sdk: docker
|
7 |
+
pinned: true
|
8 |
+
app_port: 7860
|
9 |
+
short_description: dd
|
10 |
---
|
11 |
|
12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
13 |
+
|
14 |
+
# infos
|
15 |
+
|
16 |
+
* models
|
17 |
+
|
18 |
+
``` bash
|
19 |
+
curl https://sanbo1200-ddghat.hf.space/models
|
20 |
+
```
|
21 |
+
|
22 |
+
* Request
|
23 |
+
|
24 |
+
``` bash
|
25 |
+
curl --location --request POST 'https://${|Embed this Space|--|Direct URL|}/completions' \
|
26 |
+
--header 'Content-Type: application/json' \
|
27 |
+
--data-raw '{
|
28 |
+
"model": "gpt-4o-mini",
|
29 |
+
"messages": [
|
30 |
+
{
|
31 |
+
"role": "user",
|
32 |
+
"content": "What is the principle of Bitcoin?"
|
33 |
+
}
|
34 |
+
],
|
35 |
+
"stream": true
|
36 |
+
}'
|
37 |
+
|
38 |
+
# local
|
39 |
+
curl --location --request POST 'http://0.0.0.0:8760/completions' \
|
40 |
+
--header 'Content-Type: application/json' \
|
41 |
+
--data-raw '{
|
42 |
+
"model": "gpt-4o-mini",
|
43 |
+
"messages": [
|
44 |
+
{
|
45 |
+
"role": "user",
|
46 |
+
"content": "What is the principle of Bitcoin?"
|
47 |
+
}
|
48 |
+
],
|
49 |
+
"stream": true
|
50 |
+
}'
|
51 |
+
|
52 |
+
```
|
53 |
+
|
54 |
+
## support models
|
55 |
+
|
56 |
+
- claude-3-haiku
|
57 |
+
- llama-3.1-70b
|
58 |
+
- mixtral-8x7b
|
59 |
+
- gpt-4o-mini
|
60 |
+
|
go.mod
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
module ddg
|
2 |
+
|
3 |
+
go 1.22.5
|
4 |
+
|
5 |
+
require (
|
6 |
+
github.com/bytedance/sonic v1.12.3 // indirect
|
7 |
+
github.com/bytedance/sonic/loader v0.2.0 // indirect
|
8 |
+
github.com/cloudwego/base64x v0.1.4 // indirect
|
9 |
+
github.com/cloudwego/iasm v0.2.0 // indirect
|
10 |
+
github.com/gabriel-vasile/mimetype v1.4.6 // indirect
|
11 |
+
github.com/gin-contrib/sse v0.1.0 // indirect
|
12 |
+
github.com/gin-gonic/gin v1.10.0 // indirect
|
13 |
+
github.com/go-playground/locales v0.14.1 // indirect
|
14 |
+
github.com/go-playground/universal-translator v0.18.1 // indirect
|
15 |
+
github.com/go-playground/validator/v10 v10.22.1 // indirect
|
16 |
+
github.com/goccy/go-json v0.10.3 // indirect
|
17 |
+
github.com/joho/godotenv v1.5.1 // indirect
|
18 |
+
github.com/json-iterator/go v1.1.12 // indirect
|
19 |
+
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
|
20 |
+
github.com/leodido/go-urn v1.4.0 // indirect
|
21 |
+
github.com/mattn/go-isatty v0.0.20 // indirect
|
22 |
+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
23 |
+
github.com/modern-go/reflect2 v1.0.2 // indirect
|
24 |
+
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
|
25 |
+
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
26 |
+
github.com/ugorji/go/codec v1.2.12 // indirect
|
27 |
+
golang.org/x/arch v0.11.0 // indirect
|
28 |
+
golang.org/x/crypto v0.28.0 // indirect
|
29 |
+
golang.org/x/net v0.30.0 // indirect
|
30 |
+
golang.org/x/sys v0.26.0 // indirect
|
31 |
+
golang.org/x/text v0.19.0 // indirect
|
32 |
+
google.golang.org/protobuf v1.35.1 // indirect
|
33 |
+
gopkg.in/yaml.v3 v3.0.1 // indirect
|
34 |
+
)
|
go.sum
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
github.com/bytedance/sonic v1.12.3 h1:W2MGa7RCU1QTeYRTPE3+88mVC0yXmsRQRChiyVocVjU=
|
2 |
+
github.com/bytedance/sonic v1.12.3/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk=
|
3 |
+
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
4 |
+
github.com/bytedance/sonic/loader v0.2.0 h1:zNprn+lsIP06C/IqCHs3gPQIvnvpKbbxyXQP1iU4kWM=
|
5 |
+
github.com/bytedance/sonic/loader v0.2.0/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
6 |
+
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
|
7 |
+
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
8 |
+
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
|
9 |
+
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
|
10 |
+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
11 |
+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
12 |
+
github.com/gabriel-vasile/mimetype v1.4.6 h1:3+PzJTKLkvgjeTbts6msPJt4DixhT4YtFNf1gtGe3zc=
|
13 |
+
github.com/gabriel-vasile/mimetype v1.4.6/go.mod h1:JX1qVKqZd40hUPpAfiNTe0Sne7hdfKSbOqqmkq8GCXc=
|
14 |
+
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
15 |
+
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
16 |
+
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
|
17 |
+
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
|
18 |
+
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
19 |
+
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
20 |
+
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
21 |
+
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
22 |
+
github.com/go-playground/validator/v10 v10.22.1 h1:40JcKH+bBNGFczGuoBYgX4I6m/i27HYW8P9FDk5PbgA=
|
23 |
+
github.com/go-playground/validator/v10 v10.22.1/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
|
24 |
+
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
|
25 |
+
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
26 |
+
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
27 |
+
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
28 |
+
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
29 |
+
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
30 |
+
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
31 |
+
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
32 |
+
github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM=
|
33 |
+
github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
|
34 |
+
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
35 |
+
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
36 |
+
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
37 |
+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
38 |
+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
39 |
+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
40 |
+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
41 |
+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
42 |
+
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
43 |
+
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
44 |
+
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
|
45 |
+
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
|
46 |
+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
47 |
+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
48 |
+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
49 |
+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
50 |
+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
51 |
+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
52 |
+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
53 |
+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
54 |
+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
55 |
+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
56 |
+
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
57 |
+
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
58 |
+
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
|
59 |
+
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
60 |
+
golang.org/x/arch v0.11.0 h1:KXV8WWKCXm6tRpLirl2szsO5j/oOODwZf4hATmGVNs4=
|
61 |
+
golang.org/x/arch v0.11.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
62 |
+
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
|
63 |
+
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
|
64 |
+
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
|
65 |
+
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
|
66 |
+
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
67 |
+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
68 |
+
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
|
69 |
+
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
70 |
+
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
|
71 |
+
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
72 |
+
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
|
73 |
+
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
74 |
+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
75 |
+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
76 |
+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
77 |
+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
78 |
+
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
79 |
+
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
|
main.go
ADDED
@@ -0,0 +1,532 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
package main
|
2 |
+
|
3 |
+
import (
|
4 |
+
"bufio"
|
5 |
+
"embed"
|
6 |
+
"encoding/json"
|
7 |
+
"errors"
|
8 |
+
"fmt"
|
9 |
+
"io"
|
10 |
+
"io/fs"
|
11 |
+
"log"
|
12 |
+
"net/http"
|
13 |
+
"os"
|
14 |
+
"strings"
|
15 |
+
"time"
|
16 |
+
|
17 |
+
"github.com/gin-gonic/gin"
|
18 |
+
"github.com/joho/godotenv"
|
19 |
+
)
|
20 |
+
|
21 |
+
//go:embed web/*
|
22 |
+
var staticFiles embed.FS
|
23 |
+
|
24 |
+
type Config struct {
|
25 |
+
APIPrefix string
|
26 |
+
APIKey string
|
27 |
+
MaxRetryCount int
|
28 |
+
RetryDelay time.Duration
|
29 |
+
FakeHeaders map[string]string
|
30 |
+
}
|
31 |
+
|
32 |
+
var config Config
|
33 |
+
|
34 |
+
func init() {
|
35 |
+
godotenv.Load()
|
36 |
+
config = Config{
|
37 |
+
APIKey: getEnv("API_KEY", ""),
|
38 |
+
MaxRetryCount: getIntEnv("MAX_RETRY_COUNT", 3),
|
39 |
+
RetryDelay: getDurationEnv("RETRY_DELAY", 5000),
|
40 |
+
FakeHeaders: map[string]string{
|
41 |
+
"Accept": "*/*",
|
42 |
+
"Accept-Encoding": "gzip, deflate, br, zstd",
|
43 |
+
"Accept-Language": "zh-CN,zh;q=0.9",
|
44 |
+
"Origin": "https://duckduckgo.com/",
|
45 |
+
"Cookie": "l=wt-wt; ah=wt-wt; dcm=6",
|
46 |
+
"Dnt": "1",
|
47 |
+
"Priority": "u=1, i",
|
48 |
+
"Referer": "https://duckduckgo.com/",
|
49 |
+
"Sec-Ch-Ua": `"Microsoft Edge";v="129", "Not(A:Brand";v="8", "Chromium";v="129"`,
|
50 |
+
"Sec-Ch-Ua-Mobile": "?0",
|
51 |
+
"Sec-Ch-Ua-Platform": `"Windows"`,
|
52 |
+
"Sec-Fetch-Dest": "empty",
|
53 |
+
"Sec-Fetch-Mode": "cors",
|
54 |
+
"Sec-Fetch-Site": "same-origin",
|
55 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36",
|
56 |
+
},
|
57 |
+
}
|
58 |
+
}
|
59 |
+
|
60 |
+
func authMiddleware() gin.HandlerFunc {
|
61 |
+
return func(c *gin.Context) {
|
62 |
+
apiKey := c.GetHeader("Authorization")
|
63 |
+
if apiKey == "" {
|
64 |
+
apiKey = c.Query("api_key")
|
65 |
+
}
|
66 |
+
|
67 |
+
// 当未提供或者 API Key 不合法时,允许匿名访问
|
68 |
+
if apiKey == "" || !strings.HasPrefix(apiKey, "Bearer ") {
|
69 |
+
c.Next() // 未提供 API 密钥时允许继续请求
|
70 |
+
return
|
71 |
+
}
|
72 |
+
|
73 |
+
apiKey = strings.TrimPrefix(apiKey, "Bearer ")
|
74 |
+
|
75 |
+
if apiKey != config.APIKey {
|
76 |
+
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid API key"})
|
77 |
+
c.Abort()
|
78 |
+
return
|
79 |
+
}
|
80 |
+
|
81 |
+
c.Next()
|
82 |
+
}
|
83 |
+
}
|
84 |
+
|
85 |
+
func main() {
|
86 |
+
r := gin.Default()
|
87 |
+
r.Use(corsMiddleware())
|
88 |
+
// 1. 映射 Web 目录到 /web 路由
|
89 |
+
//r.Static("/web", "./web") // 确保 ./web 文件夹存在,并包含 index.html
|
90 |
+
subFS, err := fs.Sub(staticFiles, "web")
|
91 |
+
if err != nil {
|
92 |
+
log.Fatal(err)
|
93 |
+
}
|
94 |
+
r.StaticFS("/web", http.FS(subFS))
|
95 |
+
|
96 |
+
// 2. 根路径重定向到 /web
|
97 |
+
r.GET("/", func(c *gin.Context) {
|
98 |
+
c.Redirect(http.StatusMovedPermanently, "/web")
|
99 |
+
})
|
100 |
+
// r.GET("/", func(c *gin.Context) {
|
101 |
+
// c.JSON(http.StatusOK, gin.H{"message": "API 服务运行中~"})
|
102 |
+
// })
|
103 |
+
// 3. 健康检查
|
104 |
+
r.GET("/ping", func(c *gin.Context) {
|
105 |
+
c.JSON(http.StatusOK, gin.H{"message": "pong"})
|
106 |
+
})
|
107 |
+
// 4. API 路由组
|
108 |
+
// authorized := r.Group("/")
|
109 |
+
// authorized.Use(authMiddleware())
|
110 |
+
// {
|
111 |
+
// authorized.GET("/hf/v1/models", handleModels)
|
112 |
+
// authorized.POST("/hf/v1/chat/completions", handleCompletion)
|
113 |
+
// }
|
114 |
+
apiGroup := r.Group("/")
|
115 |
+
apiGroup.Use(authMiddleware()) // 可以选择性地提供 API 密钥
|
116 |
+
{
|
117 |
+
// 原始路径 /hf/v1/*
|
118 |
+
apiGroup.GET("/hf/v1/models", handleModels)
|
119 |
+
apiGroup.POST("/hf/v1/chat/completions", handleCompletion)
|
120 |
+
|
121 |
+
// 新路径 /api/v1/*
|
122 |
+
apiGroup.GET("/api/v1/models", handleModels)
|
123 |
+
apiGroup.POST("/api/v1/chat/completions", handleCompletion)
|
124 |
+
|
125 |
+
// 新路径 /v1/*
|
126 |
+
apiGroup.GET("/v1/models", handleModels)
|
127 |
+
apiGroup.POST("/v1/chat/completions", handleCompletion)
|
128 |
+
|
129 |
+
// 新路径 /completions
|
130 |
+
apiGroup.POST("/completions", handleCompletion)
|
131 |
+
}
|
132 |
+
// 5. 从环境变量中读取端口号
|
133 |
+
port := os.Getenv("PORT")
|
134 |
+
if port == "" {
|
135 |
+
port = "7860"
|
136 |
+
}
|
137 |
+
r.Run(":" + port)
|
138 |
+
}
|
139 |
+
|
140 |
+
func handleModels(c *gin.Context) {
|
141 |
+
models := []gin.H{
|
142 |
+
{"id": "gpt-4o-mini", "object": "model", "owned_by": "ddg"},
|
143 |
+
{"id": "claude-3-haiku", "object": "model", "owned_by": "ddg"},
|
144 |
+
{"id": "llama-3.1-70b", "object": "model", "owned_by": "ddg"},
|
145 |
+
{"id": "mixtral-8x7b", "object": "model", "owned_by": "ddg"},
|
146 |
+
}
|
147 |
+
c.JSON(http.StatusOK, gin.H{"object": "list", "data": models})
|
148 |
+
}
|
149 |
+
|
150 |
+
func handleCompletion(c *gin.Context) {
|
151 |
+
var req struct {
|
152 |
+
Model string `json:"model"`
|
153 |
+
Messages []struct {
|
154 |
+
Role string `json:"role"`
|
155 |
+
Content interface{} `json:"content"`
|
156 |
+
} `json:"messages"`
|
157 |
+
Stream bool `json:"stream"`
|
158 |
+
}
|
159 |
+
|
160 |
+
if err := c.ShouldBindJSON(&req); err != nil {
|
161 |
+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
162 |
+
return
|
163 |
+
}
|
164 |
+
|
165 |
+
model := convertModel(req.Model)
|
166 |
+
content := prepareMessages(req.Messages)
|
167 |
+
// log.Printf("messages: %v", content)
|
168 |
+
|
169 |
+
reqBody := map[string]interface{}{
|
170 |
+
"model": model,
|
171 |
+
"messages": []map[string]interface{}{
|
172 |
+
{
|
173 |
+
"role": "user",
|
174 |
+
"content": content,
|
175 |
+
},
|
176 |
+
},
|
177 |
+
}
|
178 |
+
|
179 |
+
body, err := json.Marshal(reqBody)
|
180 |
+
if err != nil {
|
181 |
+
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("请求体序列化失败: %v", err)})
|
182 |
+
return
|
183 |
+
}
|
184 |
+
|
185 |
+
token, err := requestToken()
|
186 |
+
if err != nil {
|
187 |
+
c.JSON(http.StatusInternalServerError, gin.H{"error": "无法获取token"})
|
188 |
+
return
|
189 |
+
}
|
190 |
+
|
191 |
+
upstreamReq, err := http.NewRequest("POST", "https://duckduckgo.com/duckchat/v1/chat", strings.NewReader(string(body)))
|
192 |
+
if err != nil {
|
193 |
+
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("创建请求失败: %v", err)})
|
194 |
+
return
|
195 |
+
}
|
196 |
+
|
197 |
+
for k, v := range config.FakeHeaders {
|
198 |
+
upstreamReq.Header.Set(k, v)
|
199 |
+
}
|
200 |
+
upstreamReq.Header.Set("x-vqd-4", token)
|
201 |
+
upstreamReq.Header.Set("Content-Type", "application/json")
|
202 |
+
|
203 |
+
client := &http.Client{
|
204 |
+
Timeout: 30 * time.Second,
|
205 |
+
}
|
206 |
+
|
207 |
+
resp, err := client.Do(upstreamReq)
|
208 |
+
if err != nil {
|
209 |
+
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("请求失败: %v", err)})
|
210 |
+
return
|
211 |
+
}
|
212 |
+
defer resp.Body.Close()
|
213 |
+
|
214 |
+
if req.Stream {
|
215 |
+
// 启用 SSE 流式响应
|
216 |
+
c.Writer.Header().Set("Content-Type", "text/event-stream")
|
217 |
+
c.Writer.Header().Set("Cache-Control", "no-cache")
|
218 |
+
c.Writer.Header().Set("Connection", "keep-alive")
|
219 |
+
|
220 |
+
flusher, ok := c.Writer.(http.Flusher)
|
221 |
+
if !ok {
|
222 |
+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Streaming not supported"})
|
223 |
+
return
|
224 |
+
}
|
225 |
+
|
226 |
+
reader := bufio.NewReader(resp.Body)
|
227 |
+
for {
|
228 |
+
line, err := reader.ReadString('\n')
|
229 |
+
if err != nil {
|
230 |
+
if err != io.EOF {
|
231 |
+
log.Printf("读取流式响应失败: %v", err)
|
232 |
+
}
|
233 |
+
break
|
234 |
+
}
|
235 |
+
if strings.HasPrefix(line, "data: ") {
|
236 |
+
// 解析响应中的 JSON 数据块
|
237 |
+
line = strings.TrimPrefix(line, "data: ")
|
238 |
+
line = strings.TrimSpace(line)
|
239 |
+
// 忽略非 JSON 数据块(例如特殊标记 [DONE])
|
240 |
+
if line == "[DONE]" {
|
241 |
+
//log.Printf("响应行 DONE, 即将跳过")
|
242 |
+
break
|
243 |
+
}
|
244 |
+
var chunk map[string]interface{}
|
245 |
+
if err := json.Unmarshal([]byte(line), &chunk); err != nil {
|
246 |
+
log.Printf("解析响应行失败: %v", err)
|
247 |
+
continue
|
248 |
+
}
|
249 |
+
|
250 |
+
// 检查 chunk 是否包含 message
|
251 |
+
if msg, exists := chunk["message"]; exists && msg != nil {
|
252 |
+
if msgStr, ok := msg.(string); ok {
|
253 |
+
response := map[string]interface{}{
|
254 |
+
"id": "chatcmpl-QXlha2FBbmROaXhpZUFyZUF3ZXNvbWUK",
|
255 |
+
"object": "chat.completion.chunk",
|
256 |
+
"created": time.Now().Unix(),
|
257 |
+
"model": model,
|
258 |
+
"choices": []map[string]interface{}{
|
259 |
+
{
|
260 |
+
"index": 0,
|
261 |
+
"delta": map[string]string{
|
262 |
+
"content": msgStr,
|
263 |
+
},
|
264 |
+
"finish_reason": nil,
|
265 |
+
},
|
266 |
+
},
|
267 |
+
}
|
268 |
+
// 将响应格式化为 SSE 数据块
|
269 |
+
sseData, _ := json.Marshal(response)
|
270 |
+
sseMessage := fmt.Sprintf("data: %s\n\n", sseData)
|
271 |
+
|
272 |
+
// 发送数据并刷新缓冲区
|
273 |
+
_, writeErr := c.Writer.Write([]byte(sseMessage))
|
274 |
+
if writeErr != nil {
|
275 |
+
log.Printf("写入响应失败: %v", writeErr)
|
276 |
+
break
|
277 |
+
}
|
278 |
+
flusher.Flush()
|
279 |
+
} else {
|
280 |
+
log.Printf("chunk[message] 不是字符串: %v", msg)
|
281 |
+
}
|
282 |
+
} else {
|
283 |
+
// 解析行中有空行
|
284 |
+
log.Println("chunk 中未包含 message 或 message 为 nil")
|
285 |
+
}
|
286 |
+
}
|
287 |
+
}
|
288 |
+
} else {
|
289 |
+
// 非流式响应,返回完整的 JSON
|
290 |
+
var fullResponse strings.Builder
|
291 |
+
reader := bufio.NewReader(resp.Body)
|
292 |
+
|
293 |
+
for {
|
294 |
+
line, err := reader.ReadString('\n')
|
295 |
+
if err == io.EOF {
|
296 |
+
break
|
297 |
+
} else if err != nil {
|
298 |
+
log.Printf("读取响应失败: %v", err)
|
299 |
+
break
|
300 |
+
}
|
301 |
+
|
302 |
+
if strings.HasPrefix(line, "data: ") {
|
303 |
+
line = strings.TrimPrefix(line, "data: ")
|
304 |
+
line = strings.TrimSpace(line)
|
305 |
+
|
306 |
+
if line == "[DONE]" {
|
307 |
+
break
|
308 |
+
}
|
309 |
+
|
310 |
+
var chunk map[string]interface{}
|
311 |
+
if err := json.Unmarshal([]byte(line), &chunk); err != nil {
|
312 |
+
log.Printf("解析响应行失败: %v", err)
|
313 |
+
continue
|
314 |
+
}
|
315 |
+
|
316 |
+
if message, exists := chunk["message"]; exists {
|
317 |
+
if msgStr, ok := message.(string); ok {
|
318 |
+
fullResponse.WriteString(msgStr)
|
319 |
+
}
|
320 |
+
}
|
321 |
+
}
|
322 |
+
}
|
323 |
+
|
324 |
+
// 返回完整 JSON 响应
|
325 |
+
response := map[string]interface{}{
|
326 |
+
"id": "chatcmpl-QXlha2FBbmROaXhpZUFyZUF3ZXNvbWUK",
|
327 |
+
"object": "chat.completion",
|
328 |
+
"created": time.Now().Unix(),
|
329 |
+
"model": model,
|
330 |
+
"usage": map[string]int{
|
331 |
+
"prompt_tokens": 0,
|
332 |
+
"completion_tokens": 0,
|
333 |
+
"total_tokens": 0,
|
334 |
+
},
|
335 |
+
"choices": []map[string]interface{}{
|
336 |
+
{
|
337 |
+
"message": map[string]string{
|
338 |
+
"role": "assistant",
|
339 |
+
"content": fullResponse.String(),
|
340 |
+
},
|
341 |
+
"index": 0,
|
342 |
+
},
|
343 |
+
},
|
344 |
+
}
|
345 |
+
|
346 |
+
c.JSON(http.StatusOK, response)
|
347 |
+
}
|
348 |
+
}
|
349 |
+
|
350 |
+
//func requestToken() (string, error) {
|
351 |
+
// req, err := http.NewRequest("GET", "https://duckduckgo.com/duckchat/v1/status", nil)
|
352 |
+
// if err != nil {
|
353 |
+
// return "", fmt.Errorf("创建请求失败: %v", err)
|
354 |
+
// }
|
355 |
+
// for k, v := range config.FakeHeaders {
|
356 |
+
// req.Header.Set(k, v)
|
357 |
+
// }
|
358 |
+
// req.Header.Set("x-vqd-accept", "1")
|
359 |
+
//
|
360 |
+
// client := &http.Client{
|
361 |
+
// Timeout: 10 * time.Second,
|
362 |
+
// }
|
363 |
+
//
|
364 |
+
// log.Println("发送 token 请求")
|
365 |
+
// resp, err := client.Do(req)
|
366 |
+
// if err != nil {
|
367 |
+
// return "", fmt.Errorf("请求失败: %v", err)
|
368 |
+
// }
|
369 |
+
// defer resp.Body.Close()
|
370 |
+
//
|
371 |
+
// if resp.StatusCode != http.StatusOK {
|
372 |
+
// bodyBytes, _ := io.ReadAll(resp.Body)
|
373 |
+
// bodyString := string(bodyBytes)
|
374 |
+
// log.Printf("requestToken: 非200响应: %d, 内容: %s\n", resp.StatusCode, bodyString)
|
375 |
+
// return "", fmt.Errorf("非200响应: %d, 内容: %s", resp.StatusCode, bodyString)
|
376 |
+
// }
|
377 |
+
//
|
378 |
+
// token := resp.Header.Get("x-vqd-4")
|
379 |
+
// if token == "" {
|
380 |
+
// return "", errors.New("响应中未包含x-vqd-4头")
|
381 |
+
// }
|
382 |
+
//
|
383 |
+
// // log.Printf("获取到的 token: %s\n", token)
|
384 |
+
// return token, nil
|
385 |
+
//}
|
386 |
+
|
387 |
+
func requestToken() (string, error) {
|
388 |
+
url := "https://duckduckgo.com/duckchat/v1/status"
|
389 |
+
client := &http.Client{
|
390 |
+
Timeout: 15 * time.Second, // 设置超时时间
|
391 |
+
}
|
392 |
+
|
393 |
+
maxRetries := config.MaxRetryCount
|
394 |
+
retryDelay := config.RetryDelay
|
395 |
+
|
396 |
+
for attempt := 0; attempt < maxRetries; attempt++ {
|
397 |
+
if attempt > 0 {
|
398 |
+
log.Printf("requestToken: 第 %d 次重试,等待 %v...", attempt, retryDelay)
|
399 |
+
time.Sleep(retryDelay)
|
400 |
+
}
|
401 |
+
log.Printf("requestToken: 发送 GET 请求到 %s", url)
|
402 |
+
|
403 |
+
// 创建请求
|
404 |
+
req, err := http.NewRequest("GET", url, nil)
|
405 |
+
if err != nil {
|
406 |
+
log.Printf("requestToken: 创建请求失败: %v", err)
|
407 |
+
return "", fmt.Errorf("无法创建请求: %w", err)
|
408 |
+
}
|
409 |
+
|
410 |
+
// 添加假头部
|
411 |
+
for k, v := range config.FakeHeaders {
|
412 |
+
req.Header.Set(k, v)
|
413 |
+
}
|
414 |
+
req.Header.Set("x-vqd-accept", "1")
|
415 |
+
|
416 |
+
// 发送请求
|
417 |
+
resp, err := client.Do(req)
|
418 |
+
if err != nil {
|
419 |
+
log.Printf("requestToken: 请求失败: %v", err)
|
420 |
+
continue // 网络通信失败,进行重试
|
421 |
+
}
|
422 |
+
defer resp.Body.Close()
|
423 |
+
|
424 |
+
// 检查状态码是否为 200
|
425 |
+
if resp.StatusCode != http.StatusOK {
|
426 |
+
bodyBytes, _ := io.ReadAll(resp.Body) // 读取响应体,错误时也需要记录响应内容
|
427 |
+
bodyString := string(bodyBytes)
|
428 |
+
log.Printf("requestToken: 非200响应,状态码=%d, 响应内容: %s", resp.StatusCode, bodyString)
|
429 |
+
continue
|
430 |
+
}
|
431 |
+
|
432 |
+
// 尝试从头部提取 token
|
433 |
+
token := resp.Header.Get("x-vqd-4")
|
434 |
+
if token == "" {
|
435 |
+
log.Println("requestToken: 响应中未包含 x-vqd-4 头部")
|
436 |
+
bodyBytes, _ := io.ReadAll(resp.Body)
|
437 |
+
bodyString := string(bodyBytes)
|
438 |
+
log.Printf("requestToken: 响应内容: %s", bodyString)
|
439 |
+
continue
|
440 |
+
}
|
441 |
+
|
442 |
+
// 成功获取到 token
|
443 |
+
log.Printf("requestToken: 成功获取到 token: %s", token)
|
444 |
+
return token, nil
|
445 |
+
}
|
446 |
+
|
447 |
+
// 如果所有重试均失败,返回错误
|
448 |
+
return "", errors.New("requestToken: 无法获取到 token,多次重试仍失败")
|
449 |
+
}
|
450 |
+
|
451 |
+
func prepareMessages(messages []struct {
|
452 |
+
Role string `json:"role"`
|
453 |
+
Content interface{} `json:"content"`
|
454 |
+
}) string {
|
455 |
+
var contentBuilder strings.Builder
|
456 |
+
|
457 |
+
for _, msg := range messages {
|
458 |
+
// Determine the role - 'system' becomes 'user'
|
459 |
+
role := msg.Role
|
460 |
+
if role == "system" {
|
461 |
+
role = "user"
|
462 |
+
}
|
463 |
+
|
464 |
+
// Process the content as string
|
465 |
+
contentStr := ""
|
466 |
+
switch v := msg.Content.(type) {
|
467 |
+
case string:
|
468 |
+
contentStr = v
|
469 |
+
case []interface{}:
|
470 |
+
for _, item := range v {
|
471 |
+
if itemMap, ok := item.(map[string]interface{}); ok {
|
472 |
+
if text, exists := itemMap["text"].(string); exists {
|
473 |
+
contentStr += text
|
474 |
+
}
|
475 |
+
}
|
476 |
+
}
|
477 |
+
default:
|
478 |
+
contentStr = fmt.Sprintf("%v", msg.Content)
|
479 |
+
}
|
480 |
+
|
481 |
+
// Append the role and content to the builder
|
482 |
+
contentBuilder.WriteString(fmt.Sprintf("%s:%s;\r\n", role, contentStr))
|
483 |
+
}
|
484 |
+
|
485 |
+
return contentBuilder.String()
|
486 |
+
}
|
487 |
+
|
488 |
+
func convertModel(inputModel string) string {
|
489 |
+
switch strings.ToLower(inputModel) {
|
490 |
+
case "claude-3-haiku":
|
491 |
+
return "claude-3-haiku-20240307"
|
492 |
+
case "llama-3.1-70b":
|
493 |
+
return "meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo"
|
494 |
+
case "mixtral-8x7b":
|
495 |
+
return "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
496 |
+
default:
|
497 |
+
return "gpt-4o-mini"
|
498 |
+
}
|
499 |
+
}
|
500 |
+
|
501 |
+
func corsMiddleware() gin.HandlerFunc {
|
502 |
+
return func(c *gin.Context) {
|
503 |
+
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
504 |
+
c.Writer.Header().Set("Access-Control-Allow-Methods", "*")
|
505 |
+
c.Writer.Header().Set("Access-Control-Allow-Headers", "*")
|
506 |
+
if c.Request.Method == http.MethodOptions {
|
507 |
+
c.AbortWithStatus(http.StatusNoContent)
|
508 |
+
return
|
509 |
+
}
|
510 |
+
c.Next()
|
511 |
+
}
|
512 |
+
}
|
513 |
+
|
514 |
+
func getEnv(key, fallback string) string {
|
515 |
+
if value, exists := os.LookupEnv(key); exists {
|
516 |
+
return value
|
517 |
+
}
|
518 |
+
return fallback
|
519 |
+
}
|
520 |
+
|
521 |
+
func getIntEnv(key string, fallback int) int {
|
522 |
+
if value, exists := os.LookupEnv(key); exists {
|
523 |
+
var intValue int
|
524 |
+
fmt.Sscanf(value, "%d", &intValue)
|
525 |
+
return intValue
|
526 |
+
}
|
527 |
+
return fallback
|
528 |
+
}
|
529 |
+
|
530 |
+
func getDurationEnv(key string, fallback int) time.Duration {
|
531 |
+
return time.Duration(getIntEnv(key, fallback)) * time.Millisecond
|
532 |
+
}
|
web/avatar.png
ADDED
web/icon.png
ADDED
web/index.html
ADDED
The diff for this file is too large to render.
See raw diff
|
|