valyu-js 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +42 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.js +134 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +99 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +36 -13
- package/src/api.js +0 -136
- package/src/types.js +0 -36
- package/tests/test.js +0 -39
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
type SearchType = "web" | "proprietary";
|
|
2
|
+
type FeedbackSentiment = "very good" | "good" | "bad" | "very bad";
|
|
3
|
+
interface SearchResponse {
|
|
4
|
+
success: boolean;
|
|
5
|
+
error?: string;
|
|
6
|
+
tx_id: string | null;
|
|
7
|
+
query: string;
|
|
8
|
+
results: Array<any>;
|
|
9
|
+
results_by_source: {
|
|
10
|
+
web: number;
|
|
11
|
+
proprietary: number;
|
|
12
|
+
};
|
|
13
|
+
total_deduction_pcm: number;
|
|
14
|
+
total_deduction_dollars: number;
|
|
15
|
+
total_characters: number;
|
|
16
|
+
}
|
|
17
|
+
interface FeedbackResponse {
|
|
18
|
+
success: boolean;
|
|
19
|
+
error?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
declare class Valyu {
|
|
23
|
+
private baseUrl;
|
|
24
|
+
private headers;
|
|
25
|
+
constructor(apiKey?: string, baseUrl?: string);
|
|
26
|
+
context({ query, searchType, maxNumResults, queryRewrite, similarityThreshold, maxPrice, dataSources }: {
|
|
27
|
+
query: string;
|
|
28
|
+
searchType: SearchType;
|
|
29
|
+
maxNumResults?: number;
|
|
30
|
+
queryRewrite?: boolean;
|
|
31
|
+
similarityThreshold?: number;
|
|
32
|
+
maxPrice?: number;
|
|
33
|
+
dataSources?: string[];
|
|
34
|
+
}): Promise<SearchResponse>;
|
|
35
|
+
feedback({ tx_id, feedback, sentiment }: {
|
|
36
|
+
tx_id: string;
|
|
37
|
+
feedback: string;
|
|
38
|
+
sentiment: FeedbackSentiment;
|
|
39
|
+
}): Promise<FeedbackResponse>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export { type FeedbackResponse, type FeedbackSentiment, type SearchResponse, type SearchType, Valyu };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
type SearchType = "web" | "proprietary";
|
|
2
|
+
type FeedbackSentiment = "very good" | "good" | "bad" | "very bad";
|
|
3
|
+
interface SearchResponse {
|
|
4
|
+
success: boolean;
|
|
5
|
+
error?: string;
|
|
6
|
+
tx_id: string | null;
|
|
7
|
+
query: string;
|
|
8
|
+
results: Array<any>;
|
|
9
|
+
results_by_source: {
|
|
10
|
+
web: number;
|
|
11
|
+
proprietary: number;
|
|
12
|
+
};
|
|
13
|
+
total_deduction_pcm: number;
|
|
14
|
+
total_deduction_dollars: number;
|
|
15
|
+
total_characters: number;
|
|
16
|
+
}
|
|
17
|
+
interface FeedbackResponse {
|
|
18
|
+
success: boolean;
|
|
19
|
+
error?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
declare class Valyu {
|
|
23
|
+
private baseUrl;
|
|
24
|
+
private headers;
|
|
25
|
+
constructor(apiKey?: string, baseUrl?: string);
|
|
26
|
+
context({ query, searchType, maxNumResults, queryRewrite, similarityThreshold, maxPrice, dataSources }: {
|
|
27
|
+
query: string;
|
|
28
|
+
searchType: SearchType;
|
|
29
|
+
maxNumResults?: number;
|
|
30
|
+
queryRewrite?: boolean;
|
|
31
|
+
similarityThreshold?: number;
|
|
32
|
+
maxPrice?: number;
|
|
33
|
+
dataSources?: string[];
|
|
34
|
+
}): Promise<SearchResponse>;
|
|
35
|
+
feedback({ tx_id, feedback, sentiment }: {
|
|
36
|
+
tx_id: string;
|
|
37
|
+
feedback: string;
|
|
38
|
+
sentiment: FeedbackSentiment;
|
|
39
|
+
}): Promise<FeedbackResponse>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export { type FeedbackResponse, type FeedbackSentiment, type SearchResponse, type SearchType, Valyu };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
Valyu: () => Valyu
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(index_exports);
|
|
36
|
+
var import_axios = __toESM(require("axios"));
|
|
37
|
+
var Valyu = class {
|
|
38
|
+
constructor(apiKey, baseUrl = "https://api.valyu.network/v1") {
|
|
39
|
+
if (!apiKey) {
|
|
40
|
+
apiKey = process.env.VALYU_API_KEY;
|
|
41
|
+
if (!apiKey) {
|
|
42
|
+
throw new Error("VALYU_API_KEY is not set");
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
this.baseUrl = baseUrl;
|
|
46
|
+
this.headers = {
|
|
47
|
+
"Content-Type": "application/json",
|
|
48
|
+
"x-api-key": apiKey
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
async context({
|
|
52
|
+
query,
|
|
53
|
+
searchType,
|
|
54
|
+
maxNumResults = 10,
|
|
55
|
+
queryRewrite = true,
|
|
56
|
+
similarityThreshold = 0.4,
|
|
57
|
+
maxPrice = 1,
|
|
58
|
+
dataSources
|
|
59
|
+
}) {
|
|
60
|
+
try {
|
|
61
|
+
const payload = {
|
|
62
|
+
query,
|
|
63
|
+
search_type: searchType,
|
|
64
|
+
max_num_results: maxNumResults,
|
|
65
|
+
query_rewrite: queryRewrite,
|
|
66
|
+
similarity_threshold: similarityThreshold,
|
|
67
|
+
max_price: maxPrice
|
|
68
|
+
};
|
|
69
|
+
if (dataSources !== void 0) {
|
|
70
|
+
payload.data_sources = dataSources;
|
|
71
|
+
}
|
|
72
|
+
const response = await import_axios.default.post(
|
|
73
|
+
`${this.baseUrl}/knowledge`,
|
|
74
|
+
payload,
|
|
75
|
+
{ headers: this.headers }
|
|
76
|
+
);
|
|
77
|
+
if (!response.status || response.status < 200 || response.status >= 300) {
|
|
78
|
+
return {
|
|
79
|
+
success: false,
|
|
80
|
+
error: response.data?.error,
|
|
81
|
+
tx_id: null,
|
|
82
|
+
query,
|
|
83
|
+
results: [],
|
|
84
|
+
results_by_source: { web: 0, proprietary: 0 },
|
|
85
|
+
total_deduction_pcm: 0,
|
|
86
|
+
total_deduction_dollars: 0,
|
|
87
|
+
total_characters: 0
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
return response.data;
|
|
91
|
+
} catch (e) {
|
|
92
|
+
return {
|
|
93
|
+
success: false,
|
|
94
|
+
error: e.message,
|
|
95
|
+
tx_id: null,
|
|
96
|
+
query,
|
|
97
|
+
results: [],
|
|
98
|
+
results_by_source: { web: 0, proprietary: 0 },
|
|
99
|
+
total_deduction_pcm: 0,
|
|
100
|
+
total_deduction_dollars: 0,
|
|
101
|
+
total_characters: 0
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
async feedback({
|
|
106
|
+
tx_id,
|
|
107
|
+
feedback,
|
|
108
|
+
sentiment
|
|
109
|
+
}) {
|
|
110
|
+
try {
|
|
111
|
+
const payload = {
|
|
112
|
+
tx_id,
|
|
113
|
+
feedback,
|
|
114
|
+
sentiment: sentiment.toLowerCase()
|
|
115
|
+
};
|
|
116
|
+
const response = await import_axios.default.post(
|
|
117
|
+
`${this.baseUrl}/feedback`,
|
|
118
|
+
payload,
|
|
119
|
+
{ headers: this.headers }
|
|
120
|
+
);
|
|
121
|
+
return response.data;
|
|
122
|
+
} catch (e) {
|
|
123
|
+
return {
|
|
124
|
+
success: false,
|
|
125
|
+
error: e.message
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
131
|
+
0 && (module.exports = {
|
|
132
|
+
Valyu
|
|
133
|
+
});
|
|
134
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import axios from 'axios';\nimport { SearchResponse, SearchType, FeedbackSentiment, FeedbackResponse } from './types';\n\nexport class Valyu {\n private baseUrl: string;\n private headers: Record<string, string>;\n\n constructor(apiKey?: string, baseUrl: string = \"https://api.valyu.network/v1\") {\n if (!apiKey) {\n apiKey = process.env.VALYU_API_KEY;\n if (!apiKey) {\n throw new Error(\"VALYU_API_KEY is not set\");\n }\n }\n this.baseUrl = baseUrl;\n this.headers = {\n \"Content-Type\": \"application/json\",\n \"x-api-key\": apiKey\n };\n }\n\n async context({\n query,\n searchType,\n maxNumResults = 10,\n queryRewrite = true,\n similarityThreshold = 0.4,\n maxPrice = 1,\n dataSources\n }: {\n query: string;\n searchType: SearchType;\n maxNumResults?: number;\n queryRewrite?: boolean;\n similarityThreshold?: number;\n maxPrice?: number;\n dataSources?: string[];\n }): Promise<SearchResponse> {\n try {\n const payload: Record<string, any> = {\n query,\n search_type: searchType,\n max_num_results: maxNumResults,\n query_rewrite: queryRewrite,\n similarity_threshold: similarityThreshold,\n max_price: maxPrice\n };\n\n if (dataSources !== undefined) {\n payload.data_sources = dataSources;\n }\n\n const response = await axios.post(\n `${this.baseUrl}/knowledge`,\n payload,\n { headers: this.headers }\n );\n\n if (!response.status || response.status < 200 || response.status >= 300) {\n return {\n success: false,\n error: response.data?.error,\n tx_id: null,\n query,\n results: [],\n results_by_source: { web: 0, proprietary: 0 },\n total_deduction_pcm: 0.0,\n total_deduction_dollars: 0.0,\n total_characters: 0\n };\n }\n\n return response.data;\n } catch (e: any) {\n return {\n success: false,\n error: e.message,\n tx_id: null,\n query,\n results: [],\n results_by_source: { web: 0, proprietary: 0 },\n total_deduction_pcm: 0.0,\n total_deduction_dollars: 0.0,\n total_characters: 0\n };\n }\n }\n\n async feedback({\n tx_id,\n feedback,\n sentiment\n }: {\n tx_id: string;\n feedback: string;\n sentiment: FeedbackSentiment;\n }): Promise<FeedbackResponse> {\n try {\n const payload = {\n tx_id,\n feedback,\n sentiment: sentiment.toLowerCase()\n };\n\n const response = await axios.post(\n `${this.baseUrl}/feedback`,\n payload,\n { headers: this.headers }\n );\n\n return response.data;\n } catch (e: any) {\n return {\n success: false,\n error: e.message\n };\n }\n }\n}\n\nexport type { \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AEA,aAAO,SAAS;AAAA,IAClB,SAAS,GAAQ;AACf,aAAO;AAAA,QACL,SAAS;AAAA,QACT,OAAO,EAAE;AAAA,QACT,OAAO;AAAA,QACP;AAAA,QACA,SAAS,CAAC;AAAA,QACV,mBAAmB,EAAE,KAAK,GAAG,aAAa,EAAE;AAAA,QAC5C,qBAAqB;AAAA,QACrB,yBAAyB;AAAA,QACzB,kBAAkB;AAAA,MACpB;AAAA,IACF;AAAA,EACF;AAAA,EAEA,MAAM,SAAS;AAAA,IACb;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAI8B;AAC5B,QAAI;AACF,YAAM,UAAU;AAAA,QACd;AAAA,QACA;AAAA,QACA,WAAW,UAAU,YAAY;AAAA,MACnC;AAEA,YAAM,WAAW,MAAM,aAAAA,QAAM;AAAA,QAC3B,GAAG,KAAK,OAAO;AAAA,QACf;AAAA,QACA,EAAE,SAAS,KAAK,QAAQ;AAAA,MAC1B;AAEA,aAAO,SAAS;AAAA,IAClB,SAAS,GAAQ;AACf,aAAO;AAAA,QACL,SAAS;AAAA,QACT,OAAO,EAAE;AAAA,MACX;AAAA,IACF;AAAA,EACF;AACF;","names":["axios"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import axios from "axios";
|
|
3
|
+
var Valyu = class {
|
|
4
|
+
constructor(apiKey, baseUrl = "https://api.valyu.network/v1") {
|
|
5
|
+
if (!apiKey) {
|
|
6
|
+
apiKey = process.env.VALYU_API_KEY;
|
|
7
|
+
if (!apiKey) {
|
|
8
|
+
throw new Error("VALYU_API_KEY is not set");
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
this.baseUrl = baseUrl;
|
|
12
|
+
this.headers = {
|
|
13
|
+
"Content-Type": "application/json",
|
|
14
|
+
"x-api-key": apiKey
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
async context({
|
|
18
|
+
query,
|
|
19
|
+
searchType,
|
|
20
|
+
maxNumResults = 10,
|
|
21
|
+
queryRewrite = true,
|
|
22
|
+
similarityThreshold = 0.4,
|
|
23
|
+
maxPrice = 1,
|
|
24
|
+
dataSources
|
|
25
|
+
}) {
|
|
26
|
+
try {
|
|
27
|
+
const payload = {
|
|
28
|
+
query,
|
|
29
|
+
search_type: searchType,
|
|
30
|
+
max_num_results: maxNumResults,
|
|
31
|
+
query_rewrite: queryRewrite,
|
|
32
|
+
similarity_threshold: similarityThreshold,
|
|
33
|
+
max_price: maxPrice
|
|
34
|
+
};
|
|
35
|
+
if (dataSources !== void 0) {
|
|
36
|
+
payload.data_sources = dataSources;
|
|
37
|
+
}
|
|
38
|
+
const response = await axios.post(
|
|
39
|
+
`${this.baseUrl}/knowledge`,
|
|
40
|
+
payload,
|
|
41
|
+
{ headers: this.headers }
|
|
42
|
+
);
|
|
43
|
+
if (!response.status || response.status < 200 || response.status >= 300) {
|
|
44
|
+
return {
|
|
45
|
+
success: false,
|
|
46
|
+
error: response.data?.error,
|
|
47
|
+
tx_id: null,
|
|
48
|
+
query,
|
|
49
|
+
results: [],
|
|
50
|
+
results_by_source: { web: 0, proprietary: 0 },
|
|
51
|
+
total_deduction_pcm: 0,
|
|
52
|
+
total_deduction_dollars: 0,
|
|
53
|
+
total_characters: 0
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
return response.data;
|
|
57
|
+
} catch (e) {
|
|
58
|
+
return {
|
|
59
|
+
success: false,
|
|
60
|
+
error: e.message,
|
|
61
|
+
tx_id: null,
|
|
62
|
+
query,
|
|
63
|
+
results: [],
|
|
64
|
+
results_by_source: { web: 0, proprietary: 0 },
|
|
65
|
+
total_deduction_pcm: 0,
|
|
66
|
+
total_deduction_dollars: 0,
|
|
67
|
+
total_characters: 0
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async feedback({
|
|
72
|
+
tx_id,
|
|
73
|
+
feedback,
|
|
74
|
+
sentiment
|
|
75
|
+
}) {
|
|
76
|
+
try {
|
|
77
|
+
const payload = {
|
|
78
|
+
tx_id,
|
|
79
|
+
feedback,
|
|
80
|
+
sentiment: sentiment.toLowerCase()
|
|
81
|
+
};
|
|
82
|
+
const response = await axios.post(
|
|
83
|
+
`${this.baseUrl}/feedback`,
|
|
84
|
+
payload,
|
|
85
|
+
{ headers: this.headers }
|
|
86
|
+
);
|
|
87
|
+
return response.data;
|
|
88
|
+
} catch (e) {
|
|
89
|
+
return {
|
|
90
|
+
success: false,
|
|
91
|
+
error: e.message
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
export {
|
|
97
|
+
Valyu
|
|
98
|
+
};
|
|
99
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import axios from 'axios';\nimport { SearchResponse, SearchType, FeedbackSentiment, FeedbackResponse } from './types';\n\nexport class Valyu {\n private baseUrl: string;\n private headers: Record<string, string>;\n\n constructor(apiKey?: string, baseUrl: string = \"https://api.valyu.network/v1\") {\n if (!apiKey) {\n apiKey = process.env.VALYU_API_KEY;\n if (!apiKey) {\n throw new Error(\"VALYU_API_KEY is not set\");\n }\n }\n this.baseUrl = baseUrl;\n this.headers = {\n \"Content-Type\": \"application/json\",\n \"x-api-key\": apiKey\n };\n }\n\n async context({\n query,\n searchType,\n maxNumResults = 10,\n queryRewrite = true,\n similarityThreshold = 0.4,\n maxPrice = 1,\n dataSources\n }: {\n query: string;\n searchType: SearchType;\n maxNumResults?: number;\n queryRewrite?: boolean;\n similarityThreshold?: number;\n maxPrice?: number;\n dataSources?: string[];\n }): Promise<SearchResponse> {\n try {\n const payload: Record<string, any> = {\n query,\n search_type: searchType,\n max_num_results: maxNumResults,\n query_rewrite: queryRewrite,\n similarity_threshold: similarityThreshold,\n max_price: maxPrice\n };\n\n if (dataSources !== undefined) {\n payload.data_sources = dataSources;\n }\n\n const response = await axios.post(\n `${this.baseUrl}/knowledge`,\n payload,\n { headers: this.headers }\n );\n\n if (!response.status || response.status < 200 || response.status >= 300) {\n return {\n success: false,\n error: response.data?.error,\n tx_id: null,\n query,\n results: [],\n results_by_source: { web: 0, proprietary: 0 },\n total_deduction_pcm: 0.0,\n total_deduction_dollars: 0.0,\n total_characters: 0\n };\n }\n\n return response.data;\n } catch (e: any) {\n return {\n success: false,\n error: e.message,\n tx_id: null,\n query,\n results: [],\n results_by_source: { web: 0, proprietary: 0 },\n total_deduction_pcm: 0.0,\n total_deduction_dollars: 0.0,\n total_characters: 0\n };\n }\n }\n\n async feedback({\n tx_id,\n feedback,\n sentiment\n }: {\n tx_id: string;\n feedback: string;\n sentiment: FeedbackSentiment;\n }): Promise<FeedbackResponse> {\n try {\n const payload = {\n tx_id,\n feedback,\n sentiment: sentiment.toLowerCase()\n };\n\n const response = await axios.post(\n `${this.baseUrl}/feedback`,\n payload,\n { headers: this.headers }\n );\n\n return response.data;\n } catch (e: any) {\n return {\n success: false,\n error: e.message\n };\n }\n }\n}\n\nexport type { \n SearchResponse, \n SearchType, \n FeedbackSentiment, \n FeedbackResponse \n} from './types'; "],"mappings":";AAAA,OAAO,WAAW;AAGX,IAAM,QAAN,MAAY;AAAA,EAIjB,YAAY,QAAiB,UAAkB,gCAAgC;AAC7E,QAAI,CAAC,QAAQ;AACX,eAAS,QAAQ,IAAI;AACrB,UAAI,CAAC,QAAQ;AACX,cAAM,IAAI,MAAM,0BAA0B;AAAA,MAC5C;AAAA,IACF;AACA,SAAK,UAAU;AACf,SAAK,UAAU;AAAA,MACb,gBAAgB;AAAA,MAChB,aAAa;AAAA,IACf;AAAA,EACF;AAAA,EAEA,MAAM,QAAQ;AAAA,IACZ;AAAA,IACA;AAAA,IACA,gBAAgB;AAAA,IAChB,eAAe;AAAA,IACf,sBAAsB;AAAA,IACtB,WAAW;AAAA,IACX;AAAA,EACF,GAQ4B;AAC1B,QAAI;AACF,YAAM,UAA+B;AAAA,QACnC;AAAA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,SAAS;AAAA,QACT,OAAO,EAAE;AAAA,MACX;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,29 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "valyu-js",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "We handle the data. You handle the AI.",
|
|
5
|
-
"
|
|
5
|
+
"files": [
|
|
6
|
+
"dist"
|
|
7
|
+
],
|
|
8
|
+
"main": "./dist/index.js",
|
|
9
|
+
"module": "./dist/index.mjs",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"require": "./dist/index.js",
|
|
14
|
+
"module": "./dist/index.mjs",
|
|
15
|
+
"import": "./dist/index.mjs"
|
|
16
|
+
},
|
|
17
|
+
"./package.json": "./package.json"
|
|
18
|
+
},
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
6
20
|
"scripts": {
|
|
7
|
-
|
|
21
|
+
"build": "tsup",
|
|
22
|
+
"test": "jest",
|
|
23
|
+
"prepublishOnly": "npm run build"
|
|
8
24
|
},
|
|
9
25
|
"repository": {
|
|
10
|
-
|
|
11
|
-
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/ValyuNetwork/valyu-js.git"
|
|
12
28
|
},
|
|
13
29
|
"keywords": [
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
30
|
+
"ai",
|
|
31
|
+
"sdk",
|
|
32
|
+
"rag",
|
|
33
|
+
"valyu"
|
|
18
34
|
],
|
|
19
35
|
"author": "Valyu",
|
|
20
36
|
"license": "MIT",
|
|
21
37
|
"bugs": {
|
|
22
|
-
|
|
38
|
+
"url": "https://github.com/ValyuNetwork/valyu-js/issues"
|
|
23
39
|
},
|
|
24
40
|
"homepage": "https://valyu.network",
|
|
25
41
|
"dependencies": {
|
|
26
|
-
|
|
42
|
+
"axios": "^1.4.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@types/node": "^20.11.24",
|
|
46
|
+
"@types/jest": "^29.5.12",
|
|
47
|
+
"typescript": "^5.3.3",
|
|
48
|
+
"jest": "^29.7.0",
|
|
49
|
+
"ts-jest": "^29.1.2",
|
|
50
|
+
"tsup": "^8.0.2"
|
|
27
51
|
}
|
|
28
|
-
|
|
29
|
-
|
|
52
|
+
}
|
package/src/api.js
DELETED
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
// src/api.js
|
|
2
|
-
|
|
3
|
-
const axios = require('axios');
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Valyu API Client.
|
|
7
|
-
*/
|
|
8
|
-
class Valyu {
|
|
9
|
-
/**
|
|
10
|
-
* Initialize the Valyu client.
|
|
11
|
-
*
|
|
12
|
-
* @param {string} [apiKey] - The API key to use for the client.
|
|
13
|
-
* If not provided, the environment variable `VALYU_API_KEY` is used.
|
|
14
|
-
* @param {string} [baseUrl="https://api.valyu.network/v1"] - The base URL for the Valyu API.
|
|
15
|
-
*
|
|
16
|
-
* @throws {Error} If no API key is provided.
|
|
17
|
-
*/
|
|
18
|
-
constructor(apiKey, baseUrl = "https://api.valyu.network/v1") {
|
|
19
|
-
if (!apiKey) {
|
|
20
|
-
apiKey = process.env.VALYU_API_KEY;
|
|
21
|
-
if (!apiKey) {
|
|
22
|
-
throw new Error("VALYU_API_KEY is not set");
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
this.baseUrl = baseUrl;
|
|
26
|
-
this.headers = {
|
|
27
|
-
"Content-Type": "application/json",
|
|
28
|
-
"x-api-key": apiKey
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Fetch context from the Valyu API.
|
|
34
|
-
*
|
|
35
|
-
* @param {string} query - The query to search for.
|
|
36
|
-
* @param {"web"|"proprietary"} search_type - The type of search to perform.
|
|
37
|
-
* @param {number} [max_num_results=10] - The maximum number of results to return.
|
|
38
|
-
* @param {boolean} [query_rewrite=true] - Whether to rewrite the query to improve search quality.
|
|
39
|
-
* @param {number} [similarity_threshold=0.4] - The similarity threshold to not return results below.
|
|
40
|
-
* @param {number} [max_price=1] - The maximum price (per thousand queries) to spend on the search.
|
|
41
|
-
* @param {string[]} [data_sources] - Optional list of data sources.
|
|
42
|
-
*
|
|
43
|
-
* @returns {Promise<SearchResponse>} The search response.
|
|
44
|
-
*/
|
|
45
|
-
async context(
|
|
46
|
-
query,
|
|
47
|
-
search_type,
|
|
48
|
-
max_num_results = 10,
|
|
49
|
-
query_rewrite = true,
|
|
50
|
-
similarity_threshold = 0.4,
|
|
51
|
-
max_price = 1,
|
|
52
|
-
data_sources
|
|
53
|
-
) {
|
|
54
|
-
try {
|
|
55
|
-
const payload = {
|
|
56
|
-
query,
|
|
57
|
-
search_type,
|
|
58
|
-
max_num_results,
|
|
59
|
-
query_rewrite,
|
|
60
|
-
similarity_threshold,
|
|
61
|
-
max_price
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
if (data_sources !== undefined) {
|
|
65
|
-
payload.data_sources = data_sources;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const response = await axios.post(
|
|
69
|
-
`${this.baseUrl}/knowledge`,
|
|
70
|
-
payload,
|
|
71
|
-
{ headers: this.headers }
|
|
72
|
-
);
|
|
73
|
-
|
|
74
|
-
if (!response.status || response.status < 200 || response.status >= 300) {
|
|
75
|
-
return {
|
|
76
|
-
success: false,
|
|
77
|
-
error: response.data?.error,
|
|
78
|
-
tx_id: null,
|
|
79
|
-
query,
|
|
80
|
-
results: [],
|
|
81
|
-
results_by_source: { web: 0, proprietary: 0 },
|
|
82
|
-
total_deduction_pcm: 0.0,
|
|
83
|
-
total_deduction_dollars: 0.0,
|
|
84
|
-
total_characters: 0
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return response.data;
|
|
89
|
-
} catch (e) {
|
|
90
|
-
return {
|
|
91
|
-
success: false,
|
|
92
|
-
error: e.message,
|
|
93
|
-
tx_id: null,
|
|
94
|
-
query,
|
|
95
|
-
results: [],
|
|
96
|
-
results_by_source: { web: 0, proprietary: 0 },
|
|
97
|
-
total_deduction_pcm: 0.0,
|
|
98
|
-
total_deduction_dollars: 0.0,
|
|
99
|
-
total_characters: 0
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Send feedback about a previous search response.
|
|
106
|
-
*
|
|
107
|
-
* @param {string} tx_id - The transaction ID from a previous search response
|
|
108
|
-
* @param {string} feedback - Feedback message about the search results
|
|
109
|
-
* @param {"very good"|"good"|"bad"|"very bad"} sentiment - The sentiment of the feedback
|
|
110
|
-
* @returns {Promise<Object>} Response containing success status and optional error message
|
|
111
|
-
*/
|
|
112
|
-
async feedback(tx_id, feedback, sentiment) {
|
|
113
|
-
try {
|
|
114
|
-
const payload = {
|
|
115
|
-
tx_id,
|
|
116
|
-
feedback,
|
|
117
|
-
sentiment: sentiment.toLowerCase()
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
const response = await axios.post(
|
|
121
|
-
`${this.baseUrl}/feedback`,
|
|
122
|
-
payload,
|
|
123
|
-
{ headers: this.headers }
|
|
124
|
-
);
|
|
125
|
-
|
|
126
|
-
return response.data;
|
|
127
|
-
} catch (e) {
|
|
128
|
-
return {
|
|
129
|
-
success: false,
|
|
130
|
-
error: e.message
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
module.exports = { Valyu };
|
package/src/types.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
// src/types.js
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @typedef {"web" | "proprietary"} SearchType
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* @typedef {Object} SearchResult
|
|
9
|
-
* @property {string} title
|
|
10
|
-
* @property {string} url
|
|
11
|
-
* @property {string} content
|
|
12
|
-
* @property {string} [description]
|
|
13
|
-
* @property {string} source
|
|
14
|
-
* @property {number} price
|
|
15
|
-
* @property {number} length
|
|
16
|
-
* @property {number} relevance_score
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* @typedef {Object} ResultsBySource
|
|
21
|
-
* @property {number} web
|
|
22
|
-
* @property {number} proprietary
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* @typedef {Object} SearchResponse
|
|
27
|
-
* @property {boolean} success
|
|
28
|
-
* @property {string} [error]
|
|
29
|
-
* @property {string} tx_id
|
|
30
|
-
* @property {string} query
|
|
31
|
-
* @property {SearchResult[]} results
|
|
32
|
-
* @property {ResultsBySource} results_by_source
|
|
33
|
-
* @property {number} total_deduction_pcm
|
|
34
|
-
* @property {number} total_deduction_dollars
|
|
35
|
-
* @property {number} total_characters
|
|
36
|
-
*/
|
package/tests/test.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
const { Valyu } = require('../src/api');
|
|
2
|
-
const assert = require('assert');
|
|
3
|
-
|
|
4
|
-
async function runTests() {
|
|
5
|
-
console.log('Running Valyu SDK tests...');
|
|
6
|
-
|
|
7
|
-
// Test constructor
|
|
8
|
-
console.log('\nTesting constructor...');
|
|
9
|
-
try {
|
|
10
|
-
const valyu = new Valyu('H5TkbtiBau8WZDkthZZAlqp695btCXk9kZFiG2W9');
|
|
11
|
-
assert(valyu instanceof Valyu, 'Should create Valyu instance');
|
|
12
|
-
console.log('✓ Constructor test passed');
|
|
13
|
-
} catch (e) {
|
|
14
|
-
console.error('✗ Constructor test failed:', e.message);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// Test API call
|
|
18
|
-
console.log('\nTesting API call...');
|
|
19
|
-
try {
|
|
20
|
-
const valyu = new Valyu('H5TkbtiBau8WZDkthZZAlqp695btCXk9kZFiG2W9');
|
|
21
|
-
const response = await valyu.context(
|
|
22
|
-
'Test query',
|
|
23
|
-
'web',
|
|
24
|
-
1,
|
|
25
|
-
1,
|
|
26
|
-
10
|
|
27
|
-
);
|
|
28
|
-
|
|
29
|
-
// Verify response structure
|
|
30
|
-
assert(typeof response === 'object', 'Response should be an object');
|
|
31
|
-
assert('success' in response, 'Response should have success field');
|
|
32
|
-
assert('results' in response, 'Response should have results field');
|
|
33
|
-
console.log('✓ API call test passed');
|
|
34
|
-
} catch (e) {
|
|
35
|
-
console.error('✗ API call test failed:', e.message);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
runTests().catch(console.error);
|