-
Notifications
You must be signed in to change notification settings - Fork 5
/
question_answering.go
62 lines (48 loc) · 1.62 KB
/
question_answering.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
package hfapigo
import (
"encoding/json"
"errors"
)
const RecommendedQuestionAnsweringModel = "bert-large-uncased-whole-word-masking-finetuned-squad"
// Request structure for question answering model
type QuestionAnsweringRequest struct {
// (Required)
Inputs QuestionAnsweringInputs `json:"inputs,omitempty"`
Options Options `json:"options,omitempty"`
}
type QuestionAnsweringInputs struct {
// (Required) The question as a string that has an answer within Context.
Question string `json:"question,omitempty"`
// (Required) A string that contains the answer to the question
Context string `json:"context,omitempty"`
}
// Response structure for question answering model
type QuestionAnsweringResponse struct {
// A string that’s the answer within the Context text.
Answer string `json:"answer,omitempty"`
// A float that represents how likely that the answer is correct.
Score float64 `json:"score,omitempty"`
// The string index of the start of the answer within Context.
Start int `json:"start,omitempty"`
// The string index of the stop of the answer within Context.
End int `json:"end,omitempty"`
}
func SendQuestionAnsweringRequest(model string, request *QuestionAnsweringRequest) (*QuestionAnsweringResponse, error) {
if request == nil {
return nil, errors.New("nil QuestionAnsweringRequest")
}
jsonBuf, err := json.Marshal(request)
if err != nil {
return nil, err
}
respBody, err := MakeHFAPIRequest(jsonBuf, model)
if err != nil {
return nil, err
}
qaResp := QuestionAnsweringResponse{}
err = json.Unmarshal(respBody, &qaResp)
if err != nil {
return nil, err
}
return &qaResp, nil
}