Skip to content

Commit

Permalink
feat: 添加free gpt35接口
Browse files Browse the repository at this point in the history
  • Loading branch information
bincooo committed May 15, 2024
1 parent f274933 commit 0dd5030
Show file tree
Hide file tree
Showing 20 changed files with 519 additions and 93 deletions.
12 changes: 11 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,14 @@ llm:
# key请到https://bigjpg.com获取,每月30次免费
# 目前注册没有限制,可配置多个key轮询
#magnify:
# - "xxx"
# - "xxx"

# 用于429限流时切换代理ip,proxies/xxx 为 策略组,names 为组内的线路名称 (clash需开启webapi)
# names 修改自己的节点
clash:
url: http://127.0.0.1:7891/proxies/xxx
names:
- "英国"
- "美国"
- "日本"
- "xxx"
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ require (
github.com/bincooo/claude-api v1.0.4-0.20240323131054-e8068584fb71
github.com/bincooo/cohere-api v0.0.0-20240408053055-744e6f22b310
github.com/bincooo/coze-api v1.0.2-0.20240510042405-0f4058f868f3
github.com/bincooo/edge-api v1.0.4-0.20240513025500-6634798bd70c
github.com/bincooo/gio.emits v0.0.0-20240509021256-cd7ba8cf5f33
github.com/bincooo/edge-api v1.0.4-0.20240514224459-9c5b7df21536
github.com/bincooo/gio.emits v0.0.0-20240515122257-0d8f88abdca0
github.com/bincooo/goole15 v0.0.0-20240410222503-7e0cbb57020b
github.com/dlclark/regexp2 v1.7.0
github.com/gin-gonic/gin v1.9.1
Expand All @@ -16,6 +16,7 @@ require (
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
golang.org/x/crypto v0.22.0
)

require (
Expand Down Expand Up @@ -62,7 +63,6 @@ require (
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sys v0.19.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ github.com/bincooo/cohere-api v0.0.0-20240408053055-744e6f22b310 h1:NtvRGHhzEd5Z
github.com/bincooo/cohere-api v0.0.0-20240408053055-744e6f22b310/go.mod h1:fi6FdO5X1gGcXNpn/6vGICraupaaoruMbDOIOtO5PZg=
github.com/bincooo/coze-api v1.0.2-0.20240510042405-0f4058f868f3 h1:mNHK/BoCORCwhYFQy+KO8dDoNDGRFiSw7n3AuRnoMqU=
github.com/bincooo/coze-api v1.0.2-0.20240510042405-0f4058f868f3/go.mod h1:/BwOAxy8B8f/xwOKcGQd4qGgpj16M3XDdAqAf4s5pyc=
github.com/bincooo/edge-api v1.0.4-0.20240513025500-6634798bd70c h1:dYbJKPEQIUUrT/CU9A5I6H4JY+b4YVbzoNIjP5uGOxg=
github.com/bincooo/edge-api v1.0.4-0.20240513025500-6634798bd70c/go.mod h1:KqUn1YF4q4FMy1I6Z3Zjq7qEoFQ9p4lbrFTJKcYwP5I=
github.com/bincooo/gio.emits v0.0.0-20240509021256-cd7ba8cf5f33 h1:uU81a7+lSGv5JHhkb42cCVybPcUtc4Eijr3Pc0ilcRg=
github.com/bincooo/gio.emits v0.0.0-20240509021256-cd7ba8cf5f33/go.mod h1:y1ZVb+yakVh/Dge6ZZp/yufDbzFmO8dBlzlGbu/ohEw=
github.com/bincooo/edge-api v1.0.4-0.20240514224459-9c5b7df21536 h1:Ofht6amujCAzOzUS3Vhyimorvo3Z+O1khTUu9gmWTmM=
github.com/bincooo/edge-api v1.0.4-0.20240514224459-9c5b7df21536/go.mod h1:KqUn1YF4q4FMy1I6Z3Zjq7qEoFQ9p4lbrFTJKcYwP5I=
github.com/bincooo/gio.emits v0.0.0-20240515122257-0d8f88abdca0 h1:BZgPDxCNCaXoUfWTaVjqigdeAwnJdWs6VBY+qZxw+/k=
github.com/bincooo/gio.emits v0.0.0-20240515122257-0d8f88abdca0/go.mod h1:y1ZVb+yakVh/Dge6ZZp/yufDbzFmO8dBlzlGbu/ohEw=
github.com/bincooo/goole15 v0.0.0-20240410222503-7e0cbb57020b h1:rJDNFBL46xBqMfGykXtt3pVR3K44c3+vOlhseVpTbQM=
github.com/bincooo/goole15 v0.0.0-20240410222503-7e0cbb57020b/go.mod h1:BhvAZuM5Neg6GbSGrWSStsU1dtpj9WJzC2zlgmT+DAA=
github.com/bincooo/requests v0.0.0-20230720064210-7eae5d6c9d1e h1:38ztKJW0K6qQGitqAlcJs8O2nal60qYoS9oNZnpkZWE=
Expand Down
14 changes: 7 additions & 7 deletions internal/common/bigjpg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/bincooo/gio.emits/common"
emits "github.com/bincooo/gio.emits"
"net/http"
"time"
)
Expand All @@ -31,19 +31,19 @@ func magnify(ctx context.Context, url, key, style, x2 string) (string, error) {
"input": url,
}

response, err := common.ClientBuilder().
response, err := emits.ClientBuilder().
Context(ctx).
URL(baseURL).
Method(http.MethodPost).
JHeader().
Header("X-API-KEY", key).
Body(payload).
DoWith(http.StatusOK)
DoS(http.StatusOK)
if err != nil {
return "", err
}

if err = common.ToObject(response, &payload); err != nil {
if err = emits.ToObject(response, &payload); err != nil {
return "", err
}

Expand All @@ -64,15 +64,15 @@ func magnify(ctx context.Context, url, key, style, x2 string) (string, error) {
}
retry--

response, err = common.ClientBuilder().
response, err = emits.ClientBuilder().
Context(ctx).
URL(baseURL + taskId).
DoWith(http.StatusOK)
DoS(http.StatusOK)
if err != nil {
return "", err
}

if err = common.ToObject(response, &payload); err != nil {
if err = emits.ToObject(response, &payload); err != nil {
return "", err
}

Expand Down
58 changes: 58 additions & 0 deletions internal/common/clash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package common

import (
"context"
"github.com/bincooo/chatgpt-adapter/v2/pkg"
emits "github.com/bincooo/gio.emits"
"github.com/sirupsen/logrus"
"net/http"
"sync"
"time"
)

var (
clashNames []string
clashPos int
clashOnce sync.Once
)

func clashInit() {
names := pkg.Config.GetStringSlice("clash.names")
if len(names) == 0 {
return
}
clashNames = names
clashPos = 0
}

func ChangeClashIP() {
nameL := len(clashNames)
if nameL == 0 {
logrus.Info("clash配置未开启")
return
}

url := pkg.Config.GetString("clash.url")
clashOnce.Do(func() {
clashPos++
if clashPos >= nameL {
clashPos = 0
}

str := clashNames[clashPos]
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()

_, err := emits.ClientBuilder().
PUT(url).
Context(ctx).
JHeader().
Body(map[string]string{"name": str}).
DoS(http.StatusNoContent)
if err != nil {
logrus.Error(err)
return
}
logrus.Infof("clash[%s]切换执行完毕", str)
})
}
1 change: 1 addition & 0 deletions internal/common/com.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

func Init() {
fileInit()
clashInit()
}

// 删除子元素
Expand Down
10 changes: 5 additions & 5 deletions internal/common/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/base64"
"errors"
"github.com/bincooo/chatgpt-adapter/v2/pkg"
"github.com/bincooo/gio.emits/common"
emits "github.com/bincooo/gio.emits"
"github.com/sirupsen/logrus"
"io"
"net/http"
Expand All @@ -23,12 +23,12 @@ type cache struct {
}

func fileInit() {
magnify := pkg.Config.GetStringSlice("magnify")
if len(magnify) == 0 {
m := pkg.Config.GetStringSlice("magnify")
if len(m) == 0 {
return
}

for _, key := range magnify {
for _, key := range m {
cached = append(cached, cache{
key: key,
disable: false,
Expand Down Expand Up @@ -98,7 +98,7 @@ func SaveBase64(base64Encoding, suffix string) (file string, err error) {
}

func Download(proxies, url, suffix string) (file string, err error) {
response, err := common.ClientBuilder().
response, err := emits.ClientBuilder().
Proxies(proxies).
URL(url).
Do()
Expand Down
24 changes: 14 additions & 10 deletions internal/common/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
)

const (
MAT_DEFAULT int = iota // 接收字符,并执行下一个匹配器
MAT_MATCHING // 匹配中
MAT_MATCHED // 匹配器命中
MAT_DEFAULT int = iota // 执行下一个匹配器
MAT_MATCHING // 匹配中, 字符被缓存
MAT_MATCHED // 匹配器命中,不再执行下一个
)

var (
Expand All @@ -20,16 +20,17 @@ var (
}
)

// 匹配器,匹配常量符号流式结果处理
// 匹配器接口
type Matcher interface {
match(content string) (state int, result string)
}

// 符号串符号适配器
// 字符块匹配器,只向后匹配
type SymbolMatcher struct {
cache string
Find string
H func(index int, content string) (state int, result string)
cache string // 缓存的字符
Find string // 字符块匹配前置,'*'则匹配任意
// 具体的匹配实现
H func(index int, content string) (state int, result string)
}

func NewMatchers() []Matcher {
Expand All @@ -38,6 +39,7 @@ func NewMatchers() []Matcher {
return slice
}

// 中断匹配器,返回一个error channel,用于控制是否终止输出
func NewCancelMather() (chan error, Matcher) {
count := 0
cancel := make(chan error, 1)
Expand All @@ -58,16 +60,18 @@ func NewCancelMather() (chan error, Matcher) {
return MAT_MATCHED, ""
}
}
return MAT_DEFAULT, content
return MAT_MATCHED, content
},
}
}

func ExecMatchers(matchers []Matcher, raw string) string {
// MAT_DEFAULT 没有命中,继续执行下一个
// MAT_MATCHING 匹配中,缓存消息不执行下一个
// MAT_MATCHED 命中,不再执行下一个
for _, mat := range matchers {
state, result := mat.match(raw)
if state == MAT_DEFAULT {
raw = result
continue
}
if state == MAT_MATCHING {
Expand Down
5 changes: 5 additions & 0 deletions internal/gin.handler/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ func models(ctx *gin.Context) {
"object": "model",
"created": 1686935002,
"owned_by": "gemini-adapter",
}, {
"id": "freeGpt35",
"object": "model",
"created": 1686935002,
"owned_by": "chatgpt-adapter",
}, {
"id": "command",
"object": "model",
Expand Down
3 changes: 3 additions & 0 deletions internal/gin.handler/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/bincooo/chatgpt-adapter/v2/internal/middle/lmsys"
pg "github.com/bincooo/chatgpt-adapter/v2/internal/middle/playground"
"github.com/bincooo/chatgpt-adapter/v2/internal/middle/sd"
v1 "github.com/bincooo/chatgpt-adapter/v2/internal/middle/v1"
"github.com/bincooo/chatgpt-adapter/v2/pkg/gpt"
"github.com/bincooo/cohere-api"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -72,6 +73,8 @@ func completions(ctx *gin.Context) {
cohere.COMMAND_NIGHTLY,
cohere.COMMAND_R_PLUS:
coh.Complete(ctx, chatCompletionRequest, matchers)
case "freeGpt35":
v1.Complete(ctx, chatCompletionRequest, matchers)
default:
if strings.HasPrefix(chatCompletionRequest.Model, "lmsys/") {
lmsys.Complete(ctx, chatCompletionRequest, matchers)
Expand Down
14 changes: 1 addition & 13 deletions internal/middle/bing/assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func buildConversation(pad bool, max int, messages []map[string]string) (pMessag
defer buffer.Reset()
var result []edge.ChatMessage
if previous == "system" {
result = append(result, edge.BuildSwitchMessage(condition(previous), buffer.String()))
result = append(result, edge.BuildUserMessage(buffer.String()))
result = append(result, edge.BuildBotMessage("<|assistant|>ok ~<|end|>\n"))
buffer.Reset()
}
Expand Down Expand Up @@ -284,18 +284,6 @@ func buildConversation(pad bool, max int, messages []map[string]string) (pMessag
}))
pMessages = append(pMessages, message)
newMessages = newMessages[len(newMessages)-max*2:]
if newMessages[0]["author"] == "user" {
newMessages[0] = edge.BuildMessage("CurrentWebpageContextRequest", newMessages[0]["text"])
}
} else {
if newMessages[0]["author"] == "user" && strings.HasPrefix(newMessages[0]["text"], "<|system|>") {
message := edge.BuildPageMessage(newMessages[0]["text"])
pMessages = append(pMessages, message)
newMessages = newMessages[1:]
if newMessages[0]["author"] == "user" {
newMessages[0] = edge.BuildMessage("CurrentWebpageContextRequest", newMessages[0]["text"])
}
}
}

pMessages = append(pMessages, newMessages...)
Expand Down
8 changes: 4 additions & 4 deletions internal/middle/gemini/assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/bincooo/chatgpt-adapter/v2/internal/middle"
"github.com/bincooo/chatgpt-adapter/v2/pkg"
"github.com/bincooo/chatgpt-adapter/v2/pkg/gpt"
"github.com/bincooo/gio.emits/common"
emits "github.com/bincooo/gio.emits"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"io"
Expand Down Expand Up @@ -178,7 +178,7 @@ func extCookie15(ctx context.Context, token, proxies string) (sign, auth, key, u
timeout, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()

response, e := common.ClientBuilder().
response, e := emits.ClientBuilder().
Proxies(proxies).
POST(gLogin).
Context(timeout).
Expand All @@ -189,14 +189,14 @@ func extCookie15(ctx context.Context, token, proxies string) (sign, auth, key, u
"passwd": s[2],
}).
JHeader().
DoWith(http.StatusOK)
DoS(http.StatusOK)
if e != nil {
err = fmt.Errorf("fetch cookies failed: %v", e)
return
}

var result map[string]interface{}
e = common.ToObject(response, &result)
e = emits.ToObject(response, &result)
if e != nil {
err = errors.New(fmt.Sprintf("fetch cookies failed: %v", e))
return
Expand Down
4 changes: 2 additions & 2 deletions internal/middle/gemini/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"errors"
"fmt"
"github.com/bincooo/chatgpt-adapter/v2/pkg/gpt"
"github.com/bincooo/gio.emits/common"
emits "github.com/bincooo/gio.emits"
"github.com/sirupsen/logrus"
"io"
"net/http"
Expand Down Expand Up @@ -120,7 +120,7 @@ func build(ctx context.Context, proxies, token string, messages []map[string]int
return nil, err
}

res, err := common.ClientBuilder().
res, err := emits.ClientBuilder().
Proxies(proxies).
Context(ctx).
POST(burl).
Expand Down
2 changes: 1 addition & 1 deletion internal/middle/lmsys/assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ label:

// 违反内容中断并返回错误
matchers = append(matchers, &common.SymbolMatcher{
Find: "I did not actually provide any input that",
Find: "I did not actually provide",
H: func(index int, content string) (state int, result string) {
cancel <- errors.New("I did not actually provide any input that could violate content guidelines")
return common.MAT_MATCHED, ""
Expand Down
Loading

0 comments on commit 0dd5030

Please sign in to comment.