|
| 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