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

Added the azure openai proxy resource endpoint #56

Merged
merged 30 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
7925b2a
added the azure openai proxy resource endpoint
edwardcqian Sep 20, 2023
f720acb
add api-version url paramter
edwardcqian Sep 20, 2023
573de4b
added Azure openai setting
edwardcqian Sep 20, 2023
8c3cfd8
Added azure api key
edwardcqian Sep 20, 2023
5128a3a
updated resource field
edwardcqian Sep 20, 2023
514f796
added streaming azure path
edwardcqian Sep 20, 2023
ac73e60
merged azure and openai proxy
edwardcqian Sep 21, 2023
12a4d08
removed azure openai streaming logic
edwardcqian Sep 22, 2023
9dfbc72
added streaming proxy settings
edwardcqian Sep 22, 2023
2ef99e6
update setting to match frontend
edwardcqian Sep 22, 2023
bad2b62
fixed resource proxy routing
edwardcqian Sep 22, 2023
4862d70
added useAzureOpenAI logic
edwardcqian Sep 22, 2023
a0d5195
updated deployment logic
edwardcqian Sep 22, 2023
f6970d8
added azureModelMapping Logic
edwardcqian Sep 22, 2023
24d8116
remove log statements
edwardcqian Sep 22, 2023
8c3078f
Merge remote-tracking branch 'origin/main' into azure-openai-proxy-re…
edwardcqian Sep 25, 2023
1274d3c
update stream method error handling
edwardcqian Sep 25, 2023
02f3aa3
replaced ioutil with io
edwardcqian Sep 25, 2023
6d44046
updated error handling
edwardcqian Sep 25, 2023
799d235
remove unused structs
edwardcqian Sep 25, 2023
10b26b2
updated stream error handling
edwardcqian Sep 25, 2023
21dad62
fix formatting
edwardcqian Sep 25, 2023
5beff3d
added missing error handling
edwardcqian Sep 25, 2023
a22e0dc
Update pkg/plugin/stream.go
edwardcqian Sep 25, 2023
241120a
Wrap OpenAI/Azure httputil reverse proxies so we can fail more gracef…
sd2k Sep 26, 2023
b56c3d3
Update CHANGELOG
sd2k Sep 26, 2023
a180bb8
Merge branch 'main' into azure-openai-proxy-resource
sd2k Sep 26, 2023
cde83a4
fixed stream request issues
edwardcqian Sep 26, 2023
521b7b5
remove debug statements
edwardcqian Sep 26, 2023
ebbcc80
updated error msg formatting
edwardcqian Sep 26, 2023
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
29 changes: 29 additions & 0 deletions pkg/plugin/resources.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package plugin

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
sd2k marked this conversation as resolved.
Show resolved Hide resolved
"net/http"
"net/http/httputil"
"net/url"
Expand Down Expand Up @@ -63,6 +66,31 @@ func newOpenAIProxy() http.Handler {
}
}

func newAzureOpenAIProxy() http.Handler {
director := func(req *http.Request) {
config := httpadapter.PluginConfigFromContext(req.Context())
settings := loadSettings(*config.AppInstanceSettings)

bodyBytes, _ := ioutil.ReadAll(req.Body)
var requestBody map[string]interface{}
json.Unmarshal(bodyBytes, &requestBody)

req.URL.Scheme = "https"
req.URL.Host = fmt.Sprintf("%s.openai.azure.com", settings.AzureOpenAI.ResourceName)
req.URL.Path = fmt.Sprintf("/openai/deployments/%s/chat/completions", requestBody["deployment"])
req.URL.RawQuery = "api-version=2023-03-15-preview"
req.Header.Add("api-key", settings.AzureOpenAI.apiKey)

// Remove extra fields
delete(requestBody, "deployment")

newBodyBytes, _ := json.Marshal(requestBody)
req.Body = ioutil.NopCloser(bytes.NewBuffer(newBodyBytes))
req.ContentLength = int64(len(newBodyBytes))
}
return &httputil.ReverseProxy{Director: director}
}

type vectorSearchRequest struct {
Query string `json:"query"`
Collection string `json:"collection"`
Expand Down Expand Up @@ -110,5 +138,6 @@ func (a *App) registerRoutes(mux *http.ServeMux) {
mux.HandleFunc("/ping", a.handlePing)
mux.HandleFunc("/echo", a.handleEcho)
mux.Handle("/openai/", newOpenAIProxy())
mux.Handle("/azure/", newAzureOpenAIProxy())
mux.HandleFunc("/vector/search", a.handleVectorSearch)
}
10 changes: 9 additions & 1 deletion pkg/plugin/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@ import (
)

const openAIKey = "openAIKey"
const azureOpenAIKey = "azureOpenAIKey"

type OpenAISettings struct {
URL string `json:"url"`
OrganizationID string `json:"organizationId"`
apiKey string
}

type AzureOpenAISettings struct {
ResourceName string `json:"resource"`
apiKey string
}

type Settings struct {
OpenAI OpenAISettings `json:"openAI"`
OpenAI OpenAISettings `json:"openAI"`
AzureOpenAI AzureOpenAISettings `json:"azureOpenAI"`

Vector vector.VectorSettings `json:"vector"`
}
Expand All @@ -30,5 +37,6 @@ func loadSettings(appSettings backend.AppInstanceSettings) Settings {
_ = json.Unmarshal(appSettings.JSONData, &settings)

settings.OpenAI.apiKey = appSettings.DecryptedSecureJSONData[openAIKey]
settings.AzureOpenAI.apiKey = appSettings.DecryptedSecureJSONData[azureOpenAIKey]
return settings
}
5 changes: 3 additions & 2 deletions pkg/plugin/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
)

const openAIChatCompletionsPath = "openai/v1/chat/completions"
const azureOpenAIChatCompletionsPath = "azure/completions"

type chatCompletionsMessage struct {
Role string `json:"role"`
Expand All @@ -32,7 +33,7 @@ func (a *App) SubscribeStream(ctx context.Context, req *backend.SubscribeStreamR
resp := &backend.SubscribeStreamResponse{
Status: backend.SubscribeStreamStatusNotFound,
}
if req.Path == openAIChatCompletionsPath {
if req.Path == openAIChatCompletionsPath || req.Path == azureOpenAIChatCompletionsPath {
resp.Status = backend.SubscribeStreamStatusOK
}
return resp, nil
Expand Down Expand Up @@ -135,7 +136,7 @@ func (a *App) runOpenAIChatCompletionsStream(ctx context.Context, req *backend.R

func (a *App) RunStream(ctx context.Context, req *backend.RunStreamRequest, sender *backend.StreamSender) error {
log.DefaultLogger.Debug(fmt.Sprintf("RunStream: %s", req.Path), "data", string(req.Data))
if req.Path == openAIChatCompletionsPath {
if req.Path == openAIChatCompletionsPath || req.Path == azureOpenAIChatCompletionsPath {
return a.runOpenAIChatCompletionsStream(ctx, req, sender)
}
return fmt.Errorf("unknown stream path: %s", req.Path)
Expand Down
3 changes: 3 additions & 0 deletions provisioning/plugins/grafana-llm-app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ apps:
type: qdrant
qdrant:
address: qdrant:6334
azureopenai:
resource: grafana-machine-learning-dev

secureJsonData:
openAIKey: $OPENAI_API_KEY
azureOpenAIKey: $AZURE_OPENAI_API_KEY