mirror of
https://github.com/songquanpeng/one-api.git
synced 2026-04-23 18:24:25 +08:00
Compare commits
3 Commits
71919afcd5
...
e0b6c72df3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e0b6c72df3 | ||
|
|
6ab87f8a08 | ||
|
|
cc367dd95b |
@@ -91,7 +91,7 @@ func preConsumeQuota(ctx context.Context, textRequest *relaymodel.GeneralOpenAIR
|
||||
return preConsumedQuota, nil
|
||||
}
|
||||
|
||||
func postConsumeQuota(ctx context.Context, usage *relaymodel.Usage, meta *meta.Meta, textRequest *relaymodel.GeneralOpenAIRequest, ratio float64, preConsumedQuota int64, modelRatio float64, groupRatio float64) {
|
||||
func postConsumeQuota(ctx context.Context, usage *relaymodel.Usage, meta *meta.Meta, textRequest *relaymodel.GeneralOpenAIRequest, ratio float64, preConsumedQuota int64, modelRatio float64, groupRatio float64, systemPromptReset bool) {
|
||||
if usage == nil {
|
||||
logger.Error(ctx, "usage is nil, which is unexpected")
|
||||
return
|
||||
@@ -119,7 +119,11 @@ func postConsumeQuota(ctx context.Context, usage *relaymodel.Usage, meta *meta.M
|
||||
if err != nil {
|
||||
logger.Error(ctx, "error update user quota cache: "+err.Error())
|
||||
}
|
||||
logContent := fmt.Sprintf("模型倍率 %.2f,分组倍率 %.2f,补全倍率 %.2f", modelRatio, groupRatio, completionRatio)
|
||||
var extraLog string
|
||||
if systemPromptReset {
|
||||
extraLog = " (注意系统提示词已被重置)"
|
||||
}
|
||||
logContent := fmt.Sprintf("模型倍率 %.2f,分组倍率 %.2f,补全倍率 %.2f%s", modelRatio, groupRatio, completionRatio, extraLog)
|
||||
model.RecordConsumeLog(ctx, meta.UserId, meta.ChannelId, promptTokens, completionTokens, textRequest.Model, meta.TokenName, quota, logContent)
|
||||
model.UpdateUserUsedQuotaAndRequestCount(meta.UserId, quota)
|
||||
model.UpdateChannelUsedQuota(meta.ChannelId, quota)
|
||||
@@ -156,21 +160,22 @@ func isErrorHappened(meta *meta.Meta, resp *http.Response) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func setSystemPrompt(ctx context.Context, request *relaymodel.GeneralOpenAIRequest, prompt string) {
|
||||
func setSystemPrompt(ctx context.Context, request *relaymodel.GeneralOpenAIRequest, prompt string) (reset bool) {
|
||||
if prompt == "" {
|
||||
return
|
||||
return false
|
||||
}
|
||||
if len(request.Messages) == 0 {
|
||||
return
|
||||
return false
|
||||
}
|
||||
if request.Messages[0].Role == role.System {
|
||||
request.Messages[0].Content = prompt
|
||||
logger.Infof(ctx, "rewrite system prompt")
|
||||
return
|
||||
return true
|
||||
}
|
||||
request.Messages = append([]relaymodel.Message{{
|
||||
Role: role.System,
|
||||
Content: prompt,
|
||||
}}, request.Messages...)
|
||||
logger.Infof(ctx, "add system prompt")
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ func RelayTextHelper(c *gin.Context) *model.ErrorWithStatusCode {
|
||||
textRequest.Model, _ = getMappedModelName(textRequest.Model, meta.ModelMapping)
|
||||
meta.ActualModelName = textRequest.Model
|
||||
// set system prompt if not empty
|
||||
setSystemPrompt(ctx, textRequest, meta.SystemPrompt)
|
||||
systemPromptReset := setSystemPrompt(ctx, textRequest, meta.SystemPrompt)
|
||||
// get model ratio & group ratio
|
||||
modelRatio := billingratio.GetModelRatio(textRequest.Model, meta.ChannelType)
|
||||
groupRatio := billingratio.GetGroupRatio(meta.Group)
|
||||
@@ -82,7 +82,7 @@ func RelayTextHelper(c *gin.Context) *model.ErrorWithStatusCode {
|
||||
return respErr
|
||||
}
|
||||
// post-consume quota
|
||||
go postConsumeQuota(ctx, usage, meta, textRequest, ratio, preConsumedQuota, modelRatio, groupRatio)
|
||||
go postConsumeQuota(ctx, usage, meta, textRequest, ratio, preConsumedQuota, modelRatio, groupRatio, systemPromptReset)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -8,11 +8,12 @@ import {
|
||||
IconKey,
|
||||
IconGardenCart,
|
||||
IconUser,
|
||||
IconUserScan
|
||||
IconUserScan,
|
||||
IconMessageCircle
|
||||
} from '@tabler/icons-react';
|
||||
|
||||
// constant
|
||||
const icons = { IconDashboard, IconSitemap, IconArticle, IconCoin, IconAdjustments, IconKey, IconGardenCart, IconUser, IconUserScan };
|
||||
const icons = { IconDashboard, IconSitemap, IconArticle, IconCoin, IconAdjustments, IconKey, IconGardenCart, IconUser, IconUserScan,IconMessageCircle };
|
||||
|
||||
// ==============================|| DASHBOARD MENU ITEMS ||============================== //
|
||||
|
||||
@@ -29,6 +30,15 @@ const panel = {
|
||||
breadcrumbs: false,
|
||||
isAdmin: false
|
||||
},
|
||||
{
|
||||
id: 'chat',
|
||||
title: '聊天',
|
||||
type: 'item',
|
||||
url: '/panel/chat',
|
||||
icon: icons.IconMessageCircle,
|
||||
breadcrumbs: false,
|
||||
isAdmin: false
|
||||
},
|
||||
{
|
||||
id: 'channel',
|
||||
title: '渠道',
|
||||
|
||||
@@ -3,6 +3,7 @@ import { lazy } from 'react';
|
||||
// project imports
|
||||
import MainLayout from 'layout/MainLayout';
|
||||
import Loadable from 'ui-component/Loadable';
|
||||
import Chat from "../views/Chat";
|
||||
|
||||
const Channel = Loadable(lazy(() => import('views/Channel')));
|
||||
const Log = Loadable(lazy(() => import('views/Log')));
|
||||
@@ -31,6 +32,10 @@ const MainRoutes = {
|
||||
path: 'dashboard',
|
||||
element: <Dashboard />
|
||||
},
|
||||
{
|
||||
path: 'chat',
|
||||
element: <Chat />
|
||||
},
|
||||
{
|
||||
path: 'channel',
|
||||
element: <Channel />
|
||||
|
||||
28
web/berry/src/views/Chat/index.css
Normal file
28
web/berry/src/views/Chat/index.css
Normal file
@@ -0,0 +1,28 @@
|
||||
.MuiContainer-root {
|
||||
padding-left: 0;
|
||||
padding-right: 0;
|
||||
height: calc(100% - 1px);
|
||||
max-width: unset;
|
||||
}
|
||||
|
||||
.css-1xnbu7n-MuiContainer-root {
|
||||
/* 如果有特定样式,请在此处添加 */
|
||||
}
|
||||
|
||||
.css-9d4wr9 {
|
||||
background-color: #eef2f6;
|
||||
width: 100%;
|
||||
min-height: calc(100vh - 88px);
|
||||
flex-grow: 1;
|
||||
padding: 0;
|
||||
margin-top: 83.746px;
|
||||
margin-right: 0;
|
||||
border-radius: 12px;
|
||||
border-bottom-left-radius: 0;
|
||||
border-bottom-right-radius: 0;
|
||||
transition: margin 225ms cubic-bezier(0.0, 0, 0.2, 1) 0ms;
|
||||
}
|
||||
|
||||
.chat-container {
|
||||
height: 100%;
|
||||
}
|
||||
69
web/berry/src/views/Chat/index.js
Normal file
69
web/berry/src/views/Chat/index.js
Normal file
@@ -0,0 +1,69 @@
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { API } from "../../utils/api";
|
||||
import "./index.css";
|
||||
|
||||
const useIsSmallScreen = () => {
|
||||
const [isSmallScreen, setIsSmallScreen] = useState(window.innerWidth <= 768);
|
||||
|
||||
useEffect(() => {
|
||||
const handleResize = () => {
|
||||
setIsSmallScreen(window.innerWidth <= 768);
|
||||
};
|
||||
|
||||
window.addEventListener('resize', handleResize);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('resize', handleResize);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return isSmallScreen;
|
||||
};
|
||||
|
||||
const Chat = () => {
|
||||
const [chatUrl, setChatUrl] = useState("");
|
||||
const [loading, setLoading] = useState(true);
|
||||
// const isSmallScreen = useIsSmallScreen();
|
||||
|
||||
const loadTokens = async () => {
|
||||
try {
|
||||
const res = await API.get(`/api/token/`);
|
||||
const siteInfo = JSON.parse(localStorage.getItem('siteInfo'));
|
||||
if (!siteInfo) {
|
||||
console.error("siteInfo not found in localStorage");
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
// const url = `https://like.chatapi.asia/#/?settings={"key":"sk-xxx","url":"https://chat.chatapi.asia"}`;
|
||||
const serverAddress = siteInfo.server_address;
|
||||
const key = res.data.data[0].key;
|
||||
const url = `${siteInfo.chat_link}/#/?settings={"key":"sk-${key}","url":"${serverAddress}"}`;
|
||||
|
||||
setChatUrl(url);
|
||||
} catch (error) {
|
||||
console.error("Error loading tokens:", error);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
loadTokens();
|
||||
}, []);
|
||||
|
||||
if (loading) {
|
||||
return <div className="chat-container">Loading...</div>;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="chat-container">
|
||||
<iframe
|
||||
src={chatUrl}
|
||||
style={{ height: '100%', width: '100%', padding: 0, border: 'none' }}
|
||||
title="Chat"
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default Chat;
|
||||
Reference in New Issue
Block a user