-
Notifications
You must be signed in to change notification settings - Fork 6
/
table_question_answering.go
64 lines (51 loc) · 2.01 KB
/
table_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
63
64
package huggingface
import (
"context"
"encoding/json"
"errors"
)
// Request structure for table question answering model
type TableQuestionAnsweringRequest struct {
Inputs TableQuestionAnsweringInputs `json:"inputs"`
Options Options `json:"options,omitempty"`
Model string `json:"-"`
}
type TableQuestionAnsweringInputs struct {
// (Required) The query in plain text that you want to ask the table
Query string `json:"query"`
// (Required) A table of data represented as a dict of list where entries
// are headers and the lists are all the values, all lists must
// have the same size.
Table map[string][]string `json:"table"`
}
// Response structure for table question answering model
type TableQuestionAnsweringResponse struct {
// The plaintext answer
Answer string `json:"answer,omitempty"`
// A list of coordinates of the cells references in the answer
Coordinates [][]int `json:"coordinates,omitempty"`
// A list of coordinates of the cells contents
Cells []string `json:"cells,omitempty"`
// The aggregator used to get the answer
Aggregator string `json:"aggregator,omitempty"`
}
// TableQuestionAnswering performs table-based question answering using the specified model.
// It sends a POST request to the Hugging Face inference endpoint with the provided inputs.
// The response contains the answer or an error if the request fails.
func (ic *InferenceClient) TableQuestionAnswering(ctx context.Context, req *TableQuestionAnsweringRequest) (*TableQuestionAnsweringResponse, error) {
if req.Inputs.Query == "" {
return nil, errors.New("query is required")
}
if req.Inputs.Table == nil {
return nil, errors.New("table is required")
}
body, err := ic.post(ctx, req.Model, "table-question-answering", req)
if err != nil {
return nil, err
}
tablequestionAnsweringResponse := TableQuestionAnsweringResponse{}
if err := json.Unmarshal(body, &tablequestionAnsweringResponse); err != nil {
return nil, err
}
return &tablequestionAnsweringResponse, nil
}