mirror of
https://github.com/linux-do/new-api.git
synced 2025-11-08 15:13:42 +08:00
优化一些交互逻辑
This commit is contained in:
@@ -9,11 +9,10 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"one-api/common"
|
"one-api/common"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func openaiStreamHandler(c *gin.Context, resp *http.Response, relayMode int) (*OpenAIErrorWithStatusCode, string) {
|
func openaiStreamHandler(c *gin.Context, resp *http.Response, relayMode int) (*OpenAIErrorWithStatusCode, string) {
|
||||||
var responseTextBuilder strings.Builder
|
responseText := ""
|
||||||
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 {
|
||||||
@@ -29,10 +28,7 @@ func openaiStreamHandler(c *gin.Context, resp *http.Response, relayMode int) (*O
|
|||||||
})
|
})
|
||||||
dataChan := make(chan string)
|
dataChan := make(chan string)
|
||||||
stopChan := make(chan bool)
|
stopChan := make(chan bool)
|
||||||
var wg sync.WaitGroup
|
|
||||||
go func() {
|
go func() {
|
||||||
wg.Add(1)
|
|
||||||
var streamItems []string
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
data := scanner.Text()
|
data := scanner.Text()
|
||||||
if len(data) < 6 { // ignore blank line or wrong format
|
if len(data) < 6 { // ignore blank line or wrong format
|
||||||
@@ -44,39 +40,30 @@ func openaiStreamHandler(c *gin.Context, resp *http.Response, relayMode int) (*O
|
|||||||
dataChan <- data
|
dataChan <- data
|
||||||
data = data[6:]
|
data = data[6:]
|
||||||
if !strings.HasPrefix(data, "[DONE]") {
|
if !strings.HasPrefix(data, "[DONE]") {
|
||||||
streamItems = append(streamItems, data)
|
switch relayMode {
|
||||||
}
|
case RelayModeChatCompletions:
|
||||||
}
|
var streamResponse ChatCompletionsStreamResponseSimple
|
||||||
streamResp := "[" + strings.Join(streamItems, ",") + "]"
|
err := json.Unmarshal(common.StringToByteSlice(data), &streamResponse)
|
||||||
switch relayMode {
|
if err != nil {
|
||||||
case RelayModeChatCompletions:
|
common.SysError("error unmarshalling stream response: " + err.Error())
|
||||||
var streamResponses []ChatCompletionsStreamResponseSimple
|
continue // just ignore the error
|
||||||
err := json.Unmarshal(common.StringToByteSlice(streamResp), &streamResponses)
|
}
|
||||||
if err != nil {
|
for _, choice := range streamResponse.Choices {
|
||||||
common.SysError("error unmarshalling stream response: " + err.Error())
|
responseText += choice.Delta.Content
|
||||||
wg.Done()
|
}
|
||||||
return // just ignore the error
|
case RelayModeCompletions:
|
||||||
}
|
var streamResponse CompletionsStreamResponse
|
||||||
for _, streamResponse := range streamResponses {
|
err := json.Unmarshal(common.StringToByteSlice(data), &streamResponse)
|
||||||
for _, choice := range streamResponse.Choices {
|
if err != nil {
|
||||||
responseTextBuilder.WriteString(choice.Delta.Content)
|
common.SysError("error unmarshalling stream response: " + err.Error())
|
||||||
}
|
continue
|
||||||
}
|
}
|
||||||
case RelayModeCompletions:
|
for _, choice := range streamResponse.Choices {
|
||||||
var streamResponses []CompletionsStreamResponse
|
responseText += choice.Text
|
||||||
err := json.Unmarshal(common.StringToByteSlice(streamResp), &streamResponses)
|
}
|
||||||
if err != nil {
|
|
||||||
common.SysError("error unmarshalling stream response: " + err.Error())
|
|
||||||
wg.Done()
|
|
||||||
return // just ignore the error
|
|
||||||
}
|
|
||||||
for _, streamResponse := range streamResponses {
|
|
||||||
for _, choice := range streamResponse.Choices {
|
|
||||||
responseTextBuilder.WriteString(choice.Text)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
wg.Done()
|
|
||||||
stopChan <- true
|
stopChan <- true
|
||||||
}()
|
}()
|
||||||
setEventStreamHeaders(c)
|
setEventStreamHeaders(c)
|
||||||
@@ -98,8 +85,7 @@ func openaiStreamHandler(c *gin.Context, resp *http.Response, relayMode int) (*O
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return errorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), ""
|
return errorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), ""
|
||||||
}
|
}
|
||||||
wg.Wait()
|
return nil, responseText
|
||||||
return nil, responseTextBuilder.String()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func openaiHandler(c *gin.Context, resp *http.Response, promptTokens int, model string) (*OpenAIErrorWithStatusCode, *Usage) {
|
func openaiHandler(c *gin.Context, resp *http.Response, promptTokens int, model string) (*OpenAIErrorWithStatusCode, *Usage) {
|
||||||
|
|||||||
@@ -1,13 +1,10 @@
|
|||||||
import React, {useContext, useEffect, useState} from 'react';
|
import React, {useContext, useEffect, useState} from 'react';
|
||||||
import {
|
|
||||||
Modal,
|
|
||||||
} from 'semantic-ui-react';
|
|
||||||
import {Link, useNavigate, useSearchParams} from 'react-router-dom';
|
import {Link, useNavigate, useSearchParams} from 'react-router-dom';
|
||||||
import {UserContext} from '../context/User';
|
import {UserContext} from '../context/User';
|
||||||
import {API, getLogo, isMobile, showError, showInfo, showSuccess, showWarning} from '../helpers';
|
import {API, getLogo, isMobile, showError, showInfo, showSuccess, showWarning} from '../helpers';
|
||||||
import {onGitHubOAuthClicked} from './utils';
|
import {onGitHubOAuthClicked} from './utils';
|
||||||
import Turnstile from "react-turnstile";
|
import Turnstile from "react-turnstile";
|
||||||
import {Layout, Card, Image, Form, Button, Divider} from "@douyinfe/semi-ui";
|
import {Layout, Card, Image, Form, Button, Divider, Modal} from "@douyinfe/semi-ui";
|
||||||
import Title from "@douyinfe/semi-ui/lib/es/typography/title";
|
import Title from "@douyinfe/semi-ui/lib/es/typography/title";
|
||||||
import Text from "@douyinfe/semi-ui/lib/es/typography/text";
|
import Text from "@douyinfe/semi-ui/lib/es/typography/text";
|
||||||
|
|
||||||
@@ -92,8 +89,7 @@ const LoginForm = () => {
|
|||||||
localStorage.setItem('user', JSON.stringify(data));
|
localStorage.setItem('user', JSON.stringify(data));
|
||||||
showSuccess('登录成功!');
|
showSuccess('登录成功!');
|
||||||
if (username === 'root' && password === '123456') {
|
if (username === 'root' && password === '123456') {
|
||||||
showWarning('请立刻修改默认密码!');
|
Modal.error({title: '您正在使用默认密码!', content: '请立刻修改默认密码!', centered: true});
|
||||||
Modal.error({title: '您正在使用默认密码!', content: '请立刻修改默认密码!'});
|
|
||||||
}
|
}
|
||||||
navigate('/token');
|
navigate('/token');
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -80,7 +80,6 @@ const UsersTable = () => {
|
|||||||
<Popconfirm
|
<Popconfirm
|
||||||
title="确定?"
|
title="确定?"
|
||||||
okType={'warning'}
|
okType={'warning'}
|
||||||
position={'left'}
|
|
||||||
onConfirm={() => {
|
onConfirm={() => {
|
||||||
manageUser(record.username, 'promote', record)
|
manageUser(record.username, 'promote', record)
|
||||||
}}
|
}}
|
||||||
@@ -90,7 +89,6 @@ const UsersTable = () => {
|
|||||||
<Popconfirm
|
<Popconfirm
|
||||||
title="确定?"
|
title="确定?"
|
||||||
okType={'warning'}
|
okType={'warning'}
|
||||||
position={'left'}
|
|
||||||
onConfirm={() => {
|
onConfirm={() => {
|
||||||
manageUser(record.username, 'demote', record)
|
manageUser(record.username, 'demote', record)
|
||||||
}}
|
}}
|
||||||
|
|||||||
Reference in New Issue
Block a user