Skip to content

Commit 3076785

Browse files
Add files via upload
1 parent 9dc5b52 commit 3076785

File tree

4 files changed

+187
-0
lines changed

4 files changed

+187
-0
lines changed

README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
> I'm currently trying to publish it on jsDelivr, so stay tuned!
2+
3+
<h1>FreeGPT.js</h1>
4+
🤖 A powerful client-side JavaScript library for ChatGPT allowing you to use ChatGPT without any limits! No
5+
6+
<br><div align="center">
7+
8+
[![](https://img.shields.io/github/stars/ashishagarwal2023/freegptjs?label=Stars&color=af68ff&logo=github&logoColor=white&labelColor=464646&style=for-the-badge)](https://github.com/ashishagarwal2023/freegptjs/stargazers) [![](https://img.shields.io/badge/License-MIT-green.svg?logo=internetarchive&logoColor=white&labelColor=464646&style=for-the-badge)](https://github.com/ashishagarwal2023/freegptjs/blob/main/LICENSE.md) [![](https://img.shields.io/github/commit-activity/m/ashishagarwal2023/freegptjs?label=Commits&logo=github&logoColor=white&labelColor=464646&style=for-the-badge)](https://github.com/ashishagarwal2023/freegptjs/commits/main)
9+
10+
</div>
11+
12+
</div>
13+
14+
<div id="intro">
15+
16+
## 💡 About
17+
18+
</div>
19+
20+
freegpt is a powerful javascript library you can use to get ChatGPT on your website, and its free!
21+
22+
- Simple, just one function
23+
- Open-source, free
24+
- Easy-to-use
25+
- Lightweight (yet optimally performant)
26+
- No authentication, free for all
27+
- Unlimited GPT 3.5 Model
28+
29+
<div id="importing">
30+
31+
## ⚡ Importing the library
32+
33+
</div>
34+
35+
> **Note** _To always import the latest version (NOT recommended in production!) replace the versioned jsDelivr URL with: `https://cdn.jsdelivr.net/npm/@ashishagarwal2023/freegptjs`_
36+
37+
## 💻 Usage
38+
39+
```js
40+
(async () => {
41+
let response = await gpt.ask("Explain variables in javascript");
42+
console.log(response); // you got it!
43+
})();
44+
```
45+
46+
There's literally nothing more coded. It's a reverse engineering of You.com API (which is completely free, I was shocked to see)
47+
48+
## 🤖 Why client-side?
49+
50+
You got it, I also got it. The route where I recieve the fetch in the library is protected by Cloudflare. Got it?
51+
52+
> When I tried to get the response using NodeJS, it appeared that it gave HTML, and oh, there was Cloudflare security. I tried several web scrapers for NodeJS (and even tried python) but there was one paid, that I cannot afford and so this AJAX request still worked.
53+
54+
> **Why we just care about the AJAX?** Listen, you can simply do your client's fetch requests using AJAX when the user comes to chat, and its simple when using pure HTML-JavaScript, so give it a try!
55+
56+
> Generative Mode is **enabled**. This means GPT will not remember anything you tell it.
57+
58+
### Why I just made it?
59+
60+
There was a chatgptpy library for Python and several for NodeJS also. But I wanted to build a completely free one, so I made this.
61+
62+
Give it a star if you want to help me keep it active. Follow me when?
63+
64+
It depends on **You.com**'s API, however its reverse engineered, but it has no tokens/login or such.
65+
66+
## 🤝Contributing
67+
68+
You are welcome! I'm looking for some cool peoples to help me push this project further!
69+
70+
> The API routes are given in plan.js, so read it if you would like to know how the API would work
71+
72+
If you made a working webscraper and would like me to use it (Cloudflare Bypass), you are welcome, but remember I cannot afford a bit, sorry.

index.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>FreeGPT.js Web Example</title>
7+
</head>
8+
<body>
9+
<script src="src/youapi.js"></script>
10+
<script>
11+
(async () => {
12+
let confirmed = false;
13+
while (!confirmed) {
14+
const result = await gpt.ask(prompt("Ask a question with GPT: "));
15+
confirmed = confirm(
16+
"Click OK to open the GitHub page now. Cancel to re-ask.\n\n" +
17+
result
18+
);
19+
}
20+
window.open("https://github.com/ashishagarwal2023/freegptjs", "_blank");
21+
})();
22+
</script>
23+
</body>
24+
</html>

plan.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* The way we get the response from the API
3+
* https://you.com/api/streamingSearch?q=query&page=page&count=10&domain=youchat
4+
[1] @param {string} query - Query, what to ask
5+
[2] @param {number} page - Ideal to re-write responses however it does not always rewrite
6+
[3] the last domain parameter is needed to be youchat or it will reject it.
7+
8+
* Just the plan here
9+
*/

src/youapi.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
(function () {
2+
/**
3+
* Main library for gpt.ask method to fetch GPT's response from YouAPI
4+
* Completely free, open-source API with no verification or tokenization required
5+
* By @ashishagarwal2023 (GitHub)
6+
*/
7+
8+
const fetchAndExtract = async (url) => {
9+
try {
10+
const response = await fetch(url);
11+
const text = await response.text();
12+
const result = parseAndExtract(text);
13+
return result;
14+
} catch (error) {
15+
console.error("Bot failed to fetch response:", error);
16+
return null;
17+
}
18+
};
19+
20+
const parseAndExtract = (text) => {
21+
const youChatTokens = [];
22+
const firstIndex = text.indexOf("event: youChatToken");
23+
if (firstIndex !== -1) {
24+
let trimmedText = text.substring(firstIndex);
25+
const nextEventIndex = text.indexOf("event:", firstIndex + 1);
26+
if (nextEventIndex !== -1) {
27+
const abTestSlicesIndex = trimmedText.indexOf("event: abTestSlices");
28+
if (abTestSlicesIndex !== -1) {
29+
trimmedText = trimmedText.substring(0, abTestSlicesIndex);
30+
} else {
31+
trimmedText = trimmedText.substring(0, nextEventIndex);
32+
}
33+
}
34+
trimmedText.split("\n").forEach((line) => {
35+
if (line.trim() !== "" && line.includes("data:")) {
36+
try {
37+
const data = JSON.parse(line.substring(line.indexOf("{")));
38+
youChatTokens.push(data.youChatToken);
39+
} catch (error) {
40+
console.error("Error parsing JSON:", error);
41+
}
42+
}
43+
});
44+
} else {
45+
console.error("No 'event: youChatToken' found in the response.");
46+
}
47+
return youChatTokens.join("");
48+
};
49+
50+
/**
51+
* The ask method that is used to fetch GPT's response.
52+
*
53+
* @param {string} query - The query to be used for fetching data.
54+
* @param {number} [page=1] - Optional. The page number for pagination (default is 1).
55+
* @returns {Promise<string|null>} A promise that resolves with the extracted information or null if an error occurs.
56+
*/
57+
const ask = async (query, page = 1) => {
58+
const url =
59+
"https://you.com/api/streamingSearch?q=" +
60+
encodeURIComponent(query) +
61+
"&page=" +
62+
page +
63+
"&count=10&domain=youchat";
64+
if (query.trim() === "") {
65+
console.error("Cannot parse a blank query.");
66+
return null;
67+
}
68+
return await fetchAndExtract(url);
69+
};
70+
71+
/*
72+
* Exporting only the ask function
73+
*/
74+
window.gpt = {
75+
ask: ask,
76+
};
77+
})();
78+
/*
79+
* Example usage
80+
* console.log(await gpt.ask("Hello there!", 2))
81+
* You can skip the second page paramter its by default as 1. Its literally useless if you want to only ask the question once.
82+
*/

0 commit comments

Comments
 (0)