Skip to content

Commit

Permalink
feat: support for channel configuration proxy (coaidev#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Mar 14, 2024
1 parent 9323abb commit e037276
Show file tree
Hide file tree
Showing 39 changed files with 311 additions and 93 deletions.
1 change: 1 addition & 0 deletions adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var channelFactories = map[string]adaptercommon.FactoryCreator{

func createChatRequest(conf globals.ChannelConfig, props *adaptercommon.ChatProps, hook globals.Hook) error {
props.Model = conf.GetModelReflect(props.OriginalModel)
props.Proxy = conf.GetProxy()

factoryType := conf.GetType()
if factory, ok := channelFactories[factoryType]; ok {
Expand Down
3 changes: 2 additions & 1 deletion adapter/azure/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func (c *ChatInstance) CreateChatRequest(props *adaptercommon.ChatProps) (string
c.GetChatEndpoint(props),
c.GetHeader(),
c.GetChatBody(props, false),
props.Proxy,
)

if err != nil || res == nil {
Expand Down Expand Up @@ -108,7 +109,7 @@ func (c *ChatInstance) CreateStreamChatRequest(props *adaptercommon.ChatProps, c
}
return callback(partial)
},
})
}, props.Proxy)

if err != nil {
if form := processChatErrorResponse(err.Body); form != nil {
Expand Down
4 changes: 3 additions & 1 deletion adapter/azure/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type ImageProps struct {
Model string
Prompt string
Size ImageSize
Proxy globals.ProxyConfig
}

func (c *ChatInstance) GetImageEndpoint(model string) string {
Expand All @@ -31,7 +32,7 @@ func (c *ChatInstance) CreateImageRequest(props ImageProps) (string, error) {
ImageSize512,
),
N: 1,
})
}, props.Proxy)
if err != nil || res == nil {
return "", fmt.Errorf("openai error: %s", err.Error())
}
Expand All @@ -51,6 +52,7 @@ func (c *ChatInstance) CreateImage(props *adaptercommon.ChatProps) (string, erro
url, err := c.CreateImageRequest(ImageProps{
Model: props.Model,
Prompt: c.GetLatestPrompt(props),
Proxy: props.Proxy,
})
if err != nil {
if strings.Contains(err.Error(), "safety") {
Expand Down
3 changes: 2 additions & 1 deletion adapter/baichuan/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (c *ChatInstance) CreateChatRequest(props *adaptercommon.ChatProps) (string
c.GetChatEndpoint(),
c.GetHeader(),
c.GetChatBody(props, false),
props.Proxy,
)

if err != nil || res == nil {
Expand Down Expand Up @@ -77,7 +78,7 @@ func (c *ChatInstance) CreateStreamChatRequest(props *adaptercommon.ChatProps, c
}
return callback(partial)
},
})
}, props.Proxy)

if err != nil {
if form := processChatErrorResponse(err.Body); form != nil {
Expand Down
31 changes: 17 additions & 14 deletions adapter/claude/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ import (
const defaultTokens = 2500

func (c *ChatInstance) GetChatEndpoint() string {
return fmt.Sprintf("%s/v1/complete", c.GetEndpoint())
return fmt.Sprintf("%s/v1/messages", c.GetEndpoint())
}

func (c *ChatInstance) GetChatHeaders() map[string]string {
return map[string]string{
"content-type": "application/json",
"accept": "application/json",
"x-api-key": c.GetApiKey(),
"content-type": "application/json",
"anthropic-version": "2023-06-01",
"x-api-key": c.GetApiKey(),
}
}

func (c *ChatInstance) ConvertMessage(message []globals.Message) string {
// ConvertCompletionMessage converts the completion message to anthropic complete format (deprecated)
func (c *ChatInstance) ConvertCompletionMessage(message []globals.Message) string {
mapper := map[string]string{
globals.System: "Assistant",
globals.User: "Human",
Expand Down Expand Up @@ -54,19 +55,19 @@ func (c *ChatInstance) GetTokens(props *adaptercommon.ChatProps) int {

func (c *ChatInstance) GetChatBody(props *adaptercommon.ChatProps, stream bool) *ChatBody {
return &ChatBody{
Prompt: c.ConvertMessage(props.Message),
MaxTokensToSample: c.GetTokens(props),
Model: props.Model,
Stream: stream,
Temperature: props.Temperature,
TopP: props.TopP,
TopK: props.TopK,
Messages: props.Message,
MaxTokens: c.GetTokens(props),
Model: props.Model,
Stream: stream,
Temperature: props.Temperature,
TopP: props.TopP,
TopK: props.TopK,
}
}

// CreateChatRequest is the request for anthropic claude
func (c *ChatInstance) CreateChatRequest(props *adaptercommon.ChatProps) (string, error) {
data, err := utils.Post(c.GetChatEndpoint(), c.GetChatHeaders(), c.GetChatBody(props, false))
data, err := utils.Post(c.GetChatEndpoint(), c.GetChatHeaders(), c.GetChatBody(props, false), props.Proxy)
if err != nil {
return "", fmt.Errorf("claude error: %s", err.Error())
}
Expand Down Expand Up @@ -126,5 +127,7 @@ func (c *ChatInstance) CreateStreamChatRequest(props *adaptercommon.ChatProps, h
}

return nil
})
},
props.Proxy,
)
}
16 changes: 9 additions & 7 deletions adapter/claude/types.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package claude

import "chat/globals"

// ChatBody is the request body for anthropic claude
type ChatBody struct {
Prompt string `json:"prompt"`
MaxTokensToSample int `json:"max_tokens_to_sample"`
Model string `json:"model"`
Stream bool `json:"stream"`
Temperature *float32 `json:"temperature,omitempty"`
TopP *float32 `json:"top_p,omitempty"`
TopK *int `json:"top_k,omitempty"`
Messages []globals.Message `json:"messages"`
MaxTokens int `json:"max_tokens"`
Model string `json:"model"`
Stream bool `json:"stream"`
Temperature *float32 `json:"temperature,omitempty"`
TopP *float32 `json:"top_p,omitempty"`
TopK *int `json:"top_k,omitempty"`
}

// ChatResponse is the native http request and stream response for anthropic claude
Expand Down
2 changes: 2 additions & 0 deletions adapter/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ type RequestProps struct {
MaxRetries *int
Current int
Group string

Proxy globals.ProxyConfig
}

type ChatProps struct {
Expand Down
1 change: 1 addition & 0 deletions adapter/dashscope/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,6 @@ func (c *ChatInstance) CreateStreamChatRequest(props *adaptercommon.ChatProps, c

return nil
},
props.Proxy,
)
}
7 changes: 5 additions & 2 deletions adapter/midjourney/api.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package midjourney

import (
"chat/globals"
"chat/utils"
"fmt"
)
Expand Down Expand Up @@ -29,11 +30,12 @@ func (c *ChatInstance) GetChangeRequest(action string, task string, index *int)
}
}

func (c *ChatInstance) CreateImagineRequest(prompt string) (*CommonResponse, error) {
func (c *ChatInstance) CreateImagineRequest(proxy globals.ProxyConfig, prompt string) (*CommonResponse, error) {
content, err := utils.PostRaw(
c.GetImagineEndpoint(),
c.GetMidjourneyHeaders(),
c.GetImagineRequest(prompt),
proxy,
)

if err != nil {
Expand All @@ -47,11 +49,12 @@ func (c *ChatInstance) CreateImagineRequest(prompt string) (*CommonResponse, err
}
}

func (c *ChatInstance) CreateChangeRequest(action string, task string, index *int) (*CommonResponse, error) {
func (c *ChatInstance) CreateChangeRequest(proxy globals.ProxyConfig, action string, task string, index *int) (*CommonResponse, error) {
res, err := utils.Post(
c.GetChangeEndpoint(),
c.GetMidjourneyHeaders(),
c.GetChangeRequest(action, task, index),
proxy,
)

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion adapter/midjourney/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (c *ChatInstance) CreateStreamChatRequest(props *adaptercommon.ChatProps, c

var begin bool

form, err := c.CreateStreamTask(action, prompt, func(form *StorageForm, progress int) error {
form, err := c.CreateStreamTask(props, action, prompt, func(form *StorageForm, progress int) error {
if progress == -1 {
// ping event
return callback(&globals.Chunk{Content: ""})
Expand Down
12 changes: 7 additions & 5 deletions adapter/midjourney/handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package midjourney

import (
adaptercommon "chat/adapter/common"
"chat/globals"
"chat/utils"
"fmt"
"strings"
Expand Down Expand Up @@ -66,21 +68,21 @@ func (c *ChatInstance) ExtractCommand(input string) (task string, index *int) {
return
}

func (c *ChatInstance) CreateRequest(action string, prompt string) (*CommonResponse, error) {
func (c *ChatInstance) CreateRequest(proxy globals.ProxyConfig, action string, prompt string) (*CommonResponse, error) {
switch action {
case ImagineCommand:
return c.CreateImagineRequest(prompt)
return c.CreateImagineRequest(proxy, prompt)
case VariationCommand, UpscaleCommand, RerollCommand:
task, index := c.ExtractCommand(prompt)

return c.CreateChangeRequest(c.GetAction(action), task, index)
return c.CreateChangeRequest(proxy, c.GetAction(action), task, index)
default:
return nil, fmt.Errorf("unknown action: %s", action)
}
}

func (c *ChatInstance) CreateStreamTask(action string, prompt string, hook func(form *StorageForm, progress int) error) (*StorageForm, error) {
res, err := c.CreateRequest(action, prompt)
func (c *ChatInstance) CreateStreamTask(props *adaptercommon.ChatProps, action string, prompt string, hook func(form *StorageForm, progress int) error) (*StorageForm, error) {
res, err := c.CreateRequest(props.Proxy, action, prompt)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion adapter/openai/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func (c *ChatInstance) CreateChatRequest(props *adaptercommon.ChatProps) (string
c.GetChatEndpoint(props),
c.GetHeader(),
c.GetChatBody(props, false),
props.Proxy,
)

if err != nil || res == nil {
Expand Down Expand Up @@ -120,7 +121,7 @@ func (c *ChatInstance) CreateStreamChatRequest(props *adaptercommon.ChatProps, c
}
return callback(partial)
},
})
}, props.Proxy)

if err != nil {
if form := processChatErrorResponse(err.Body); form != nil {
Expand Down
4 changes: 3 additions & 1 deletion adapter/openai/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type ImageProps struct {
Model string
Prompt string
Size ImageSize
Proxy globals.ProxyConfig
}

func (c *ChatInstance) GetImageEndpoint() string {
Expand All @@ -31,7 +32,7 @@ func (c *ChatInstance) CreateImageRequest(props ImageProps) (string, error) {
ImageSize512,
),
N: 1,
})
}, props.Proxy)
if err != nil || res == nil {
return "", fmt.Errorf(err.Error())
}
Expand All @@ -51,6 +52,7 @@ func (c *ChatInstance) CreateImage(props *adaptercommon.ChatProps) (string, erro
original, err := c.CreateImageRequest(ImageProps{
Model: props.Model,
Prompt: c.GetLatestPrompt(props),
Proxy: props.Proxy,
})
if err != nil {
if strings.Contains(err.Error(), "safety") {
Expand Down
4 changes: 2 additions & 2 deletions adapter/palm2/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (c *ChatInstance) CreateChatRequest(props *adaptercommon.ChatProps) (string
if props.Model == globals.ChatBison001 {
data, err := utils.Post(uri, map[string]string{
"Content-Type": "application/json",
}, c.GetPalm2ChatBody(props))
}, c.GetPalm2ChatBody(props), props.Proxy)

if err != nil {
return "", fmt.Errorf("palm2 error: %s", err.Error())
Expand All @@ -103,7 +103,7 @@ func (c *ChatInstance) CreateChatRequest(props *adaptercommon.ChatProps) (string

data, err := utils.Post(uri, map[string]string{
"Content-Type": "application/json",
}, c.GetGeminiChatBody(props))
}, c.GetGeminiChatBody(props), props.Proxy)

if err != nil {
return "", fmt.Errorf("gemini error: %s", err.Error())
Expand Down
2 changes: 2 additions & 0 deletions adapter/zhinao/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func (c *ChatInstance) CreateChatRequest(props *adaptercommon.ChatProps) (string
c.GetChatEndpoint(),
c.GetHeader(),
c.GetChatBody(props, false),
props.Proxy,
)

if err != nil || res == nil {
Expand Down Expand Up @@ -100,6 +101,7 @@ func (c *ChatInstance) CreateStreamChatRequest(props *adaptercommon.ChatProps, c
}
return nil
},
props.Proxy,
)

if err != nil {
Expand Down
1 change: 1 addition & 0 deletions adapter/zhipuai/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@ func (c *ChatInstance) CreateStreamChatRequest(props *adaptercommon.ChatProps, h
data = strings.TrimPrefix(data, "data:")
return hook(&globals.Chunk{Content: data})
},
props.Proxy,
)
}
29 changes: 28 additions & 1 deletion app/src/admin/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ export type Channel = {
mapper: string;
state: boolean;
group?: string[];
proxy?: {
proxy: string;
proxy_type: number;
};
};

export enum proxyType {
NoneProxy = 0,
HttpProxy = 1,
HttpsProxy = 2,
Socks5Proxy = 3,
}

export const ProxyTypes: Record<number, string> = {
[proxyType.NoneProxy]: "None Proxy",
[proxyType.HttpProxy]: "HTTP Proxy",
[proxyType.HttpsProxy]: "HTTPS Proxy",
[proxyType.Socks5Proxy]: "SOCKS5 Proxy",
};

export type ChannelInfo = {
Expand Down Expand Up @@ -133,7 +151,16 @@ export const ChannelInfos: Record<string, ChannelInfo> = {
claude: {
endpoint: "https://api.anthropic.com",
format: "<x-api-key>",
models: ["claude-instant-1", "claude-2", "claude-2.1"],
description:
"> Anthropic Claude 密钥格式为 **x-api-key**,Anthropic 对请求 IP 地域有限制,可能出现 **Request not allowed** 的错误,请尝试更换 IP 或者使用代理。\n",
models: [
"claude-instant-1.2",
"claude-2",
"claude-2.1",
"claude-3-opus-20240229",
"claude-3-sonnet-20240229",
"claude-3-haiku-20240307",
],
},
slack: {
endpoint: "your-channel",
Expand Down
4 changes: 4 additions & 0 deletions app/src/admin/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export const modelColorMapper: Record<string, string> = {
"claude-2": "orange-400",
"claude-2.1": "orange-400",
"claude-2-100k": "orange-400",
"claude-instant-1.2": "orange-400",
"claude-3-opus-20240229": "orange-400",
"claude-3-sonnet-20240229": "orange-400",
"claude-3-haiku-20240307": "orange-400",

"spark-desk-v1.5": "blue-400",
"spark-desk-v2": "blue-400",
Expand Down
1 change: 1 addition & 0 deletions app/src/admin/datasets/charge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export const pricing: PricingDataset = [
"claude-1.2",
"claude-1.3",
"claude-instant",
"claude-instant-1.2",
"claude-slack",
],
input: 0.0008,
Expand Down
Loading

0 comments on commit e037276

Please sign in to comment.