rerank.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package dto
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "one-api/types"
  6. "strings"
  7. )
  8. type RerankRequest struct {
  9. Documents []any `json:"documents"`
  10. Query string `json:"query"`
  11. Model string `json:"model"`
  12. TopN int `json:"top_n,omitempty"`
  13. ReturnDocuments *bool `json:"return_documents,omitempty"`
  14. MaxChunkPerDoc int `json:"max_chunk_per_doc,omitempty"`
  15. OverLapTokens int `json:"overlap_tokens,omitempty"`
  16. }
  17. func (r *RerankRequest) IsStream(c *gin.Context) bool {
  18. return false
  19. }
  20. func (r *RerankRequest) GetTokenCountMeta() *types.TokenCountMeta {
  21. var texts = make([]string, 0)
  22. for _, document := range r.Documents {
  23. texts = append(texts, fmt.Sprintf("%v", document))
  24. }
  25. if r.Query != "" {
  26. texts = append(texts, r.Query)
  27. }
  28. return &types.TokenCountMeta{
  29. CombineText: strings.Join(texts, "\n"),
  30. }
  31. }
  32. func (r *RerankRequest) GetReturnDocuments() bool {
  33. if r.ReturnDocuments == nil {
  34. return false
  35. }
  36. return *r.ReturnDocuments
  37. }
  38. type RerankResponseResult struct {
  39. Document any `json:"document,omitempty"`
  40. Index int `json:"index"`
  41. RelevanceScore float64 `json:"relevance_score"`
  42. }
  43. type RerankDocument struct {
  44. Text any `json:"text"`
  45. }
  46. type RerankResponse struct {
  47. Results []RerankResponseResult `json:"results"`
  48. Usage Usage `json:"usage"`
  49. }