Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support azure openai service proxy #78

Merged
merged 1 commit into from
May 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 49 additions & 16 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"net/http"
"net/url"
"time"

"github.com/go-zoox/chalk"
Expand Down Expand Up @@ -537,6 +538,10 @@ func ServeFeishuBot(cfg *FeishuBotConfig) (err error) {
cfg.APIPath,
cfg.OpenAIAPIKey,
cfg.OpenAIAPIServer,
cfg.OpenAIAPIType,
cfg.OpenAIAzureResource,
cfg.OpenAIAzureDeployment,
cfg.OpenAIAzureAPIVersion,
cfg.ProxyOpenAIAPIPath,
cfg.ProxyOpenAIAPIToken,
)
Expand All @@ -545,10 +550,14 @@ func ServeFeishuBot(cfg *FeishuBotConfig) (err error) {
func run(
chatbot chatbot.ChatBot,
port int64,
path string,
OpenAIAPIKey string,
OpenAIAPIServer string,
ProxyOpenAIAPIPath string,
path,
OpenAIAPIKey,
OpenAIAPIServer,
OpenAIAPIType,
OpenAIAzureResource,
OpenAIAzureDeployment,
OpenAIAzureAPIVersion,
ProxyOpenAIAPIPath,
ProxyOpenAIAPIToken string,
) error {
if OpenAIAPIServer == "" {
Expand All @@ -571,19 +580,43 @@ func run(
app.Group(ProxyOpenAIAPIPath, func(group *zoox.RouterGroup) {
group.Use(middleware.BearerToken(strings.Split(ProxyOpenAIAPIToken, ",")))

group.Proxy("/", OpenAIAPIServer, func(cfg *proxy.SingleTargetConfig) {
cfg.RequestHeaders = http.Header{
"Authorization": []string{fmt.Sprintf("Bearer %s", OpenAIAPIKey)},
"User-Agent": []string{fmt.Sprintf("GoZoox/ChatGPT-for-ChatBot-Feishu@%s", Version)},
}
switch OpenAIAPIType {
case "azure":
target := fmt.Sprintf("https://%s.openai.azure.com", OpenAIAzureResource)

group.Proxy("/*", target, func(cfg *proxy.SingleTargetConfig) {
cfg.RequestHeaders = http.Header{
"User-Agent": []string{fmt.Sprintf("GoZoox/ChatGPT-for-ChatBot-Feishu@%s", Version)},
"api-key": []string{OpenAIAPIKey},
}

cfg.Rewrites = rewriter.Rewriters{
{
From: fmt.Sprintf("^%s/(.*)$", ProxyOpenAIAPIPath),
To: fmt.Sprintf("/openai/deployments/%s/$1", OpenAIAzureDeployment),
},
}

cfg.Query = url.Values{
"api-version": []string{OpenAIAzureAPIVersion},
}
})
default:
group.Proxy("/", OpenAIAPIServer, func(cfg *proxy.SingleTargetConfig) {
cfg.RequestHeaders = http.Header{
"User-Agent": []string{fmt.Sprintf("GoZoox/ChatGPT-for-ChatBot-Feishu@%s", Version)},
"Authorization": []string{fmt.Sprintf("Bearer %s", OpenAIAPIKey)},
}

cfg.Rewrites = rewriter.Rewriters{
{
From: fmt.Sprintf("^%s/(.*)$", ProxyOpenAIAPIPath),
To: "/v1/$1",
},
}
})
}

cfg.Rewrites = rewriter.Rewriters{
{
From: fmt.Sprintf("^%s/(.*)$", ProxyOpenAIAPIPath),
To: "/$1",
},
}
})
})
}

Expand Down