Compare commits
2 Commits
df4e86ae5f
...
ebe2ae4b19
| Author | SHA1 | Date | |
|---|---|---|---|
| ebe2ae4b19 | |||
| cc3e735cbd |
+1
-1
@@ -3,7 +3,7 @@ WORKDIR /frontend-build
|
|||||||
COPY ./web/ .
|
COPY ./web/ .
|
||||||
RUN npm install && npm run build && rm -rf node_modules
|
RUN npm install && npm run build && rm -rf node_modules
|
||||||
|
|
||||||
FROM golang:1.19.7-alpine as builder
|
FROM golang:1.19-alpine as builder
|
||||||
LABEL anther="github.com/Sakurasan"
|
LABEL anther="github.com/Sakurasan"
|
||||||
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && apk --no-cache add make cmake upx
|
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && apk --no-cache add make cmake upx
|
||||||
WORKDIR /build
|
WORKDIR /build
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ package azureopenai
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -46,7 +47,7 @@ type ModelsList struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Models(endpoint, apikey string) (*ModelsList, error) {
|
func Models(endpoint, apikey string) (*ModelsList, error) {
|
||||||
endpoint = removeTrailingSlash(endpoint)
|
endpoint = RemoveTrailingSlash(endpoint)
|
||||||
var modelsl ModelsList
|
var modelsl ModelsList
|
||||||
req, _ := http.NewRequest(http.MethodGet, endpoint+"/openai/deployments?api-version=2022-12-01", nil)
|
req, _ := http.NewRequest(http.MethodGet, endpoint+"/openai/deployments?api-version=2022-12-01", nil)
|
||||||
req.Header.Set("api-key", apikey)
|
req.Header.Set("api-key", apikey)
|
||||||
@@ -63,10 +64,19 @@ func Models(endpoint, apikey string) (*ModelsList, error) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeTrailingSlash(s string) string {
|
func RemoveTrailingSlash(s string) string {
|
||||||
const prefix = "openai.azure.com/"
|
const prefix = "openai.azure.com/"
|
||||||
if strings.HasPrefix(s, prefix) && strings.HasSuffix(s, "/") {
|
if strings.HasSuffix(strings.TrimSpace(s), prefix) && strings.HasSuffix(s, "/") {
|
||||||
return s[:len(s)-1]
|
return s[:len(s)-1]
|
||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetResourceName(url string) string {
|
||||||
|
re := regexp.MustCompile(`https?://(.+)\.openai\.azure\.com/?`)
|
||||||
|
match := re.FindStringSubmatch(url)
|
||||||
|
if len(match) > 1 {
|
||||||
|
return match[1]
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,54 +0,0 @@
|
|||||||
/*
|
|
||||||
https://learn.microsoft.com/zh-cn/azure/cognitive-services/openai/chatgpt-quickstart
|
|
||||||
|
|
||||||
curl $AZURE_OPENAI_ENDPOINT/openai/deployments/gpt-35-turbo/chat/completions?api-version=2023-03-15-preview \
|
|
||||||
-H "Content-Type: application/json" \
|
|
||||||
-H "api-key: $AZURE_OPENAI_KEY" \
|
|
||||||
-d '{
|
|
||||||
"model": "gpt-3.5-turbo",
|
|
||||||
"messages": [{"role": "user", "content": "你好"}]
|
|
||||||
}'
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
package azureopenai
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestModels(t *testing.T) {
|
|
||||||
type args struct {
|
|
||||||
endpoint string
|
|
||||||
apikey string
|
|
||||||
}
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
args args
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "test",
|
|
||||||
args: args{
|
|
||||||
endpoint: "https://mirrors2.openai.azure.com",
|
|
||||||
apikey: "696a7729234c438cb38f24da22ee602d",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
got, err := Models(tt.args.endpoint, tt.args.apikey)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Models() error = %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for _, data := range got.Data {
|
|
||||||
fmt.Println(data.Model, data.ID)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// curl https://mirrors2.openai.azure.com/openai/deployments?api-version=2023-03-15-preview \
|
|
||||||
// -H "Content-Type: application/json" \
|
|
||||||
// -H "api-key: 696a7729234c438cb38f24da22ee602d"
|
|
||||||
+37
-7
@@ -12,6 +12,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
|
"opencatd-open/pkg/azureopenai"
|
||||||
"opencatd-open/store"
|
"opencatd-open/store"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -46,8 +47,10 @@ type User struct {
|
|||||||
type Key struct {
|
type Key struct {
|
||||||
ID int `json:"id,omitempty"`
|
ID int `json:"id,omitempty"`
|
||||||
Key string `json:"key,omitempty"`
|
Key string `json:"key,omitempty"`
|
||||||
UpdatedAt string `json:"updatedAt,omitempty"`
|
|
||||||
Name string `json:"name,omitempty"`
|
Name string `json:"name,omitempty"`
|
||||||
|
ApiType string `json:"api_type,omitempty"`
|
||||||
|
Endpoint string `json:"endpoint,omitempty"`
|
||||||
|
UpdatedAt string `json:"updatedAt,omitempty"`
|
||||||
CreatedAt string `json:"createdAt,omitempty"`
|
CreatedAt string `json:"createdAt,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -223,7 +226,9 @@ func HandleAddKey(c *gin.Context) {
|
|||||||
}})
|
}})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(strings.ToLower(body.Name), "azure") {
|
body.Name = strings.ToLower(strings.TrimSpace(body.Name))
|
||||||
|
body.Key = strings.TrimSpace(body.Key)
|
||||||
|
if strings.HasPrefix(body.Name, "azure.") {
|
||||||
keynames := strings.Split(body.Name, ".")
|
keynames := strings.Split(body.Name, ".")
|
||||||
if len(keynames) < 2 {
|
if len(keynames) < 2 {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": gin.H{
|
c.JSON(http.StatusBadRequest, gin.H{"error": gin.H{
|
||||||
@@ -236,6 +241,7 @@ func HandleAddKey(c *gin.Context) {
|
|||||||
Name: body.Name,
|
Name: body.Name,
|
||||||
Key: body.Key,
|
Key: body.Key,
|
||||||
ResourceNmae: keynames[1],
|
ResourceNmae: keynames[1],
|
||||||
|
EndPoint: body.Endpoint,
|
||||||
}
|
}
|
||||||
if err := store.CreateKey(k); err != nil {
|
if err := store.CreateKey(k); err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": gin.H{
|
c.JSON(http.StatusInternalServerError, gin.H{"error": gin.H{
|
||||||
@@ -244,12 +250,29 @@ func HandleAddKey(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if body.ApiType == "" {
|
||||||
if err := store.AddKey("openai", body.Key, body.Name); err != nil {
|
if err := store.AddKey("openai", body.Key, body.Name); err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": gin.H{
|
c.JSON(http.StatusInternalServerError, gin.H{"error": gin.H{
|
||||||
"message": err.Error(),
|
"message": err.Error(),
|
||||||
}})
|
}})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
k := &store.Key{
|
||||||
|
ApiType: body.ApiType,
|
||||||
|
Name: body.Name,
|
||||||
|
Key: body.Key,
|
||||||
|
ResourceNmae: azureopenai.GetResourceName(body.Endpoint),
|
||||||
|
EndPoint: body.Endpoint,
|
||||||
|
}
|
||||||
|
if err := store.CreateKey(k); err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": gin.H{
|
||||||
|
"message": err.Error(),
|
||||||
|
}})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
k, err := store.GetKeyrByName(body.Name)
|
k, err := store.GetKeyrByName(body.Name)
|
||||||
@@ -281,10 +304,10 @@ func HandleAddUser(c *gin.Context) {
|
|||||||
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
|
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// if len(body.Name) == 0 {
|
if len(body.Name) == 0 {
|
||||||
// c.JSON(http.StatusOK, gin.H{"error": "invalid user name"})
|
c.JSON(http.StatusOK, gin.H{"error": "invalid user name"})
|
||||||
// return
|
return
|
||||||
// }
|
}
|
||||||
|
|
||||||
if err := store.AddUser(body.Name, uuid.NewString()); err != nil {
|
if err := store.AddUser(body.Name, uuid.NewString()); err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
|
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
|
||||||
@@ -396,7 +419,14 @@ func HandleProy(c *gin.Context) {
|
|||||||
// 创建 API 请求
|
// 创建 API 请求
|
||||||
switch onekey.ApiType {
|
switch onekey.ApiType {
|
||||||
case "azure_openai":
|
case "azure_openai":
|
||||||
req, err = http.NewRequest(c.Request.Method, fmt.Sprintf("https://%s.openai.azure.com/openai/deployments/%s/chat/completions?api-version=2023-05-15", onekey.ResourceNmae, modelmap(chatreq.Model)), &body)
|
var buildurl string
|
||||||
|
var apiVersion = "2023-05-15"
|
||||||
|
if onekey.EndPoint != "" {
|
||||||
|
buildurl = fmt.Sprintf("https://%s/openai/deployments/%s/chat/completions?api-version=%s", onekey.EndPoint, modelmap(chatreq.Model), apiVersion)
|
||||||
|
} else {
|
||||||
|
buildurl = fmt.Sprintf("https://%s.openai.azure.com/openai/deployments/%s/chat/completions?api-version=%s", onekey.ResourceNmae, modelmap(chatreq.Model), apiVersion)
|
||||||
|
}
|
||||||
|
req, err = http.NewRequest(c.Request.Method, buildurl, &body)
|
||||||
req.Header = c.Request.Header
|
req.Header = c.Request.Header
|
||||||
req.Header.Set("api-key", onekey.Key)
|
req.Header.Set("api-key", onekey.Key)
|
||||||
case "openai":
|
case "openai":
|
||||||
|
|||||||
Reference in New Issue
Block a user