Skip to content

Commit

Permalink
style: 调试标记,请求体回声
Browse files Browse the repository at this point in the history
  • Loading branch information
bincooo committed Jul 1, 2024
1 parent 202f72d commit 48a92b1
Show file tree
Hide file tree
Showing 19 changed files with 188 additions and 25 deletions.
21 changes: 21 additions & 0 deletions flags.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,27 @@ flag: debug
}
```

#### 开启请求体响应:不消耗tokens,直接返回请求体
```text
flag: echo
```
例子:
```text
<echo />
>>>>>
{
"messages": [
{
"content": "<echo />\n研读书籍,准备明天的测验",
"role": "user"
}
],
"model": "coze",
"stream": false
}
```

#### 注释,只为了标注说明,没有实际作用
```text
<!-- 我是注释 -->
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/bincooo/coze-api v1.0.2-0.20240620163352-ad627f0dffd2
github.com/bincooo/edge-api v1.0.4-0.20240630075039-f55db93af0ab
github.com/bincooo/emit.io v0.0.0-20240630085209-49be277194ac
github.com/bincooo/you.com v0.0.0-20240626230203-cf0d17856e76
github.com/bincooo/you.com v0.0.0-20240630230515-3db4983fc758
github.com/dlclark/regexp2 v1.7.0
github.com/eko/gocache/lib/v4 v4.1.6
github.com/eko/gocache/store/go_cache/v4 v4.2.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ github.com/bincooo/edge-api v1.0.4-0.20240630075039-f55db93af0ab h1:X10eWl5imQOM
github.com/bincooo/edge-api v1.0.4-0.20240630075039-f55db93af0ab/go.mod h1:cSJi20GKnl0PCe4UIzxJ8xHZkQy9xGOF7tR/T7aE/9U=
github.com/bincooo/emit.io v0.0.0-20240630085209-49be277194ac h1:wd5jDiQKX7eKUHX1kwX+aV5trnWKwMEtXZq01Sc/3QI=
github.com/bincooo/emit.io v0.0.0-20240630085209-49be277194ac/go.mod h1:TM7ofgLvBROkDM6SWaLduIMYWNiTOvYtRbQy0u4X5SY=
github.com/bincooo/you.com v0.0.0-20240626230203-cf0d17856e76 h1:I5Z8EZp6FcxIxHTSmDy/adBHUbuOElceLbi5TQ8K/sQ=
github.com/bincooo/you.com v0.0.0-20240626230203-cf0d17856e76/go.mod h1:SOuP1CRtNiOr+gYyYsZ/K7pj/Wc6Ko5lT3rFn07yLng=
github.com/bincooo/you.com v0.0.0-20240630230515-3db4983fc758 h1:n/cFI9dQGIbnTqlU3ixYiP3Wv49cyED6/l6R8NjnNQM=
github.com/bincooo/you.com v0.0.0-20240630230515-3db4983fc758/go.mod h1:SOuP1CRtNiOr+gYyYsZ/K7pj/Wc6Ko5lT3rFn07yLng=
github.com/bitly/go-simplejson v0.5.0 h1:6IH+V8/tVMab511d5bn4M7EwGXZf9Hj6i2xSwkNEM+Y=
github.com/bitly/go-simplejson v0.5.0/go.mod h1:cXHtHw4XUPsvGaxgjIAn8PhEWG9NfngEKAMDJEczWVA=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY=
Expand Down
8 changes: 8 additions & 0 deletions internal/common/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ func xmlFlagsToContents(ctx *gin.Context, messages []pkg.Keyv[interface{}]) (han
"histories",
"char_sequences", // 角色序列映射
"tool",
"echo", // 不与AI交互,仅获取处理后的上下文
})
)

Expand Down Expand Up @@ -669,6 +670,13 @@ func xmlFlagsToContents(ctx *gin.Context, messages []pkg.Keyv[interface{}]) (han
continue
}

// 不与AI交互,仅获取处理后的上下文
if node.t == XML_TYPE_X && node.tag == "echo" {
ctx.Set(vars.GinEcho, true)
clean(content[node.index:node.end])
continue
}

// 历史记录
if node.t == XML_TYPE_X && node.tag == "histories" {
str := strings.TrimSpace(node.content)
Expand Down
11 changes: 11 additions & 0 deletions internal/gin.handler/response/com.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@ func Response(ctx *gin.Context, model, content string) {
})
}

func Echo(ctx *gin.Context, mode, content string, sse bool) {
if !sse {
Response(ctx, mode, content)
} else {
created := time.Now().Unix()
SSEResponse(ctx, mode, content, created)
time.Sleep(time.Second)
SSEResponse(ctx, mode, "[DONE]", created)
}
}

func SSEResponse(ctx *gin.Context, model, content string, created int64) {
ctx.Set(canResponse, "No!")
setSSEHeader(ctx)
Expand Down
12 changes: 11 additions & 1 deletion internal/plugin/llm/bing/adapter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package bing

import (
"encoding/json"
"fmt"
"github.com/bincooo/chatgpt-adapter/internal/common"
"github.com/bincooo/chatgpt-adapter/internal/gin.handler/response"
"github.com/bincooo/chatgpt-adapter/internal/plugin"
Expand Down Expand Up @@ -59,7 +61,9 @@ func (API) Completion(ctx *gin.Context) {
cookie = ctx.GetString("token")
proxies = ctx.GetString("proxies")
notebook = ctx.GetBool("notebook")
pad = ctx.GetBool("pad")

pad = ctx.GetBool("pad")
echo = ctx.GetBool(vars.GinEcho)

completion = common.GetGinCompletion(ctx)
matchers = common.GetGinMatchers(ctx)
Expand Down Expand Up @@ -116,6 +120,12 @@ func (API) Completion(ctx *gin.Context) {
return
}

if echo {
bytes, _ := json.MarshalIndent(pMessages, "", " ")
response.Echo(ctx, completion.Model, fmt.Sprintf("PREVIOUS MESSAGES:\n%s\n\n\n------\nCURR QUESTION:\n%s", bytes, currMessage), completion.Stream)
return
}

// 清理多余的标签
var cancel chan error
cancel, matchers = joinMatchers(ctx, matchers)
Expand Down
12 changes: 11 additions & 1 deletion internal/plugin/llm/bing/toolcall.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/bincooo/chatgpt-adapter/internal/common"
"github.com/bincooo/chatgpt-adapter/internal/gin.handler/response"
"github.com/bincooo/chatgpt-adapter/internal/plugin"
"github.com/bincooo/chatgpt-adapter/internal/vars"
"github.com/bincooo/chatgpt-adapter/logger"
"github.com/bincooo/chatgpt-adapter/pkg"
"github.com/bincooo/edge-api"
Expand All @@ -14,7 +15,11 @@ import (

func completeToolCalls(ctx *gin.Context, cookie, proxies string, completion pkg.ChatCompletion) bool {
logger.Infof("completeTools ...")
baseUrl := pkg.Config.GetString("bing.baseUrl")

var (
baseUrl = pkg.Config.GetString("bing.baseUrl")
echo = ctx.GetBool(vars.GinEcho)
)

// 删除来自LobeChat中多余的tool提示,这部分提示会让弱智的bing更加弱智
// ## Tools\n\nYou can use these tools below:
Expand All @@ -26,6 +31,11 @@ func completeToolCalls(ctx *gin.Context, cookie, proxies string, completion pkg.
}

exec, err := plugin.CompleteToolCalls(ctx, completion, func(message string) (string, error) {
if echo {
logger.Infof("toolCall message: \n%s", message)
return "", nil
}

retry := 3
options, err := edge.NewDefaultOptions(cookie, baseUrl)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions internal/plugin/llm/claude/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/bincooo/chatgpt-adapter/internal/common"
"github.com/bincooo/chatgpt-adapter/internal/gin.handler/response"
"github.com/bincooo/chatgpt-adapter/internal/plugin"
"github.com/bincooo/chatgpt-adapter/internal/vars"
"github.com/bincooo/chatgpt-adapter/logger"
"github.com/bincooo/chatgpt-adapter/pkg"
claude3 "github.com/bincooo/claude-api"
Expand Down Expand Up @@ -84,6 +85,8 @@ func (API) Completion(ctx *gin.Context) {
completion = common.GetGinCompletion(ctx)
matchers = common.GetGinMatchers(ctx)
model = ""

echo = ctx.GetBool(vars.GinEcho)
)

if strings.HasPrefix(completion.Model, "claude-") {
Expand Down Expand Up @@ -115,6 +118,11 @@ func (API) Completion(ctx *gin.Context) {

attachments, tokens := mergeMessages(ctx, completion.Messages)
ctx.Set(ginTokens, tokens)
if echo {
response.Echo(ctx, completion.Model, attachments[0].Content, completion.Stream)
return
}

chat, err := claude3.New(options)
if err != nil {
logger.Error(err)
Expand Down
12 changes: 11 additions & 1 deletion internal/plugin/llm/claude/toolcall.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/bincooo/chatgpt-adapter/internal/common"
"github.com/bincooo/chatgpt-adapter/internal/gin.handler/response"
"github.com/bincooo/chatgpt-adapter/internal/plugin"
"github.com/bincooo/chatgpt-adapter/internal/vars"
"github.com/bincooo/chatgpt-adapter/logger"
"github.com/bincooo/chatgpt-adapter/pkg"
claude3 "github.com/bincooo/claude-api"
Expand All @@ -13,8 +14,17 @@ import (

func completeToolCalls(ctx *gin.Context, cookie string, completion pkg.ChatCompletion) bool {
logger.Infof("completeTools ...")
var (
model = ""
echo = ctx.GetBool(vars.GinEcho)
)

exec, err := plugin.CompleteToolCalls(ctx, completion, func(message string) (string, error) {
model := ""
if echo {
logger.Infof("toolCall message: \n%s", message)
return "", nil
}

if strings.HasPrefix(completion.Model, "claude-") {
if completion.Model != "claude-3" {
model = completion.Model
Expand Down
15 changes: 15 additions & 0 deletions internal/plugin/llm/cohere/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package cohere

import (
"encoding/json"
"fmt"
"github.com/bincooo/chatgpt-adapter/internal/common"
"github.com/bincooo/chatgpt-adapter/internal/gin.handler/response"
"github.com/bincooo/chatgpt-adapter/internal/plugin"
"github.com/bincooo/chatgpt-adapter/internal/vars"
"github.com/bincooo/chatgpt-adapter/logger"
"github.com/bincooo/chatgpt-adapter/pkg"
coh "github.com/bincooo/cohere-api"
Expand Down Expand Up @@ -88,6 +90,8 @@ func (API) Completion(ctx *gin.Context) {
// Tools: convertTools(completion),
// Results: convertToolResults(completion),
//}

echo = ctx.GetBool(vars.GinEcho)
)

// 官方的文档toolCall描述十分模糊,简测功能不佳,改回提示词实现
Expand All @@ -102,6 +106,11 @@ func (API) Completion(ctx *gin.Context) {
//toolObject = coh.ToolObject{}
message = mergeMessages(ctx, completion.Messages)
ctx.Set(ginTokens, common.CalcTokens(message))
if echo {
response.Echo(ctx, completion.Model, message, completion.Stream)
return
}

chat = coh.New(cookie, completion.Temperature, completion.Model, false)
chat.TopK(completion.TopK)
chat.MaxTokens(completion.MaxTokens)
Expand All @@ -115,6 +124,12 @@ func (API) Completion(ctx *gin.Context) {
var tokens = 0
pMessages, system, message, tokens = mergeChatMessages(completion.Messages)
ctx.Set(ginTokens, tokens)
if echo {
bytes, _ := json.MarshalIndent(pMessages, "", " ")
response.Echo(ctx, completion.Model, fmt.Sprintf("SYSTEM\n%s\n\n\n-------PREVIOUS MESSAGES:\n%s\n\n\n------\nCURR QUESTION:\n%s", system, bytes, message), completion.Stream)
return
}

chat = coh.New(cookie, completion.Temperature, completion.Model, true)

}
Expand Down
8 changes: 8 additions & 0 deletions internal/plugin/llm/cohere/toolcall.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/bincooo/chatgpt-adapter/internal/common"
"github.com/bincooo/chatgpt-adapter/internal/gin.handler/response"
"github.com/bincooo/chatgpt-adapter/internal/plugin"
"github.com/bincooo/chatgpt-adapter/internal/vars"
"github.com/bincooo/chatgpt-adapter/logger"
"github.com/bincooo/chatgpt-adapter/pkg"
"github.com/bincooo/cohere-api"
Expand All @@ -13,7 +14,14 @@ import (

func completeToolCalls(ctx *gin.Context, cookie, proxies string, completion pkg.ChatCompletion) bool {
logger.Info("completeTools ...")
echo := ctx.GetBool(vars.GinEcho)

exec, err := plugin.CompleteToolCalls(ctx, completion, func(message string) (string, error) {
if echo {
logger.Infof("toolCall message: \n%s", message)
return "", nil
}

chat := cohere.New(cookie, 0.4, completion.Model, false)
chat.Proxies(proxies)
chat.TopK(completion.TopK)
Expand Down
9 changes: 9 additions & 0 deletions internal/plugin/llm/coze/adapter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package coze

import (
"encoding/json"
"errors"
"fmt"
"github.com/bincooo/chatgpt-adapter/internal/common"
Expand Down Expand Up @@ -127,6 +128,8 @@ func (API) Completion(ctx *gin.Context) {

user = ""
assistant = ""

echo = ctx.GetBool(vars.GinEcho)
)

{
Expand Down Expand Up @@ -168,6 +171,12 @@ func (API) Completion(ctx *gin.Context) {
return
}

if echo {
bytes, _ := json.MarshalIndent(pMessages, "", " ")
response.Echo(ctx, completion.Model, string(bytes), completion.Stream)
return
}

ctx.Set(ginTokens, tokens)
options, mode, err := newOptions(proxies, completion.Model, pMessages)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions internal/plugin/llm/coze/toolcall.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package coze

import (
"encoding/json"
"github.com/bincooo/chatgpt-adapter/internal/common"
"github.com/bincooo/chatgpt-adapter/internal/gin.handler/response"
"github.com/bincooo/chatgpt-adapter/internal/plugin"
"github.com/bincooo/chatgpt-adapter/internal/vars"
"github.com/bincooo/chatgpt-adapter/logger"
"github.com/bincooo/chatgpt-adapter/pkg"
"github.com/bincooo/coze-api"
Expand All @@ -14,6 +16,8 @@ import (

func completeToolCalls(ctx *gin.Context, cookie, proxies string, completion pkg.ChatCompletion) bool {
logger.Info("completeTools ...")
echo := ctx.GetBool(vars.GinEcho)

exec, err := plugin.CompleteToolCalls(ctx, completion, func(message string) (string, error) {
message = strings.TrimSpace(message)
system := ""
Expand All @@ -36,6 +40,12 @@ func completeToolCalls(ctx *gin.Context, cookie, proxies string, completion pkg.
Content: message,
})

if echo {
bytes, _ := json.MarshalIndent(pMessages, "", " ")
logger.Infof("toolCall message: \n%s", bytes)
return "", nil
}

co, msToken := extCookie(cookie)
options, mode, err := newOptions(proxies, completion.Model, pMessages)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions internal/plugin/llm/gemini/adapter.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package gemini

import (
"encoding/json"
"errors"
"github.com/bincooo/chatgpt-adapter/internal/common"
"github.com/bincooo/chatgpt-adapter/internal/gin.handler/response"
"github.com/bincooo/chatgpt-adapter/internal/plugin"
"github.com/bincooo/chatgpt-adapter/internal/vars"
"github.com/bincooo/chatgpt-adapter/logger"
"github.com/gin-gonic/gin"
"net/url"
Expand Down Expand Up @@ -70,6 +72,7 @@ func (API) Completion(ctx *gin.Context) {
proxies = ctx.GetString("proxies")
completion = common.GetGinCompletion(ctx)
matchers = common.GetGinMatchers(ctx)
echo = ctx.GetBool(vars.GinEcho)
)

newMessages, tokens, err := mergeMessages(completion.Messages)
Expand All @@ -79,6 +82,12 @@ func (API) Completion(ctx *gin.Context) {
return
}

if echo {
bytes, _ := json.MarshalIndent(newMessages, "", " ")
response.Echo(ctx, completion.Model, string(bytes), completion.Stream)
return
}

ctx.Set(ginTokens, tokens)
r, err := build(common.GetGinContext(ctx), proxies, cookie, newMessages, completion)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions internal/plugin/llm/lmsys/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ func (API) Completion(ctx *gin.Context) {
proxies = ctx.GetString("proxies")
completion = common.GetGinCompletion(ctx)
matchers = common.GetGinMatchers(ctx)
echo = ctx.GetBool(vars.GinEcho)
)

completion.Model = completion.Model[6:]
Expand All @@ -271,6 +272,11 @@ func (API) Completion(ctx *gin.Context) {

newMessages := mergeMessages(ctx, completion.Messages)
ctx.Set(ginTokens, common.CalcTokens(newMessages))
if echo {
response.Echo(ctx, completion.Model, newMessages, completion.Stream)
return
}

retry := 3
label:
ch, err := fetch(common.GetGinContext(ctx), proxies, token, newMessages, options{
Expand Down
Loading

0 comments on commit 48a92b1

Please sign in to comment.