Skip to content

Commit

Permalink
Upstream merge (#13)
Browse files Browse the repository at this point in the history
* fix: Groq organization not auto-disabled when blocked (songquanpeng#1822)

* fix: postgres use COALESCE replace null (songquanpeng#1793)

Co-authored-by: jinqi.guo <[email protected]>

* feat: update disabled channel (songquanpeng#1780)

* Update disabled channel

* Update manage.go

* Update manage.go

* chore: add missing space

---------

Co-authored-by: JustSong <[email protected]>
Co-authored-by: JustSong <[email protected]>

* feat: ResponseFormat support json_schema (songquanpeng#1759)

* feat: responseFormat support json_schema

* chore: rename struct name

---------

Co-authored-by: JustSong <[email protected]>

* fix: fix ali embedding model always use v1 (songquanpeng#1747)

* fix:ali embedding model: v2 and v3

* chore: use ctxkey.RequestModel to eliminate hardcoding

---------

Co-authored-by: xuejia <[email protected]>
Co-authored-by: JustSong <[email protected]>

* feat: update stepfun models (songquanpeng#1740)

Co-authored-by: chenlinfeng <[email protected]>

* feat: add lobechat open link options (songquanpeng#1741)

Co-authored-by: Star <[email protected]>

* fix: getTokenById return token nil, make panic (songquanpeng#1728)

* fix:getTokenById return token nil, make panic

* chore: remove useless err check

---------

Co-authored-by: JustSong <[email protected]>

* feat: support new openai models (4o 0806, chatgpt-4o-latest) (songquanpeng#1721)

* feat: support new model gpt-4o-2024-08-06

* feat: support new model chatgpt-4o-latest

* feat: add oidc support (songquanpeng#1725)

* feat: add the ui for configuring the third-party standard OAuth2.0/OIDC.

- update SystemSetting.js
- add setup ui
- add configuration

* feat: add the ui for "allow the OAuth 2.0 to login"

- update SystemSetting.js

* feat: add OAuth 2.0 web ui and its process functions

- update common.js
- update AuthLogin.js
- update config.js

* fix: missing "Userinfo" endpoint configuration entry, used by OAuth clients to request user information from the IdP.

- update config.js
- update SystemSetting.js

* feat: updated the icons for Lark and OIDC to match the style of the icons for WeChat, EMail, GitHub.

- update lark.svg
- new oidc.svg

* refactor: Changing OAuth 2.0 to OIDC

* feat: add OIDC login method

* feat: Add support for OIDC login to the backend

* fix: Change the AppId and AppSecret on the Web UI to the standard usage: ClientId, ClientSecret.

* feat: Support quick configuration of OIDC through Well-Known Discovery Endpoint

* feat: Standardize terminology, add well-known configuration

- Change the AppId and AppSecret on the Server End to the standard usage: ClientId, ClientSecret.
- add Well-Known configuration to store in database, no actual use in server end but store and display in web ui only

* feat: support SparkDesk-v3.1-128K (songquanpeng#1732)

* feat: 支持SparkDesk-v3.1-128K以及hunyuan-vision

* feat: 支持SparkDesk-v3.1-128K以及hunyuan-vision

---------

Co-authored-by: lihangfu <[email protected]>

* feat: add siliconflow usage (songquanpeng#1798)

* fix: return the usage info if not null (songquanpeng#1792)

Usage is missing.

* fix: modify the type of token models to be text (songquanpeng#1761)

* fix: modify the type of token models to be text

* chore: update receiver name

---------

Co-authored-by: JustSong <[email protected]>

* feat: support multipart/form-data format request (songquanpeng#1690)

* "add parser multipart/form-data"

* chore: fix impl

* chore: update impl

---------

Co-authored-by: JustSong <[email protected]>

* feat: support SparkDesk-v3.5-32K (songquanpeng#1832)

Co-authored-by: lihangfu <[email protected]>

* fix:unsuccessful lobechat redirection link (songquanpeng#1843)

* feat: add Vertex AI gemini-1.5-pro-002 and gemini-1.5-flash-002 (songquanpeng#1854)

* fix: use modelMap when testing a channel (songquanpeng#1855)

Co-authored-by: oliang <[email protected]>

* feat: update groq model and price (songquanpeng#1864)

* feat: add support for Claude Sonnet 3.5 v2 (songquanpeng#1888)

* feat: update Gemini adaptor to support custom response format (songquanpeng#1892)

* feat: always return usage in stream mode

* feat: able to hide test model selector and balance col

* feat: added support for Claude 3.5 Haiku (songquanpeng#1912)

* feat: add support for xAI (songquanpeng#1915)

* feat: add new claude models (songquanpeng#1910)

* feat: Add new models to ModelList in constants.go

* feat: update model lists and mappings for Claude 3.5 versions

---------

Co-authored-by: JustSong <[email protected]>

* fix: changeoptional field to pointer type (songquanpeng#1907)

* fix:修复在渠道配置中设置模型重定向时,temperature为0被忽略的问题

* fix: set optional fields to pointer type

---------

Co-authored-by: JustSong <[email protected]>

* feat: update GeneralOpenAIRequest

* fix: update Spark Lite's domain to lite (songquanpeng#1896)

* feat: able to use ENFORCE_INCLUDE_USAGE to enforce include usage in response

* feat: support set system prompt for channel (close songquanpeng#1920)

* feat: support set system_prompt for theme air & berry

* feat: add warning in log when system prompt is reset

* feat: support gzip decode (songquanpeng#1962)

* feat: update feishu oauth login

* docs: update readme

* feat: support replicate chat models (songquanpeng#1989)

* feat: add Replicate adaptor and integrate into channel and API types

* feat: support llm chat on replicate

* feat: add support for new OpenAI models and update billing ratios (songquanpeng#1990)

* feat: add gemini-2.0-flash-exp and fix race condition in processChannelRelayError (songquanpeng#1983)

Co-authored-by: JustSong <[email protected]>

* chore(deps): bump golang.org/x/crypto from 0.24.0 to 0.31.0 (songquanpeng#1976)

Bumps [golang.org/x/crypto](https://github.com/golang/crypto) from 0.24.0 to 0.31.0.
- [Commits](golang/crypto@v0.24.0...v0.31.0)

---
updated-dependencies:
- dependency-name: golang.org/x/crypto
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* feat: update qwen model and price (songquanpeng#1966)

* feat: enhance response handling to support gemini-2.0-thinking (songquanpeng#1995)

* fix: fix balance query for siliconflow (songquanpeng#1960)

* docs: add tutorial section for BT Panel installation (songquanpeng#1985)

* Update README.md

在国内有大部分用户都在使用宝塔面板管理服务器,因此增加使用宝塔面板部署的教程,可视化的部署方式可以帮助用户更加便捷的部署one-api

* docs: update readme

---------

Co-authored-by: JustSong <[email protected]>

* fix: remove the duplicate `claude-3-5-haiku-20241022` in Anthropic's base model list (songquanpeng#1957)

* Update constants.go

Remove the duplicate `claude-3-5-haiku-20241022` causing issue 1928

* fix: fix syntax error

---------

Co-authored-by: JustSong <[email protected]>

* feat: support Redis Sentinel and Redis Cluster (songquanpeng#1952)

* feature: support Redis Sentinel and Redis Cluster

* chore: update implementation

---------

Co-authored-by: JustSong <[email protected]>

* feat: add balance query support for DeepSeek (songquanpeng#1946)

* Support Balance Query for DeepSeek

* Fix

* chore: update model mapping implementation for audio (songquanpeng#1932)

* fixed model mapping

* chore: update implementation

---------

Co-authored-by: JustSong <[email protected]>

* feat: support gpt-4o-2024-11-20 (songquanpeng#1941)

* fix: add branch limitation and drop pull_request trigger for ci.yml

* fix: add branch check

* docs: update README.md

* chore: update readme

* chore: update ci yaml

* new disable

* fix

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: AJ's Life Journey <[email protected]>
Co-authored-by: guogeer <[email protected]>
Co-authored-by: jinqi.guo <[email protected]>
Co-authored-by: Ghostz <[email protected]>
Co-authored-by: JustSong <[email protected]>
Co-authored-by: JustSong <[email protected]>
Co-authored-by: majian <[email protected]>
Co-authored-by: leavegee <[email protected]>
Co-authored-by: xuejia <[email protected]>
Co-authored-by: forrestlinfeng <[email protected]>
Co-authored-by: chenlinfeng <[email protected]>
Co-authored-by: 千寻简 <[email protected]>
Co-authored-by: Star <[email protected]>
Co-authored-by: qinguoyi <[email protected]>
Co-authored-by: TAKO <[email protected]>
Co-authored-by: OnEvent <[email protected]>
Co-authored-by: lihangfu <[email protected]>
Co-authored-by: lihangfu <[email protected]>
Co-authored-by: TimeTrapzz <[email protected]>
Co-authored-by: byte911 <[email protected]>
Co-authored-by: 徐瑞东 <[email protected]>
Co-authored-by: 抒情熊 <[email protected]>
Co-authored-by: Pan, Wen-Ming <[email protected]>
Co-authored-by: liangjs <[email protected]>
Co-authored-by: oliang <[email protected]>
Co-authored-by: longkeyy <[email protected]>
Co-authored-by: shaoyun <[email protected]>
Co-authored-by: Wei Tingjiang <[email protected]>
Co-authored-by: Ryo Shen <[email protected]>
Co-authored-by: Laisky.Cai <[email protected]>
Co-authored-by: wanthigh <[email protected]>
Co-authored-by: Calcium-Ion <[email protected]>
Co-authored-by: JustSong <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Ke Wang <[email protected]>
Co-authored-by: bestlaw66 <[email protected]>
Co-authored-by: ZhangTianrong <[email protected]>
Co-authored-by: Yuwei Ba <[email protected]>
Co-authored-by: Qiying Wang <[email protected]>
Co-authored-by: liuliming <[email protected]>
  • Loading branch information
Show file tree
Hide file tree
Showing 107 changed files with 2,698 additions and 611 deletions.
10 changes: 4 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
name: CI

# This setup assumes that you run the unit tests with code coverage in the same
# workflow that will also print the coverage report as comment to the pull request.
# workflow that will also print the coverage report as comment to the pull request.
# Therefore, you need to trigger this workflow when a pull request is (re)opened or
# when new code is pushed to the branch of the pull request. In addition, you also
# need to trigger this workflow when new code is pushed to the main branch because
# need to trigger this workflow when new code is pushed to the main branch because
# we need to upload the code coverage results as artifact for the main branch as
# well since it will be the baseline code coverage.
#
#
# We do not want to trigger the workflow for pushes to *any* branch because this
# would trigger our jobs twice on pull requests (once from "push" event and once
# from "pull_request->synchronize")
on:
pull_request:
types: [opened, reopened, synchronize]
push:
branches:
- 'main'
Expand All @@ -31,7 +29,7 @@ jobs:
with:
go-version: ^1.22

# When you execute your unit tests, make sure to use the "-coverprofile" flag to write a
# When you execute your unit tests, make sure to use the "-coverprofile" flag to write a
# coverage profile to a file. You will need the name of the file (e.g. "coverage.txt")
# in the next step as well as the next job.
- name: Test
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ logs
data
/web/node_modules
cmd.md
.env
.env
/one-api
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ _✨ 通过标准的 OpenAI API 格式访问所有的大模型,开箱即用
+ [x] [together.ai](https://www.together.ai/)
+ [x] [novita.ai](https://www.novita.ai/)
+ [x] [硅基流动 SiliconCloud](https://siliconflow.cn/siliconcloud)
+ [x] [xAI](https://x.ai/)
2. 支持配置镜像以及众多[第三方代理服务](https://iamazing.cn/page/openai-api-third-party-services)
3. 支持通过**负载均衡**的方式访问多个渠道。
4. 支持 **stream 模式**,可以通过流式传输实现打字机效果。
Expand All @@ -114,8 +115,8 @@ _✨ 通过标准的 OpenAI API 格式访问所有的大模型,开箱即用
21. 支持 Cloudflare Turnstile 用户校验。
22. 支持用户管理,支持**多种用户登录注册方式**
+ 邮箱登录注册(支持注册邮箱白名单)以及通过邮箱进行密码重置。
+ 支持使用飞书进行授权登录
+ [GitHub 开放授权](https://github.com/settings/applications/new)
+ 支持[飞书授权登录](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/authen-v1/authorize/get)[这里有 One API 的实现细节阐述供参考](https://iamazing.cn/page/feishu-oauth-login)
+ 支持 [GitHub 授权登录](https://github.com/settings/applications/new)
+ 微信公众号授权(需要额外部署 [WeChat Server](https://github.com/songquanpeng/wechat-server))。
23. 支持主题切换,设置环境变量 `THEME` 即可,默认为 `default`,欢迎 PR 更多主题,具体参考[此处](./web/README.md)
24. 配合 [Message Pusher](https://github.com/songquanpeng/message-pusher) 可将报警信息推送到多种 App 上。
Expand Down Expand Up @@ -174,6 +175,10 @@ sudo service nginx restart

初始账号用户名为 `root`,密码为 `123456`

### 通过宝塔面板进行一键部署
1. 安装宝塔面板9.2.0及以上版本,前往 [宝塔面板](https://www.bt.cn/new/download.html?r=dk_oneapi) 官网,选择正式版的脚本下载安装;
2. 安装后登录宝塔面板,在左侧菜单栏中点击 `Docker`,首次进入会提示安装 `Docker` 服务,点击立即安装,按提示完成安装;
3. 安装完成后在应用商店中搜索 `One-API`,点击安装,配置域名等基本信息即可完成安装;

### 基于 Docker Compose 进行部署

Expand Down Expand Up @@ -217,7 +222,7 @@ docker-compose ps
3. 所有从服务器必须设置 `NODE_TYPE``slave`,不设置则默认为主服务器。
4. 设置 `SYNC_FREQUENCY` 后服务器将定期从数据库同步配置,在使用远程数据库的情况下,推荐设置该项并启用 Redis,无论主从。
5. 从服务器可以选择设置 `FRONTEND_BASE_URL`,以重定向页面请求到主服务器。
6. 从服务器上**分别**装好 Redis,设置好 `REDIS_CONN_STRING`,这样可以做到在缓存未过期的情况下数据库零访问,可以减少延迟。
6. 从服务器上**分别**装好 Redis,设置好 `REDIS_CONN_STRING`,这样可以做到在缓存未过期的情况下数据库零访问,可以减少延迟(Redis 集群或者哨兵模式的支持请参考环境变量说明)
7. 如果主服务器访问数据库延迟也比较高,则也需要启用 Redis,并设置 `SYNC_FREQUENCY`,以定期从数据库同步配置。

环境变量的具体使用方法详见[此处](#环境变量)。
Expand Down Expand Up @@ -346,6 +351,11 @@ graph LR
1. `REDIS_CONN_STRING`:设置之后将使用 Redis 作为缓存使用。
+ 例子:`REDIS_CONN_STRING=redis://default:redispw@localhost:49153`
+ 如果数据库访问延迟很低,没有必要启用 Redis,启用后反而会出现数据滞后的问题。
+ 如果需要使用哨兵或者集群模式:
+ 则需要把该环境变量设置为节点列表,例如:`localhost:49153,localhost:49154,localhost:49155`
+ 除此之外还需要设置以下环境变量:
+ `REDIS_PASSWORD`:Redis 集群或者哨兵模式下的密码设置。
+ `REDIS_MASTER_NAME`:Redis 哨兵模式下主节点的名称。
2. `SESSION_SECRET`:设置之后将使用固定的会话密钥,这样系统重新启动后已登录用户的 cookie 将依旧有效。
+ 例子:`SESSION_SECRET=random_string`
3. `SQL_DSN`:设置之后将使用指定数据库而非 SQLite,请使用 MySQL 或 PostgreSQL。
Expand Down Expand Up @@ -399,6 +409,7 @@ graph LR
26. `METRIC_SUCCESS_RATE_THRESHOLD`:请求成功率阈值,默认为 `0.8`
27. `INITIAL_ROOT_TOKEN`:如果设置了该值,则在系统首次启动时会自动创建一个值为该环境变量值的 root 用户令牌。
28. `INITIAL_ROOT_ACCESS_TOKEN`:如果设置了该值,则在系统首次启动时会自动创建一个值为该环境变量的 root 用户创建系统管理令牌。
29. `ENFORCE_INCLUDE_USAGE`:是否强制在 stream 模型下返回 usage,默认不开启,可选值为 `true``false`

### 命令行参数
1. `--port <port_number>`: 指定服务器监听的端口号,默认为 `3000`
Expand Down
10 changes: 10 additions & 0 deletions common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var PasswordLoginEnabled = true
var PasswordRegisterEnabled = true
var EmailVerificationEnabled = false
var GitHubOAuthEnabled = false
var OidcEnabled = false
var WeChatAuthEnabled = false
var TurnstileCheckEnabled = false
var RegisterEnabled = true
Expand Down Expand Up @@ -74,6 +75,13 @@ var GoogleClientSecret = ""
var LarkClientId = ""
var LarkClientSecret = ""

var OidcClientId = ""
var OidcClientSecret = ""
var OidcWellKnown = ""
var OidcAuthorizationEndpoint = ""
var OidcTokenEndpoint = ""
var OidcUserinfoEndpoint = ""

var WeChatServerAddress = ""
var WeChatServerToken = ""
var WeChatAccountQRCodeImageURL = ""
Expand Down Expand Up @@ -156,3 +164,5 @@ var OnlyOneLogFile = env.Bool("ONLY_ONE_LOG_FILE", false)
var RelayProxy = env.String("RELAY_PROXY", "")
var UserContentRequestProxy = env.String("USER_CONTENT_REQUEST_PROXY", "")
var UserContentRequestTimeout = env.Int("USER_CONTENT_REQUEST_TIMEOUT", 30)

var EnforceIncludeUsage = env.Bool("ENFORCE_INCLUDE_USAGE", false)
1 change: 1 addition & 0 deletions common/ctxkey/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ const (
BaseURL = "base_url"
AvailableModels = "available_models"
KeyRequestBody = "key_request_body"
SystemPrompt = "system_prompt"
)
20 changes: 20 additions & 0 deletions common/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,23 @@ func String2Int(str string) int {
}
return num
}

func Float64PtrMax(p *float64, maxValue float64) *float64 {
if p == nil {
return nil
}
if *p > maxValue {
return &maxValue
}
return p
}

func Float64PtrMin(p *float64, minValue float64) *float64 {
if p == nil {
return nil
}
if *p < minValue {
return &minValue
}
return p
}
62 changes: 36 additions & 26 deletions common/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,46 @@ package common

import (
"context"
"github.com/go-redis/redis/v8"
"github.com/songquanpeng/one-api/common/logger"
"os"
"strings"
"time"

"github.com/go-redis/redis/v8"
"github.com/songquanpeng/one-api/common/logger"
)

var RDB *redis.Client
var RDB redis.Cmdable
var RedisEnabled = true

// InitRedisClient This function is called after init()
func InitRedisClient() (err error) {
//if os.Getenv("REDIS_CONN_STRING") == "" {
// RedisEnabled = false
// logger.SysLog("REDIS_CONN_STRING not set, Redis is not enabled")
// return nil
//}
//if os.Getenv("SYNC_FREQUENCY") == "" {
// RedisEnabled = false
// logger.SysLog("SYNC_FREQUENCY not set, Redis is disabled")
// return nil
//}
//logger.SysLog("Redis is enabled")
//opt, err := redis.ParseURL(os.Getenv("REDIS_CONN_STRING"))
//if err != nil {
// logger.FatalLog("failed to parse Redis connection string: " + err.Error())
//}
if os.Getenv("REDIS_HOST") == "" {
if os.Getenv("REDIS_CONN_STRING") == "" {
RedisEnabled = false
logger.SysLog("REDIS_HOST not set, Redis is not enabled")
logger.SysLog("REDIS_CONN_STRING not set, Redis is not enabled")
return nil
}
opt := &redis.Options{
Addr: os.Getenv("REDIS_HOST"),
Password: os.Getenv("REDIS_PASSWORD"),
DB: 0,
if os.Getenv("SYNC_FREQUENCY") == "" {
RedisEnabled = false
logger.SysLog("SYNC_FREQUENCY not set, Redis is disabled")
return nil
}
redisConnString := os.Getenv("REDIS_CONN_STRING")
if os.Getenv("REDIS_MASTER_NAME") == "" {
logger.SysLog("Redis is enabled")
opt, err := redis.ParseURL(redisConnString)
if err != nil {
logger.FatalLog("failed to parse Redis connection string: " + err.Error())
}
RDB = redis.NewClient(opt)
} else {
// cluster mode
logger.SysLog("Redis cluster mode enabled")
RDB = redis.NewUniversalClient(&redis.UniversalOptions{
Addrs: strings.Split(redisConnString, ","),
Password: os.Getenv("REDIS_PASSWORD"),
MasterName: os.Getenv("REDIS_MASTER_NAME"),
})
}
RDB = redis.NewClient(opt)

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

Expand All @@ -50,6 +52,14 @@ func InitRedisClient() (err error) {
return err
}

func ParseRedisOption() *redis.Options {
opt, err := redis.ParseURL(os.Getenv("REDIS_CONN_STRING"))
if err != nil {
logger.FatalLog("failed to parse Redis connection string: " + err.Error())
}
return opt
}

func RedisSet(key string, value string, expiration time.Duration) error {
ctx := context.Background()
return RDB.Set(ctx, key, value, expiration).Err()
Expand Down
3 changes: 2 additions & 1 deletion common/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package render
import (
"encoding/json"
"fmt"
"strings"

"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common"
"strings"
)

func RawData(c *gin.Context, str string) {
Expand Down
2 changes: 1 addition & 1 deletion controller/auth/lark.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func getLarkUserInfoByCode(code string) (*LarkUser, error) {
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", "https://passport.feishu.cn/suite/passport/oauth/token", bytes.NewBuffer(jsonData))
req, err := http.NewRequest("POST", "https://open.feishu.cn/open-apis/authen/v2/oauth/token", bytes.NewBuffer(jsonData))
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 45b7703

Please sign in to comment.