File size: 2,819 Bytes
b2ecf7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type {
	WidgetExample,
	WidgetExampleAssetAndPromptInput,
	WidgetExampleAssetAndTextInput,
	WidgetExampleAssetAndZeroShotInput,
	WidgetExampleAssetInput,
	WidgetExampleSentenceSimilarityInput,
	WidgetExampleStructuredDataInput,
	WidgetExampleTableDataInput,
	WidgetExampleTextAndContextInput,
	WidgetExampleTextAndTableInput,
	WidgetExampleTextInput,
	WidgetExampleZeroShotTextInput,
} from "./WidgetExample.js";

export function isTextInput<TOutput>(sample: WidgetExample<TOutput>): sample is WidgetExampleTextInput<TOutput> {
	return "text" in sample;
}

export function isTextAndContextInput<TOutput>(
	sample: WidgetExample<TOutput>
): sample is WidgetExampleTextAndContextInput<TOutput> {
	return isTextInput(sample) && "context" in sample;
}

export function isAssetInput<TOutput>(sample: WidgetExample<TOutput>): sample is WidgetExampleAssetInput<TOutput> {
	return "src" in sample;
}

export function isAssetAndPromptInput<TOutput>(
	sample: WidgetExample<TOutput>
): sample is WidgetExampleAssetAndPromptInput<TOutput> {
	return isAssetInput(sample) && "prompt" in sample && typeof sample.prompt === "string";
}

export function isAssetAndTextInput<TOutput>(
	sample: WidgetExample<TOutput>
): sample is WidgetExampleAssetAndTextInput<TOutput> {
	return isAssetInput(sample) && isTextInput(sample);
}

export function isStructuredDataInput<TOutput>(
	sample: WidgetExample<TOutput>
): sample is WidgetExampleStructuredDataInput<TOutput> {
	return "structured_data" in sample;
}

export function isTableDataInput<TOutput>(
	sample: WidgetExample<TOutput>
): sample is WidgetExampleTableDataInput<TOutput> {
	return "table" in sample;
}

function _isZeroShotTextInput<TOutput>(
	sample: WidgetExample<TOutput>
): sample is Exclude<WidgetExampleZeroShotTextInput<TOutput>, "text"> {
	return "candidate_labels" in sample && "multi_class" in sample;
}

export function isZeroShotTextInput<TOutput>(
	sample: WidgetExample<TOutput>
): sample is WidgetExampleZeroShotTextInput<TOutput> {
	return isTextInput(sample) && _isZeroShotTextInput(sample);
}

export function isSentenceSimilarityInput<TOutput>(
	sample: WidgetExample<TOutput>
): sample is WidgetExampleSentenceSimilarityInput<TOutput> {
	return "source_sentence" in sample && "sentences" in sample;
}

export function isTextAndTableInput<TOutput>(
	sample: WidgetExample<TOutput>
): sample is WidgetExampleTextAndTableInput<TOutput> {
	return (
		isTextInput(sample) &&
		"table" in sample &&
		Array.isArray(sample.table) &&
		sample.table.every((r) => Array.isArray(r) && r.every((c) => typeof c === "string" || typeof c === "number"))
	);
}

export function isAssetAndZeroShotInput<TOutput>(
	sample: WidgetExample<TOutput>
): sample is WidgetExampleAssetAndZeroShotInput<TOutput> {
	return isAssetInput(sample) && _isZeroShotTextInput(sample);
}