Merge commit '1c2654320e5b6268b13b6efca40ce37a523d032b'

This commit is contained in:
Laisky.Cai 2024-04-28 01:53:31 +00:00
commit ca23dd9a97
162 changed files with 1351 additions and 731 deletions

View File

@ -44,7 +44,7 @@ jobs:
- name: Build Backend (amd64) - name: Build Backend (amd64)
run: | run: |
go mod download 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) - name: Build Backend (arm64)
run: | run: |

View File

@ -44,7 +44,7 @@ jobs:
- name: Build Backend - name: Build Backend
run: | run: |
go mod download 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 - name: Release
uses: softprops/action-gh-release@v1 uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/') if: startsWith(github.ref, 'refs/tags/')

View File

@ -47,7 +47,7 @@ jobs:
- name: Build Backend - name: Build Backend
run: | run: |
go mod download 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 - name: Release
uses: softprops/action-gh-release@v1 uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/') if: startsWith(github.ref, 'refs/tags/')

View File

@ -23,7 +23,7 @@ ADD go.mod go.sum ./
RUN go mod download RUN go mod download
COPY . . COPY . .
COPY --from=builder /web/build ./web/build 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 FROM debian:bullseye

View File

@ -3,7 +3,7 @@
</p> </p>
<p align="center"> <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> </p>
<div align="center"> <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"> <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"> <img src="https://img.shields.io/github/license/songquanpeng/one-api?color=brightgreen" alt="license">
</a> </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"> <img src="https://img.shields.io/github/v/release/songquanpeng/one-api?color=brightgreen&include_prereleases" alt="release">
</a> </a>
<a href="https://hub.docker.com/repository/docker/justsong/one-api"> <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"> <img src="https://img.shields.io/docker/pulls/justsong/one-api?color=brightgreen" alt="docker pull">
</a> </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"> <img src="https://img.shields.io/github/downloads/songquanpeng/one-api/total?color=brightgreen&include_prereleases" alt="release">
</a> </a>
<a href="https://goreportcard.com/report/github.com/Laisky/one-api"> <a href="https://goreportcard.com/report/github.com/songquanpeng/one-api">
<img src="https://goreportcard.com/badge/github.com/Laisky/one-api" alt="GoReportCard"> <img src="https://goreportcard.com/badge/github.com/songquanpeng/one-api" alt="GoReportCard">
</a> </a>
</p> </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="#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> <a href="#screenshots">Screenshots</a>
· ·
@ -129,9 +129,9 @@ sudo service nginx restart
The initial account username is `root` and password is `123456`. The initial account username is `root` and password is `123456`.
### Manual Deployment ### 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 ```shell
git clone https://github.com/Laisky/one-api.git git clone https://github.com/songquanpeng/one-api.git
# Build the frontend # Build the frontend
cd one-api/web/default 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. Please refer to the [environment variables](#environment-variables) section for details on using environment variables.
### Deployment on Control Panels (e.g., Baota) ### 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 ### Deployment on Third-Party Platforms
<details> <details>

View File

@ -3,7 +3,7 @@
</p> </p>
<p align="center"> <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> </p>
<div align="center"> <div align="center">
@ -18,17 +18,17 @@ _✨ 標準的な OpenAI API フォーマットを通じてすべての LLM に
<a href="https://raw.githubusercontent.com/songquanpeng/one-api/main/LICENSE"> <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"> <img src="https://img.shields.io/github/license/songquanpeng/one-api?color=brightgreen" alt="license">
</a> </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"> <img src="https://img.shields.io/github/v/release/songquanpeng/one-api?color=brightgreen&include_prereleases" alt="release">
</a> </a>
<a href="https://hub.docker.com/repository/docker/justsong/one-api"> <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"> <img src="https://img.shields.io/docker/pulls/justsong/one-api?color=brightgreen" alt="docker pull">
</a> </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"> <img src="https://img.shields.io/github/downloads/songquanpeng/one-api/total?color=brightgreen&include_prereleases" alt="release">
</a> </a>
<a href="https://goreportcard.com/report/github.com/Laisky/one-api"> <a href="https://goreportcard.com/report/github.com/songquanpeng/one-api">
<img src="https://goreportcard.com/badge/github.com/Laisky/one-api" alt="GoReportCard"> <img src="https://goreportcard.com/badge/github.com/songquanpeng/one-api" alt="GoReportCard">
</a> </a>
</p> </p>
@ -37,7 +37,7 @@ _✨ 標準的な OpenAI API フォーマットを通じてすべての LLM に
· ·
<a href="#usage">使用方法</a> <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> <a href="#screenshots">スクリーンショット</a>
· ·
@ -130,9 +130,9 @@ sudo service nginx restart
初期アカウントのユーザー名は `root` で、パスワードは `123456` です。 初期アカウントのユーザー名は `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 ```shell
git clone https://github.com/Laisky/one-api.git git clone https://github.com/songquanpeng/one-api.git
# フロントエンドのビルド # フロントエンドのビルド
cd one-api/web/default 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. Please refer to the [environment variables](#environment-variables) section for details on using environment variables.
### コントロールパネル(例: Baotaへの展開 ### コントロールパネル(例: 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> <details>

View File

@ -10,7 +10,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/Laisky/one-api/common/env" "github.com/songquanpeng/one-api/common/env"
) )
func init() { func init() {

View File

@ -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"
)

View File

@ -1,6 +1,7 @@
package ctxkey package ctxkey
const ( const (
Config = "config"
Id = "id" Id = "id"
RequestId = "X-Oneapi-Request-Id" RequestId = "X-Oneapi-Request-Id"
Username = "username" Username = "username"

View File

@ -1,7 +1,7 @@
package common package common
import ( import (
"github.com/Laisky/one-api/common/env" "github.com/songquanpeng/one-api/common/env"
) )
var UsingSQLite = false var UsingSQLite = false

View File

@ -10,7 +10,8 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/Laisky/one-api/common/random" "github.com/songquanpeng/one-api/common/random"
"github.com/gin-gonic/gin"
) )
func OpenBrowser(url string) { func OpenBrowser(url string) {
@ -106,6 +107,11 @@ func GenRequestID() string {
return GetTimeString() + random.GetRandomNumberString(8) 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 { func Max(a int, b int) int {
if a >= b { if a >= b {
return a return a

5
common/helper/key.go Normal file
View File

@ -0,0 +1,5 @@
package helper
const (
RequestIdKey = "X-Oneapi-Request-Id"
)

View File

@ -12,7 +12,7 @@ import (
"strings" "strings"
"testing" "testing"
img "github.com/Laisky/one-api/common/image" img "github.com/songquanpeng/one-api/common/image"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
_ "golang.org/x/image/webp" _ "golang.org/x/image/webp"

View File

@ -3,8 +3,8 @@ package common
import ( import (
"flag" "flag"
"fmt" "fmt"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
@ -20,7 +20,7 @@ var (
func printHelp() { func printHelp() {
fmt.Println("One API " + Version + " - All in one API service for OpenAI API.") fmt.Println("One API " + Version + " - All in one API service for OpenAI API.")
fmt.Println("Copyright (C) 2023 JustSong. All rights reserved.") 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]") fmt.Println("Usage: one-api [--port <port>] [--log-dir <log directory>] [--version] [--help]")
} }

View File

@ -10,9 +10,8 @@ import (
"sync" "sync"
"time" "time"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/ctxkey" "github.com/songquanpeng/one-api/common/helper"
"github.com/Laisky/one-api/common/helper"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -88,7 +87,7 @@ func logHelper(ctx context.Context, level string, msg string) {
if level == loggerINFO { if level == loggerINFO {
writer = gin.DefaultWriter writer = gin.DefaultWriter
} }
id := ctx.Value(ctxkey.RequestId) id := ctx.Value(helper.RequestIdKey)
if id == nil { if id == nil {
id = helper.GenRequestID() id = helper.GenRequestID()
} }

View File

@ -10,7 +10,7 @@ import (
"time" "time"
"github.com/Laisky/errors/v2" "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 { func SendEmail(subject string, receiver string, content string) error {

View File

@ -2,7 +2,7 @@ package message
import ( import (
"github.com/Laisky/errors/v2" "github.com/Laisky/errors/v2"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
) )
const ( const (

View File

@ -4,7 +4,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"github.com/Laisky/errors/v2" "github.com/Laisky/errors/v2"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"net/http" "net/http"
) )

View File

@ -3,7 +3,7 @@ package network
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"net" "net"
"strings" "strings"
) )

View File

@ -2,7 +2,7 @@ package common
import ( import (
"context" "context"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
"os" "os"
"time" "time"

View File

@ -2,7 +2,7 @@ package common
import ( import (
"fmt" "fmt"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
) )
func LogQuota(quota int64) string { func LogQuota(quota int64) string {

View File

@ -5,11 +5,11 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/Laisky/errors/v2" "github.com/Laisky/errors/v2"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"github.com/Laisky/one-api/common/random" "github.com/songquanpeng/one-api/common/random"
"github.com/Laisky/one-api/controller" "github.com/songquanpeng/one-api/controller"
"github.com/Laisky/one-api/model" "github.com/songquanpeng/one-api/model"
"github.com/gin-contrib/sessions" "github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http" "net/http"

View File

@ -9,10 +9,10 @@ import (
"time" "time"
"github.com/Laisky/errors/v2" "github.com/Laisky/errors/v2"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"github.com/Laisky/one-api/controller" "github.com/songquanpeng/one-api/controller"
"github.com/Laisky/one-api/model" "github.com/songquanpeng/one-api/model"
"github.com/gin-contrib/sessions" "github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )

View File

@ -8,10 +8,10 @@ import (
"time" "time"
"github.com/Laisky/errors/v2" "github.com/Laisky/errors/v2"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/ctxkey" "github.com/songquanpeng/one-api/common/ctxkey"
"github.com/Laisky/one-api/controller" "github.com/songquanpeng/one-api/controller"
"github.com/Laisky/one-api/model" "github.com/songquanpeng/one-api/model"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )

View File

@ -1,10 +1,10 @@
package controller package controller
import ( import (
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/ctxkey" "github.com/songquanpeng/one-api/common/ctxkey"
"github.com/Laisky/one-api/model" "github.com/songquanpeng/one-api/model"
relaymodel "github.com/Laisky/one-api/relay/model" relaymodel "github.com/songquanpeng/one-api/relay/model"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )

View File

@ -9,17 +9,17 @@ import (
"time" "time"
"github.com/Laisky/errors/v2" "github.com/Laisky/errors/v2"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"github.com/Laisky/one-api/model" "github.com/songquanpeng/one-api/model"
"github.com/Laisky/one-api/monitor" "github.com/songquanpeng/one-api/monitor"
"github.com/Laisky/one-api/relay/channeltype" "github.com/songquanpeng/one-api/relay/channeltype"
"github.com/Laisky/one-api/relay/client" "github.com/songquanpeng/one-api/relay/client"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
// https://github.com/Laisky/one-api/issues/79 // https://github.com/songquanpeng/one-api/issues/79
type OpenAISubscriptionResponse struct { type OpenAISubscriptionResponse struct {
Object string `json:"object"` Object string `json:"object"`

View File

@ -4,6 +4,21 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "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" "io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -12,22 +27,6 @@ import (
"strings" "strings"
"sync" "sync"
"time" "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 { 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.Request.Header.Set("Content-Type", "application/json")
c.Set(ctxkey.Channel, channel.Type) c.Set(ctxkey.Channel, channel.Type)
c.Set(ctxkey.BaseURL, channel.GetBaseURL()) c.Set(ctxkey.BaseURL, channel.GetBaseURL())
cfg, _ := channel.LoadConfig()
c.Set(ctxkey.Config, cfg)
middleware.SetupContextForSelectedChannel(c, channel, "") middleware.SetupContextForSelectedChannel(c, channel, "")
meta := meta.GetByContext(c) meta := meta.GetByContext(c)
apiType := channeltype.ToAPIType(channel.Type) apiType := channeltype.ToAPIType(channel.Type)
@ -67,6 +68,7 @@ func testChannel(channel *model.Channel) (err error, openaiErr *relaymodel.Error
adaptor.Init(meta) adaptor.Init(meta)
var modelName string var modelName string
modelList := adaptor.GetModelList() modelList := adaptor.GetModelList()
modelMap := channel.GetModelMapping()
if len(modelList) != 0 { if len(modelList) != 0 {
modelName = modelList[0] modelName = modelList[0]
} }
@ -75,6 +77,9 @@ func testChannel(channel *model.Channel) (err error, openaiErr *relaymodel.Error
if len(modelNames) > 0 { if len(modelNames) > 0 {
modelName = modelNames[0] modelName = modelNames[0]
} }
if modelMap != nil && modelMap[modelName] != "" {
modelName = modelMap[modelName]
}
} }
request := buildTestRequest() request := buildTestRequest()
request.Model = modelName request.Model = modelName

View File

@ -1,9 +1,9 @@
package controller package controller
import ( import (
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/helper" "github.com/songquanpeng/one-api/common/helper"
"github.com/Laisky/one-api/model" "github.com/songquanpeng/one-api/model"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http" "net/http"
"strconv" "strconv"

View File

@ -1,7 +1,7 @@
package controller package controller
import ( import (
billingratio "github.com/Laisky/one-api/relay/billing/ratio" billingratio "github.com/songquanpeng/one-api/relay/billing/ratio"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http" "net/http"
) )

View File

@ -4,9 +4,9 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/ctxkey" "github.com/songquanpeng/one-api/common/ctxkey"
"github.com/Laisky/one-api/model" "github.com/songquanpeng/one-api/model"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )

View File

@ -3,10 +3,10 @@ package controller
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/Laisky/one-api/common" "github.com/songquanpeng/one-api/common"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/message" "github.com/songquanpeng/one-api/common/message"
"github.com/Laisky/one-api/model" "github.com/songquanpeng/one-api/model"
"net/http" "net/http"
"strings" "strings"

View File

@ -5,14 +5,14 @@ import (
"net/http" "net/http"
"strings" "strings"
"github.com/Laisky/one-api/common/ctxkey" "github.com/songquanpeng/one-api/common/ctxkey"
"github.com/Laisky/one-api/model" "github.com/songquanpeng/one-api/model"
relay "github.com/Laisky/one-api/relay" relay "github.com/songquanpeng/one-api/relay"
"github.com/Laisky/one-api/relay/adaptor/openai" "github.com/songquanpeng/one-api/relay/adaptor/openai"
"github.com/Laisky/one-api/relay/apitype" "github.com/songquanpeng/one-api/relay/apitype"
"github.com/Laisky/one-api/relay/channeltype" "github.com/songquanpeng/one-api/relay/channeltype"
"github.com/Laisky/one-api/relay/meta" "github.com/songquanpeng/one-api/relay/meta"
relaymodel "github.com/Laisky/one-api/relay/model" relaymodel "github.com/songquanpeng/one-api/relay/model"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )

View File

@ -2,9 +2,9 @@ package controller
import ( import (
"encoding/json" "encoding/json"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/helper" "github.com/songquanpeng/one-api/common/helper"
"github.com/Laisky/one-api/model" "github.com/songquanpeng/one-api/model"
"net/http" "net/http"
"strings" "strings"

View File

@ -4,11 +4,11 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/ctxkey" "github.com/songquanpeng/one-api/common/ctxkey"
"github.com/Laisky/one-api/common/helper" "github.com/songquanpeng/one-api/common/helper"
"github.com/Laisky/one-api/common/random" "github.com/songquanpeng/one-api/common/random"
"github.com/Laisky/one-api/model" "github.com/songquanpeng/one-api/model"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )

View File

@ -7,17 +7,17 @@ import (
"io" "io"
"net/http" "net/http"
"github.com/Laisky/one-api/common" "github.com/songquanpeng/one-api/common"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/ctxkey" "github.com/songquanpeng/one-api/common/ctxkey"
"github.com/Laisky/one-api/common/helper" "github.com/songquanpeng/one-api/common/helper"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"github.com/Laisky/one-api/middleware" "github.com/songquanpeng/one-api/middleware"
dbmodel "github.com/Laisky/one-api/model" dbmodel "github.com/songquanpeng/one-api/model"
"github.com/Laisky/one-api/monitor" "github.com/songquanpeng/one-api/monitor"
"github.com/Laisky/one-api/relay/controller" "github.com/songquanpeng/one-api/relay/controller"
"github.com/Laisky/one-api/relay/model" "github.com/songquanpeng/one-api/relay/model"
"github.com/Laisky/one-api/relay/relaymode" "github.com/songquanpeng/one-api/relay/relaymode"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -55,9 +55,9 @@ func Relay(c *gin.Context) {
channelName := c.GetString(ctxkey.ChannelName) channelName := c.GetString(ctxkey.ChannelName)
group := c.GetString(ctxkey.Group) group := c.GetString(ctxkey.Group)
originalModel := c.GetString(ctxkey.OriginalModel) originalModel := c.GetString(ctxkey.OriginalModel)
// bizErr is shared, should not run this function in goroutine to avoid race // BUG: bizErr is shared, should not run this function in goroutine to avoid race
processChannelRelayError(ctx, channelId, channelName, bizErr) go processChannelRelayError(ctx, channelId, channelName, bizErr)
requestId := c.GetString(ctxkey.RequestId) requestId := c.GetString(helper.RequestIdKey)
retryTimes := config.RetryTimes retryTimes := config.RetryTimes
if err := shouldRetry(c, bizErr.StatusCode); err != nil { if err := shouldRetry(c, bizErr.StatusCode); err != nil {
logger.Errorf(ctx, "relay error happen, won't retry since of %v", err.Error()) logger.Errorf(ctx, "relay error happen, won't retry since of %v", err.Error())

View File

@ -5,13 +5,13 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"github.com/Laisky/one-api/common" "github.com/songquanpeng/one-api/common"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/ctxkey" "github.com/songquanpeng/one-api/common/ctxkey"
"github.com/Laisky/one-api/common/helper" "github.com/songquanpeng/one-api/common/helper"
"github.com/Laisky/one-api/common/network" "github.com/songquanpeng/one-api/common/network"
"github.com/Laisky/one-api/common/random" "github.com/songquanpeng/one-api/common/random"
"github.com/Laisky/one-api/model" "github.com/songquanpeng/one-api/model"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/jinzhu/copier" "github.com/jinzhu/copier"
) )

View File

@ -7,11 +7,11 @@ import (
"strconv" "strconv"
"time" "time"
"github.com/Laisky/one-api/common" "github.com/songquanpeng/one-api/common"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/ctxkey" "github.com/songquanpeng/one-api/common/ctxkey"
"github.com/Laisky/one-api/common/random" "github.com/songquanpeng/one-api/common/random"
"github.com/Laisky/one-api/model" "github.com/songquanpeng/one-api/model"
"github.com/gin-contrib/sessions" "github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )

2
go.mod
View File

@ -1,4 +1,4 @@
module github.com/Laisky/one-api module github.com/songquanpeng/one-api
// +heroku goVersion go1.18 // +heroku goVersion go1.18
go 1.21 go 1.21

16
main.go
View File

@ -7,14 +7,14 @@ import (
"os" "os"
"strconv" "strconv"
"github.com/Laisky/one-api/common" "github.com/songquanpeng/one-api/common"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"github.com/Laisky/one-api/controller" "github.com/songquanpeng/one-api/controller"
"github.com/Laisky/one-api/middleware" "github.com/songquanpeng/one-api/middleware"
"github.com/Laisky/one-api/model" "github.com/songquanpeng/one-api/model"
"github.com/Laisky/one-api/relay/adaptor/openai" "github.com/songquanpeng/one-api/relay/adaptor/openai"
"github.com/Laisky/one-api/router" "github.com/songquanpeng/one-api/router"
"github.com/gin-contrib/sessions" "github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie" "github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"

View File

@ -5,11 +5,11 @@ import (
"net/http" "net/http"
"strings" "strings"
"github.com/Laisky/one-api/common/blacklist" "github.com/songquanpeng/one-api/common/blacklist"
"github.com/Laisky/one-api/common/ctxkey" "github.com/songquanpeng/one-api/common/ctxkey"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"github.com/Laisky/one-api/common/network" "github.com/songquanpeng/one-api/common/network"
"github.com/Laisky/one-api/model" "github.com/songquanpeng/one-api/model"
"github.com/gin-contrib/sessions" "github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )

View File

@ -7,11 +7,11 @@ import (
"strings" "strings"
gutils "github.com/Laisky/go-utils/v4" gutils "github.com/Laisky/go-utils/v4"
"github.com/Laisky/one-api/common/ctxkey" "github.com/songquanpeng/one-api/common/ctxkey"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"github.com/Laisky/one-api/model" "github.com/songquanpeng/one-api/model"
"github.com/Laisky/one-api/relay/billing/ratio" "github.com/songquanpeng/one-api/relay/billing/ratio"
"github.com/Laisky/one-api/relay/channeltype" "github.com/songquanpeng/one-api/relay/channeltype"
"github.com/gin-gonic/gin" "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.Set(ctxkey.OriginalModel, modelName) // for retry
c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", channel.Key)) c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", channel.Key))
c.Set(ctxkey.BaseURL, channel.GetBaseURL()) c.Set(ctxkey.BaseURL, channel.GetBaseURL())
cfg, _ := channel.LoadConfig()
// this is for backward compatibility // this is for backward compatibility
switch channel.Type { switch channel.Type {
case channeltype.Azure: case channeltype.Azure:
c.Set(ctxkey.ConfigAPIVersion, channel.Other) if cfg.APIVersion == "" {
cfg.APIVersion = channel.Other
}
case channeltype.Xunfei: case channeltype.Xunfei:
c.Set(ctxkey.ConfigAPIVersion, channel.Other) if cfg.APIVersion == "" {
cfg.APIVersion = channel.Other
}
case channeltype.Gemini: case channeltype.Gemini:
c.Set(ctxkey.ConfigAPIVersion, channel.Other) if cfg.APIVersion == "" {
cfg.APIVersion = channel.Other
}
case channeltype.AIProxyLibrary: case channeltype.AIProxyLibrary:
c.Set(ctxkey.ConfigLibraryID, channel.Other) if cfg.LibraryID == "" {
cfg.LibraryID = channel.Other
}
case channeltype.Ali: case channeltype.Ali:
c.Set(ctxkey.ConfigPlugin, channel.Other) if cfg.Plugin == "" {
} cfg.Plugin = channel.Other
cfg, _ := channel.LoadConfig() }
for k, v := range cfg {
c.Set(ctxkey.ConfigPrefix+k, v)
} }
c.Set(ctxkey.Config, cfg)
} }

View File

@ -3,7 +3,7 @@ package middleware
import ( import (
"fmt" "fmt"
"github.com/Laisky/one-api/common/ctxkey" "github.com/songquanpeng/one-api/common/helper"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -11,7 +11,7 @@ func SetUpLogger(server *gin.Engine) {
server.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string { server.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
var requestID string var requestID string
if param.Keys != nil { 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", return fmt.Sprintf("[GIN] %s | %s | %3d | %13v | %15s | %7s %s\n",
param.TimeStamp.Format("2006/01/02 - 15:04:05"), param.TimeStamp.Format("2006/01/02 - 15:04:05"),

View File

@ -3,8 +3,8 @@ package middleware
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/Laisky/one-api/common" "github.com/songquanpeng/one-api/common"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http" "net/http"
"time" "time"

View File

@ -2,8 +2,8 @@ package middleware
import ( import (
"fmt" "fmt"
"github.com/Laisky/one-api/common" "github.com/songquanpeng/one-api/common"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http" "net/http"
"runtime/debug" "runtime/debug"
@ -21,7 +21,7 @@ func RelayPanicRecover() gin.HandlerFunc {
logger.Errorf(ctx, fmt.Sprintf("request body: %s", string(body))) logger.Errorf(ctx, fmt.Sprintf("request body: %s", string(body)))
c.JSON(http.StatusInternalServerError, gin.H{ c.JSON(http.StatusInternalServerError, gin.H{
"error": 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", "type": "one_api_panic",
}, },
}) })

View File

@ -3,18 +3,17 @@ package middleware
import ( import (
"context" "context"
"github.com/Laisky/one-api/common/ctxkey" "github.com/songquanpeng/one-api/common/helper"
"github.com/Laisky/one-api/common/helper"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func RequestId() func(c *gin.Context) { func RequestId() func(c *gin.Context) {
return func(c *gin.Context) { return func(c *gin.Context) {
id := helper.GenRequestID() id := helper.GenRequestID()
c.Set(ctxkey.RequestId, id) c.Set(helper.RequestIdKey, id)
ctx := context.WithValue(c.Request.Context(), ctxkey.RequestId, id) ctx := context.WithValue(c.Request.Context(), helper.RequestIdKey, id)
c.Request = c.Request.WithContext(ctx) c.Request = c.Request.WithContext(ctx)
c.Header(ctxkey.RequestId, id) c.Header(helper.RequestIdKey, id)
c.Next() c.Next()
} }
} }

View File

@ -2,8 +2,8 @@ package middleware
import ( import (
"encoding/json" "encoding/json"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"github.com/gin-contrib/sessions" "github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http" "net/http"

View File

@ -4,17 +4,16 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/Laisky/one-api/common" "github.com/songquanpeng/one-api/common"
"github.com/Laisky/one-api/common/ctxkey" "github.com/songquanpeng/one-api/common/helper"
"github.com/Laisky/one-api/common/helper" "github.com/songquanpeng/one-api/common/logger"
"github.com/Laisky/one-api/common/logger"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func abortWithMessage(c *gin.Context, statusCode int, message string) { func abortWithMessage(c *gin.Context, statusCode int, message string) {
c.JSON(statusCode, gin.H{ c.JSON(statusCode, gin.H{
"error": 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", "type": "one_api_error",
}, },
}) })

View File

@ -2,7 +2,7 @@ package model
import ( import (
"context" "context"
"github.com/Laisky/one-api/common" "github.com/songquanpeng/one-api/common"
"gorm.io/gorm" "gorm.io/gorm"
"sort" "sort"
"strings" "strings"

View File

@ -5,10 +5,10 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/Laisky/errors/v2" "github.com/Laisky/errors/v2"
"github.com/Laisky/one-api/common" "github.com/songquanpeng/one-api/common"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"github.com/Laisky/one-api/common/random" "github.com/songquanpeng/one-api/common/random"
"math/rand" "math/rand"
"sort" "sort"
"strconv" "strconv"

View File

@ -3,9 +3,9 @@ package model
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/helper" "github.com/songquanpeng/one-api/common/helper"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -38,6 +38,16 @@ type Channel struct {
Config string `json:"config"` 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) { func GetAllChannels(startIdx int, num int, scope string) ([]*Channel, error) {
var channels []*Channel var channels []*Channel
var err error var err error
@ -161,14 +171,14 @@ func (channel *Channel) Delete() error {
return err return err
} }
func (channel *Channel) LoadConfig() (map[string]string, error) { func (channel *Channel) LoadConfig() (ChannelConfig, error) {
var cfg ChannelConfig
if channel.Config == "" { if channel.Config == "" {
return nil, nil return cfg, nil
} }
cfg := make(map[string]string)
err := json.Unmarshal([]byte(channel.Config), &cfg) err := json.Unmarshal([]byte(channel.Config), &cfg)
if err != nil { if err != nil {
return nil, err return cfg, err
} }
return cfg, nil return cfg, nil
} }

View File

@ -1,7 +1,7 @@
package model package model
import ( import (
"github.com/Laisky/one-api/common/helper" "github.com/songquanpeng/one-api/common/helper"
"github.com/pkg/errors" "github.com/pkg/errors"
) )

View File

@ -3,10 +3,10 @@ package model
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/Laisky/one-api/common" "github.com/songquanpeng/one-api/common"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/helper" "github.com/songquanpeng/one-api/common/helper"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"gorm.io/gorm" "gorm.io/gorm"
) )

View File

@ -6,12 +6,12 @@ import (
"strings" "strings"
"time" "time"
"github.com/Laisky/one-api/common" "github.com/songquanpeng/one-api/common"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/env" "github.com/songquanpeng/one-api/common/env"
"github.com/Laisky/one-api/common/helper" "github.com/songquanpeng/one-api/common/helper"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"github.com/Laisky/one-api/common/random" "github.com/songquanpeng/one-api/common/random"
"github.com/pkg/errors" "github.com/pkg/errors"
"gorm.io/driver/mysql" "gorm.io/driver/mysql"
"gorm.io/driver/postgres" "gorm.io/driver/postgres"

View File

@ -1,9 +1,9 @@
package model package model
import ( import (
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
billingratio "github.com/Laisky/one-api/relay/billing/ratio" billingratio "github.com/songquanpeng/one-api/relay/billing/ratio"
"strconv" "strconv"
"strings" "strings"
"time" "time"

View File

@ -3,8 +3,8 @@ package model
import ( import (
"fmt" "fmt"
"github.com/Laisky/errors/v2" "github.com/Laisky/errors/v2"
"github.com/Laisky/one-api/common" "github.com/songquanpeng/one-api/common"
"github.com/Laisky/one-api/common/helper" "github.com/songquanpeng/one-api/common/helper"
"gorm.io/gorm" "gorm.io/gorm"
) )

View File

@ -4,11 +4,11 @@ import (
"fmt" "fmt"
"github.com/Laisky/errors/v2" "github.com/Laisky/errors/v2"
"github.com/Laisky/one-api/common" "github.com/songquanpeng/one-api/common"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/helper" "github.com/songquanpeng/one-api/common/helper"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"github.com/Laisky/one-api/common/message" "github.com/songquanpeng/one-api/common/message"
"gorm.io/gorm" "gorm.io/gorm"
) )

View File

@ -5,11 +5,11 @@ import (
"strings" "strings"
"github.com/Laisky/errors/v2" "github.com/Laisky/errors/v2"
"github.com/Laisky/one-api/common" "github.com/songquanpeng/one-api/common"
"github.com/Laisky/one-api/common/blacklist" "github.com/songquanpeng/one-api/common/blacklist"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"github.com/Laisky/one-api/common/random" "github.com/songquanpeng/one-api/common/random"
"gorm.io/gorm" "gorm.io/gorm"
) )

View File

@ -1,8 +1,8 @@
package model package model
import ( import (
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"sync" "sync"
"time" "time"
) )

View File

@ -2,10 +2,10 @@ package monitor
import ( import (
"fmt" "fmt"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"github.com/Laisky/one-api/common/message" "github.com/songquanpeng/one-api/common/message"
"github.com/Laisky/one-api/model" "github.com/songquanpeng/one-api/model"
) )
func notifyRootUser(subject string, content string) { func notifyRootUser(subject string, content string) {

View File

@ -1,8 +1,8 @@
package monitor package monitor
import ( import (
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/relay/model" "github.com/songquanpeng/one-api/relay/model"
"net/http" "net/http"
"strings" "strings"
) )

View File

@ -1,7 +1,7 @@
package monitor package monitor
import ( import (
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
) )
var store = make(map[int][]bool) var store = make(map[int][]bool)

View File

@ -1,22 +1,24 @@
package relay package relay
import ( import (
"github.com/Laisky/one-api/relay/adaptor" "github.com/songquanpeng/one-api/relay/adaptor"
"github.com/Laisky/one-api/relay/adaptor/aiproxy" "github.com/songquanpeng/one-api/relay/adaptor/aiproxy"
"github.com/Laisky/one-api/relay/adaptor/ali" "github.com/songquanpeng/one-api/relay/adaptor/ali"
"github.com/Laisky/one-api/relay/adaptor/anthropic" "github.com/songquanpeng/one-api/relay/adaptor/anthropic"
"github.com/Laisky/one-api/relay/adaptor/aws" "github.com/songquanpeng/one-api/relay/adaptor/aws"
"github.com/Laisky/one-api/relay/adaptor/baidu" "github.com/songquanpeng/one-api/relay/adaptor/baidu"
"github.com/Laisky/one-api/relay/adaptor/cohere" "github.com/songquanpeng/one-api/relay/adaptor/cloudflare"
"github.com/Laisky/one-api/relay/adaptor/coze" "github.com/songquanpeng/one-api/relay/adaptor/cohere"
"github.com/Laisky/one-api/relay/adaptor/gemini" "github.com/songquanpeng/one-api/relay/adaptor/coze"
"github.com/Laisky/one-api/relay/adaptor/ollama" "github.com/songquanpeng/one-api/relay/adaptor/deepl"
"github.com/Laisky/one-api/relay/adaptor/openai" "github.com/songquanpeng/one-api/relay/adaptor/gemini"
"github.com/Laisky/one-api/relay/adaptor/palm" "github.com/songquanpeng/one-api/relay/adaptor/ollama"
"github.com/Laisky/one-api/relay/adaptor/tencent" "github.com/songquanpeng/one-api/relay/adaptor/openai"
"github.com/Laisky/one-api/relay/adaptor/xunfei" "github.com/songquanpeng/one-api/relay/adaptor/palm"
"github.com/Laisky/one-api/relay/adaptor/zhipu" "github.com/songquanpeng/one-api/relay/adaptor/tencent"
"github.com/Laisky/one-api/relay/apitype" "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 { func GetAdaptor(apiType int) adaptor.Adaptor {
@ -49,6 +51,10 @@ func GetAdaptor(apiType int) adaptor.Adaptor {
return &coze.Adaptor{} return &coze.Adaptor{}
case apitype.Cohere: case apitype.Cohere:
return &cohere.Adaptor{} return &cohere.Adaptor{}
case apitype.Cloudflare:
return &cloudflare.Adaptor{}
case apitype.DeepL:
return &deepl.Adaptor{}
} }
return nil return nil

View File

@ -6,18 +6,18 @@ import (
"net/http" "net/http"
"github.com/Laisky/errors/v2" "github.com/Laisky/errors/v2"
"github.com/Laisky/one-api/common/ctxkey" "github.com/songquanpeng/one-api/relay/adaptor"
"github.com/Laisky/one-api/relay/adaptor" "github.com/songquanpeng/one-api/relay/meta"
"github.com/Laisky/one-api/relay/meta" "github.com/songquanpeng/one-api/relay/model"
"github.com/Laisky/one-api/relay/model"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
type Adaptor struct { type Adaptor struct {
meta *meta.Meta
} }
func (a *Adaptor) Init(meta *meta.Meta) { func (a *Adaptor) Init(meta *meta.Meta) {
a.meta = meta
} }
func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) { 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") return nil, errors.New("request is nil")
} }
aiProxyLibraryRequest := ConvertRequest(*request) aiProxyLibraryRequest := ConvertRequest(*request)
aiProxyLibraryRequest.LibraryId = c.GetString(ctxkey.ConfigLibraryID) aiProxyLibraryRequest.LibraryId = a.meta.Config.LibraryID
return aiProxyLibraryRequest, nil return aiProxyLibraryRequest, nil
} }

View File

@ -1,6 +1,6 @@
package aiproxy package aiproxy
import "github.com/Laisky/one-api/relay/adaptor/openai" import "github.com/songquanpeng/one-api/relay/adaptor/openai"
var ModelList = []string{""} var ModelList = []string{""}

View File

@ -9,13 +9,13 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/Laisky/one-api/common" "github.com/songquanpeng/one-api/common"
"github.com/Laisky/one-api/common/helper" "github.com/songquanpeng/one-api/common/helper"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"github.com/Laisky/one-api/common/random" "github.com/songquanpeng/one-api/common/random"
"github.com/Laisky/one-api/relay/adaptor/openai" "github.com/songquanpeng/one-api/relay/adaptor/openai"
"github.com/Laisky/one-api/relay/constant" "github.com/songquanpeng/one-api/relay/constant"
"github.com/Laisky/one-api/relay/model" "github.com/songquanpeng/one-api/relay/model"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )

View File

@ -6,21 +6,21 @@ import (
"io" "io"
"net/http" "net/http"
"github.com/Laisky/one-api/common/ctxkey" "github.com/songquanpeng/one-api/relay/adaptor"
"github.com/Laisky/one-api/relay/adaptor" "github.com/songquanpeng/one-api/relay/meta"
"github.com/Laisky/one-api/relay/meta" "github.com/songquanpeng/one-api/relay/model"
"github.com/Laisky/one-api/relay/model" "github.com/songquanpeng/one-api/relay/relaymode"
"github.com/Laisky/one-api/relay/relaymode"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
// https://help.aliyun.com/zh/dashscope/developer-reference/api-details // https://help.aliyun.com/zh/dashscope/developer-reference/api-details
type Adaptor struct { type Adaptor struct {
meta *meta.Meta
} }
func (a *Adaptor) Init(meta *meta.Meta) { func (a *Adaptor) Init(meta *meta.Meta) {
a.meta = meta
} }
func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) { 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 { if meta.Mode == relaymode.ImagesGenerations {
req.Header.Set("X-DashScope-Async", "enable") req.Header.Set("X-DashScope-Async", "enable")
} }
if c.GetString(ctxkey.ConfigPlugin) != "" { if a.meta.Config.Plugin != "" {
req.Header.Set("X-DashScope-Plugin", c.GetString(ctxkey.ConfigPlugin)) req.Header.Set("X-DashScope-Plugin", a.meta.Config.Plugin)
} }
return nil return nil
} }

View File

@ -10,10 +10,10 @@ import (
"strings" "strings"
"time" "time"
"github.com/Laisky/one-api/common/helper" "github.com/songquanpeng/one-api/common/helper"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"github.com/Laisky/one-api/relay/adaptor/openai" "github.com/songquanpeng/one-api/relay/adaptor/openai"
"github.com/Laisky/one-api/relay/model" "github.com/songquanpeng/one-api/relay/model"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )

View File

@ -7,11 +7,11 @@ import (
"net/http" "net/http"
"strings" "strings"
"github.com/Laisky/one-api/common" "github.com/songquanpeng/one-api/common"
"github.com/Laisky/one-api/common/helper" "github.com/songquanpeng/one-api/common/helper"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"github.com/Laisky/one-api/relay/adaptor/openai" "github.com/songquanpeng/one-api/relay/adaptor/openai"
"github.com/Laisky/one-api/relay/model" "github.com/songquanpeng/one-api/relay/model"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )

View File

@ -1,8 +1,8 @@
package ali package ali
import ( import (
"github.com/Laisky/one-api/relay/adaptor/openai" "github.com/songquanpeng/one-api/relay/adaptor/openai"
"github.com/Laisky/one-api/relay/model" "github.com/songquanpeng/one-api/relay/model"
) )
type Message struct { type Message struct {

View File

@ -6,9 +6,9 @@ import (
"net/http" "net/http"
"github.com/Laisky/errors/v2" "github.com/Laisky/errors/v2"
"github.com/Laisky/one-api/relay/adaptor" "github.com/songquanpeng/one-api/relay/adaptor"
"github.com/Laisky/one-api/relay/meta" "github.com/songquanpeng/one-api/relay/meta"
"github.com/Laisky/one-api/relay/model" "github.com/songquanpeng/one-api/relay/model"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )

View File

@ -8,12 +8,12 @@ import (
"net/http" "net/http"
"strings" "strings"
"github.com/Laisky/one-api/common" "github.com/songquanpeng/one-api/common"
"github.com/Laisky/one-api/common/helper" "github.com/songquanpeng/one-api/common/helper"
"github.com/Laisky/one-api/common/image" "github.com/songquanpeng/one-api/common/image"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"github.com/Laisky/one-api/relay/adaptor/openai" "github.com/songquanpeng/one-api/relay/adaptor/openai"
"github.com/Laisky/one-api/relay/model" "github.com/songquanpeng/one-api/relay/model"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -177,10 +177,10 @@ func StreamHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusC
if len(data) < 6 { if len(data) < 6 {
continue continue
} }
if !strings.HasPrefix(data, "data: ") { if !strings.HasPrefix(data, "data:") {
continue continue
} }
data = strings.TrimPrefix(data, "data: ") data = strings.TrimPrefix(data, "data:")
dataChan <- data dataChan <- data
} }
@ -194,7 +194,7 @@ func StreamHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusC
select { select {
case data := <-dataChan: case data := <-dataChan:
// some implementations may add \r at the end of data // some implementations may add \r at the end of data
data = strings.TrimSuffix(data, "\r") data = strings.TrimSpace(data)
var claudeResponse StreamResponse var claudeResponse StreamResponse
err := json.Unmarshal([]byte(data), &claudeResponse) err := json.Unmarshal([]byte(data), &claudeResponse)
if err != nil { if err != nil {

View File

@ -4,11 +4,14 @@ import (
"io" "io"
"net/http" "net/http"
"github.com/Laisky/one-api/common/ctxkey" "github.com/songquanpeng/one-api/common/ctxkey"
"github.com/Laisky/one-api/relay/adaptor" "github.com/songquanpeng/one-api/relay/adaptor"
"github.com/Laisky/one-api/relay/adaptor/anthropic" "github.com/songquanpeng/one-api/relay/adaptor/anthropic"
"github.com/Laisky/one-api/relay/meta" "github.com/songquanpeng/one-api/relay/meta"
"github.com/Laisky/one-api/relay/model" "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/gin-gonic/gin"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -16,10 +19,16 @@ import (
var _ adaptor.Adaptor = new(Adaptor) var _ adaptor.Adaptor = new(Adaptor)
type Adaptor struct { type Adaptor struct {
meta *meta.Meta
awsClient *bedrockruntime.Client
} }
func (a *Adaptor) Init(meta *meta.Meta) { 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) { 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) { func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, meta *meta.Meta) (usage *model.Usage, err *model.ErrorWithStatusCode) {
if meta.IsStream { if meta.IsStream {
err, usage = StreamHandler(c, resp) err, usage = StreamHandler(c, a.awsClient)
} else { } else {
err, usage = Handler(c, resp, meta.PromptTokens, meta.ActualModelName) err, usage = Handler(c, a.awsClient, meta.ActualModelName)
} }
return return
} }
@ -65,7 +74,6 @@ func (a *Adaptor) GetModelList() (models []string) {
for n := range awsModelIDMap { for n := range awsModelIDMap {
models = append(models, n) models = append(models, n)
} }
return return
} }

View File

@ -8,14 +8,13 @@ import (
"io" "io"
"net/http" "net/http"
"github.com/Laisky/one-api/common" "github.com/songquanpeng/one-api/common"
"github.com/Laisky/one-api/common/ctxkey" "github.com/songquanpeng/one-api/common/ctxkey"
"github.com/Laisky/one-api/common/helper" "github.com/songquanpeng/one-api/common/helper"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"github.com/Laisky/one-api/relay/adaptor/anthropic" "github.com/songquanpeng/one-api/relay/adaptor/anthropic"
relaymodel "github.com/Laisky/one-api/relay/model" relaymodel "github.com/songquanpeng/one-api/relay/model"
"github.com/aws/aws-sdk-go-v2/aws" "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"
"github.com/aws/aws-sdk-go-v2/service/bedrockruntime/types" "github.com/aws/aws-sdk-go-v2/service/bedrockruntime/types"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -23,18 +22,6 @@ import (
"github.com/pkg/errors" "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 { func wrapErr(err error) *relaymodel.ErrorWithStatusCode {
return &relaymodel.ErrorWithStatusCode{ return &relaymodel.ErrorWithStatusCode{
StatusCode: http.StatusInternalServerError, StatusCode: http.StatusInternalServerError,
@ -62,12 +49,7 @@ func awsModelID(requestModel string) (string, error) {
return "", errors.Errorf("model %s not found", requestModel) return "", errors.Errorf("model %s not found", requestModel)
} }
func Handler(c *gin.Context, resp *http.Response, promptTokens int, modelName string) (*relaymodel.ErrorWithStatusCode, *relaymodel.Usage) { func Handler(c *gin.Context, awsCli *bedrockruntime.Client, modelName string) (*relaymodel.ErrorWithStatusCode, *relaymodel.Usage) {
awsCli, err := newAwsClient(c)
if err != nil {
return wrapErr(errors.Wrap(err, "newAwsClient")), nil
}
awsModelId, err := awsModelID(c.GetString(ctxkey.RequestModel)) awsModelId, err := awsModelID(c.GetString(ctxkey.RequestModel))
if err != nil { if err != nil {
return wrapErr(errors.Wrap(err, "awsModelID")), 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 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() createdTime := helper.GetTimestamp()
awsCli, err := newAwsClient(c)
if err != nil {
return wrapErr(errors.Wrap(err, "newAwsClient")), nil
}
awsModelId, err := awsModelID(c.GetString(ctxkey.RequestModel)) awsModelId, err := awsModelID(c.GetString(ctxkey.RequestModel))
if err != nil { if err != nil {
return wrapErr(errors.Wrap(err, "awsModelID")), nil return wrapErr(errors.Wrap(err, "awsModelID")), nil

View File

@ -1,6 +1,6 @@
package aws 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 // Request is the request to AWS Claude
// //

View File

@ -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
}

View File

@ -7,10 +7,10 @@ import (
"net/http" "net/http"
"strings" "strings"
"github.com/Laisky/one-api/relay/adaptor" "github.com/songquanpeng/one-api/relay/adaptor"
"github.com/Laisky/one-api/relay/meta" "github.com/songquanpeng/one-api/relay/meta"
"github.com/Laisky/one-api/relay/model" "github.com/songquanpeng/one-api/relay/model"
"github.com/Laisky/one-api/relay/relaymode" "github.com/songquanpeng/one-api/relay/relaymode"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )

View File

@ -11,12 +11,12 @@ import (
"sync" "sync"
"time" "time"
"github.com/Laisky/one-api/common" "github.com/songquanpeng/one-api/common"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"github.com/Laisky/one-api/relay/adaptor/openai" "github.com/songquanpeng/one-api/relay/adaptor/openai"
"github.com/Laisky/one-api/relay/client" "github.com/songquanpeng/one-api/relay/client"
"github.com/Laisky/one-api/relay/constant" "github.com/songquanpeng/one-api/relay/constant"
"github.com/Laisky/one-api/relay/model" "github.com/songquanpeng/one-api/relay/model"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )

View File

@ -3,7 +3,7 @@ package baidu
import ( import (
"time" "time"
"github.com/Laisky/one-api/relay/model" "github.com/songquanpeng/one-api/relay/model"
) )
type ChatResponse struct { type ChatResponse struct {

View 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"
}

View 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",
}

View 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
}

View 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"`
}

View File

@ -6,9 +6,9 @@ import (
"io" "io"
"net/http" "net/http"
"github.com/Laisky/one-api/relay/adaptor" "github.com/songquanpeng/one-api/relay/adaptor"
"github.com/Laisky/one-api/relay/meta" "github.com/songquanpeng/one-api/relay/meta"
"github.com/Laisky/one-api/relay/model" "github.com/songquanpeng/one-api/relay/model"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )

View File

@ -5,3 +5,10 @@ var ModelList = []string{
"command-light", "command-light-nightly", "command-light", "command-light-nightly",
"command-r", "command-r-plus", "command-r", "command-r-plus",
} }
func init() {
num := len(ModelList)
for i := 0; i < num; i++ {
ModelList = append(ModelList, ModelList[i]+"-internet")
}
}

View File

@ -9,14 +9,18 @@ import (
"net/http" "net/http"
"strings" "strings"
"github.com/Laisky/one-api/common" "github.com/songquanpeng/one-api/common"
"github.com/Laisky/one-api/common/helper" "github.com/songquanpeng/one-api/common/helper"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"github.com/Laisky/one-api/relay/adaptor/openai" "github.com/songquanpeng/one-api/relay/adaptor/openai"
"github.com/Laisky/one-api/relay/model" "github.com/songquanpeng/one-api/relay/model"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
var (
WebSearchConnector = Connector{ID: "web-search"}
)
func stopReasonCohere2OpenAI(reason *string) string { func stopReasonCohere2OpenAI(reason *string) string {
if reason == nil { if reason == nil {
return "" return ""
@ -45,6 +49,10 @@ func ConvertRequest(textRequest model.GeneralOpenAIRequest) *Request {
if cohereRequest.Model == "" { if cohereRequest.Model == "" {
cohereRequest.Model = "command-r" 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 { for _, message := range textRequest.Messages {
if message.Role == "user" { if message.Role == "user" {
cohereRequest.Message = message.Content.(string) cohereRequest.Message = message.Content.(string)

View File

@ -5,9 +5,9 @@ import (
"net/http" "net/http"
"github.com/Laisky/errors/v2" "github.com/Laisky/errors/v2"
"github.com/Laisky/one-api/common/ctxkey" "github.com/songquanpeng/one-api/common/ctxkey"
"github.com/Laisky/one-api/relay/client" "github.com/songquanpeng/one-api/relay/client"
"github.com/Laisky/one-api/relay/meta" "github.com/songquanpeng/one-api/relay/meta"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )

View File

@ -6,19 +6,19 @@ import (
"io" "io"
"net/http" "net/http"
"github.com/Laisky/one-api/common/ctxkey" "github.com/songquanpeng/one-api/relay/adaptor"
"github.com/Laisky/one-api/relay/adaptor" "github.com/songquanpeng/one-api/relay/adaptor/openai"
"github.com/Laisky/one-api/relay/adaptor/openai" "github.com/songquanpeng/one-api/relay/meta"
"github.com/Laisky/one-api/relay/meta" "github.com/songquanpeng/one-api/relay/model"
"github.com/Laisky/one-api/relay/model"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
type Adaptor struct { type Adaptor struct {
meta *meta.Meta
} }
func (a *Adaptor) Init(meta *meta.Meta) { func (a *Adaptor) Init(meta *meta.Meta) {
a.meta = meta
} }
func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) { 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 { if request == nil {
return nil, errors.New("request is nil") return nil, errors.New("request is nil")
} }
request.User = c.GetString(ctxkey.ConfigUserID) request.User = a.meta.Config.UserID
return ConvertRequest(*request), nil return ConvertRequest(*request), nil
} }

View File

@ -1,6 +1,6 @@
package coze 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 { func event2StopReason(e *string) string {
if e == nil || *e == event.Message { if e == nil || *e == event.Message {

View File

@ -4,13 +4,13 @@ import (
"bufio" "bufio"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/Laisky/one-api/common" "github.com/songquanpeng/one-api/common"
"github.com/Laisky/one-api/common/conv" "github.com/songquanpeng/one-api/common/conv"
"github.com/Laisky/one-api/common/helper" "github.com/songquanpeng/one-api/common/helper"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"github.com/Laisky/one-api/relay/adaptor/coze/constant/messagetype" "github.com/songquanpeng/one-api/relay/adaptor/coze/constant/messagetype"
"github.com/Laisky/one-api/relay/adaptor/openai" "github.com/songquanpeng/one-api/relay/adaptor/openai"
"github.com/Laisky/one-api/relay/model" "github.com/songquanpeng/one-api/relay/model"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"io" "io"
"net/http" "net/http"

View 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"
}

View File

@ -0,0 +1,9 @@
package deepl
// https://developers.deepl.com/docs/api-reference/glossaries
var ModelList = []string{
"deepl-zh",
"deepl-en",
"deepl-ja",
}

View 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
View 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
}

View 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"`
}

View File

@ -6,12 +6,12 @@ import (
"net/http" "net/http"
"github.com/Laisky/errors/v2" "github.com/Laisky/errors/v2"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/helper" "github.com/songquanpeng/one-api/common/helper"
channelhelper "github.com/Laisky/one-api/relay/adaptor" channelhelper "github.com/songquanpeng/one-api/relay/adaptor"
"github.com/Laisky/one-api/relay/adaptor/openai" "github.com/songquanpeng/one-api/relay/adaptor/openai"
"github.com/Laisky/one-api/relay/meta" "github.com/songquanpeng/one-api/relay/meta"
"github.com/Laisky/one-api/relay/model" "github.com/songquanpeng/one-api/relay/model"
"github.com/gin-gonic/gin" "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) { 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" action := "generateContent"
if meta.IsStream { 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 return fmt.Sprintf("%s/%s/models/%s:%s?key=%s", meta.BaseURL, version, meta.ActualModelName, action, meta.APIKey), nil
} }

View File

@ -8,15 +8,15 @@ import (
"net/http" "net/http"
"strings" "strings"
"github.com/Laisky/one-api/common" "github.com/songquanpeng/one-api/common"
"github.com/Laisky/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/Laisky/one-api/common/helper" "github.com/songquanpeng/one-api/common/helper"
"github.com/Laisky/one-api/common/image" "github.com/songquanpeng/one-api/common/image"
"github.com/Laisky/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"github.com/Laisky/one-api/common/random" "github.com/songquanpeng/one-api/common/random"
"github.com/Laisky/one-api/relay/adaptor/openai" "github.com/songquanpeng/one-api/relay/adaptor/openai"
"github.com/Laisky/one-api/relay/constant" "github.com/songquanpeng/one-api/relay/constant"
"github.com/Laisky/one-api/relay/model" "github.com/songquanpeng/one-api/relay/model"
"github.com/gin-gonic/gin" "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) { func StreamHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusCode, string) {
responseText := "" responseText := ""
dataChan := make(chan string)
stopChan := make(chan bool)
scanner := bufio.NewScanner(resp.Body) scanner := bufio.NewScanner(resp.Body)
scanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) { scanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 { if atEOF && len(data) == 0 {
@ -246,14 +244,16 @@ func StreamHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusC
} }
return 0, nil, nil return 0, nil, nil
}) })
dataChan := make(chan string)
stopChan := make(chan bool)
go func() { go func() {
for scanner.Scan() { for scanner.Scan() {
data := scanner.Text() data := scanner.Text()
data = strings.TrimSpace(data) data = strings.TrimSpace(data)
if !strings.HasPrefix(data, "\"text\": \"") { if !strings.HasPrefix(data, "data: ") {
continue continue
} }
data = strings.TrimPrefix(data, "\"text\": \"") data = strings.TrimPrefix(data, "data: ")
data = strings.TrimSuffix(data, "\"") data = strings.TrimSuffix(data, "\"")
dataChan <- data dataChan <- data
} }
@ -263,23 +263,17 @@ func StreamHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusC
c.Stream(func(w io.Writer) bool { c.Stream(func(w io.Writer) bool {
select { select {
case data := <-dataChan: case data := <-dataChan:
// this is used to prevent annoying \ related format bug var geminiResponse ChatResponse
data = fmt.Sprintf("{\"content\": \"%s\"}", data) err := json.Unmarshal([]byte(data), &geminiResponse)
type dummyStruct struct { if err != nil {
Content string `json:"content"` logger.SysError("error unmarshalling stream response: " + err.Error())
return true
} }
var dummy dummyStruct response := streamResponseGeminiChat2OpenAI(&geminiResponse)
err := json.Unmarshal([]byte(data), &dummy) if response == nil {
responseText += dummy.Content return true
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},
} }
responseText += response.Choices[0].Delta.StringContent()
jsonResponse, err := json.Marshal(response) jsonResponse, err := json.Marshal(response)
if err != nil { if err != nil {
logger.SysError("error marshalling stream response: " + err.Error()) logger.SysError("error marshalling stream response: " + err.Error())

View File

@ -1,8 +1,8 @@
package adaptor package adaptor
import ( import (
"github.com/Laisky/one-api/relay/meta" "github.com/songquanpeng/one-api/relay/meta"
"github.com/Laisky/one-api/relay/model" "github.com/songquanpeng/one-api/relay/model"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"io" "io"
"net/http" "net/http"

View File

@ -2,8 +2,8 @@ package minimax
import ( import (
"fmt" "fmt"
"github.com/Laisky/one-api/relay/meta" "github.com/songquanpeng/one-api/relay/meta"
"github.com/Laisky/one-api/relay/relaymode" "github.com/songquanpeng/one-api/relay/relaymode"
) )
func GetRequestURL(meta *meta.Meta) (string, error) { func GetRequestURL(meta *meta.Meta) (string, error) {

View File

@ -6,10 +6,10 @@ import (
"net/http" "net/http"
"github.com/Laisky/errors/v2" "github.com/Laisky/errors/v2"
"github.com/Laisky/one-api/relay/adaptor" "github.com/songquanpeng/one-api/relay/adaptor"
"github.com/Laisky/one-api/relay/meta" "github.com/songquanpeng/one-api/relay/meta"
"github.com/Laisky/one-api/relay/model" "github.com/songquanpeng/one-api/relay/model"
"github.com/Laisky/one-api/relay/relaymode" "github.com/songquanpeng/one-api/relay/relaymode"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )

View File

@ -1,5 +1,11 @@
package ollama package ollama
var ModelList = []string{ var ModelList = []string{
"codellama:7b-instruct",
"llama2:7b",
"llama2:latest",
"llama3:latest",
"phi3:latest",
"qwen:0.5b-chat", "qwen:0.5b-chat",
"qwen:7b",
} }

Some files were not shown because too many files have changed in this diff Show More