File size: 1,798 Bytes
94753b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { InferenceOutputError } from "../../lib/InferenceOutputError";
import type { BaseArgs, Options } from "../../types";
import { request } from "../custom/request";

export type TableQuestionAnsweringArgs = BaseArgs & {
	inputs: {
		/**
		 * The query in plain text that you want to ask the table
		 */
		query: string;
		/**
		 * 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: Record<string, string[]>;
	};
};

export interface TableQuestionAnsweringOutput {
	/**
	 * The aggregator used to get the answer
	 */
	aggregator: string;
	/**
	 * The plaintext answer
	 */
	answer: string;
	/**
	 * A list of coordinates of the cells contents
	 */
	cells: string[];
	/**
	 * a list of coordinates of the cells referenced in the answer
	 */
	coordinates: number[][];
}

/**
 * Don’t know SQL? Don’t want to dive into a large spreadsheet? Ask questions in plain english! Recommended model: google/tapas-base-finetuned-wtq.
 */
export async function tableQuestionAnswering(
	args: TableQuestionAnsweringArgs,
	options?: Options
): Promise<TableQuestionAnsweringOutput> {
	const res = await request<TableQuestionAnsweringOutput>(args, {
		...options,
		taskHint: "table-question-answering",
	});
	const isValidOutput =
		typeof res?.aggregator === "string" &&
		typeof res.answer === "string" &&
		Array.isArray(res.cells) &&
		res.cells.every((x) => typeof x === "string") &&
		Array.isArray(res.coordinates) &&
		res.coordinates.every((coord) => Array.isArray(coord) && coord.every((x) => typeof x === "number"));
	if (!isValidOutput) {
		throw new InferenceOutputError(
			"Expected {aggregator: string, answer: string, cells: string[], coordinates: number[][]}"
		);
	}
	return res;
}