google_bard 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1b7c773ce9c1832625e7de8c552d5cf4ad7c425eb81a802dca1b647895bfa5a2
4
+ data.tar.gz: 67e96fc0c273bd5dbf8475f271caac1b0cda72459990c965993296631a10f33b
5
+ SHA512:
6
+ metadata.gz: 6a9162fa419a8c26c3da1c0e9832625b8b5713185d84f80d32e013645cd1dd556e693e30f2eb4c0306c36b504d47773e516d8663fe850fc726906e50a115cae1
7
+ data.tar.gz: eb8c43d55f674c633dbfc8212f478b2c3a2782f5b8a1d821235ccc5a36dfa960cf9f66d91d35b06b7d3b94f29ad82fbe904bb1a7bab0ee06fde422083839db14
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-05-22
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at TODO: Write your email address. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in google_bard.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
data/Gemfile.lock ADDED
@@ -0,0 +1,66 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ google_bard (0.1.0)
5
+ httparty (~> 0.18.1)
6
+ json (~> 2.3)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.2)
12
+ diff-lcs (1.5.0)
13
+ httparty (0.18.1)
14
+ mime-types (~> 3.0)
15
+ multi_xml (>= 0.5.2)
16
+ json (2.6.3)
17
+ mime-types (3.4.1)
18
+ mime-types-data (~> 3.2015)
19
+ mime-types-data (3.2023.0218.1)
20
+ multi_xml (0.6.0)
21
+ parallel (1.23.0)
22
+ parser (3.2.2.0)
23
+ ast (~> 2.4.1)
24
+ rainbow (3.1.1)
25
+ rake (13.0.6)
26
+ regexp_parser (2.8.0)
27
+ rexml (3.2.5)
28
+ rspec (3.12.0)
29
+ rspec-core (~> 3.12.0)
30
+ rspec-expectations (~> 3.12.0)
31
+ rspec-mocks (~> 3.12.0)
32
+ rspec-core (3.12.2)
33
+ rspec-support (~> 3.12.0)
34
+ rspec-expectations (3.12.3)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.12.0)
37
+ rspec-mocks (3.12.5)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.12.0)
40
+ rspec-support (3.12.0)
41
+ rubocop (1.50.2)
42
+ json (~> 2.3)
43
+ parallel (~> 1.10)
44
+ parser (>= 3.2.0.0)
45
+ rainbow (>= 2.2.2, < 4.0)
46
+ regexp_parser (>= 1.8, < 3.0)
47
+ rexml (>= 3.2.5, < 4.0)
48
+ rubocop-ast (>= 1.28.0, < 2.0)
49
+ ruby-progressbar (~> 1.7)
50
+ unicode-display_width (>= 2.4.0, < 3.0)
51
+ rubocop-ast (1.28.0)
52
+ parser (>= 3.2.1.0)
53
+ ruby-progressbar (1.13.0)
54
+ unicode-display_width (2.4.2)
55
+
56
+ PLATFORMS
57
+ x86_64-darwin-22
58
+
59
+ DEPENDENCIES
60
+ google_bard!
61
+ rake (~> 13.0)
62
+ rspec (~> 3.0)
63
+ rubocop (~> 1.21)
64
+
65
+ BUNDLED WITH
66
+ 2.4.12
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,91 @@
1
+ Sure, here's a draft for the README.md:
2
+
3
+ markdown
4
+ Copy code
5
+
6
+ # Google Bard
7
+
8
+ [![BardAPI](https://img.shields.io/badge/pypi-BardAPI-black)](https://pypi.org/project/BardAPI/)
9
+
10
+ Google Bard is a Ruby gem adaptation of the Python package [BardAPI](https://github.com/dsdanielpark/BardAPI). This Python package is using reverse engineering to access Bard in an unofficial way, allowing developers to generate text completions with an API.
11
+
12
+ ## Demo
13
+
14
+ I actually made this gem as wanted Bard for my latest project: a WhastsApp bot supercharged with AI. Check it out here: [ciel.chat](https://ciel.chat)
15
+
16
+ ## Disclamer
17
+
18
+ This gem is exploiting reverse engineering for Bard, and it might fail in the future due to Google's code evolutions. Not sure yet wether I will maintain it or change into the official API when it's live. Feel free to contribute.
19
+
20
+ ## Installation
21
+
22
+ You can install the gem by adding it to your Gemfile:
23
+
24
+ ```ruby
25
+ gem 'google_bard'
26
+ ```
27
+
28
+ Then run:
29
+
30
+ ```ruby
31
+ bundle install
32
+ ```
33
+
34
+ Or install it yourself:
35
+
36
+ ```ruby
37
+ gem install google_bard
38
+ ```
39
+
40
+ ### Authentication
41
+
42
+ 1. Visit https://bard.google.com/
43
+ 2. `F12` for console
44
+ 3. Session: Application → Cookies → Copy the value of `__Secure-1PSID` cookie.
45
+ -> This is your token.
46
+
47
+ ### Usage
48
+
49
+ First, require the gem in your code:
50
+
51
+ ```ruby
52
+ require 'google_bard'
53
+ ```
54
+
55
+ Initialize the Bard object:
56
+
57
+ ```ruby
58
+ bard = GoogleBard.new(token, timeout, proxies)
59
+ ```
60
+
61
+ Parameters:
62
+
63
+ - token (required): Your \_\_Secure-1PSID value from Google Bard.
64
+ - timeout (optional): Timeout for the requests (default is 20 seconds).
65
+ - proxies (optional): Proxy configuration if you want to use proxies.
66
+ - Then call the completion method with your text input:
67
+
68
+ ```ruby
69
+ response = bard.completion("Hello, world!")
70
+ ```
71
+
72
+ The completion method returns an `OpenStruct` with the following attributes:
73
+
74
+ - success: Whether the request was successful.
75
+ - content: The generated completion.
76
+ - conversation_id: The ID of the conversation.
77
+ - response_id: The ID of the response.
78
+ - factuality_queries: The factuality queries.
79
+ - text_query: The text query.
80
+ - choices: The choices provided by the completion.
81
+
82
+ ```ruby
83
+ response.success
84
+ response.content # -> completion if success, error if not
85
+ ```
86
+
87
+ Contributing
88
+ Bug reports and pull requests are welcome on GitHub at github.com/username/google_bard.
89
+
90
+ License
91
+ The gem is available as open source under the terms of the MIT License.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/google_bard/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "google_bard"
7
+ spec.version = GoogleBard::VERSION
8
+ spec.authors = ["Maël Harnois"]
9
+ spec.email = ["mael.harnois@gmail.com"]
10
+
11
+ spec.summary = "Bard's API access for completions"
12
+ spec.description = "Reverse engineering inspired from the python package https://github.com/dsdanielpark/Bard-API "
13
+ spec.homepage = "https://github.com/mael-ha/google_bard"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/mael-ha/google_bard"
19
+ spec.metadata["changelog_uri"] = "https://github.com/mael-ha/google_bard"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(__dir__) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
26
+ end
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ # spec.add_dependency "example-gem", "~> 1.0"
34
+ spec.add_dependency "httparty", "~> 0.18.1"
35
+ spec.add_dependency "json", "~> 2.3"
36
+ # For more information and examples about making a new gem, check out our
37
+ # guide at: https://bundler.io/guides/creating_gem.html
38
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class GoogleBard
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "google_bard/version"
4
+ require 'httparty'
5
+ require 'json'
6
+ require 'cgi'
7
+
8
+ class GoogleBard
9
+ class Error < StandardError; end
10
+ include HTTParty
11
+ HEADERS = {
12
+ 'Host' => 'bard.google.com',
13
+ 'X-Same-Domain' => '1',
14
+ 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36',
15
+ 'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8',
16
+ 'Origin' => 'https://bard.google.com',
17
+ 'Referer' => 'https://bard.google.com/'
18
+ }
19
+
20
+ def initialize(token = nil, timeout = 20, proxies = nil)
21
+ @token = token || ENV['GOOGLE_BARD_SECURE_1PSID']
22
+ @timeout = timeout
23
+ @proxies = proxies
24
+ @reqid = rand(1000..9999)
25
+ @conversation_id = ''
26
+ @response_id = ''
27
+ @choice_id = ''
28
+ @snim0e = get_snim0e
29
+ end
30
+
31
+ def get_snim0e
32
+ if @token.nil? || !@token.end_with?('.')
33
+ raise '__Secure-1PSID value must end with a single dot. Enter correct __Secure-1PSID value.'
34
+ end
35
+
36
+ response = self.class.get('https://bard.google.com/', timeout: @timeout, cookies: { '__Secure-1PSID' => @token })
37
+ raise "Response code not 200. Response Status is #{response.code}" unless response.code == 200
38
+
39
+ snim0e_match = response.body.match(/SNlM0e":"(.*?)"/)
40
+ raise 'SNlM0e value not found in response. Check __Secure-1PSID value.' unless snim0e_match
41
+
42
+ snim0e_match[1]
43
+ end
44
+
45
+ def completion(text_input)
46
+ # Send the request with the payload in the body
47
+ response = HTTParty.post(
48
+ 'https://bard.google.com/_/BardChatUi/data/assistant.lamda.BardFrontendService/StreamGenerate',
49
+ query:,
50
+ body: payload(text_input),
51
+ headers: HEADERS,
52
+ cookies: { '__Secure-1PSID' => @token },
53
+ timeout: @timeout
54
+ )
55
+
56
+ handle_response(response)
57
+ end
58
+
59
+ def query
60
+ {
61
+ 'bl' => 'boq_assistant-bard-web-server_20230419.00_p1',
62
+ '_reqid' => @reqid.to_s,
63
+ 'rt' => 'c'
64
+ }
65
+ end
66
+
67
+ def payload(text_input)
68
+ input_text_struct = [[text_input], nil, [@conversation_id, @response_id, @choice_id]]
69
+ nested_json = [nil, input_text_struct.to_json].to_json
70
+ data = {
71
+ 'f.req' => nested_json,
72
+ 'at' => @snim0e
73
+ }
74
+ payload = URI.encode_www_form(data)
75
+ end
76
+
77
+ def handle_response(response)
78
+ response_dict = JSON.parse(response.body.split("\n")[3])[0][2]
79
+
80
+ if !response_dict.nil?
81
+ parsed_answer = JSON.parse(response_dict)
82
+ bard_answer = OpenStruct.new(
83
+ success: true,
84
+ content: parsed_answer[0][0],
85
+ conversation_id: parsed_answer[1][0],
86
+ response_id: parsed_answer[1][1],
87
+ factualityQueries: parsed_answer[3],
88
+ textQuery: (parsed_answer[2][0] if parsed_answer[2]),
89
+ choices: parsed_answer[4]&.map { |i| { id: i[0], content: i[1] } }
90
+ )
91
+ @conversation_id = bard_answer.conversation_id
92
+ @response_id = bard_answer.response_id
93
+ @choice_id = bard_answer.choices[0][:id]
94
+ @reqid += 100_000
95
+ bard_answer
96
+ else
97
+ OpenStruct.new(
98
+ success: false,
99
+ content: "Error: #{response.body}."
100
+ )
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,4 @@
1
+ module GoogleBard
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_bard
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Maël Harnois
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-05-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.18.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.18.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.3'
41
+ description: 'Reverse engineering inspired from the python package https://github.com/dsdanielpark/Bard-API '
42
+ email:
43
+ - mael.harnois@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rspec"
49
+ - ".rubocop.yml"
50
+ - CHANGELOG.md
51
+ - CODE_OF_CONDUCT.md
52
+ - Gemfile
53
+ - Gemfile.lock
54
+ - LICENSE.txt
55
+ - README.md
56
+ - Rakefile
57
+ - google_bard.gemspec
58
+ - lib/google_bard.rb
59
+ - lib/google_bard/version.rb
60
+ - sig/google_bard.rbs
61
+ homepage: https://github.com/mael-ha/google_bard
62
+ licenses:
63
+ - MIT
64
+ metadata:
65
+ homepage_uri: https://github.com/mael-ha/google_bard
66
+ source_code_uri: https://github.com/mael-ha/google_bard
67
+ changelog_uri: https://github.com/mael-ha/google_bard
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 2.6.0
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubygems_version: 3.4.6
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Bard's API access for completions
87
+ test_files: []