2024-07-06 17:09:22 +08:00
|
|
|
package dto
|
|
|
|
|
|
2025-08-14 20:05:06 +08:00
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"one-api/types"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2024-07-06 17:09:22 +08:00
|
|
|
type RerankRequest struct {
|
2024-08-16 18:27:26 +08:00
|
|
|
Documents []any `json:"documents"`
|
|
|
|
|
Query string `json:"query"`
|
|
|
|
|
Model string `json:"model"`
|
2025-07-03 15:45:32 +08:00
|
|
|
TopN int `json:"top_n,omitempty"`
|
2025-03-17 16:44:53 +08:00
|
|
|
ReturnDocuments *bool `json:"return_documents,omitempty"`
|
2024-08-16 18:27:26 +08:00
|
|
|
MaxChunkPerDoc int `json:"max_chunk_per_doc,omitempty"`
|
|
|
|
|
OverLapTokens int `json:"overlap_tokens,omitempty"`
|
2024-07-06 17:09:22 +08:00
|
|
|
}
|
|
|
|
|
|
2025-08-14 20:05:06 +08:00
|
|
|
func (r *RerankRequest) IsStream(c *gin.Context) bool {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *RerankRequest) GetTokenCountMeta() *types.TokenCountMeta {
|
|
|
|
|
var texts = make([]string, 0)
|
|
|
|
|
|
|
|
|
|
for _, document := range r.Documents {
|
|
|
|
|
texts = append(texts, fmt.Sprintf("%v", document))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if r.Query != "" {
|
|
|
|
|
texts = append(texts, r.Query)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &types.TokenCountMeta{
|
|
|
|
|
CombineText: strings.Join(texts, "\n"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-23 13:12:15 +08:00
|
|
|
func (r *RerankRequest) SetModelName(modelName string) {
|
|
|
|
|
if modelName != "" {
|
|
|
|
|
r.Model = modelName
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-17 16:44:53 +08:00
|
|
|
func (r *RerankRequest) GetReturnDocuments() bool {
|
|
|
|
|
if r.ReturnDocuments == nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return *r.ReturnDocuments
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-16 21:06:29 +08:00
|
|
|
type RerankResponseResult struct {
|
2024-08-16 18:27:26 +08:00
|
|
|
Document any `json:"document,omitempty"`
|
2024-07-06 17:09:22 +08:00
|
|
|
Index int `json:"index"`
|
|
|
|
|
RelevanceScore float64 `json:"relevance_score"`
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-16 21:06:29 +08:00
|
|
|
type RerankDocument struct {
|
|
|
|
|
Text any `json:"text"`
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-06 17:09:22 +08:00
|
|
|
type RerankResponse struct {
|
2025-03-16 21:06:29 +08:00
|
|
|
Results []RerankResponseResult `json:"results"`
|
|
|
|
|
Usage Usage `json:"usage"`
|
2024-07-06 17:09:22 +08:00
|
|
|
}
|