-
Notifications
You must be signed in to change notification settings - Fork 5
/
conversational_test.go
115 lines (102 loc) · 3 KB
/
conversational_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package hfapigo_test
import (
"encoding/json"
"testing"
"github.com/Kardbord/hfapigo/v3"
"github.com/google/go-cmp/cmp"
)
func TestMarshalUnMarshalConversationalRequest(t *testing.T) {
// No options
{
crExpected := hfapigo.ConversationalRequest{
Inputs: hfapigo.ConverstationalInputs{
Text: "Hey my name is Julien! How are you?",
},
}
jsonBuf, err := json.Marshal(crExpected)
if err != nil {
t.Fatal(err)
}
crActual := hfapigo.ConversationalRequest{}
err = json.Unmarshal(jsonBuf, &crActual)
if err != nil {
t.Fatal(err)
}
if !cmp.Equal(crExpected, crActual) {
t.Fatalf("Expected %v, got %v", crExpected, crActual)
}
}
// Options
{
crExpected := hfapigo.ConversationalRequest{
Inputs: hfapigo.ConverstationalInputs{
Text: "Hey my name is Julien! How are you?",
},
Parameters: *(&hfapigo.ConversationalParameters{}).
SetTempurature(0.2345).
SetMinLength(10).
SetMaxLength(20).
SetRepetitionPenalty(20),
Options: *hfapigo.NewOptions().SetWaitForModel(true),
}
jsonBuf, err := json.Marshal(crExpected)
if err != nil {
t.Fatal(err)
}
crActual := hfapigo.ConversationalRequest{}
err = json.Unmarshal(jsonBuf, &crActual)
if err != nil {
t.Fatal(err)
}
if !cmp.Equal(crExpected, crActual) {
t.Fatalf("Expected %v, got %v", crExpected, crActual)
}
}
}
func TestConversationalRequest(t *testing.T) {
t.Skip("The conversational endpoint seems to be undergoing deprecation/refactor. Disabling the unit tests for it until more information is available and updates can be made.")
// Basic request
{
cresp, err := hfapigo.SendConversationalRequest(hfapigo.RecommendedConversationalModel, &hfapigo.ConversationalRequest{
Inputs: hfapigo.ConverstationalInputs{
Text: "Hey my name is Julien! How are you?",
},
Parameters: *(&hfapigo.ConversationalParameters{}).
SetTempurature(0.2345).
SetMinLength(10).
SetMaxLength(20).
SetRepetitionPenalty(20),
Options: *hfapigo.NewOptions().SetWaitForModel(true),
})
if err != nil {
t.Fatal(err)
}
if cresp == nil {
t.Fatal("Expected non-nil response")
}
if cresp.GeneratedText == "" {
t.Fatal("Expected generated text")
}
if len(cresp.Conversation.GeneratedResponses) == 0 {
t.Fatal("Expected non-empty previous responses")
}
if len(cresp.Conversation.PastUserInputs) == 0 {
t.Fatal("Expected non-empty previous inputs")
}
if len(cresp.Conversation.GeneratedResponses) != len(cresp.Conversation.PastUserInputs) {
t.Fatalf("Expected same number of past response and past user inputs, got %d and %d", len(cresp.Conversation.GeneratedResponses), len(cresp.Conversation.PastUserInputs))
}
}
// Invalid request
{
cresp, err := hfapigo.SendConversationalRequest(hfapigo.RecommendedConversationalModel, &hfapigo.ConversationalRequest{
Options: *hfapigo.NewOptions().SetWaitForModel(true),
})
if err == nil {
t.Fatal("Expected error - invalid request")
}
if cresp != nil {
t.Fatal("Expected nil response - invalid request")
}
}
}