mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-09-18 01:26:37 +08:00
Merge commit '1c2654320e5b6268b13b6efca40ce37a523d032b'
This commit is contained in:
commit
ca23dd9a97
2
.github/workflows/linux-release.yml
vendored
2
.github/workflows/linux-release.yml
vendored
@ -44,7 +44,7 @@ jobs:
|
||||
- name: Build Backend (amd64)
|
||||
run: |
|
||||
go mod download
|
||||
go build -ldflags "-s -w -X 'github.com/Laisky/one-api/common.Version=$(git describe --tags)' -extldflags '-static'" -o one-api
|
||||
go build -ldflags "-s -w -X 'github.com/songquanpeng/one-api/common.Version=$(git describe --tags)' -extldflags '-static'" -o one-api
|
||||
|
||||
- name: Build Backend (arm64)
|
||||
run: |
|
||||
|
2
.github/workflows/macos-release.yml
vendored
2
.github/workflows/macos-release.yml
vendored
@ -44,7 +44,7 @@ jobs:
|
||||
- name: Build Backend
|
||||
run: |
|
||||
go mod download
|
||||
go build -ldflags "-X 'github.com/Laisky/one-api/common.Version=$(git describe --tags)'" -o one-api-macos
|
||||
go build -ldflags "-X 'github.com/songquanpeng/one-api/common.Version=$(git describe --tags)'" -o one-api-macos
|
||||
- name: Release
|
||||
uses: softprops/action-gh-release@v1
|
||||
if: startsWith(github.ref, 'refs/tags/')
|
||||
|
2
.github/workflows/windows-release.yml
vendored
2
.github/workflows/windows-release.yml
vendored
@ -47,7 +47,7 @@ jobs:
|
||||
- name: Build Backend
|
||||
run: |
|
||||
go mod download
|
||||
go build -ldflags "-s -w -X 'github.com/Laisky/one-api/common.Version=$(git describe --tags)'" -o one-api.exe
|
||||
go build -ldflags "-s -w -X 'github.com/songquanpeng/one-api/common.Version=$(git describe --tags)'" -o one-api.exe
|
||||
- name: Release
|
||||
uses: softprops/action-gh-release@v1
|
||||
if: startsWith(github.ref, 'refs/tags/')
|
||||
|
@ -23,7 +23,7 @@ ADD go.mod go.sum ./
|
||||
RUN go mod download
|
||||
COPY . .
|
||||
COPY --from=builder /web/build ./web/build
|
||||
RUN go build -ldflags "-s -w -X 'github.com/Laisky/one-api/common.Version=$(cat VERSION)' -extldflags '-static'" -o one-api
|
||||
RUN go build -ldflags "-s -w -X 'github.com/songquanpeng/one-api/common.Version=$(cat VERSION)' -extldflags '-static'" -o one-api
|
||||
|
||||
FROM debian:bullseye
|
||||
|
||||
|
20
README.en.md
20
README.en.md
@ -3,7 +3,7 @@
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
<a href="https://github.com/Laisky/one-api"><img src="https://raw.githubusercontent.com/songquanpeng/one-api/main/web/default/public/logo.png" width="150" height="150" alt="one-api logo"></a>
|
||||
<a href="https://github.com/songquanpeng/one-api"><img src="https://raw.githubusercontent.com/songquanpeng/one-api/main/web/default/public/logo.png" width="150" height="150" alt="one-api logo"></a>
|
||||
</p>
|
||||
|
||||
<div align="center">
|
||||
@ -18,17 +18,17 @@ _✨ Access all LLM through the standard OpenAI API format, easy to deploy & use
|
||||
<a href="https://raw.githubusercontent.com/songquanpeng/one-api/main/LICENSE">
|
||||
<img src="https://img.shields.io/github/license/songquanpeng/one-api?color=brightgreen" alt="license">
|
||||
</a>
|
||||
<a href="https://github.com/Laisky/one-api/releases/latest">
|
||||
<a href="https://github.com/songquanpeng/one-api/releases/latest">
|
||||
<img src="https://img.shields.io/github/v/release/songquanpeng/one-api?color=brightgreen&include_prereleases" alt="release">
|
||||
</a>
|
||||
<a href="https://hub.docker.com/repository/docker/justsong/one-api">
|
||||
<img src="https://img.shields.io/docker/pulls/justsong/one-api?color=brightgreen" alt="docker pull">
|
||||
</a>
|
||||
<a href="https://github.com/Laisky/one-api/releases/latest">
|
||||
<a href="https://github.com/songquanpeng/one-api/releases/latest">
|
||||
<img src="https://img.shields.io/github/downloads/songquanpeng/one-api/total?color=brightgreen&include_prereleases" alt="release">
|
||||
</a>
|
||||
<a href="https://goreportcard.com/report/github.com/Laisky/one-api">
|
||||
<img src="https://goreportcard.com/badge/github.com/Laisky/one-api" alt="GoReportCard">
|
||||
<a href="https://goreportcard.com/report/github.com/songquanpeng/one-api">
|
||||
<img src="https://goreportcard.com/badge/github.com/songquanpeng/one-api" alt="GoReportCard">
|
||||
</a>
|
||||
</p>
|
||||
|
||||
@ -37,7 +37,7 @@ _✨ Access all LLM through the standard OpenAI API format, easy to deploy & use
|
||||
·
|
||||
<a href="#usage">Usage</a>
|
||||
·
|
||||
<a href="https://github.com/Laisky/one-api/issues">Feedback</a>
|
||||
<a href="https://github.com/songquanpeng/one-api/issues">Feedback</a>
|
||||
·
|
||||
<a href="#screenshots">Screenshots</a>
|
||||
·
|
||||
@ -129,9 +129,9 @@ sudo service nginx restart
|
||||
The initial account username is `root` and password is `123456`.
|
||||
|
||||
### Manual Deployment
|
||||
1. Download the executable file from [GitHub Releases](https://github.com/Laisky/one-api/releases/latest) or compile from source:
|
||||
1. Download the executable file from [GitHub Releases](https://github.com/songquanpeng/one-api/releases/latest) or compile from source:
|
||||
```shell
|
||||
git clone https://github.com/Laisky/one-api.git
|
||||
git clone https://github.com/songquanpeng/one-api.git
|
||||
|
||||
# Build the frontend
|
||||
cd one-api/web/default
|
||||
@ -164,9 +164,9 @@ For more detailed deployment tutorials, please refer to [this page](https://iama
|
||||
Please refer to the [environment variables](#environment-variables) section for details on using environment variables.
|
||||
|
||||
### Deployment on Control Panels (e.g., Baota)
|
||||
Refer to [#175](https://github.com/Laisky/one-api/issues/175) for detailed instructions.
|
||||
Refer to [#175](https://github.com/songquanpeng/one-api/issues/175) for detailed instructions.
|
||||
|
||||
If you encounter a blank page after deployment, refer to [#97](https://github.com/Laisky/one-api/issues/97) for possible solutions.
|
||||
If you encounter a blank page after deployment, refer to [#97](https://github.com/songquanpeng/one-api/issues/97) for possible solutions.
|
||||
|
||||
### Deployment on Third-Party Platforms
|
||||
<details>
|
||||
|
20
README.ja.md
20
README.ja.md
@ -3,7 +3,7 @@
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
<a href="https://github.com/Laisky/one-api"><img src="https://raw.githubusercontent.com/songquanpeng/one-api/main/web/default/public/logo.png" width="150" height="150" alt="one-api logo"></a>
|
||||
<a href="https://github.com/songquanpeng/one-api"><img src="https://raw.githubusercontent.com/songquanpeng/one-api/main/web/default/public/logo.png" width="150" height="150" alt="one-api logo"></a>
|
||||
</p>
|
||||
|
||||
<div align="center">
|
||||
@ -18,17 +18,17 @@ _✨ 標準的な OpenAI API フォーマットを通じてすべての LLM に
|
||||
<a href="https://raw.githubusercontent.com/songquanpeng/one-api/main/LICENSE">
|
||||
<img src="https://img.shields.io/github/license/songquanpeng/one-api?color=brightgreen" alt="license">
|
||||
</a>
|
||||
<a href="https://github.com/Laisky/one-api/releases/latest">
|
||||
<a href="https://github.com/songquanpeng/one-api/releases/latest">
|
||||
<img src="https://img.shields.io/github/v/release/songquanpeng/one-api?color=brightgreen&include_prereleases" alt="release">
|
||||
</a>
|
||||
<a href="https://hub.docker.com/repository/docker/justsong/one-api">
|
||||
<img src="https://img.shields.io/docker/pulls/justsong/one-api?color=brightgreen" alt="docker pull">
|
||||
</a>
|
||||
<a href="https://github.com/Laisky/one-api/releases/latest">
|
||||
<a href="https://github.com/songquanpeng/one-api/releases/latest">
|
||||
<img src="https://img.shields.io/github/downloads/songquanpeng/one-api/total?color=brightgreen&include_prereleases" alt="release">
|
||||
</a>
|
||||
<a href="https://goreportcard.com/report/github.com/Laisky/one-api">
|
||||
<img src="https://goreportcard.com/badge/github.com/Laisky/one-api" alt="GoReportCard">
|
||||
<a href="https://goreportcard.com/report/github.com/songquanpeng/one-api">
|
||||
<img src="https://goreportcard.com/badge/github.com/songquanpeng/one-api" alt="GoReportCard">
|
||||
</a>
|
||||
</p>
|
||||
|
||||
@ -37,7 +37,7 @@ _✨ 標準的な OpenAI API フォーマットを通じてすべての LLM に
|
||||
·
|
||||
<a href="#usage">使用方法</a>
|
||||
·
|
||||
<a href="https://github.com/Laisky/one-api/issues">フィードバック</a>
|
||||
<a href="https://github.com/songquanpeng/one-api/issues">フィードバック</a>
|
||||
·
|
||||
<a href="#screenshots">スクリーンショット</a>
|
||||
·
|
||||
@ -130,9 +130,9 @@ sudo service nginx restart
|
||||
初期アカウントのユーザー名は `root` で、パスワードは `123456` です。
|
||||
|
||||
### マニュアルデプロイ
|
||||
1. [GitHub Releases](https://github.com/Laisky/one-api/releases/latest) から実行ファイルをダウンロードする、もしくはソースからコンパイルする:
|
||||
1. [GitHub Releases](https://github.com/songquanpeng/one-api/releases/latest) から実行ファイルをダウンロードする、もしくはソースからコンパイルする:
|
||||
```shell
|
||||
git clone https://github.com/Laisky/one-api.git
|
||||
git clone https://github.com/songquanpeng/one-api.git
|
||||
|
||||
# フロントエンドのビルド
|
||||
cd one-api/web/default
|
||||
@ -165,9 +165,9 @@ sudo service nginx restart
|
||||
Please refer to the [environment variables](#environment-variables) section for details on using environment variables.
|
||||
|
||||
### コントロールパネル(例: Baota)への展開
|
||||
詳しい手順は [#175](https://github.com/Laisky/one-api/issues/175) を参照してください。
|
||||
詳しい手順は [#175](https://github.com/songquanpeng/one-api/issues/175) を参照してください。
|
||||
|
||||
配置後に空白のページが表示される場合は、[#97](https://github.com/Laisky/one-api/issues/97) を参照してください。
|
||||
配置後に空白のページが表示される場合は、[#97](https://github.com/songquanpeng/one-api/issues/97) を参照してください。
|
||||
|
||||
### サードパーティプラットフォームへのデプロイ
|
||||
<details>
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/Laisky/one-api/common/env"
|
||||
"github.com/songquanpeng/one-api/common/env"
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
@ -1,13 +0,0 @@
|
||||
package ctxkey
|
||||
|
||||
const (
|
||||
ConfigPrefix = "cfg_"
|
||||
|
||||
ConfigAPIVersion = ConfigPrefix + "api_version"
|
||||
ConfigLibraryID = ConfigPrefix + "library_id"
|
||||
ConfigPlugin = ConfigPrefix + "plugin"
|
||||
ConfigSK = ConfigPrefix + "sk"
|
||||
ConfigAK = ConfigPrefix + "ak"
|
||||
ConfigRegion = ConfigPrefix + "region"
|
||||
ConfigUserID = ConfigPrefix + "user_id"
|
||||
)
|
@ -1,6 +1,7 @@
|
||||
package ctxkey
|
||||
|
||||
const (
|
||||
Config = "config"
|
||||
Id = "id"
|
||||
RequestId = "X-Oneapi-Request-Id"
|
||||
Username = "username"
|
||||
|
@ -1,7 +1,7 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"github.com/Laisky/one-api/common/env"
|
||||
"github.com/songquanpeng/one-api/common/env"
|
||||
)
|
||||
|
||||
var UsingSQLite = false
|
||||
|
@ -10,7 +10,8 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/Laisky/one-api/common/random"
|
||||
"github.com/songquanpeng/one-api/common/random"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func OpenBrowser(url string) {
|
||||
@ -106,6 +107,11 @@ func GenRequestID() string {
|
||||
return GetTimeString() + random.GetRandomNumberString(8)
|
||||
}
|
||||
|
||||
func GetResponseID(c *gin.Context) string {
|
||||
logID := c.GetString(RequestIdKey)
|
||||
return fmt.Sprintf("chatcmpl-%s", logID)
|
||||
}
|
||||
|
||||
func Max(a int, b int) int {
|
||||
if a >= b {
|
||||
return a
|
||||
|
5
common/helper/key.go
Normal file
5
common/helper/key.go
Normal file
@ -0,0 +1,5 @@
|
||||
package helper
|
||||
|
||||
const (
|
||||
RequestIdKey = "X-Oneapi-Request-Id"
|
||||
)
|
@ -12,7 +12,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
img "github.com/Laisky/one-api/common/image"
|
||||
img "github.com/songquanpeng/one-api/common/image"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
_ "golang.org/x/image/webp"
|
||||
|
@ -3,8 +3,8 @@ package common
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@ -20,7 +20,7 @@ var (
|
||||
func printHelp() {
|
||||
fmt.Println("One API " + Version + " - All in one API service for OpenAI API.")
|
||||
fmt.Println("Copyright (C) 2023 JustSong. All rights reserved.")
|
||||
fmt.Println("GitHub: https://github.com/Laisky/one-api")
|
||||
fmt.Println("GitHub: https://github.com/songquanpeng/one-api")
|
||||
fmt.Println("Usage: one-api [--port <port>] [--log-dir <log directory>] [--version] [--help]")
|
||||
}
|
||||
|
||||
|
@ -10,9 +10,8 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/ctxkey"
|
||||
"github.com/Laisky/one-api/common/helper"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@ -88,7 +87,7 @@ func logHelper(ctx context.Context, level string, msg string) {
|
||||
if level == loggerINFO {
|
||||
writer = gin.DefaultWriter
|
||||
}
|
||||
id := ctx.Value(ctxkey.RequestId)
|
||||
id := ctx.Value(helper.RequestIdKey)
|
||||
if id == nil {
|
||||
id = helper.GenRequestID()
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/Laisky/errors/v2"
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
)
|
||||
|
||||
func SendEmail(subject string, receiver string, content string) error {
|
||||
|
@ -2,7 +2,7 @@ package message
|
||||
|
||||
import (
|
||||
"github.com/Laisky/errors/v2"
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -4,7 +4,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/Laisky/errors/v2"
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
|
@ -3,7 +3,7 @@ package network
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"net"
|
||||
"strings"
|
||||
)
|
||||
|
@ -2,7 +2,7 @@ package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"os"
|
||||
"time"
|
||||
|
@ -2,7 +2,7 @@ package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
)
|
||||
|
||||
func LogQuota(quota int64) string {
|
||||
|
@ -5,11 +5,11 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/Laisky/errors/v2"
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/Laisky/one-api/common/random"
|
||||
"github.com/Laisky/one-api/controller"
|
||||
"github.com/Laisky/one-api/model"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/common/random"
|
||||
"github.com/songquanpeng/one-api/controller"
|
||||
"github.com/songquanpeng/one-api/model"
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
|
@ -9,10 +9,10 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/Laisky/errors/v2"
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/Laisky/one-api/controller"
|
||||
"github.com/Laisky/one-api/model"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/controller"
|
||||
"github.com/songquanpeng/one-api/model"
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
@ -8,10 +8,10 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/Laisky/errors/v2"
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/ctxkey"
|
||||
"github.com/Laisky/one-api/controller"
|
||||
"github.com/Laisky/one-api/model"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/ctxkey"
|
||||
"github.com/songquanpeng/one-api/controller"
|
||||
"github.com/songquanpeng/one-api/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
@ -1,10 +1,10 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/ctxkey"
|
||||
"github.com/Laisky/one-api/model"
|
||||
relaymodel "github.com/Laisky/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/ctxkey"
|
||||
"github.com/songquanpeng/one-api/model"
|
||||
relaymodel "github.com/songquanpeng/one-api/relay/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
@ -9,17 +9,17 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/Laisky/errors/v2"
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/Laisky/one-api/model"
|
||||
"github.com/Laisky/one-api/monitor"
|
||||
"github.com/Laisky/one-api/relay/channeltype"
|
||||
"github.com/Laisky/one-api/relay/client"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/model"
|
||||
"github.com/songquanpeng/one-api/monitor"
|
||||
"github.com/songquanpeng/one-api/relay/channeltype"
|
||||
"github.com/songquanpeng/one-api/relay/client"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// https://github.com/Laisky/one-api/issues/79
|
||||
// https://github.com/songquanpeng/one-api/issues/79
|
||||
|
||||
type OpenAISubscriptionResponse struct {
|
||||
Object string `json:"object"`
|
||||
|
@ -4,6 +4,21 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/Laisky/errors/v2"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/ctxkey"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/common/message"
|
||||
"github.com/songquanpeng/one-api/middleware"
|
||||
"github.com/songquanpeng/one-api/model"
|
||||
"github.com/songquanpeng/one-api/monitor"
|
||||
relay "github.com/songquanpeng/one-api/relay"
|
||||
"github.com/songquanpeng/one-api/relay/channeltype"
|
||||
"github.com/songquanpeng/one-api/relay/controller"
|
||||
"github.com/songquanpeng/one-api/relay/meta"
|
||||
relaymodel "github.com/songquanpeng/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/relay/relaymode"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@ -12,22 +27,6 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/Laisky/errors/v2"
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/ctxkey"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/Laisky/one-api/common/message"
|
||||
"github.com/Laisky/one-api/middleware"
|
||||
"github.com/Laisky/one-api/model"
|
||||
"github.com/Laisky/one-api/monitor"
|
||||
relay "github.com/Laisky/one-api/relay"
|
||||
"github.com/Laisky/one-api/relay/channeltype"
|
||||
"github.com/Laisky/one-api/relay/controller"
|
||||
"github.com/Laisky/one-api/relay/meta"
|
||||
relaymodel "github.com/Laisky/one-api/relay/model"
|
||||
"github.com/Laisky/one-api/relay/relaymode"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func buildTestRequest() *relaymodel.GeneralOpenAIRequest {
|
||||
@ -57,6 +56,8 @@ func testChannel(channel *model.Channel) (err error, openaiErr *relaymodel.Error
|
||||
c.Request.Header.Set("Content-Type", "application/json")
|
||||
c.Set(ctxkey.Channel, channel.Type)
|
||||
c.Set(ctxkey.BaseURL, channel.GetBaseURL())
|
||||
cfg, _ := channel.LoadConfig()
|
||||
c.Set(ctxkey.Config, cfg)
|
||||
middleware.SetupContextForSelectedChannel(c, channel, "")
|
||||
meta := meta.GetByContext(c)
|
||||
apiType := channeltype.ToAPIType(channel.Type)
|
||||
@ -67,6 +68,7 @@ func testChannel(channel *model.Channel) (err error, openaiErr *relaymodel.Error
|
||||
adaptor.Init(meta)
|
||||
var modelName string
|
||||
modelList := adaptor.GetModelList()
|
||||
modelMap := channel.GetModelMapping()
|
||||
if len(modelList) != 0 {
|
||||
modelName = modelList[0]
|
||||
}
|
||||
@ -75,6 +77,9 @@ func testChannel(channel *model.Channel) (err error, openaiErr *relaymodel.Error
|
||||
if len(modelNames) > 0 {
|
||||
modelName = modelNames[0]
|
||||
}
|
||||
if modelMap != nil && modelMap[modelName] != "" {
|
||||
modelName = modelMap[modelName]
|
||||
}
|
||||
}
|
||||
request := buildTestRequest()
|
||||
request.Model = modelName
|
||||
|
@ -1,9 +1,9 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/helper"
|
||||
"github.com/Laisky/one-api/model"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
"github.com/songquanpeng/one-api/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
@ -1,7 +1,7 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
billingratio "github.com/Laisky/one-api/relay/billing/ratio"
|
||||
billingratio "github.com/songquanpeng/one-api/relay/billing/ratio"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
@ -4,9 +4,9 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/ctxkey"
|
||||
"github.com/Laisky/one-api/model"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/ctxkey"
|
||||
"github.com/songquanpeng/one-api/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
@ -3,10 +3,10 @@ package controller
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/Laisky/one-api/common"
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/message"
|
||||
"github.com/Laisky/one-api/model"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/message"
|
||||
"github.com/songquanpeng/one-api/model"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
|
@ -5,14 +5,14 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/Laisky/one-api/common/ctxkey"
|
||||
"github.com/Laisky/one-api/model"
|
||||
relay "github.com/Laisky/one-api/relay"
|
||||
"github.com/Laisky/one-api/relay/adaptor/openai"
|
||||
"github.com/Laisky/one-api/relay/apitype"
|
||||
"github.com/Laisky/one-api/relay/channeltype"
|
||||
"github.com/Laisky/one-api/relay/meta"
|
||||
relaymodel "github.com/Laisky/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/common/ctxkey"
|
||||
"github.com/songquanpeng/one-api/model"
|
||||
relay "github.com/songquanpeng/one-api/relay"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
||||
"github.com/songquanpeng/one-api/relay/apitype"
|
||||
"github.com/songquanpeng/one-api/relay/channeltype"
|
||||
"github.com/songquanpeng/one-api/relay/meta"
|
||||
relaymodel "github.com/songquanpeng/one-api/relay/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
@ -2,9 +2,9 @@ package controller
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/helper"
|
||||
"github.com/Laisky/one-api/model"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
"github.com/songquanpeng/one-api/model"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
|
@ -4,11 +4,11 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/ctxkey"
|
||||
"github.com/Laisky/one-api/common/helper"
|
||||
"github.com/Laisky/one-api/common/random"
|
||||
"github.com/Laisky/one-api/model"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/ctxkey"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
"github.com/songquanpeng/one-api/common/random"
|
||||
"github.com/songquanpeng/one-api/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
@ -7,17 +7,17 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/Laisky/one-api/common"
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/ctxkey"
|
||||
"github.com/Laisky/one-api/common/helper"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/Laisky/one-api/middleware"
|
||||
dbmodel "github.com/Laisky/one-api/model"
|
||||
"github.com/Laisky/one-api/monitor"
|
||||
"github.com/Laisky/one-api/relay/controller"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/Laisky/one-api/relay/relaymode"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/ctxkey"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/middleware"
|
||||
dbmodel "github.com/songquanpeng/one-api/model"
|
||||
"github.com/songquanpeng/one-api/monitor"
|
||||
"github.com/songquanpeng/one-api/relay/controller"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/relay/relaymode"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
@ -55,9 +55,9 @@ func Relay(c *gin.Context) {
|
||||
channelName := c.GetString(ctxkey.ChannelName)
|
||||
group := c.GetString(ctxkey.Group)
|
||||
originalModel := c.GetString(ctxkey.OriginalModel)
|
||||
// bizErr is shared, should not run this function in goroutine to avoid race
|
||||
processChannelRelayError(ctx, channelId, channelName, bizErr)
|
||||
requestId := c.GetString(ctxkey.RequestId)
|
||||
// BUG: bizErr is shared, should not run this function in goroutine to avoid race
|
||||
go processChannelRelayError(ctx, channelId, channelName, bizErr)
|
||||
requestId := c.GetString(helper.RequestIdKey)
|
||||
retryTimes := config.RetryTimes
|
||||
if err := shouldRetry(c, bizErr.StatusCode); err != nil {
|
||||
logger.Errorf(ctx, "relay error happen, won't retry since of %v", err.Error())
|
||||
|
@ -5,13 +5,13 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/Laisky/one-api/common"
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/ctxkey"
|
||||
"github.com/Laisky/one-api/common/helper"
|
||||
"github.com/Laisky/one-api/common/network"
|
||||
"github.com/Laisky/one-api/common/random"
|
||||
"github.com/Laisky/one-api/model"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/ctxkey"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
"github.com/songquanpeng/one-api/common/network"
|
||||
"github.com/songquanpeng/one-api/common/random"
|
||||
"github.com/songquanpeng/one-api/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jinzhu/copier"
|
||||
)
|
||||
|
@ -7,11 +7,11 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/Laisky/one-api/common"
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/ctxkey"
|
||||
"github.com/Laisky/one-api/common/random"
|
||||
"github.com/Laisky/one-api/model"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/ctxkey"
|
||||
"github.com/songquanpeng/one-api/common/random"
|
||||
"github.com/songquanpeng/one-api/model"
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
2
go.mod
2
go.mod
@ -1,4 +1,4 @@
|
||||
module github.com/Laisky/one-api
|
||||
module github.com/songquanpeng/one-api
|
||||
|
||||
// +heroku goVersion go1.18
|
||||
go 1.21
|
||||
|
16
main.go
16
main.go
@ -7,14 +7,14 @@ import (
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/Laisky/one-api/common"
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/Laisky/one-api/controller"
|
||||
"github.com/Laisky/one-api/middleware"
|
||||
"github.com/Laisky/one-api/model"
|
||||
"github.com/Laisky/one-api/relay/adaptor/openai"
|
||||
"github.com/Laisky/one-api/router"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/controller"
|
||||
"github.com/songquanpeng/one-api/middleware"
|
||||
"github.com/songquanpeng/one-api/model"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
||||
"github.com/songquanpeng/one-api/router"
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-contrib/sessions/cookie"
|
||||
"github.com/gin-gonic/gin"
|
||||
|
@ -5,11 +5,11 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/Laisky/one-api/common/blacklist"
|
||||
"github.com/Laisky/one-api/common/ctxkey"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/Laisky/one-api/common/network"
|
||||
"github.com/Laisky/one-api/model"
|
||||
"github.com/songquanpeng/one-api/common/blacklist"
|
||||
"github.com/songquanpeng/one-api/common/ctxkey"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/common/network"
|
||||
"github.com/songquanpeng/one-api/model"
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
@ -7,11 +7,11 @@ import (
|
||||
"strings"
|
||||
|
||||
gutils "github.com/Laisky/go-utils/v4"
|
||||
"github.com/Laisky/one-api/common/ctxkey"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/Laisky/one-api/model"
|
||||
"github.com/Laisky/one-api/relay/billing/ratio"
|
||||
"github.com/Laisky/one-api/relay/channeltype"
|
||||
"github.com/songquanpeng/one-api/common/ctxkey"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/model"
|
||||
"github.com/songquanpeng/one-api/relay/billing/ratio"
|
||||
"github.com/songquanpeng/one-api/relay/channeltype"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@ -89,21 +89,29 @@ func SetupContextForSelectedChannel(c *gin.Context, channel *model.Channel, mode
|
||||
c.Set(ctxkey.OriginalModel, modelName) // for retry
|
||||
c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", channel.Key))
|
||||
c.Set(ctxkey.BaseURL, channel.GetBaseURL())
|
||||
cfg, _ := channel.LoadConfig()
|
||||
// this is for backward compatibility
|
||||
switch channel.Type {
|
||||
case channeltype.Azure:
|
||||
c.Set(ctxkey.ConfigAPIVersion, channel.Other)
|
||||
if cfg.APIVersion == "" {
|
||||
cfg.APIVersion = channel.Other
|
||||
}
|
||||
case channeltype.Xunfei:
|
||||
c.Set(ctxkey.ConfigAPIVersion, channel.Other)
|
||||
if cfg.APIVersion == "" {
|
||||
cfg.APIVersion = channel.Other
|
||||
}
|
||||
case channeltype.Gemini:
|
||||
c.Set(ctxkey.ConfigAPIVersion, channel.Other)
|
||||
if cfg.APIVersion == "" {
|
||||
cfg.APIVersion = channel.Other
|
||||
}
|
||||
case channeltype.AIProxyLibrary:
|
||||
c.Set(ctxkey.ConfigLibraryID, channel.Other)
|
||||
if cfg.LibraryID == "" {
|
||||
cfg.LibraryID = channel.Other
|
||||
}
|
||||
case channeltype.Ali:
|
||||
c.Set(ctxkey.ConfigPlugin, channel.Other)
|
||||
}
|
||||
cfg, _ := channel.LoadConfig()
|
||||
for k, v := range cfg {
|
||||
c.Set(ctxkey.ConfigPrefix+k, v)
|
||||
if cfg.Plugin == "" {
|
||||
cfg.Plugin = channel.Other
|
||||
}
|
||||
}
|
||||
c.Set(ctxkey.Config, cfg)
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ package middleware
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/Laisky/one-api/common/ctxkey"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@ -11,7 +11,7 @@ func SetUpLogger(server *gin.Engine) {
|
||||
server.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
|
||||
var requestID string
|
||||
if param.Keys != nil {
|
||||
requestID = param.Keys[ctxkey.RequestId].(string)
|
||||
requestID = param.Keys[helper.RequestIdKey].(string)
|
||||
}
|
||||
return fmt.Sprintf("[GIN] %s | %s | %3d | %13v | %15s | %7s %s\n",
|
||||
param.TimeStamp.Format("2006/01/02 - 15:04:05"),
|
||||
|
@ -3,8 +3,8 @@ package middleware
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/Laisky/one-api/common"
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"time"
|
||||
|
@ -2,8 +2,8 @@ package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Laisky/one-api/common"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"runtime/debug"
|
||||
@ -21,7 +21,7 @@ func RelayPanicRecover() gin.HandlerFunc {
|
||||
logger.Errorf(ctx, fmt.Sprintf("request body: %s", string(body)))
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": gin.H{
|
||||
"message": fmt.Sprintf("Panic detected, error: %v. Please submit an issue with the related log here: https://github.com/Laisky/one-api", err),
|
||||
"message": fmt.Sprintf("Panic detected, error: %v. Please submit an issue with the related log here: https://github.com/songquanpeng/one-api", err),
|
||||
"type": "one_api_panic",
|
||||
},
|
||||
})
|
||||
|
@ -3,18 +3,17 @@ package middleware
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/Laisky/one-api/common/ctxkey"
|
||||
"github.com/Laisky/one-api/common/helper"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RequestId() func(c *gin.Context) {
|
||||
return func(c *gin.Context) {
|
||||
id := helper.GenRequestID()
|
||||
c.Set(ctxkey.RequestId, id)
|
||||
ctx := context.WithValue(c.Request.Context(), ctxkey.RequestId, id)
|
||||
c.Set(helper.RequestIdKey, id)
|
||||
ctx := context.WithValue(c.Request.Context(), helper.RequestIdKey, id)
|
||||
c.Request = c.Request.WithContext(ctx)
|
||||
c.Header(ctxkey.RequestId, id)
|
||||
c.Header(helper.RequestIdKey, id)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
@ -2,8 +2,8 @@ package middleware
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
|
@ -4,17 +4,16 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/Laisky/one-api/common"
|
||||
"github.com/Laisky/one-api/common/ctxkey"
|
||||
"github.com/Laisky/one-api/common/helper"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func abortWithMessage(c *gin.Context, statusCode int, message string) {
|
||||
c.JSON(statusCode, gin.H{
|
||||
"error": gin.H{
|
||||
"message": helper.MessageWithRequestId(message, c.GetString(ctxkey.RequestId)),
|
||||
"message": helper.MessageWithRequestId(message, c.GetString(helper.RequestIdKey)),
|
||||
"type": "one_api_error",
|
||||
},
|
||||
})
|
||||
|
@ -2,7 +2,7 @@ package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/Laisky/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"gorm.io/gorm"
|
||||
"sort"
|
||||
"strings"
|
||||
|
@ -5,10 +5,10 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/Laisky/errors/v2"
|
||||
"github.com/Laisky/one-api/common"
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/Laisky/one-api/common/random"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/common/random"
|
||||
"math/rand"
|
||||
"sort"
|
||||
"strconv"
|
||||
|
@ -3,9 +3,9 @@ package model
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/helper"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@ -38,6 +38,16 @@ type Channel struct {
|
||||
Config string `json:"config"`
|
||||
}
|
||||
|
||||
type ChannelConfig struct {
|
||||
Region string `json:"region,omitempty"`
|
||||
SK string `json:"sk,omitempty"`
|
||||
AK string `json:"ak,omitempty"`
|
||||
UserID string `json:"user_id,omitempty"`
|
||||
APIVersion string `json:"api_version,omitempty"`
|
||||
LibraryID string `json:"library_id,omitempty"`
|
||||
Plugin string `json:"plugin,omitempty"`
|
||||
}
|
||||
|
||||
func GetAllChannels(startIdx int, num int, scope string) ([]*Channel, error) {
|
||||
var channels []*Channel
|
||||
var err error
|
||||
@ -161,14 +171,14 @@ func (channel *Channel) Delete() error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (channel *Channel) LoadConfig() (map[string]string, error) {
|
||||
func (channel *Channel) LoadConfig() (ChannelConfig, error) {
|
||||
var cfg ChannelConfig
|
||||
if channel.Config == "" {
|
||||
return nil, nil
|
||||
return cfg, nil
|
||||
}
|
||||
cfg := make(map[string]string)
|
||||
err := json.Unmarshal([]byte(channel.Config), &cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return cfg, err
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"github.com/Laisky/one-api/common/helper"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
|
@ -3,10 +3,10 @@ package model
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/Laisky/one-api/common"
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/helper"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
|
@ -6,12 +6,12 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Laisky/one-api/common"
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/env"
|
||||
"github.com/Laisky/one-api/common/helper"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/Laisky/one-api/common/random"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/env"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/common/random"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/driver/postgres"
|
||||
|
@ -1,9 +1,9 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
billingratio "github.com/Laisky/one-api/relay/billing/ratio"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
billingratio "github.com/songquanpeng/one-api/relay/billing/ratio"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
@ -3,8 +3,8 @@ package model
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Laisky/errors/v2"
|
||||
"github.com/Laisky/one-api/common"
|
||||
"github.com/Laisky/one-api/common/helper"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
|
@ -4,11 +4,11 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/Laisky/errors/v2"
|
||||
"github.com/Laisky/one-api/common"
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/helper"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/Laisky/one-api/common/message"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/common/message"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
|
@ -5,11 +5,11 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/Laisky/errors/v2"
|
||||
"github.com/Laisky/one-api/common"
|
||||
"github.com/Laisky/one-api/common/blacklist"
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/Laisky/one-api/common/random"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/blacklist"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/common/random"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
|
@ -1,8 +1,8 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
@ -2,10 +2,10 @@ package monitor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/Laisky/one-api/common/message"
|
||||
"github.com/Laisky/one-api/model"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/common/message"
|
||||
"github.com/songquanpeng/one-api/model"
|
||||
)
|
||||
|
||||
func notifyRootUser(subject string, content string) {
|
||||
|
@ -1,8 +1,8 @@
|
||||
package monitor
|
||||
|
||||
import (
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
@ -1,7 +1,7 @@
|
||||
package monitor
|
||||
|
||||
import (
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
)
|
||||
|
||||
var store = make(map[int][]bool)
|
||||
|
@ -1,22 +1,24 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"github.com/Laisky/one-api/relay/adaptor"
|
||||
"github.com/Laisky/one-api/relay/adaptor/aiproxy"
|
||||
"github.com/Laisky/one-api/relay/adaptor/ali"
|
||||
"github.com/Laisky/one-api/relay/adaptor/anthropic"
|
||||
"github.com/Laisky/one-api/relay/adaptor/aws"
|
||||
"github.com/Laisky/one-api/relay/adaptor/baidu"
|
||||
"github.com/Laisky/one-api/relay/adaptor/cohere"
|
||||
"github.com/Laisky/one-api/relay/adaptor/coze"
|
||||
"github.com/Laisky/one-api/relay/adaptor/gemini"
|
||||
"github.com/Laisky/one-api/relay/adaptor/ollama"
|
||||
"github.com/Laisky/one-api/relay/adaptor/openai"
|
||||
"github.com/Laisky/one-api/relay/adaptor/palm"
|
||||
"github.com/Laisky/one-api/relay/adaptor/tencent"
|
||||
"github.com/Laisky/one-api/relay/adaptor/xunfei"
|
||||
"github.com/Laisky/one-api/relay/adaptor/zhipu"
|
||||
"github.com/Laisky/one-api/relay/apitype"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/aiproxy"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/ali"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/anthropic"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/aws"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/baidu"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/cloudflare"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/cohere"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/coze"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/deepl"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/gemini"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/ollama"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/palm"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/tencent"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/xunfei"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/zhipu"
|
||||
"github.com/songquanpeng/one-api/relay/apitype"
|
||||
)
|
||||
|
||||
func GetAdaptor(apiType int) adaptor.Adaptor {
|
||||
@ -49,6 +51,10 @@ func GetAdaptor(apiType int) adaptor.Adaptor {
|
||||
return &coze.Adaptor{}
|
||||
case apitype.Cohere:
|
||||
return &cohere.Adaptor{}
|
||||
case apitype.Cloudflare:
|
||||
return &cloudflare.Adaptor{}
|
||||
case apitype.DeepL:
|
||||
return &deepl.Adaptor{}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -6,18 +6,18 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/Laisky/errors/v2"
|
||||
"github.com/Laisky/one-api/common/ctxkey"
|
||||
"github.com/Laisky/one-api/relay/adaptor"
|
||||
"github.com/Laisky/one-api/relay/meta"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor"
|
||||
"github.com/songquanpeng/one-api/relay/meta"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Adaptor struct {
|
||||
meta *meta.Meta
|
||||
}
|
||||
|
||||
func (a *Adaptor) Init(meta *meta.Meta) {
|
||||
|
||||
a.meta = meta
|
||||
}
|
||||
|
||||
func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) {
|
||||
@ -35,7 +35,7 @@ func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *model.G
|
||||
return nil, errors.New("request is nil")
|
||||
}
|
||||
aiProxyLibraryRequest := ConvertRequest(*request)
|
||||
aiProxyLibraryRequest.LibraryId = c.GetString(ctxkey.ConfigLibraryID)
|
||||
aiProxyLibraryRequest.LibraryId = a.meta.Config.LibraryID
|
||||
return aiProxyLibraryRequest, nil
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
package aiproxy
|
||||
|
||||
import "github.com/Laisky/one-api/relay/adaptor/openai"
|
||||
import "github.com/songquanpeng/one-api/relay/adaptor/openai"
|
||||
|
||||
var ModelList = []string{""}
|
||||
|
||||
|
@ -9,13 +9,13 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/Laisky/one-api/common"
|
||||
"github.com/Laisky/one-api/common/helper"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/Laisky/one-api/common/random"
|
||||
"github.com/Laisky/one-api/relay/adaptor/openai"
|
||||
"github.com/Laisky/one-api/relay/constant"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/common/random"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
||||
"github.com/songquanpeng/one-api/relay/constant"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
@ -6,21 +6,21 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/Laisky/one-api/common/ctxkey"
|
||||
"github.com/Laisky/one-api/relay/adaptor"
|
||||
"github.com/Laisky/one-api/relay/meta"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/Laisky/one-api/relay/relaymode"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor"
|
||||
"github.com/songquanpeng/one-api/relay/meta"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/relay/relaymode"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// https://help.aliyun.com/zh/dashscope/developer-reference/api-details
|
||||
|
||||
type Adaptor struct {
|
||||
meta *meta.Meta
|
||||
}
|
||||
|
||||
func (a *Adaptor) Init(meta *meta.Meta) {
|
||||
|
||||
a.meta = meta
|
||||
}
|
||||
|
||||
func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) {
|
||||
@ -48,8 +48,8 @@ func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, meta *me
|
||||
if meta.Mode == relaymode.ImagesGenerations {
|
||||
req.Header.Set("X-DashScope-Async", "enable")
|
||||
}
|
||||
if c.GetString(ctxkey.ConfigPlugin) != "" {
|
||||
req.Header.Set("X-DashScope-Plugin", c.GetString(ctxkey.ConfigPlugin))
|
||||
if a.meta.Config.Plugin != "" {
|
||||
req.Header.Set("X-DashScope-Plugin", a.meta.Config.Plugin)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -10,10 +10,10 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Laisky/one-api/common/helper"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/Laisky/one-api/relay/adaptor/openai"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
@ -7,11 +7,11 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/Laisky/one-api/common"
|
||||
"github.com/Laisky/one-api/common/helper"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/Laisky/one-api/relay/adaptor/openai"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
@ -1,8 +1,8 @@
|
||||
package ali
|
||||
|
||||
import (
|
||||
"github.com/Laisky/one-api/relay/adaptor/openai"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
|
@ -6,9 +6,9 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/Laisky/errors/v2"
|
||||
"github.com/Laisky/one-api/relay/adaptor"
|
||||
"github.com/Laisky/one-api/relay/meta"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor"
|
||||
"github.com/songquanpeng/one-api/relay/meta"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
@ -8,12 +8,12 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/Laisky/one-api/common"
|
||||
"github.com/Laisky/one-api/common/helper"
|
||||
"github.com/Laisky/one-api/common/image"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/Laisky/one-api/relay/adaptor/openai"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
"github.com/songquanpeng/one-api/common/image"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@ -194,7 +194,7 @@ func StreamHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusC
|
||||
select {
|
||||
case data := <-dataChan:
|
||||
// some implementations may add \r at the end of data
|
||||
data = strings.TrimSuffix(data, "\r")
|
||||
data = strings.TrimSpace(data)
|
||||
var claudeResponse StreamResponse
|
||||
err := json.Unmarshal([]byte(data), &claudeResponse)
|
||||
if err != nil {
|
||||
|
@ -4,11 +4,14 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/Laisky/one-api/common/ctxkey"
|
||||
"github.com/Laisky/one-api/relay/adaptor"
|
||||
"github.com/Laisky/one-api/relay/adaptor/anthropic"
|
||||
"github.com/Laisky/one-api/relay/meta"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/common/ctxkey"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/anthropic"
|
||||
"github.com/songquanpeng/one-api/relay/meta"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/credentials"
|
||||
"github.com/aws/aws-sdk-go-v2/service/bedrockruntime"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
@ -16,10 +19,16 @@ import (
|
||||
var _ adaptor.Adaptor = new(Adaptor)
|
||||
|
||||
type Adaptor struct {
|
||||
meta *meta.Meta
|
||||
awsClient *bedrockruntime.Client
|
||||
}
|
||||
|
||||
func (a *Adaptor) Init(meta *meta.Meta) {
|
||||
|
||||
a.meta = meta
|
||||
a.awsClient = bedrockruntime.New(bedrockruntime.Options{
|
||||
Region: meta.Config.Region,
|
||||
Credentials: aws.NewCredentialsCache(credentials.NewStaticCredentialsProvider(meta.Config.AK, meta.Config.SK, "")),
|
||||
})
|
||||
}
|
||||
|
||||
func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) {
|
||||
@ -54,9 +63,9 @@ func (a *Adaptor) DoRequest(c *gin.Context, meta *meta.Meta, requestBody io.Read
|
||||
|
||||
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, meta *meta.Meta) (usage *model.Usage, err *model.ErrorWithStatusCode) {
|
||||
if meta.IsStream {
|
||||
err, usage = StreamHandler(c, resp)
|
||||
err, usage = StreamHandler(c, a.awsClient)
|
||||
} else {
|
||||
err, usage = Handler(c, resp, meta.PromptTokens, meta.ActualModelName)
|
||||
err, usage = Handler(c, a.awsClient, meta.ActualModelName)
|
||||
}
|
||||
return
|
||||
}
|
||||
@ -65,7 +74,6 @@ func (a *Adaptor) GetModelList() (models []string) {
|
||||
for n := range awsModelIDMap {
|
||||
models = append(models, n)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -8,14 +8,13 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/Laisky/one-api/common"
|
||||
"github.com/Laisky/one-api/common/ctxkey"
|
||||
"github.com/Laisky/one-api/common/helper"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/Laisky/one-api/relay/adaptor/anthropic"
|
||||
relaymodel "github.com/Laisky/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/ctxkey"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/anthropic"
|
||||
relaymodel "github.com/songquanpeng/one-api/relay/model"
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/credentials"
|
||||
"github.com/aws/aws-sdk-go-v2/service/bedrockruntime"
|
||||
"github.com/aws/aws-sdk-go-v2/service/bedrockruntime/types"
|
||||
"github.com/gin-gonic/gin"
|
||||
@ -23,18 +22,6 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func newAwsClient(c *gin.Context) (*bedrockruntime.Client, error) {
|
||||
ak := c.GetString(ctxkey.ConfigAK)
|
||||
sk := c.GetString(ctxkey.ConfigSK)
|
||||
region := c.GetString(ctxkey.ConfigRegion)
|
||||
client := bedrockruntime.New(bedrockruntime.Options{
|
||||
Region: region,
|
||||
Credentials: aws.NewCredentialsCache(credentials.NewStaticCredentialsProvider(ak, sk, "")),
|
||||
})
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func wrapErr(err error) *relaymodel.ErrorWithStatusCode {
|
||||
return &relaymodel.ErrorWithStatusCode{
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
@ -62,12 +49,7 @@ func awsModelID(requestModel string) (string, error) {
|
||||
return "", errors.Errorf("model %s not found", requestModel)
|
||||
}
|
||||
|
||||
func Handler(c *gin.Context, resp *http.Response, promptTokens int, modelName string) (*relaymodel.ErrorWithStatusCode, *relaymodel.Usage) {
|
||||
awsCli, err := newAwsClient(c)
|
||||
if err != nil {
|
||||
return wrapErr(errors.Wrap(err, "newAwsClient")), nil
|
||||
}
|
||||
|
||||
func Handler(c *gin.Context, awsCli *bedrockruntime.Client, modelName string) (*relaymodel.ErrorWithStatusCode, *relaymodel.Usage) {
|
||||
awsModelId, err := awsModelID(c.GetString(ctxkey.RequestModel))
|
||||
if err != nil {
|
||||
return wrapErr(errors.Wrap(err, "awsModelID")), nil
|
||||
@ -120,13 +102,8 @@ func Handler(c *gin.Context, resp *http.Response, promptTokens int, modelName st
|
||||
return nil, &usage
|
||||
}
|
||||
|
||||
func StreamHandler(c *gin.Context, resp *http.Response) (*relaymodel.ErrorWithStatusCode, *relaymodel.Usage) {
|
||||
func StreamHandler(c *gin.Context, awsCli *bedrockruntime.Client) (*relaymodel.ErrorWithStatusCode, *relaymodel.Usage) {
|
||||
createdTime := helper.GetTimestamp()
|
||||
awsCli, err := newAwsClient(c)
|
||||
if err != nil {
|
||||
return wrapErr(errors.Wrap(err, "newAwsClient")), nil
|
||||
}
|
||||
|
||||
awsModelId, err := awsModelID(c.GetString(ctxkey.RequestModel))
|
||||
if err != nil {
|
||||
return wrapErr(errors.Wrap(err, "awsModelID")), nil
|
||||
|
@ -1,6 +1,6 @@
|
||||
package aws
|
||||
|
||||
import "github.com/Laisky/one-api/relay/adaptor/anthropic"
|
||||
import "github.com/songquanpeng/one-api/relay/adaptor/anthropic"
|
||||
|
||||
// Request is the request to AWS Claude
|
||||
//
|
||||
|
@ -1,15 +0,0 @@
|
||||
package azure
|
||||
|
||||
import (
|
||||
"github.com/Laisky/one-api/common/ctxkey"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func GetAPIVersion(c *gin.Context) string {
|
||||
query := c.Request.URL.Query()
|
||||
apiVersion := query.Get("api-version")
|
||||
if apiVersion == "" {
|
||||
apiVersion = c.GetString(ctxkey.ConfigAPIVersion)
|
||||
}
|
||||
return apiVersion
|
||||
}
|
@ -7,10 +7,10 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/Laisky/one-api/relay/adaptor"
|
||||
"github.com/Laisky/one-api/relay/meta"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/Laisky/one-api/relay/relaymode"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor"
|
||||
"github.com/songquanpeng/one-api/relay/meta"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/relay/relaymode"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
@ -11,12 +11,12 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/Laisky/one-api/common"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/Laisky/one-api/relay/adaptor/openai"
|
||||
"github.com/Laisky/one-api/relay/client"
|
||||
"github.com/Laisky/one-api/relay/constant"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
||||
"github.com/songquanpeng/one-api/relay/client"
|
||||
"github.com/songquanpeng/one-api/relay/constant"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
@ -3,7 +3,7 @@ package baidu
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
)
|
||||
|
||||
type ChatResponse struct {
|
||||
|
66
relay/adaptor/cloudflare/adaptor.go
Normal file
66
relay/adaptor/cloudflare/adaptor.go
Normal file
@ -0,0 +1,66 @@
|
||||
package cloudflare
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor"
|
||||
"github.com/songquanpeng/one-api/relay/meta"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
)
|
||||
|
||||
type Adaptor struct {
|
||||
meta *meta.Meta
|
||||
}
|
||||
|
||||
// ConvertImageRequest implements adaptor.Adaptor.
|
||||
func (*Adaptor) ConvertImageRequest(request *model.ImageRequest) (any, error) {
|
||||
return nil, errors.New("not implemented")
|
||||
}
|
||||
|
||||
// ConvertImageRequest implements adaptor.Adaptor.
|
||||
|
||||
func (a *Adaptor) Init(meta *meta.Meta) {
|
||||
a.meta = meta
|
||||
}
|
||||
|
||||
func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) {
|
||||
return fmt.Sprintf("%s/client/v4/accounts/%s/ai/run/%s", meta.BaseURL, meta.Config.UserID, meta.ActualModelName), nil
|
||||
}
|
||||
|
||||
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, meta *meta.Meta) error {
|
||||
adaptor.SetupCommonRequestHeader(c, req, meta)
|
||||
req.Header.Set("Authorization", "Bearer "+meta.APIKey)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *model.GeneralOpenAIRequest) (any, error) {
|
||||
if request == nil {
|
||||
return nil, errors.New("request is nil")
|
||||
}
|
||||
return ConvertRequest(*request), nil
|
||||
}
|
||||
|
||||
func (a *Adaptor) DoRequest(c *gin.Context, meta *meta.Meta, requestBody io.Reader) (*http.Response, error) {
|
||||
return adaptor.DoRequestHelper(a, c, meta, requestBody)
|
||||
}
|
||||
|
||||
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, meta *meta.Meta) (usage *model.Usage, err *model.ErrorWithStatusCode) {
|
||||
if meta.IsStream {
|
||||
err, usage = StreamHandler(c, resp, meta.PromptTokens, meta.ActualModelName)
|
||||
} else {
|
||||
err, usage = Handler(c, resp, meta.PromptTokens, meta.ActualModelName)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (a *Adaptor) GetModelList() []string {
|
||||
return ModelList
|
||||
}
|
||||
|
||||
func (a *Adaptor) GetChannelName() string {
|
||||
return "cloudflare"
|
||||
}
|
36
relay/adaptor/cloudflare/constant.go
Normal file
36
relay/adaptor/cloudflare/constant.go
Normal file
@ -0,0 +1,36 @@
|
||||
package cloudflare
|
||||
|
||||
var ModelList = []string{
|
||||
"@cf/meta/llama-2-7b-chat-fp16",
|
||||
"@cf/meta/llama-2-7b-chat-int8",
|
||||
"@cf/mistral/mistral-7b-instruct-v0.1",
|
||||
"@hf/thebloke/deepseek-coder-6.7b-base-awq",
|
||||
"@hf/thebloke/deepseek-coder-6.7b-instruct-awq",
|
||||
"@cf/deepseek-ai/deepseek-math-7b-base",
|
||||
"@cf/deepseek-ai/deepseek-math-7b-instruct",
|
||||
"@cf/thebloke/discolm-german-7b-v1-awq",
|
||||
"@cf/tiiuae/falcon-7b-instruct",
|
||||
"@cf/google/gemma-2b-it-lora",
|
||||
"@hf/google/gemma-7b-it",
|
||||
"@cf/google/gemma-7b-it-lora",
|
||||
"@hf/nousresearch/hermes-2-pro-mistral-7b",
|
||||
"@hf/thebloke/llama-2-13b-chat-awq",
|
||||
"@cf/meta-llama/llama-2-7b-chat-hf-lora",
|
||||
"@cf/meta/llama-3-8b-instruct",
|
||||
"@hf/thebloke/llamaguard-7b-awq",
|
||||
"@hf/thebloke/mistral-7b-instruct-v0.1-awq",
|
||||
"@hf/mistralai/mistral-7b-instruct-v0.2",
|
||||
"@cf/mistral/mistral-7b-instruct-v0.2-lora",
|
||||
"@hf/thebloke/neural-chat-7b-v3-1-awq",
|
||||
"@cf/openchat/openchat-3.5-0106",
|
||||
"@hf/thebloke/openhermes-2.5-mistral-7b-awq",
|
||||
"@cf/microsoft/phi-2",
|
||||
"@cf/qwen/qwen1.5-0.5b-chat",
|
||||
"@cf/qwen/qwen1.5-1.8b-chat",
|
||||
"@cf/qwen/qwen1.5-14b-chat-awq",
|
||||
"@cf/qwen/qwen1.5-7b-chat-awq",
|
||||
"@cf/defog/sqlcoder-7b-2",
|
||||
"@hf/nexusflow/starling-lm-7b-beta",
|
||||
"@cf/tinyllama/tinyllama-1.1b-chat-v1.0",
|
||||
"@hf/thebloke/zephyr-7b-beta-awq",
|
||||
}
|
152
relay/adaptor/cloudflare/main.go
Normal file
152
relay/adaptor/cloudflare/main.go
Normal file
@ -0,0 +1,152 @@
|
||||
package cloudflare
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
)
|
||||
|
||||
func ConvertRequest(textRequest model.GeneralOpenAIRequest) *Request {
|
||||
lastMessage := textRequest.Messages[len(textRequest.Messages)-1]
|
||||
return &Request{
|
||||
MaxTokens: textRequest.MaxTokens,
|
||||
Prompt: lastMessage.StringContent(),
|
||||
Stream: textRequest.Stream,
|
||||
Temperature: textRequest.Temperature,
|
||||
}
|
||||
}
|
||||
|
||||
func ResponseCloudflare2OpenAI(cloudflareResponse *Response) *openai.TextResponse {
|
||||
choice := openai.TextResponseChoice{
|
||||
Index: 0,
|
||||
Message: model.Message{
|
||||
Role: "assistant",
|
||||
Content: cloudflareResponse.Result.Response,
|
||||
},
|
||||
FinishReason: "stop",
|
||||
}
|
||||
fullTextResponse := openai.TextResponse{
|
||||
Object: "chat.completion",
|
||||
Created: helper.GetTimestamp(),
|
||||
Choices: []openai.TextResponseChoice{choice},
|
||||
}
|
||||
return &fullTextResponse
|
||||
}
|
||||
|
||||
func StreamResponseCloudflare2OpenAI(cloudflareResponse *StreamResponse) *openai.ChatCompletionsStreamResponse {
|
||||
var choice openai.ChatCompletionsStreamResponseChoice
|
||||
choice.Delta.Content = cloudflareResponse.Response
|
||||
choice.Delta.Role = "assistant"
|
||||
openaiResponse := openai.ChatCompletionsStreamResponse{
|
||||
Object: "chat.completion.chunk",
|
||||
Choices: []openai.ChatCompletionsStreamResponseChoice{choice},
|
||||
Created: helper.GetTimestamp(),
|
||||
}
|
||||
return &openaiResponse
|
||||
}
|
||||
|
||||
func StreamHandler(c *gin.Context, resp *http.Response, promptTokens int, modelName string) (*model.ErrorWithStatusCode, *model.Usage) {
|
||||
scanner := bufio.NewScanner(resp.Body)
|
||||
scanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
||||
if atEOF && len(data) == 0 {
|
||||
return 0, nil, nil
|
||||
}
|
||||
if i := bytes.IndexByte(data, '\n'); i >= 0 {
|
||||
return i + 1, data[0:i], nil
|
||||
}
|
||||
if atEOF {
|
||||
return len(data), data, nil
|
||||
}
|
||||
return 0, nil, nil
|
||||
})
|
||||
|
||||
dataChan := make(chan string)
|
||||
stopChan := make(chan bool)
|
||||
go func() {
|
||||
for scanner.Scan() {
|
||||
data := scanner.Text()
|
||||
if len(data) < len("data: ") {
|
||||
continue
|
||||
}
|
||||
data = strings.TrimPrefix(data, "data: ")
|
||||
dataChan <- data
|
||||
}
|
||||
stopChan <- true
|
||||
}()
|
||||
common.SetEventStreamHeaders(c)
|
||||
id := helper.GetResponseID(c)
|
||||
responseModel := c.GetString("original_model")
|
||||
var responseText string
|
||||
c.Stream(func(w io.Writer) bool {
|
||||
select {
|
||||
case data := <-dataChan:
|
||||
// some implementations may add \r at the end of data
|
||||
data = strings.TrimSuffix(data, "\r")
|
||||
var cloudflareResponse StreamResponse
|
||||
err := json.Unmarshal([]byte(data), &cloudflareResponse)
|
||||
if err != nil {
|
||||
logger.SysError("error unmarshalling stream response: " + err.Error())
|
||||
return true
|
||||
}
|
||||
response := StreamResponseCloudflare2OpenAI(&cloudflareResponse)
|
||||
if response == nil {
|
||||
return true
|
||||
}
|
||||
responseText += cloudflareResponse.Response
|
||||
response.Id = id
|
||||
response.Model = responseModel
|
||||
jsonStr, err := json.Marshal(response)
|
||||
if err != nil {
|
||||
logger.SysError("error marshalling stream response: " + err.Error())
|
||||
return true
|
||||
}
|
||||
c.Render(-1, common.CustomEvent{Data: "data: " + string(jsonStr)})
|
||||
return true
|
||||
case <-stopChan:
|
||||
c.Render(-1, common.CustomEvent{Data: "data: [DONE]"})
|
||||
return false
|
||||
}
|
||||
})
|
||||
_ = resp.Body.Close()
|
||||
usage := openai.ResponseText2Usage(responseText, responseModel, promptTokens)
|
||||
return nil, usage
|
||||
}
|
||||
|
||||
func Handler(c *gin.Context, resp *http.Response, promptTokens int, modelName string) (*model.ErrorWithStatusCode, *model.Usage) {
|
||||
responseBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return openai.ErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError), nil
|
||||
}
|
||||
err = resp.Body.Close()
|
||||
if err != nil {
|
||||
return openai.ErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), nil
|
||||
}
|
||||
var cloudflareResponse Response
|
||||
err = json.Unmarshal(responseBody, &cloudflareResponse)
|
||||
if err != nil {
|
||||
return openai.ErrorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError), nil
|
||||
}
|
||||
fullTextResponse := ResponseCloudflare2OpenAI(&cloudflareResponse)
|
||||
fullTextResponse.Model = modelName
|
||||
usage := openai.ResponseText2Usage(cloudflareResponse.Result.Response, modelName, promptTokens)
|
||||
fullTextResponse.Usage = *usage
|
||||
fullTextResponse.Id = helper.GetResponseID(c)
|
||||
jsonResponse, err := json.Marshal(fullTextResponse)
|
||||
if err != nil {
|
||||
return openai.ErrorWrapper(err, "marshal_response_body_failed", http.StatusInternalServerError), nil
|
||||
}
|
||||
c.Writer.Header().Set("Content-Type", "application/json")
|
||||
c.Writer.WriteHeader(resp.StatusCode)
|
||||
_, err = c.Writer.Write(jsonResponse)
|
||||
return nil, usage
|
||||
}
|
25
relay/adaptor/cloudflare/model.go
Normal file
25
relay/adaptor/cloudflare/model.go
Normal file
@ -0,0 +1,25 @@
|
||||
package cloudflare
|
||||
|
||||
type Request struct {
|
||||
Lora string `json:"lora,omitempty"`
|
||||
MaxTokens int `json:"max_tokens,omitempty"`
|
||||
Prompt string `json:"prompt,omitempty"`
|
||||
Raw bool `json:"raw,omitempty"`
|
||||
Stream bool `json:"stream,omitempty"`
|
||||
Temperature float64 `json:"temperature,omitempty"`
|
||||
}
|
||||
|
||||
type Result struct {
|
||||
Response string `json:"response"`
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Result Result `json:"result"`
|
||||
Success bool `json:"success"`
|
||||
Errors []string `json:"errors"`
|
||||
Messages []string `json:"messages"`
|
||||
}
|
||||
|
||||
type StreamResponse struct {
|
||||
Response string `json:"response"`
|
||||
}
|
@ -6,9 +6,9 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/Laisky/one-api/relay/adaptor"
|
||||
"github.com/Laisky/one-api/relay/meta"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor"
|
||||
"github.com/songquanpeng/one-api/relay/meta"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
@ -5,3 +5,10 @@ var ModelList = []string{
|
||||
"command-light", "command-light-nightly",
|
||||
"command-r", "command-r-plus",
|
||||
}
|
||||
|
||||
func init() {
|
||||
num := len(ModelList)
|
||||
for i := 0; i < num; i++ {
|
||||
ModelList = append(ModelList, ModelList[i]+"-internet")
|
||||
}
|
||||
}
|
||||
|
@ -9,14 +9,18 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/Laisky/one-api/common"
|
||||
"github.com/Laisky/one-api/common/helper"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/Laisky/one-api/relay/adaptor/openai"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var (
|
||||
WebSearchConnector = Connector{ID: "web-search"}
|
||||
)
|
||||
|
||||
func stopReasonCohere2OpenAI(reason *string) string {
|
||||
if reason == nil {
|
||||
return ""
|
||||
@ -45,6 +49,10 @@ func ConvertRequest(textRequest model.GeneralOpenAIRequest) *Request {
|
||||
if cohereRequest.Model == "" {
|
||||
cohereRequest.Model = "command-r"
|
||||
}
|
||||
if strings.HasSuffix(cohereRequest.Model, "-internet") {
|
||||
cohereRequest.Model = strings.TrimSuffix(cohereRequest.Model, "-internet")
|
||||
cohereRequest.Connectors = append(cohereRequest.Connectors, WebSearchConnector)
|
||||
}
|
||||
for _, message := range textRequest.Messages {
|
||||
if message.Role == "user" {
|
||||
cohereRequest.Message = message.Content.(string)
|
||||
|
@ -5,9 +5,9 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/Laisky/errors/v2"
|
||||
"github.com/Laisky/one-api/common/ctxkey"
|
||||
"github.com/Laisky/one-api/relay/client"
|
||||
"github.com/Laisky/one-api/relay/meta"
|
||||
"github.com/songquanpeng/one-api/common/ctxkey"
|
||||
"github.com/songquanpeng/one-api/relay/client"
|
||||
"github.com/songquanpeng/one-api/relay/meta"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
@ -6,19 +6,19 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/Laisky/one-api/common/ctxkey"
|
||||
"github.com/Laisky/one-api/relay/adaptor"
|
||||
"github.com/Laisky/one-api/relay/adaptor/openai"
|
||||
"github.com/Laisky/one-api/relay/meta"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
||||
"github.com/songquanpeng/one-api/relay/meta"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Adaptor struct {
|
||||
meta *meta.Meta
|
||||
}
|
||||
|
||||
func (a *Adaptor) Init(meta *meta.Meta) {
|
||||
|
||||
a.meta = meta
|
||||
}
|
||||
|
||||
func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) {
|
||||
@ -35,7 +35,7 @@ func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *model.G
|
||||
if request == nil {
|
||||
return nil, errors.New("request is nil")
|
||||
}
|
||||
request.User = c.GetString(ctxkey.ConfigUserID)
|
||||
request.User = a.meta.Config.UserID
|
||||
return ConvertRequest(*request), nil
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
package coze
|
||||
|
||||
import "github.com/Laisky/one-api/relay/adaptor/coze/constant/event"
|
||||
import "github.com/songquanpeng/one-api/relay/adaptor/coze/constant/event"
|
||||
|
||||
func event2StopReason(e *string) string {
|
||||
if e == nil || *e == event.Message {
|
||||
|
@ -4,13 +4,13 @@ import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/Laisky/one-api/common"
|
||||
"github.com/Laisky/one-api/common/conv"
|
||||
"github.com/Laisky/one-api/common/helper"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/Laisky/one-api/relay/adaptor/coze/constant/messagetype"
|
||||
"github.com/Laisky/one-api/relay/adaptor/openai"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/conv"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/coze/constant/messagetype"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"net/http"
|
||||
|
73
relay/adaptor/deepl/adaptor.go
Normal file
73
relay/adaptor/deepl/adaptor.go
Normal file
@ -0,0 +1,73 @@
|
||||
package deepl
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor"
|
||||
"github.com/songquanpeng/one-api/relay/meta"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Adaptor struct {
|
||||
meta *meta.Meta
|
||||
promptText string
|
||||
}
|
||||
|
||||
func (a *Adaptor) Init(meta *meta.Meta) {
|
||||
a.meta = meta
|
||||
}
|
||||
|
||||
func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) {
|
||||
return fmt.Sprintf("%s/v2/translate", meta.BaseURL), nil
|
||||
}
|
||||
|
||||
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, meta *meta.Meta) error {
|
||||
adaptor.SetupCommonRequestHeader(c, req, meta)
|
||||
req.Header.Set("Authorization", "DeepL-Auth-Key "+meta.APIKey)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *model.GeneralOpenAIRequest) (any, error) {
|
||||
if request == nil {
|
||||
return nil, errors.New("request is nil")
|
||||
}
|
||||
convertedRequest, text := ConvertRequest(*request)
|
||||
a.promptText = text
|
||||
return convertedRequest, nil
|
||||
}
|
||||
|
||||
func (a *Adaptor) ConvertImageRequest(request *model.ImageRequest) (any, error) {
|
||||
if request == nil {
|
||||
return nil, errors.New("request is nil")
|
||||
}
|
||||
return request, nil
|
||||
}
|
||||
|
||||
func (a *Adaptor) DoRequest(c *gin.Context, meta *meta.Meta, requestBody io.Reader) (*http.Response, error) {
|
||||
return adaptor.DoRequestHelper(a, c, meta, requestBody)
|
||||
}
|
||||
|
||||
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, meta *meta.Meta) (usage *model.Usage, err *model.ErrorWithStatusCode) {
|
||||
if meta.IsStream {
|
||||
err = StreamHandler(c, resp, meta.ActualModelName)
|
||||
} else {
|
||||
err = Handler(c, resp, meta.ActualModelName)
|
||||
}
|
||||
promptTokens := len(a.promptText)
|
||||
usage = &model.Usage{
|
||||
PromptTokens: promptTokens,
|
||||
TotalTokens: promptTokens,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (a *Adaptor) GetModelList() []string {
|
||||
return ModelList
|
||||
}
|
||||
|
||||
func (a *Adaptor) GetChannelName() string {
|
||||
return "deepl"
|
||||
}
|
9
relay/adaptor/deepl/constants.go
Normal file
9
relay/adaptor/deepl/constants.go
Normal file
@ -0,0 +1,9 @@
|
||||
package deepl
|
||||
|
||||
// https://developers.deepl.com/docs/api-reference/glossaries
|
||||
|
||||
var ModelList = []string{
|
||||
"deepl-zh",
|
||||
"deepl-en",
|
||||
"deepl-ja",
|
||||
}
|
11
relay/adaptor/deepl/helper.go
Normal file
11
relay/adaptor/deepl/helper.go
Normal file
@ -0,0 +1,11 @@
|
||||
package deepl
|
||||
|
||||
import "strings"
|
||||
|
||||
func parseLangFromModelName(modelName string) string {
|
||||
parts := strings.Split(modelName, "-")
|
||||
if len(parts) == 1 {
|
||||
return "ZH"
|
||||
}
|
||||
return parts[1]
|
||||
}
|
137
relay/adaptor/deepl/main.go
Normal file
137
relay/adaptor/deepl/main.go
Normal file
@ -0,0 +1,137 @@
|
||||
package deepl
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
||||
"github.com/songquanpeng/one-api/relay/constant"
|
||||
"github.com/songquanpeng/one-api/relay/constant/finishreason"
|
||||
"github.com/songquanpeng/one-api/relay/constant/role"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// https://developers.deepl.com/docs/getting-started/your-first-api-request
|
||||
|
||||
func ConvertRequest(textRequest model.GeneralOpenAIRequest) (*Request, string) {
|
||||
var text string
|
||||
if len(textRequest.Messages) != 0 {
|
||||
text = textRequest.Messages[len(textRequest.Messages)-1].StringContent()
|
||||
}
|
||||
deeplRequest := Request{
|
||||
TargetLang: parseLangFromModelName(textRequest.Model),
|
||||
Text: []string{text},
|
||||
}
|
||||
return &deeplRequest, text
|
||||
}
|
||||
|
||||
func StreamResponseDeepL2OpenAI(deeplResponse *Response) *openai.ChatCompletionsStreamResponse {
|
||||
var choice openai.ChatCompletionsStreamResponseChoice
|
||||
if len(deeplResponse.Translations) != 0 {
|
||||
choice.Delta.Content = deeplResponse.Translations[0].Text
|
||||
}
|
||||
choice.Delta.Role = role.Assistant
|
||||
choice.FinishReason = &constant.StopFinishReason
|
||||
openaiResponse := openai.ChatCompletionsStreamResponse{
|
||||
Object: constant.StreamObject,
|
||||
Created: helper.GetTimestamp(),
|
||||
Choices: []openai.ChatCompletionsStreamResponseChoice{choice},
|
||||
}
|
||||
return &openaiResponse
|
||||
}
|
||||
|
||||
func ResponseDeepL2OpenAI(deeplResponse *Response) *openai.TextResponse {
|
||||
var responseText string
|
||||
if len(deeplResponse.Translations) != 0 {
|
||||
responseText = deeplResponse.Translations[0].Text
|
||||
}
|
||||
choice := openai.TextResponseChoice{
|
||||
Index: 0,
|
||||
Message: model.Message{
|
||||
Role: role.Assistant,
|
||||
Content: responseText,
|
||||
Name: nil,
|
||||
},
|
||||
FinishReason: finishreason.Stop,
|
||||
}
|
||||
fullTextResponse := openai.TextResponse{
|
||||
Object: constant.NonStreamObject,
|
||||
Created: helper.GetTimestamp(),
|
||||
Choices: []openai.TextResponseChoice{choice},
|
||||
}
|
||||
return &fullTextResponse
|
||||
}
|
||||
|
||||
func StreamHandler(c *gin.Context, resp *http.Response, modelName string) *model.ErrorWithStatusCode {
|
||||
responseBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return openai.ErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError)
|
||||
}
|
||||
err = resp.Body.Close()
|
||||
if err != nil {
|
||||
return openai.ErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError)
|
||||
}
|
||||
var deeplResponse Response
|
||||
err = json.Unmarshal(responseBody, &deeplResponse)
|
||||
if err != nil {
|
||||
return openai.ErrorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError)
|
||||
}
|
||||
fullTextResponse := StreamResponseDeepL2OpenAI(&deeplResponse)
|
||||
fullTextResponse.Model = modelName
|
||||
fullTextResponse.Id = helper.GetResponseID(c)
|
||||
jsonData, err := json.Marshal(fullTextResponse)
|
||||
if err != nil {
|
||||
return openai.ErrorWrapper(err, "marshal_response_body_failed", http.StatusInternalServerError)
|
||||
}
|
||||
common.SetEventStreamHeaders(c)
|
||||
c.Stream(func(w io.Writer) bool {
|
||||
if jsonData != nil {
|
||||
c.Render(-1, common.CustomEvent{Data: "data: " + string(jsonData)})
|
||||
jsonData = nil
|
||||
return true
|
||||
}
|
||||
c.Render(-1, common.CustomEvent{Data: "data: [DONE]"})
|
||||
return false
|
||||
})
|
||||
_ = resp.Body.Close()
|
||||
return nil
|
||||
}
|
||||
|
||||
func Handler(c *gin.Context, resp *http.Response, modelName string) *model.ErrorWithStatusCode {
|
||||
responseBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return openai.ErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError)
|
||||
}
|
||||
err = resp.Body.Close()
|
||||
if err != nil {
|
||||
return openai.ErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError)
|
||||
}
|
||||
var deeplResponse Response
|
||||
err = json.Unmarshal(responseBody, &deeplResponse)
|
||||
if err != nil {
|
||||
return openai.ErrorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError)
|
||||
}
|
||||
if deeplResponse.Message != "" {
|
||||
return &model.ErrorWithStatusCode{
|
||||
Error: model.Error{
|
||||
Message: deeplResponse.Message,
|
||||
Code: "deepl_error",
|
||||
},
|
||||
StatusCode: resp.StatusCode,
|
||||
}
|
||||
}
|
||||
fullTextResponse := ResponseDeepL2OpenAI(&deeplResponse)
|
||||
fullTextResponse.Model = modelName
|
||||
fullTextResponse.Id = helper.GetResponseID(c)
|
||||
jsonResponse, err := json.Marshal(fullTextResponse)
|
||||
if err != nil {
|
||||
return openai.ErrorWrapper(err, "marshal_response_body_failed", http.StatusInternalServerError)
|
||||
}
|
||||
c.Writer.Header().Set("Content-Type", "application/json")
|
||||
c.Writer.WriteHeader(resp.StatusCode)
|
||||
_, err = c.Writer.Write(jsonResponse)
|
||||
return nil
|
||||
}
|
16
relay/adaptor/deepl/model.go
Normal file
16
relay/adaptor/deepl/model.go
Normal file
@ -0,0 +1,16 @@
|
||||
package deepl
|
||||
|
||||
type Request struct {
|
||||
Text []string `json:"text"`
|
||||
TargetLang string `json:"target_lang"`
|
||||
}
|
||||
|
||||
type Translation struct {
|
||||
DetectedSourceLanguage string `json:"detected_source_language,omitempty"`
|
||||
Text string `json:"text,omitempty"`
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Translations []Translation `json:"translations,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
}
|
@ -6,12 +6,12 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/Laisky/errors/v2"
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/helper"
|
||||
channelhelper "github.com/Laisky/one-api/relay/adaptor"
|
||||
"github.com/Laisky/one-api/relay/adaptor/openai"
|
||||
"github.com/Laisky/one-api/relay/meta"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
channelhelper "github.com/songquanpeng/one-api/relay/adaptor"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
||||
"github.com/songquanpeng/one-api/relay/meta"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@ -23,10 +23,10 @@ func (a *Adaptor) Init(meta *meta.Meta) {
|
||||
}
|
||||
|
||||
func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) {
|
||||
version := helper.AssignOrDefault(meta.APIVersion, config.GeminiVersion)
|
||||
version := helper.AssignOrDefault(meta.Config.APIVersion, config.GeminiVersion)
|
||||
action := "generateContent"
|
||||
if meta.IsStream {
|
||||
action = "streamGenerateContent"
|
||||
action = "streamGenerateContent?alt=sse"
|
||||
}
|
||||
return fmt.Sprintf("%s/%s/models/%s:%s?key=%s", meta.BaseURL, version, meta.ActualModelName, action, meta.APIKey), nil
|
||||
}
|
||||
|
@ -8,15 +8,15 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/Laisky/one-api/common"
|
||||
"github.com/Laisky/one-api/common/config"
|
||||
"github.com/Laisky/one-api/common/helper"
|
||||
"github.com/Laisky/one-api/common/image"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/Laisky/one-api/common/random"
|
||||
"github.com/Laisky/one-api/relay/adaptor/openai"
|
||||
"github.com/Laisky/one-api/relay/constant"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
"github.com/songquanpeng/one-api/common/image"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/common/random"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
||||
"github.com/songquanpeng/one-api/relay/constant"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@ -231,8 +231,6 @@ func streamResponseGeminiChat2OpenAI(geminiResponse *ChatResponse) *openai.ChatC
|
||||
|
||||
func StreamHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusCode, string) {
|
||||
responseText := ""
|
||||
dataChan := make(chan string)
|
||||
stopChan := make(chan bool)
|
||||
scanner := bufio.NewScanner(resp.Body)
|
||||
scanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
||||
if atEOF && len(data) == 0 {
|
||||
@ -246,14 +244,16 @@ func StreamHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusC
|
||||
}
|
||||
return 0, nil, nil
|
||||
})
|
||||
dataChan := make(chan string)
|
||||
stopChan := make(chan bool)
|
||||
go func() {
|
||||
for scanner.Scan() {
|
||||
data := scanner.Text()
|
||||
data = strings.TrimSpace(data)
|
||||
if !strings.HasPrefix(data, "\"text\": \"") {
|
||||
if !strings.HasPrefix(data, "data: ") {
|
||||
continue
|
||||
}
|
||||
data = strings.TrimPrefix(data, "\"text\": \"")
|
||||
data = strings.TrimPrefix(data, "data: ")
|
||||
data = strings.TrimSuffix(data, "\"")
|
||||
dataChan <- data
|
||||
}
|
||||
@ -263,23 +263,17 @@ func StreamHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusC
|
||||
c.Stream(func(w io.Writer) bool {
|
||||
select {
|
||||
case data := <-dataChan:
|
||||
// this is used to prevent annoying \ related format bug
|
||||
data = fmt.Sprintf("{\"content\": \"%s\"}", data)
|
||||
type dummyStruct struct {
|
||||
Content string `json:"content"`
|
||||
var geminiResponse ChatResponse
|
||||
err := json.Unmarshal([]byte(data), &geminiResponse)
|
||||
if err != nil {
|
||||
logger.SysError("error unmarshalling stream response: " + err.Error())
|
||||
return true
|
||||
}
|
||||
var dummy dummyStruct
|
||||
err := json.Unmarshal([]byte(data), &dummy)
|
||||
responseText += dummy.Content
|
||||
var choice openai.ChatCompletionsStreamResponseChoice
|
||||
choice.Delta.Content = dummy.Content
|
||||
response := openai.ChatCompletionsStreamResponse{
|
||||
Id: fmt.Sprintf("chatcmpl-%s", random.GetUUID()),
|
||||
Object: "chat.completion.chunk",
|
||||
Created: helper.GetTimestamp(),
|
||||
Model: "gemini-pro",
|
||||
Choices: []openai.ChatCompletionsStreamResponseChoice{choice},
|
||||
response := streamResponseGeminiChat2OpenAI(&geminiResponse)
|
||||
if response == nil {
|
||||
return true
|
||||
}
|
||||
responseText += response.Choices[0].Delta.StringContent()
|
||||
jsonResponse, err := json.Marshal(response)
|
||||
if err != nil {
|
||||
logger.SysError("error marshalling stream response: " + err.Error())
|
||||
|
@ -1,8 +1,8 @@
|
||||
package adaptor
|
||||
|
||||
import (
|
||||
"github.com/Laisky/one-api/relay/meta"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/relay/meta"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"net/http"
|
||||
|
@ -2,8 +2,8 @@ package minimax
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Laisky/one-api/relay/meta"
|
||||
"github.com/Laisky/one-api/relay/relaymode"
|
||||
"github.com/songquanpeng/one-api/relay/meta"
|
||||
"github.com/songquanpeng/one-api/relay/relaymode"
|
||||
)
|
||||
|
||||
func GetRequestURL(meta *meta.Meta) (string, error) {
|
||||
|
@ -6,10 +6,10 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/Laisky/errors/v2"
|
||||
"github.com/Laisky/one-api/relay/adaptor"
|
||||
"github.com/Laisky/one-api/relay/meta"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/Laisky/one-api/relay/relaymode"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor"
|
||||
"github.com/songquanpeng/one-api/relay/meta"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/relay/relaymode"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
@ -1,5 +1,11 @@
|
||||
package ollama
|
||||
|
||||
var ModelList = []string{
|
||||
"codellama:7b-instruct",
|
||||
"llama2:7b",
|
||||
"llama2:latest",
|
||||
"llama3:latest",
|
||||
"phi3:latest",
|
||||
"qwen:0.5b-chat",
|
||||
"qwen:7b",
|
||||
}
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user