text_embedding_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package model_entities
  2. import (
  3. "testing"
  4. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  5. )
  6. func TestTextEmbeddingFullFunction(t *testing.T) {
  7. const (
  8. text_embedding = `
  9. {
  10. "model": "text_embedding",
  11. "embeddings": [[
  12. 0.1, 0.2, 0.3
  13. ]],
  14. "usage" : {
  15. "tokens": 3,
  16. "total_tokens": 100,
  17. "unit_price": 0.1,
  18. "price_unit": 1,
  19. "total_price": 10,
  20. "currency": "usd",
  21. "latency": 0.1
  22. }
  23. }`
  24. )
  25. _, err := parser.UnmarshalJsonBytes[TextEmbeddingResult]([]byte(text_embedding))
  26. if err != nil {
  27. t.Error(err)
  28. }
  29. }
  30. func TestTextEmbeddingWrongUsage(t *testing.T) {
  31. const (
  32. text_embedding = `
  33. {
  34. "model": "text_embedding",
  35. "embeddings": [[
  36. 0.1, 0.2, 0.3
  37. ]],
  38. "usage" : {
  39. "tokens": 3,
  40. "total_tokens": 100,
  41. "unit_price": 0.1,
  42. "price_unit": 1,
  43. "total_price": 10,
  44. "currency": "usd"
  45. }
  46. }`
  47. )
  48. _, err := parser.UnmarshalJsonBytes[TextEmbeddingResult]([]byte(text_embedding))
  49. if err == nil {
  50. t.Error("should have error")
  51. }
  52. }
  53. func TestTextEmbeddingWrongEmbeddings(t *testing.T) {
  54. const (
  55. text_embedding = `
  56. {
  57. "model": "text_embedding",
  58. "embeddings": [
  59. 0.1, 0.2, 0.3
  60. ],
  61. "usage" : {
  62. "tokens": 3,
  63. "total_tokens": 100,
  64. "unit_price": 0.1,
  65. "price_unit": 1,
  66. "total_price": 10,
  67. "currency": "usd",
  68. "latency": 0.1
  69. }
  70. }`
  71. )
  72. _, err := parser.UnmarshalJsonBytes[TextEmbeddingResult]([]byte(text_embedding))
  73. if err == nil {
  74. t.Error("should have error")
  75. }
  76. }