Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
devashish2024 committed Mar 12, 2024
1 parent 9dc5b52 commit 3076785
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 0 deletions.
72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
> I'm currently trying to publish it on jsDelivr, so stay tuned!
<h1>FreeGPT.js</h1>
🤖 A powerful client-side JavaScript library for ChatGPT allowing you to use ChatGPT without any limits! No

<br><div align="center">

[![](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)

</div>

</div>

<div id="intro">

## 💡 About

</div>

freegpt is a powerful javascript library you can use to get ChatGPT on your website, and its free!

- Simple, just one function
- Open-source, free
- Easy-to-use
- Lightweight (yet optimally performant)
- No authentication, free for all
- Unlimited GPT 3.5 Model

<div id="importing">

## ⚡ Importing the library

</div>

> **Note** _To always import the latest version (NOT recommended in production!) replace the versioned jsDelivr URL with: `https://cdn.jsdelivr.net/npm/@ashishagarwal2023/freegptjs`_
## 💻 Usage

```js
(async () => {
let response = await gpt.ask("Explain variables in javascript");
console.log(response); // you got it!
})();
```

There's literally nothing more coded. It's a reverse engineering of You.com API (which is completely free, I was shocked to see)

## 🤖 Why client-side?

You got it, I also got it. The route where I recieve the fetch in the library is protected by Cloudflare. Got it?

> 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.
> **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!
> Generative Mode is **enabled**. This means GPT will not remember anything you tell it.
### Why I just made it?

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.

Give it a star if you want to help me keep it active. Follow me when?

It depends on **You.com**'s API, however its reverse engineered, but it has no tokens/login or such.

## 🤝Contributing

You are welcome! I'm looking for some cool peoples to help me push this project further!

> The API routes are given in plan.js, so read it if you would like to know how the API would work
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.
24 changes: 24 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>FreeGPT.js Web Example</title>
</head>
<body>
<script src="src/youapi.js"></script>
<script>
(async () => {
let confirmed = false;
while (!confirmed) {
const result = await gpt.ask(prompt("Ask a question with GPT: "));
confirmed = confirm(
"Click OK to open the GitHub page now. Cancel to re-ask.\n\n" +
result
);
}
window.open("https://github.com/ashishagarwal2023/freegptjs", "_blank");
})();
</script>
</body>
</html>
9 changes: 9 additions & 0 deletions plan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* The way we get the response from the API
* https://you.com/api/streamingSearch?q=query&page=page&count=10&domain=youchat
[1] @param {string} query - Query, what to ask
[2] @param {number} page - Ideal to re-write responses however it does not always rewrite
[3] the last domain parameter is needed to be youchat or it will reject it.
* Just the plan here
*/
82 changes: 82 additions & 0 deletions src/youapi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
(function () {
/**
* Main library for gpt.ask method to fetch GPT's response from YouAPI
* Completely free, open-source API with no verification or tokenization required
* By @ashishagarwal2023 (GitHub)
*/

const fetchAndExtract = async (url) => {
try {
const response = await fetch(url);
const text = await response.text();
const result = parseAndExtract(text);
return result;
} catch (error) {
console.error("Bot failed to fetch response:", error);
return null;
}
};

const parseAndExtract = (text) => {
const youChatTokens = [];
const firstIndex = text.indexOf("event: youChatToken");
if (firstIndex !== -1) {
let trimmedText = text.substring(firstIndex);
const nextEventIndex = text.indexOf("event:", firstIndex + 1);
if (nextEventIndex !== -1) {
const abTestSlicesIndex = trimmedText.indexOf("event: abTestSlices");
if (abTestSlicesIndex !== -1) {
trimmedText = trimmedText.substring(0, abTestSlicesIndex);
} else {
trimmedText = trimmedText.substring(0, nextEventIndex);
}
}
trimmedText.split("\n").forEach((line) => {
if (line.trim() !== "" && line.includes("data:")) {
try {
const data = JSON.parse(line.substring(line.indexOf("{")));
youChatTokens.push(data.youChatToken);
} catch (error) {
console.error("Error parsing JSON:", error);
}
}
});
} else {
console.error("No 'event: youChatToken' found in the response.");
}
return youChatTokens.join("");
};

/**
* The ask method that is used to fetch GPT's response.
*
* @param {string} query - The query to be used for fetching data.
* @param {number} [page=1] - Optional. The page number for pagination (default is 1).
* @returns {Promise<string|null>} A promise that resolves with the extracted information or null if an error occurs.
*/
const ask = async (query, page = 1) => {
const url =
"https://you.com/api/streamingSearch?q=" +
encodeURIComponent(query) +
"&page=" +
page +
"&count=10&domain=youchat";
if (query.trim() === "") {
console.error("Cannot parse a blank query.");
return null;
}
return await fetchAndExtract(url);
};

/*
* Exporting only the ask function
*/
window.gpt = {
ask: ask,
};
})();
/*
* Example usage
* console.log(await gpt.ask("Hello there!", 2))
* You can skip the second page paramter its by default as 1. Its literally useless if you want to only ask the question once.
*/

0 comments on commit 3076785

Please sign in to comment.