github.com/hupe1980/go-huggingface@v0.0.15/table_question_answering.go (about)

     1  package huggingface
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"errors"
     7  )
     8  
     9  // Request structure for table question answering model
    10  type TableQuestionAnsweringRequest struct {
    11  	Inputs  TableQuestionAnsweringInputs `json:"inputs"`
    12  	Options Options                      `json:"options,omitempty"`
    13  	Model   string                       `json:"-"`
    14  }
    15  
    16  type TableQuestionAnsweringInputs struct {
    17  	// (Required) The query in plain text that you want to ask the table
    18  	Query string `json:"query"`
    19  
    20  	// (Required) A table of data represented as a dict of list where entries
    21  	// are headers and the lists are all the values, all lists must
    22  	// have the same size.
    23  	Table map[string][]string `json:"table"`
    24  }
    25  
    26  // Response structure for table question answering model
    27  type TableQuestionAnsweringResponse struct {
    28  	// The plaintext answer
    29  	Answer string `json:"answer,omitempty"`
    30  
    31  	// A list of coordinates of the cells references in the answer
    32  	Coordinates [][]int `json:"coordinates,omitempty"`
    33  
    34  	// A list of coordinates of the cells contents
    35  	Cells []string `json:"cells,omitempty"`
    36  
    37  	// The aggregator used to get the answer
    38  	Aggregator string `json:"aggregator,omitempty"`
    39  }
    40  
    41  // TableQuestionAnswering performs table-based question answering using the specified model.
    42  // It sends a POST request to the Hugging Face inference endpoint with the provided inputs.
    43  // The response contains the answer or an error if the request fails.
    44  func (ic *InferenceClient) TableQuestionAnswering(ctx context.Context, req *TableQuestionAnsweringRequest) (*TableQuestionAnsweringResponse, error) {
    45  	if req.Inputs.Query == "" {
    46  		return nil, errors.New("query is required")
    47  	}
    48  
    49  	if req.Inputs.Table == nil {
    50  		return nil, errors.New("table is required")
    51  	}
    52  
    53  	body, err := ic.post(ctx, req.Model, "table-question-answering", req)
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	tablequestionAnsweringResponse := TableQuestionAnsweringResponse{}
    59  	if err := json.Unmarshal(body, &tablequestionAnsweringResponse); err != nil {
    60  		return nil, err
    61  	}
    62  
    63  	return &tablequestionAnsweringResponse, nil
    64  }