Skip to content

Commit 2cabbfb

Browse files
Merge pull request #124 from intelligentnode/123-replace-axios-with-native-fetch
replace axios with native fetch
2 parents 4a72d8d + 349b911 commit 2cabbfb

19 files changed

+394
-446
lines changed

IntelliNode/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
Integrate your data with the latest language models and deep learning frameworks using intellinode **javascript**. The library provides intuitive functions for sending input to models like ChatGPT, WaveNet and Stable diffusion, and receiving generated text, speech, or images. With just a few lines of code, you can easily access the power of cutting-edge AI models to enhance your projects.
1919

2020
# Latest Updates
21-
- Support Nvidia hosted models deepseek 🐳 and llama3 🦙.
22-
- Add Anthropic claude 3 chat.
21+
- Adjust the module to work backend and frontend.
22+
- Support Nvidia hosted models deepseek and llama3 🦙.
23+
- Add Anthropic claude 3.5 chat.
2324
- Add Google Gemini chat and vision.
24-
- Add Mistral MoE model as a chatbot provider.
2525
- Update stable diffusion to use the XL model engine. 🎨
2626
- Add support for hugging face inference. 🤗
2727
- Support in-memory semantic search. 🔍

IntelliNode/package-lock.json

Lines changed: 53 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

IntelliNode/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "intellinode",
3-
"version": "2.0.6",
3+
"version": "2.1.0",
44
"description": "Create AI agents using the latest models, including ChatGPT, Llama, Diffusion, Cohere, Gemini, and Hugging Face.",
55
"main": "index.js",
66
"keywords": [
@@ -32,7 +32,7 @@
3232
},
3333
"homepage": "https://intellinode.ai",
3434
"dependencies": {
35-
"axios": "^1.7.9",
35+
"cross-fetch": "^4.1.0",
3636
"dotenv": "^16.4.7",
3737
"form-data": "^4.0.1"
3838
}

IntelliNode/test/integration/Chatbot.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ async function testOpenaiChatGPTCase3() {
7373
console.log('\nChat test case 3: \n');
7474

7575
const sysMsg = "You are a helpful assistant.";
76-
const input = new ChatGPTInput(sysMsg, { model: "gpt-3.5-turbo-0613" });
76+
const input = new ChatGPTInput(sysMsg, { model: "o3-mini" });
7777
input.addMessage(new ChatGPTMessage("Please return the current date and time in Dublin.", "user"));
7878

7979
const functions = [

IntelliNode/utils/ConnHelper.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ Copyright 2023 Github.com/Barqawiz/IntelliNode
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
*/
8-
const axios = require('axios');
9-
108
class ConnHelper {
119
constructor() {
1210
}

IntelliNode/utils/FetchClient.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
const fetch = require('cross-fetch');
2+
const FormData = require('form-data');
3+
4+
class FetchClient {
5+
constructor({ baseURL = '', headers = {} } = {}) {
6+
this.baseURL = baseURL;
7+
this.defaultHeaders = headers;
8+
}
9+
10+
/**
11+
* Send a POST request using cross-fetch.
12+
*
13+
* @param {string} endpoint - URL path or full URL if starts with http.
14+
* @param {object|FormData} data - Data to send in the request body.
15+
* @param {object} extraConfig - Optional config (e.g. { responseType: 'arraybuffer' | 'stream' }).
16+
* @returns {Promise<any|ReadableStream|ArrayBuffer>} - JSON by default, or stream/arrayBuffer if specified.
17+
*/
18+
async post(endpoint, data, extraConfig = {}) {
19+
const url = endpoint.startsWith('http')
20+
? endpoint
21+
: this.baseURL + endpoint;
22+
23+
// Decide how to handle the request body
24+
let body;
25+
if (data instanceof FormData) {
26+
// Use FormData directly (e.g., file uploads)
27+
body = data;
28+
} else if (data !== undefined) {
29+
// Assume JSON
30+
body = JSON.stringify(data);
31+
}
32+
33+
// Merge default and extra headers
34+
const headers = {
35+
...this.defaultHeaders,
36+
...(extraConfig.headers || {})
37+
};
38+
39+
// If using FormData in Node, merge the form's headers
40+
if (data instanceof FormData) {
41+
Object.assign(headers, data.getHeaders());
42+
}
43+
44+
const config = {
45+
method: 'POST',
46+
headers,
47+
body
48+
};
49+
50+
// Make the request
51+
const response = await fetch(url, config);
52+
53+
// Check for HTTP error
54+
if (!response.ok) {
55+
const errorText = await response.text();
56+
throw new Error(`HTTP error ${response.status}: ${errorText}`);
57+
}
58+
59+
// Handle custom response types
60+
if (extraConfig.responseType === 'arraybuffer') {
61+
return await response.arrayBuffer();
62+
} else if (extraConfig.responseType === 'stream') {
63+
// Return raw body stream (ReadableStream in browser / Node 18+)
64+
return response.body;
65+
} else {
66+
// Default: parse JSON
67+
return await response.json();
68+
}
69+
}
70+
71+
/**
72+
* Send a GET request using cross-fetch.
73+
*
74+
* @param {string} endpoint - URL path or full URL if starts with http.
75+
* @param {object} extraConfig - Optional config (e.g. { responseType: 'arraybuffer' }).
76+
* @returns {Promise<any|ReadableStream|ArrayBuffer>} - JSON by default, or stream/arrayBuffer if specified.
77+
*/
78+
async get(endpoint, extraConfig = {}) {
79+
const url = endpoint.startsWith('http')
80+
? endpoint
81+
: this.baseURL + endpoint;
82+
83+
const headers = {
84+
...this.defaultHeaders,
85+
...(extraConfig.headers || {})
86+
};
87+
88+
const response = await fetch(url, { method: 'GET', headers });
89+
90+
if (!response.ok) {
91+
const errorText = await response.text();
92+
throw new Error(`HTTP error ${response.status}: ${errorText}`);
93+
}
94+
95+
if (extraConfig.responseType === 'arraybuffer') {
96+
return await response.arrayBuffer();
97+
} else if (extraConfig.responseType === 'stream') {
98+
return response.body;
99+
} else {
100+
return await response.json();
101+
}
102+
}
103+
}
104+
105+
module.exports = FetchClient;
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
const axios = require('axios');
1+
const FetchClient = require('../utils/FetchClient');
22

33
class AWSEndpointWrapper {
44
constructor(apiUrl, apiKey = null) {
55
this.API_BASE_URL = apiUrl;
66

77
let headers = {
8-
'Content-Type': 'application/json'
8+
'Content-Type': 'application/json',
99
};
10-
// sdd the API kry if provided
10+
1111
if (apiKey) {
1212
headers['Authorization'] = `Bearer ${apiKey}`;
1313
}
14-
// Configure axios
15-
this.httpClient = axios.create({
14+
15+
// Create our FetchClient with the base url + default headers
16+
this.client = new FetchClient({
1617
baseURL: this.API_BASE_URL,
17-
headers
18+
headers: headers,
1819
});
1920
}
2021

2122
async predict(inputData) {
2223
try {
23-
const response = await this.httpClient.post('', inputData);
24-
return response.data;
24+
return await this.client.post('', inputData);
2525
} catch (error) {
26-
throw error;
26+
throw error; // You can wrap this in a custom error message if you wish
2727
}
2828
}
2929
}
3030

31-
module.exports = AWSEndpointWrapper;
31+
module.exports = AWSEndpointWrapper;

IntelliNode/wrappers/AnthropicWrapper.js

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,37 @@ Copyright 2023 Github.com/Barqawiz/IntelliNode
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
*/
8-
const axios = require('axios');
98
const config = require('../config.json');
109
const connHelper = require('../utils/ConnHelper');
10+
const FetchClient = require('../utils/FetchClient');
1111

1212
class AnthropicWrapper {
13-
constructor(apiKey) {
14-
this.API_BASE_URL = config.url.anthropic.base;
15-
this.API_VERSION = config.url.anthropic.version;
16-
this.httpClient = axios.create({
17-
baseURL: this.API_BASE_URL,
18-
headers: {
19-
'Content-Type': 'application/json',
20-
'Accept': 'application/json',
21-
'x-api-key': apiKey,
22-
'anthropic-version': this.API_VERSION
23-
}
24-
});
25-
}
13+
constructor(apiKey) {
14+
this.API_BASE_URL = config.url.anthropic.base;
15+
this.API_VERSION = config.url.anthropic.version;
2616

27-
async generateText(params) {
28-
const url = config.url.anthropic.messages;
29-
try {
30-
const response = await this.httpClient.post(url, params);
31-
return response.data;
32-
} catch (error) {
33-
throw new Error(connHelper.getErrorMessage(error));
34-
}
35-
}
17+
// Create our FetchClient instance
18+
this.client = new FetchClient({
19+
baseURL: this.API_BASE_URL,
20+
headers: {
21+
'Content-Type': 'application/json',
22+
Accept: 'application/json',
23+
'x-api-key': apiKey,
24+
'anthropic-version': this.API_VERSION,
25+
},
26+
});
27+
}
3628

29+
async generateText(params) {
30+
const endpoint = config.url.anthropic.messages;
31+
32+
try {
33+
// Use the client’s post method
34+
return await this.client.post(endpoint, params);
35+
} catch (error) {
36+
throw new Error(connHelper.getErrorMessage(error));
37+
}
38+
}
3739
}
3840

39-
module.exports = AnthropicWrapper;
41+
module.exports = AnthropicWrapper;

0 commit comments

Comments
 (0)