-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4f67027
Showing
9 changed files
with
290 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/vendor/ | ||
node_modules/ | ||
npm-debug.log | ||
yarn-error.log | ||
|
||
# Laravel 4 specific | ||
bootstrap/compiled.php | ||
app/storage/ | ||
|
||
# Laravel 5 & Lumen specific | ||
public/storage | ||
public/hot | ||
|
||
# Laravel 5 & Lumen specific with changed public path | ||
public_html/storage | ||
public_html/hot | ||
|
||
storage/*.key | ||
.env | ||
Homestead.yaml | ||
Homestead.json | ||
/.vagrant | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Aditya Dharmawan Saputra | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# laravel-bard | ||
A Laravel Package for Google Bard AI Chatbot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"name": "adityadees/laravel-bard", | ||
"description": "A Laravel Package for Google Bard AI Chatbot", | ||
"keyword": [ | ||
"google-bard", | ||
"laravel", | ||
"bard", | ||
"bard-google", | ||
"bard ai", | ||
"bard chatbot" | ||
], | ||
"type": "library", | ||
"license": "MIT", | ||
"autoload": { | ||
"psr-4": { | ||
"Adityadees\\LaravelBard\\": "src/" | ||
} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Aditya Dharmawan Saputra", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"AdityaDees\\LaravelBard\\LaravelBardServiceProvider" | ||
] | ||
} | ||
}, | ||
"require": { | ||
"php": "^7|^8", | ||
"illuminate/config": "^7|^8|^9|^10", | ||
"illuminate/support": "^7|^8|^9|^10" | ||
}, | ||
"config": { | ||
"sort-packages": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
return [ | ||
/* | ||
|-------------------------------------------------------------------------- | ||
| BARD_API | ||
|-------------------------------------------------------------------------- | ||
| | ||
| Using `__Secure-1PSID` inside | ||
| Developer tools > Application > Cookies and find `__Secure-1PSID` | ||
| | ||
*/ | ||
'bard_token' => env('BARD_TOKEN', ''), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AdityaDees\LaravelBard\Exceptions; | ||
|
||
use Exception; | ||
|
||
class ErrorException extends Exception | ||
{ | ||
public static function invalidToken(): self | ||
{ | ||
return new self("__Secure-1PSID value must end with a single dot. Enter correct __Secure-1PSID value."); | ||
} | ||
|
||
public static function errorResponse(String $status): self | ||
{ | ||
return new self("Response Status: " . $status); | ||
} | ||
|
||
public static function snimoeValueNotFound(): self | ||
{ | ||
return new self("SNlM0e value not found in response. Check __Secure-1PSID value."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AdityaDees\LaravelBard; | ||
|
||
use AdityaDees\LaravelBard\Exceptions\ErrorException; | ||
|
||
/** | ||
* LaravelBard | ||
* | ||
* @author Aditya Dharmawan Saputra @adityadees | ||
* | ||
*/ | ||
|
||
class LaravelBard | ||
{ | ||
private $proxies; | ||
private $timeout; | ||
private $token; | ||
private $session; | ||
private $conversation_id; | ||
private $response_id; | ||
private $choice_id; | ||
private $reqid; | ||
private $SNlM0e; | ||
|
||
public function __construct($timeout = 6, $proxies = null, $session = null) | ||
{ | ||
$this->proxies = $proxies; | ||
$this->timeout = $timeout; | ||
$this->token = config('bard-laravel.bard_token'); | ||
$headers = [ | ||
"Host: bard.google.com", | ||
"X-Same-Domain: 1", | ||
"User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36", | ||
"Content-Type: application/x-www-form-urlencoded;charset=UTF-8", | ||
"Origin: https://bard.google.com", | ||
"Referer: https://bard.google.com/", | ||
]; | ||
$this->reqid = (int) rand(pow(10, 3 - 1), pow(10, 3) - 1); | ||
$this->conversation_id = ""; | ||
$this->response_id = ""; | ||
$this->choice_id = ""; | ||
|
||
if ($session === null) { | ||
$this->session = curl_init(); | ||
curl_setopt_array($this->session, [ | ||
CURLOPT_HTTPHEADER => $headers, | ||
CURLOPT_COOKIE => "__Secure-1PSID=" . $this->token, | ||
CURLOPT_RETURNTRANSFER => true, | ||
]); | ||
} else { | ||
$this->session = $session; | ||
} | ||
|
||
$this->SNlM0e = $this->_get_snim0e(); | ||
} | ||
|
||
private function _get_snim0e() | ||
{ | ||
if (!$this->token || substr($this->token, -1) !== ".") { | ||
throw ErrorException::invalidToken(); | ||
} | ||
curl_setopt_array($this->session, [ | ||
CURLOPT_URL => "https://bard.google.com/", | ||
CURLOPT_TIMEOUT => $this->timeout, | ||
CURLOPT_PROXY => $this->proxies, | ||
CURLOPT_PROXYTYPE => CURLPROXY_HTTP, | ||
CURLOPT_FOLLOWLOCATION => true, | ||
CURLOPT_SSL_VERIFYHOST => false, | ||
CURLOPT_SSL_VERIFYPEER => false, | ||
CURLOPT_RETURNTRANSFER => true, | ||
]); | ||
|
||
$resp = curl_exec($this->session); | ||
if (curl_getinfo($this->session, CURLINFO_HTTP_CODE) !== 200) { | ||
throw ErrorException::errorResponse(curl_getinfo($this->session, CURLINFO_HTTP_CODE)); | ||
} | ||
preg_match('/"SNlM0e":"(.*?)"/', $resp, $matches); | ||
if (!$matches) { | ||
throw ErrorException::snimoeValueNotFound(); | ||
} | ||
return $matches[1]; | ||
} | ||
|
||
public function get_answer($input_text) | ||
{ | ||
$params = [ | ||
"bl" => "boq_assistant-bard-web-server_20230419.00_p1", | ||
"_reqid" => (string) $this->reqid, | ||
"rt" => "c", | ||
]; | ||
$input_text_struct = [ | ||
[$input_text], | ||
null, | ||
[$this->conversation_id, $this->response_id, $this->choice_id], | ||
]; | ||
$data = [ | ||
"f.req" => json_encode([null, json_encode($input_text_struct)]), | ||
"at" => $this->SNlM0e, | ||
]; | ||
|
||
curl_setopt_array($this->session, [ | ||
CURLOPT_URL => "https://bard.google.com/_/BardChatUi/data/assistant.lamda.BardFrontendService/StreamGenerate?" . http_build_query($params), | ||
CURLOPT_POST => true, | ||
CURLOPT_POSTFIELDS => http_build_query($data), | ||
CURLOPT_TIMEOUT => $this->timeout, | ||
CURLOPT_PROXY => $this->proxies, | ||
CURLOPT_PROXYTYPE => CURLPROXY_HTTP, | ||
CURLOPT_FOLLOWLOCATION => true, | ||
CURLOPT_SSL_VERIFYHOST => false, | ||
CURLOPT_SSL_VERIFYPEER => false, | ||
CURLOPT_RETURNTRANSFER => true, | ||
]); | ||
|
||
$resp = curl_exec($this->session); | ||
$resp_dict = json_decode(explode("\n", $resp)[3], true)[0][2]; | ||
|
||
if ($resp_dict === null) { | ||
return ["content" => "Response Error: " . $resp . "."]; | ||
} | ||
$parsed_answer = json_decode($resp_dict, true); | ||
$bard_answer = [ | ||
"content" => $parsed_answer[0][0], | ||
"conversation_id" => $parsed_answer[1][0], | ||
"response_id" => $parsed_answer[1][1], | ||
"factualityQueries" => $parsed_answer[3], | ||
"textQuery" => $parsed_answer[2][0] ?? "", | ||
"choices" => array_map(function ($i) { | ||
return ["id" => $i[0], "content" => $i[1]]; | ||
}, $parsed_answer[4]), | ||
]; | ||
$this->conversation_id = $bard_answer["conversation_id"]; | ||
$this->response_id = $bard_answer["response_id"]; | ||
$this->choice_id = $bard_answer["choices"][0]["id"]; | ||
$this->reqid += 100000; | ||
|
||
return $bard_answer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AdityaDees\LaravelBard; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
|
||
class LaravelBardServiceProvider extends ServiceProvider | ||
{ | ||
public function boot(): void | ||
{ | ||
$this->publishes([ | ||
__DIR__ . '/../config/laravel-bard.php' => config_path('laravel-bard.php'), | ||
], 'laravel-bard'); | ||
} | ||
|
||
public function register(): void | ||
{ | ||
$this->mergeConfigFrom(__DIR__ . '/../config/laravel-bard.php', 'laravel-bard'); | ||
} | ||
} |