支持 openai 的 orgid

This commit is contained in:
baomi.wbm 2023-11-13 21:23:07 +08:00
parent e92587fd35
commit 45f6285d42
4 changed files with 30 additions and 3 deletions

View File

@ -46,6 +46,7 @@ func init() {
} }
func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
channelType := c.GetInt("channel") channelType := c.GetInt("channel")
channelId := c.GetInt("channel_id") channelId := c.GetInt("channel_id")
tokenId := c.GetInt("token_id") tokenId := c.GetInt("token_id")
@ -126,6 +127,11 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
if c.GetString("base_url") != "" { if c.GetString("base_url") != "" {
baseURL = c.GetString("base_url") baseURL = c.GetString("base_url")
} }
Organization := ""
if c.GetString("openai_organization") != "" {
Organization = c.GetString("openai_organization")
}
fullRequestURL := getFullRequestURL(baseURL, requestURL, channelType) fullRequestURL := getFullRequestURL(baseURL, requestURL, channelType)
switch apiType { switch apiType {
case APITypeOpenAI: case APITypeOpenAI:
@ -348,6 +354,9 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
req.Header.Set("HTTP-Referer", "https://github.com/songquanpeng/one-api") req.Header.Set("HTTP-Referer", "https://github.com/songquanpeng/one-api")
req.Header.Set("X-Title", "One API") req.Header.Set("X-Title", "One API")
} }
if Organization != "" {
req.Header.Set("OpenAI-Organization", Organization)
}
} }
case APITypeClaude: case APITypeClaude:
req.Header.Set("x-api-key", apiKey) req.Header.Set("x-api-key", apiKey)

View File

@ -85,6 +85,7 @@ func Distribute() func(c *gin.Context) {
c.Set("model_mapping", channel.GetModelMapping()) c.Set("model_mapping", channel.GetModelMapping())
c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", channel.Key)) c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", channel.Key))
c.Set("base_url", channel.GetBaseURL()) c.Set("base_url", channel.GetBaseURL())
c.Set("openai_organization", channel.Organization)
switch channel.Type { switch channel.Type {
case common.ChannelTypeAzure: case common.ChannelTypeAzure:
c.Set("api_version", channel.Other) c.Set("api_version", channel.Other)

View File

@ -1,8 +1,9 @@
package model package model
import ( import (
"gorm.io/gorm"
"one-api/common" "one-api/common"
"gorm.io/gorm"
) )
type Channel struct { type Channel struct {
@ -24,6 +25,7 @@ type Channel struct {
UsedQuota int64 `json:"used_quota" gorm:"bigint;default:0"` UsedQuota int64 `json:"used_quota" gorm:"bigint;default:0"`
ModelMapping *string `json:"model_mapping" gorm:"type:varchar(1024);default:''"` ModelMapping *string `json:"model_mapping" gorm:"type:varchar(1024);default:''"`
Priority *int64 `json:"priority" gorm:"bigint;default:0"` Priority *int64 `json:"priority" gorm:"bigint;default:0"`
Organization string `json:"openai_organization" gorm:"column:openai_organization;default:''"`
} }
func GetAllChannels(startIdx int, num int, selectAll bool) ([]*Channel, error) { func GetAllChannels(startIdx int, num int, selectAll bool) ([]*Channel, error) {

View File

@ -1,8 +1,8 @@
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
import { Button, Form, Header, Input, Message, Segment } from 'semantic-ui-react';
import { useNavigate, useParams } from 'react-router-dom'; import { useNavigate, useParams } from 'react-router-dom';
import { API, showError, showInfo, showSuccess, verifyJSON } from '../../helpers'; import { Button, Form, Header, Input, Message, Segment } from 'semantic-ui-react';
import { CHANNEL_OPTIONS } from '../../constants'; import { CHANNEL_OPTIONS } from '../../constants';
import { API, showError, showInfo, showSuccess, verifyJSON } from '../../helpers';
const MODEL_MAPPING_EXAMPLE = { const MODEL_MAPPING_EXAMPLE = {
'gpt-3.5-turbo-0301': 'gpt-3.5-turbo', 'gpt-3.5-turbo-0301': 'gpt-3.5-turbo',
@ -41,6 +41,7 @@ const EditChannel = () => {
type: 1, type: 1,
key: '', key: '',
base_url: '', base_url: '',
openai_organization: '',
other: '', other: '',
model_mapping: '', model_mapping: '',
models: [], models: [],
@ -315,6 +316,20 @@ const EditChannel = () => {
options={groupOptions} options={groupOptions}
/> />
</Form.Field> </Form.Field>
{
inputs.type === 1 && (
<Form.Field>
<Form.Input
label='组织ID'
name='openai_organization'
placeholder={'请输入 OpenAI Organization'}
onChange={handleInputChange}
value={inputs.openai_organization}
autoComplete='new-password'
/>
</Form.Field>
)
}
{ {
inputs.type === 18 && ( inputs.type === 18 && (
<Form.Field> <Form.Field>