2024-01-14 11:21:03 +00:00
|
|
|
|
package aiproxy
|
2023-09-03 04:51:59 +00:00
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bufio"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
2024-06-30 10:36:33 +00:00
|
|
|
|
"github.com/songquanpeng/one-api/common/render"
|
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2023-09-03 04:51:59 +00:00
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-01-28 11:38:58 +00:00
|
|
|
|
"github.com/songquanpeng/one-api/common"
|
|
|
|
|
"github.com/songquanpeng/one-api/common/helper"
|
|
|
|
|
"github.com/songquanpeng/one-api/common/logger"
|
2024-04-05 17:02:35 +00:00
|
|
|
|
"github.com/songquanpeng/one-api/common/random"
|
2024-04-05 17:36:48 +00:00
|
|
|
|
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
2024-01-28 11:38:58 +00:00
|
|
|
|
"github.com/songquanpeng/one-api/relay/constant"
|
2024-02-17 16:15:31 +00:00
|
|
|
|
"github.com/songquanpeng/one-api/relay/model"
|
2023-09-03 04:51:59 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// https://docs.aiproxy.io/dev/library#使用已经定制好的知识库进行对话问答
|
|
|
|
|
|
2024-02-17 16:15:31 +00:00
|
|
|
|
func ConvertRequest(request model.GeneralOpenAIRequest) *LibraryRequest {
|
2023-09-03 04:51:59 +00:00
|
|
|
|
query := ""
|
|
|
|
|
if len(request.Messages) != 0 {
|
2023-11-19 10:38:54 +00:00
|
|
|
|
query = request.Messages[len(request.Messages)-1].StringContent()
|
2023-09-03 04:51:59 +00:00
|
|
|
|
}
|
2024-01-14 11:21:03 +00:00
|
|
|
|
return &LibraryRequest{
|
2023-09-03 04:51:59 +00:00
|
|
|
|
Model: request.Model,
|
|
|
|
|
Stream: request.Stream,
|
|
|
|
|
Query: query,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-14 11:21:03 +00:00
|
|
|
|
func aiProxyDocuments2Markdown(documents []LibraryDocument) string {
|
2023-09-03 04:51:59 +00:00
|
|
|
|
if len(documents) == 0 {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
content := "\n\n参考文档:\n"
|
|
|
|
|
for i, document := range documents {
|
|
|
|
|
content += fmt.Sprintf("%d. [%s](%s)\n", i+1, document.Title, document.URL)
|
|
|
|
|
}
|
|
|
|
|
return content
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-14 11:21:03 +00:00
|
|
|
|
func responseAIProxyLibrary2OpenAI(response *LibraryResponse) *openai.TextResponse {
|
2023-09-03 04:51:59 +00:00
|
|
|
|
content := response.Answer + aiProxyDocuments2Markdown(response.Documents)
|
2024-01-14 11:21:03 +00:00
|
|
|
|
choice := openai.TextResponseChoice{
|
2023-09-03 04:51:59 +00:00
|
|
|
|
Index: 0,
|
2024-02-17 16:15:31 +00:00
|
|
|
|
Message: model.Message{
|
2023-09-03 04:51:59 +00:00
|
|
|
|
Role: "assistant",
|
|
|
|
|
Content: content,
|
|
|
|
|
},
|
|
|
|
|
FinishReason: "stop",
|
|
|
|
|
}
|
2024-01-14 11:21:03 +00:00
|
|
|
|
fullTextResponse := openai.TextResponse{
|
2024-04-05 17:02:35 +00:00
|
|
|
|
Id: fmt.Sprintf("chatcmpl-%s", random.GetUUID()),
|
2023-09-03 04:51:59 +00:00
|
|
|
|
Object: "chat.completion",
|
2024-01-21 15:21:42 +00:00
|
|
|
|
Created: helper.GetTimestamp(),
|
2024-01-14 11:21:03 +00:00
|
|
|
|
Choices: []openai.TextResponseChoice{choice},
|
2023-09-03 04:51:59 +00:00
|
|
|
|
}
|
|
|
|
|
return &fullTextResponse
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-14 11:21:03 +00:00
|
|
|
|
func documentsAIProxyLibrary(documents []LibraryDocument) *openai.ChatCompletionsStreamResponse {
|
|
|
|
|
var choice openai.ChatCompletionsStreamResponseChoice
|
2023-09-03 04:51:59 +00:00
|
|
|
|
choice.Delta.Content = aiProxyDocuments2Markdown(documents)
|
2024-01-14 11:21:03 +00:00
|
|
|
|
choice.FinishReason = &constant.StopFinishReason
|
|
|
|
|
return &openai.ChatCompletionsStreamResponse{
|
2024-04-05 17:02:35 +00:00
|
|
|
|
Id: fmt.Sprintf("chatcmpl-%s", random.GetUUID()),
|
2023-09-03 04:51:59 +00:00
|
|
|
|
Object: "chat.completion.chunk",
|
2024-01-21 15:21:42 +00:00
|
|
|
|
Created: helper.GetTimestamp(),
|
2023-09-03 04:51:59 +00:00
|
|
|
|
Model: "",
|
2024-01-14 11:21:03 +00:00
|
|
|
|
Choices: []openai.ChatCompletionsStreamResponseChoice{choice},
|
2023-09-03 04:51:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-14 11:21:03 +00:00
|
|
|
|
func streamResponseAIProxyLibrary2OpenAI(response *LibraryStreamResponse) *openai.ChatCompletionsStreamResponse {
|
|
|
|
|
var choice openai.ChatCompletionsStreamResponseChoice
|
2023-09-03 04:51:59 +00:00
|
|
|
|
choice.Delta.Content = response.Content
|
2024-01-14 11:21:03 +00:00
|
|
|
|
return &openai.ChatCompletionsStreamResponse{
|
2024-04-05 17:02:35 +00:00
|
|
|
|
Id: fmt.Sprintf("chatcmpl-%s", random.GetUUID()),
|
2023-09-03 04:51:59 +00:00
|
|
|
|
Object: "chat.completion.chunk",
|
2024-01-21 15:21:42 +00:00
|
|
|
|
Created: helper.GetTimestamp(),
|
2023-09-03 04:51:59 +00:00
|
|
|
|
Model: response.Model,
|
2024-01-14 11:21:03 +00:00
|
|
|
|
Choices: []openai.ChatCompletionsStreamResponseChoice{choice},
|
2023-09-03 04:51:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-17 16:15:31 +00:00
|
|
|
|
func StreamHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusCode, *model.Usage) {
|
|
|
|
|
var usage model.Usage
|
2024-06-30 10:36:33 +00:00
|
|
|
|
var documents []LibraryDocument
|
2023-09-03 04:51:59 +00:00
|
|
|
|
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 := strings.Index(string(data), "\n"); i >= 0 {
|
|
|
|
|
return i + 1, data[0:i], nil
|
|
|
|
|
}
|
|
|
|
|
if atEOF {
|
|
|
|
|
return len(data), data, nil
|
|
|
|
|
}
|
|
|
|
|
return 0, nil, nil
|
|
|
|
|
})
|
2024-06-30 10:36:33 +00:00
|
|
|
|
|
2024-01-14 11:21:03 +00:00
|
|
|
|
common.SetEventStreamHeaders(c)
|
2024-06-30 10:36:33 +00:00
|
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
|
data := scanner.Text()
|
|
|
|
|
if len(data) < 5 || data[:5] != "data:" {
|
|
|
|
|
continue
|
2023-09-03 04:51:59 +00:00
|
|
|
|
}
|
2024-06-30 10:36:33 +00:00
|
|
|
|
data = data[5:]
|
|
|
|
|
|
|
|
|
|
var AIProxyLibraryResponse LibraryStreamResponse
|
|
|
|
|
err := json.Unmarshal([]byte(data), &AIProxyLibraryResponse)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.SysError("error unmarshalling stream response: " + err.Error())
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if len(AIProxyLibraryResponse.Documents) != 0 {
|
|
|
|
|
documents = AIProxyLibraryResponse.Documents
|
|
|
|
|
}
|
|
|
|
|
response := streamResponseAIProxyLibrary2OpenAI(&AIProxyLibraryResponse)
|
|
|
|
|
err = render.ObjectData(c, response)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.SysError(err.Error())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
|
logger.SysError("error reading stream: " + err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response := documentsAIProxyLibrary(documents)
|
|
|
|
|
err := render.ObjectData(c, response)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.SysError(err.Error())
|
|
|
|
|
}
|
|
|
|
|
render.Done(c)
|
|
|
|
|
|
|
|
|
|
err = resp.Body.Close()
|
2023-09-03 04:51:59 +00:00
|
|
|
|
if err != nil {
|
2024-01-14 11:21:03 +00:00
|
|
|
|
return openai.ErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), nil
|
2023-09-03 04:51:59 +00:00
|
|
|
|
}
|
2024-06-30 10:36:33 +00:00
|
|
|
|
|
2023-09-03 04:51:59 +00:00
|
|
|
|
return nil, &usage
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-17 16:15:31 +00:00
|
|
|
|
func Handler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusCode, *model.Usage) {
|
2024-01-14 11:21:03 +00:00
|
|
|
|
var AIProxyLibraryResponse LibraryResponse
|
2023-09-03 04:51:59 +00:00
|
|
|
|
responseBody, err := io.ReadAll(resp.Body)
|
|
|
|
|
if err != nil {
|
2024-01-14 11:21:03 +00:00
|
|
|
|
return openai.ErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError), nil
|
2023-09-03 04:51:59 +00:00
|
|
|
|
}
|
|
|
|
|
err = resp.Body.Close()
|
|
|
|
|
if err != nil {
|
2024-01-14 11:21:03 +00:00
|
|
|
|
return openai.ErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), nil
|
2023-09-03 04:51:59 +00:00
|
|
|
|
}
|
|
|
|
|
err = json.Unmarshal(responseBody, &AIProxyLibraryResponse)
|
|
|
|
|
if err != nil {
|
2024-01-14 11:21:03 +00:00
|
|
|
|
return openai.ErrorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError), nil
|
2023-09-03 04:51:59 +00:00
|
|
|
|
}
|
|
|
|
|
if AIProxyLibraryResponse.ErrCode != 0 {
|
2024-02-17 16:15:31 +00:00
|
|
|
|
return &model.ErrorWithStatusCode{
|
|
|
|
|
Error: model.Error{
|
2023-09-03 04:51:59 +00:00
|
|
|
|
Message: AIProxyLibraryResponse.Message,
|
|
|
|
|
Type: strconv.Itoa(AIProxyLibraryResponse.ErrCode),
|
|
|
|
|
Code: AIProxyLibraryResponse.ErrCode,
|
|
|
|
|
},
|
|
|
|
|
StatusCode: resp.StatusCode,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
fullTextResponse := responseAIProxyLibrary2OpenAI(&AIProxyLibraryResponse)
|
|
|
|
|
jsonResponse, err := json.Marshal(fullTextResponse)
|
|
|
|
|
if err != nil {
|
2024-01-14 11:21:03 +00:00
|
|
|
|
return openai.ErrorWrapper(err, "marshal_response_body_failed", http.StatusInternalServerError), nil
|
2023-09-03 04:51:59 +00:00
|
|
|
|
}
|
|
|
|
|
c.Writer.Header().Set("Content-Type", "application/json")
|
|
|
|
|
c.Writer.WriteHeader(resp.StatusCode)
|
|
|
|
|
_, err = c.Writer.Write(jsonResponse)
|
2024-02-12 13:35:40 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return openai.ErrorWrapper(err, "write_response_body_failed", http.StatusInternalServerError), nil
|
|
|
|
|
}
|
2023-09-03 04:51:59 +00:00
|
|
|
|
return nil, &fullTextResponse.Usage
|
|
|
|
|
}
|