Skip to content

Commit 8f6b8ef

Browse files
committed
🧱 add model support multi deployment with multi model
1 parent f0e62ae commit 8f6b8ef

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

azure/model.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package azure
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"github.com/pkg/errors"
7+
"log"
8+
"net/http"
9+
"net/url"
10+
"path"
11+
"strings"
12+
"text/template"
13+
)
14+
15+
type DeploymentConfig struct {
16+
DeploymentName string `yaml:"deployment_name" json:"deployment_name" mapstructure:"deployment_name"` // azure openai deployment name
17+
ModelName string `yaml:"model_name" json:"model_name" mapstructure:"model_name"` // corresponding model name in openai
18+
Endpoint string `yaml:"endpoint" json:"endpoint" mapstructure:"endpoint"` // deployment endpoint
19+
ApiKey string `yaml:"api_key" json:"api_key" mapstructure:"api_key"` // secrect key1 or 2
20+
ApiVersion string `yaml:"api_version" json:"api_version" mapstructure:"api_version"` // deployment version, not required
21+
EndpointUrl *url.URL // url.URL form deployment endpoint
22+
}
23+
24+
type Config struct {
25+
ApiBase string `yaml:"api_base" mapstructure:"api_base"` // if you use openai、langchain as sdk, it will be useful
26+
DeploymentConfig []DeploymentConfig `yaml:"deployment_config" mapstructure:"deployment_config"` // deployment config
27+
}
28+
29+
type RequestConverter interface {
30+
Name() string
31+
Convert(req *http.Request, config *DeploymentConfig) (*http.Request, error)
32+
}
33+
34+
type StripPrefixConverter struct {
35+
Prefix string
36+
}
37+
38+
func (c *StripPrefixConverter) Name() string {
39+
return "StripPrefix"
40+
}
41+
func (c *StripPrefixConverter) Convert(req *http.Request, config *DeploymentConfig) (*http.Request, error) {
42+
req.Host = config.EndpointUrl.Host
43+
req.URL.Scheme = config.EndpointUrl.Scheme
44+
req.URL.Host = config.EndpointUrl.Host
45+
req.URL.Path = path.Join(fmt.Sprintf("/openai/deployments/%s", config.DeploymentName), strings.Replace(req.URL.Path, c.Prefix+"/", "/", 1))
46+
req.URL.RawPath = req.URL.EscapedPath()
47+
48+
query := req.URL.Query()
49+
query.Add("api-version", config.ApiVersion)
50+
req.URL.RawQuery = query.Encode()
51+
return req, nil
52+
}
53+
func NewStripPrefixConverter(prefix string) *StripPrefixConverter {
54+
return &StripPrefixConverter{
55+
Prefix: prefix,
56+
}
57+
}
58+
59+
type TemplateConverter struct {
60+
Tpl string
61+
Tempalte *template.Template
62+
}
63+
64+
func (c *TemplateConverter) Name() string {
65+
return "Template"
66+
}
67+
func (c *TemplateConverter) Convert(req *http.Request, config *DeploymentConfig) (*http.Request, error) {
68+
data := map[string]interface{}{
69+
"DeploymentName": config.DeploymentName,
70+
"ModelName": config.ModelName,
71+
"Endpoint": config.Endpoint,
72+
"ApiKey": config.ApiKey,
73+
"ApiVersion": config.ApiVersion,
74+
}
75+
buff := new(bytes.Buffer)
76+
if err := c.Tempalte.Execute(buff, data); err != nil {
77+
return req, errors.Wrap(err, "template execute error")
78+
}
79+
80+
req.Host = config.EndpointUrl.Host
81+
req.URL.Scheme = config.EndpointUrl.Scheme
82+
req.URL.Host = config.EndpointUrl.Host
83+
req.URL.Path = buff.String()
84+
req.URL.RawPath = req.URL.EscapedPath()
85+
86+
query := req.URL.Query()
87+
query.Add("api-version", config.ApiVersion)
88+
req.URL.RawQuery = query.Encode()
89+
return req, nil
90+
}
91+
func NewTemplateConverter(tpl string) *TemplateConverter {
92+
_template, err := template.New("template").Parse(tpl)
93+
if err != nil {
94+
log.Fatalf("template parse error: %s", err.Error())
95+
}
96+
return &TemplateConverter{
97+
Tpl: tpl,
98+
Tempalte: _template,
99+
}
100+
}

0 commit comments

Comments
 (0)