valyu-js 1.0.0

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/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # Valyu SDK for Node.js
2
+
3
+ Connect your AI applications to high-quality proprietary data with Valyu, an AI context engine built by AI Engineers for AI Engineers.
4
+
5
+ ## Why Valyu?
6
+
7
+ - **Ready-to-use RAG Data**: All data is returned in Markdown format, optimized for AI consumption.
8
+ - **Multimodal Support**: Retrieve text, images, and other data types for comprehensive answers.
9
+ - **Pay-per-use**: Transparent pricing—you only pay for what you use.
10
+ - **Hybrid Search**: Combine proprietary dataset access with web search capabilities.
11
+ - **Built for AI**: Designed specifically for Retrieval-Augmented Generation (RAG) applications.
12
+
13
+ ## Installation
14
+
15
+ Install the Valyu SDK via npm:
16
+
17
+ ```bash
18
+ npm install valyu
19
+ ```
20
+
21
+ Quick Start
22
+ ```js
23
+ const { Valyu } = require('valyu');
24
+
25
+ // Initialize the Valyu client (provide your API key)
26
+ const valyu = new Valyu("your-api-key");
27
+
28
+ // Get relevant context for your query
29
+ (async () => {
30
+ const response = await valyu.context(
31
+ "Tell me about ancient civilizations", // query
32
+ "proprietary", // search_type ("proprietary" or "web")
33
+ 5, // num_query: number of queries to generate
34
+ 3, // num_results: number of results per query
35
+ 10 // max_price: maximum price per thousand queries
36
+ );
37
+ console.log(response);
38
+ })();
39
+ ```
40
+
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "valyu-js",
3
+ "version": "1.0.0",
4
+ "description": "We handle the data. You handle the AI.",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "node test.js"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/ValyuNetwork/valyu-js.git"
12
+ },
13
+ "keywords": [
14
+ "ai",
15
+ "sdk",
16
+ "rag",
17
+ "valyu"
18
+ ],
19
+ "author": "Valyu",
20
+ "license": "MIT",
21
+ "bugs": {
22
+ "url": "https://github.com/ValyuNetwork/valyu-js/issues"
23
+ },
24
+ "homepage": "https://valyu.network",
25
+ "dependencies": {
26
+ "axios": "^1.4.0"
27
+ }
28
+ }
29
+
package/src/api.js ADDED
@@ -0,0 +1,136 @@
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 ADDED
@@ -0,0 +1,36 @@
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 ADDED
@@ -0,0 +1,39 @@
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);