embedding.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package dto
  2. import (
  3. "one-api/types"
  4. "strings"
  5. "github.com/gin-gonic/gin"
  6. )
  7. type EmbeddingOptions struct {
  8. Seed int `json:"seed,omitempty"`
  9. Temperature *float64 `json:"temperature,omitempty"`
  10. TopK int `json:"top_k,omitempty"`
  11. TopP *float64 `json:"top_p,omitempty"`
  12. FrequencyPenalty *float64 `json:"frequency_penalty,omitempty"`
  13. PresencePenalty *float64 `json:"presence_penalty,omitempty"`
  14. NumPredict int `json:"num_predict,omitempty"`
  15. NumCtx int `json:"num_ctx,omitempty"`
  16. }
  17. type EmbeddingRequest struct {
  18. Model string `json:"model"`
  19. Input any `json:"input"`
  20. EncodingFormat string `json:"encoding_format,omitempty"`
  21. Dimensions int `json:"dimensions,omitempty"`
  22. User string `json:"user,omitempty"`
  23. Seed float64 `json:"seed,omitempty"`
  24. Temperature *float64 `json:"temperature,omitempty"`
  25. TopP float64 `json:"top_p,omitempty"`
  26. FrequencyPenalty float64 `json:"frequency_penalty,omitempty"`
  27. PresencePenalty float64 `json:"presence_penalty,omitempty"`
  28. }
  29. func (r *EmbeddingRequest) GetTokenCountMeta() *types.TokenCountMeta {
  30. var texts = make([]string, 0)
  31. inputs := r.ParseInput()
  32. for _, input := range inputs {
  33. texts = append(texts, input)
  34. }
  35. return &types.TokenCountMeta{
  36. CombineText: strings.Join(texts, "\n"),
  37. }
  38. }
  39. func (r *EmbeddingRequest) IsStream(c *gin.Context) bool {
  40. return false
  41. }
  42. func (r *EmbeddingRequest) ParseInput() []string {
  43. if r.Input == nil {
  44. return make([]string, 0)
  45. }
  46. var input []string
  47. switch r.Input.(type) {
  48. case string:
  49. input = []string{r.Input.(string)}
  50. case []any:
  51. input = make([]string, 0, len(r.Input.([]any)))
  52. for _, item := range r.Input.([]any) {
  53. if str, ok := item.(string); ok {
  54. input = append(input, str)
  55. }
  56. }
  57. }
  58. return input
  59. }
  60. type EmbeddingResponseItem struct {
  61. Object string `json:"object"`
  62. Index int `json:"index"`
  63. Embedding []float64 `json:"embedding"`
  64. }
  65. type EmbeddingResponse struct {
  66. Object string `json:"object"`
  67. Data []EmbeddingResponseItem `json:"data"`
  68. Model string `json:"model"`
  69. Usage `json:"usage"`
  70. }