Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
PawanOsman committed Mar 22, 2023
1 parent 6b9249c commit bd138a4
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 21 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ let response = await bot.ask("Hello?");
console.log(response);
```

## Usage: ***Using proxy is recomended!***
## Features

- **Simulating Response Streaming**: The package simulates response streaming, allowing you to get the response as soon as it is available.
- **Multiple conversations**: The package supports multiple conversations, allowing you to have multiple parallel conversations with the chatbot.
- **Proxy Support**: The package supports proxies, allowing you to use the chatbot from any location.
- **Lightweight**: The package is very lightweight, making it easy to use and integrate into your projects.

## Usage: **_Using proxy is recomended!_**

```javascript
import { Bard } from "GoogleBard";
Expand All @@ -51,4 +58,11 @@ let conversationId = "conversation name"; // optional: to make it remember old c

let response = await bot.ask("Hello?", conversationId);
console.log(response);

// Simulating response streaming

await bot.askStream((res) => {
console.log(res);
}, "Hello?", conversationId);

```
5 changes: 5 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PROXY_HOST=
PROXY_PORT=
PROXY_PROTOCOL=
PROXY_USERNAME=
PROXY_PASSWORD=
11 changes: 8 additions & 3 deletions examples/cli.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Bard } from "../dist/index.js";
import { config } from "dotenv";
import readline from "readline";

config();

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
Expand All @@ -16,7 +19,7 @@ let bot = new Bard(cookies, {
username: process.env.PROXY_USERNAME,
password: process.env.PROXY_PASSWORD
},
protocol: "http"
protocol: process.env.PROXY_PROTOCOL
}
});

Expand All @@ -29,8 +32,10 @@ async function main() {
});

process.stdout.write("Google Bard: ");
let response = await bot.ask(prompt, "default");
console.log(response);
await bot.askStream(res => {
process.stdout.write(res.toString());
}, prompt);
console.log();
}
}

Expand Down
15 changes: 12 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "googlebard",
"version": "1.0.1",
"version": "1.0.2",
"description": "A reverse engineered API for Google Bard chatbot",
"main": "dist/index.js",
"type": "module",
Expand Down Expand Up @@ -58,7 +58,8 @@
"dependencies": {
"axios": "1.3.2",
"cheerio": "1.0.0-rc.12",
"dbcontext": "1.0.1"
"dbcontext": "1.0.1",
"dotenv": "16.0.3"
},
"bugs": {
"url": "https://github.com/PawanOsman/GoogleBard/issues"
Expand Down
47 changes: 35 additions & 12 deletions src/classes/bard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,28 @@ import vm from "vm";
import fs from "fs";
import https from "https";
import { load } from "cheerio";
import Wait from "../utils/wait.js";
import Random from "../utils/random.js";
import Options from "../models/options.js";
import axios, { AxiosInstance } from "axios";
import AppDbContext from "./app-dbcontext.js";
import Conversation from "../models/conversation.js";
import axios, { AxiosInstance, AxiosRequestConfig } from "axios";

class Bard {
private axios: AxiosInstance;
private db: AppDbContext;
private cookies: string = "";

constructor(cookie: string, options?: Options) {
this.db = new AppDbContext();

this.cookies = cookie;

const agent = new https.Agent({
rejectUnauthorized: false,
});

let axiosOptions = {
let axiosOptions: AxiosRequestConfig = {
proxy: options?.proxy,
httpsAgent: agent,
headers: {
Expand All @@ -26,7 +32,6 @@ class Bard {
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
Connection: "keep-alive",
Cookie: cookie,
"Upgrade-Insecure-Requests": "1",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
Expand All @@ -42,7 +47,7 @@ class Bard {

private async waitForLoad() {
while (this.db === null) {
await this.wait(1000);
await Wait(1000);
}
await this.db.WaitForLoad();
}
Expand Down Expand Up @@ -89,10 +94,6 @@ class Bard {
};
}

wait(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

private ParseResponse(text: string) {
let resData = {
r: "",
Expand Down Expand Up @@ -143,7 +144,11 @@ class Bard {

private async GetRequestParams() {
try {
const response = await this.axios.get("https://bard.google.com");
const response = await this.axios.get("https://bard.google.com", {
headers: {
Cookie: this.cookies,
},
});
let $ = load(response.data);
let script = $("script[data-id=_gd]").html();
script = script.replace("window.WIZ_global_data", "googleData");
Expand All @@ -159,12 +164,22 @@ class Bard {
}
}

public async ask(prompt: string = "Hello", conversationId?: string) {
public async ask(prompt: string, conversationId?: string) {
return await this.askStream((data) => {}, prompt, conversationId);
}

public async askStream(data: (arg0: string) => void, prompt: string, conversationId?: string) {
let resData = await this.send(prompt, conversationId);
return resData[0];
let responseChunks = resData[0].split(" ");
for await (let chunk of responseChunks) {
if (chunk === "") continue;
data(`${chunk} `);
await Wait(Random(25, 250)); // simulate typing
}
return resData[1];
}

private async send(prompt: string = "Hello", conversationId?: string) {
private async send(prompt: string, conversationId?: string) {
await this.waitForLoad();
let conversation = this.getConversationById(conversationId);
try {
Expand All @@ -176,13 +191,21 @@ class Bard {
"f.req": JSON.stringify([null, `[[${JSON.stringify(prompt)}],null,${JSON.stringify([conversation.c, conversation.r, conversation.rc])}]`]),
}),
{
headers: {
Cookie: this.cookies,
},
params: {
bl: bl,
rt: "c",
_reqid: "0",
},
},
);

// let cookies = response.headers["set-cookie"];

// if (cookies) this.cookies = cookies.join("; ");

let parsedResponse = this.ParseResponse(response.data);
conversation.c = parsedResponse.c;
conversation.r = parsedResponse.r;
Expand Down
5 changes: 5 additions & 0 deletions src/utils/random.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function Random(min: number, max: number) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}

export default Random;
5 changes: 5 additions & 0 deletions src/utils/wait.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function Wait(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

export default Wait;

0 comments on commit bd138a4

Please sign in to comment.