The python package that returns Response of Google Gemini through API.
Project description
Development Status :: 1 - Planning
Google - Gemini API
A unofficial Python wrapper, python-gemini-api, operates through reverse-engineering, utilizing cookie values to interact with Google Gemini for users struggling with frequent authentication problems or unable to authenticate via Google Authentication.
Collaborated competently with Antonio Cheong.
What is Gemini?
[Paper] [Official Website] [Official API] [API Documents]
Gemini is a family of generative AI models developed by Google DeepMind that is designed for multimodal use cases. The Gemini API gives you access to the Gemini Pro and Gemini Pro Vision models. In February 2024, Google's Bard service was changed to Gemini.
Installation
pip install python-gemini-api
pip install git+https://github.com/dsdanielpark/Gemini-API.git
For the updated version, use as follows:
pip install -q -U python-gemini-api
Authentication
[!NOTE] Cookies can change quickly. Don't reopen the same session or repeat prompts too often; they'll expire faster. If the cookie value doesn't export correctly, refresh the Gemini page and export again. Check this sample cookie file.
- Visit https://gemini.google.com/ and wait for it to fully load.
- (Recommended) Export gemini site cookies using a Chrome extension. Use ExportThisCookies, open and copy the txt file contents.
Further: For manual collection or Required for a few users upon error
- For manual cookie collection, refer to this image. Press F12 → Network → Send any prompt to gemini webui → Click the post address starting with "https://gemini.google.com/_/BardChatUi/data/assistant.lamda.BardFrontendService/StreamGenerate" → Headers → Request Headers → Cookie → Copy and Reformat as JSON manually.
- (Required for a few users upon error) If errors persist after manually collecting cookies, refresh the Gemini website and collect cookies again. If errors continue, some users may need to manually set the nonce value. To do this: Press F12 → Network → Send any prompt to gemini webui → Click the post address starting with "https://gemini.google.com/_/BardChatUi/data/assistant.lamda.BardFrontendService/StreamGenerate" → Payload → Form Data → Copy the "at" key value. See this image for reference.
Usage
After switching from Bard to Gemini, multiple region or Google account-based cookies, frequently updated, are necessary.
Initialization
You must appropriately set the cookies_dict
parameter to Gemini
class. When using the auto_cookies
argument to automatically collect cookies, keep the Gemini web page opened that receives Gemini's response open in your web browser.
from gemini import Gemini
cookies = {
"key": "value"
}
GeminiClient = Gemini(cookies=cookies)
# GeminiClient = Gemini(cookie_fp="folder/cookie_file.json") # Or use cookie file path
# GeminiClient = Gemini(auto_cookies=True) # Or use auto_cookies paprameter
# If you keep getting nonce errors, pass the nonce value. See the `Authentication.further` section above.
# GeminiClient = Gemini(cookies=cookies, nonce="value")
Can update cookies automatically using broser_cookie3. For the first attempt, manually download the cookies to test the functionality. (Currently in progress)
[!IMPORTANT] Before proceeding, ensure that the GeminiClient object is defined without any errors. If the session connects successfully and 'generate_content' runs well, close Gemini web. If Gemini web stays open in the browser, cookies may expire faster.
Text generation
prompt = "Hello, Gemini. What's the weather like in Seoul today?"
response = GeminiClient.generate_content(prompt)
print(response)
[!NOTE] If the session fails to connect, works improperly, or terminates, returning an error, it is recommended to manually renew the cookies. The error is likely due to incorrect cookie values. Refresh or log out of Gemini web to renew cookies and try again. However, do not retry multiple times after one success to set session.
Parser
Gemini web returns responses from various 3rd party services, requiring different types of parsers. If all parsing methods fail, it returns the response's text as is.
Please refer to gemini.models.parser.base. The default parser attempted is gemini.models.parser.methods, and you can also use custom parsers as arguments in generate_content
like this.
How to use CustomParser
class CustomParser(ResponseParser):
def parse(self, response_text):
"""
Implements custom parsing logic.
Example logic: Simply returns the response_text as is.
Args:
response_text (str): The text to be parsed.
Returns:
str: The parsed (or in this case, unchanged) response text.
"""
return parsed_response_text
prompt = "Hello, Gemini. What's the weather like in Seoul today?"
response = GeminiClient.generate_content(prompt, CustomParser)
Image generation
prompt = "Hello, Gemini. Give me a beautiful photo of Seoul's scenery."
response = GeminiClient.generate_content(prompt)
print("\n".join(response.images)) # Print images
for i, image in enumerate(response.images): # Save images
image.save(path="folder_path/", filename=f"seoul_{i}.png")
Generate content with image
As an experimental feature, it is possible to ask questions with an image. However, this functionality is only available for accounts with image upload capability in Gemini's web UI.
prompt = "What is in the image?"
image = open("folder_path/image.jpg", "rb").read() # (jpeg, png, webp) are supported.
response = GeminiClient.generate_content(prompt, image)
Text To Speech(TTS) from Gemini
Business users and high traffic volume may be subject to account restrictions according to Google's policies. Please use the Official Google Cloud API for any other purpose.
text = "Hello, I'm developer in seoul" # Gemini will speak this sentence
response = GeminiClient.generate_content(prompt)
audio = GeminiClient.speech(text)
with open("speech.ogg", "wb") as f:
f.write(bytes(audio["audio"]))
Further
Behind a proxy
If you are working behind a proxy, use the following.
proxies = {
"http": "http://proxy.example.com:8080",
"https": "https://proxy.example.com:8080"
}
GeminiClient = Gemini(cookies=cookies, proxies=proxies, timeout=30)
GeminiClient.generate_content("Hello, Gemini. Give me a beautiful photo of Seoul's scenery.")
Use rotating proxies
If you want to avoid blocked requests and bans, then use Smart Proxy by Crawlbase. It forwards your connection requests to a randomly rotating IP address in a pool of proxies before reaching the target website. The combination of AI and ML make it more effective to avoid CAPTCHAs and blocks.
# Get your proxy url at crawlbase https://crawlbase.com/docs/smart-proxy/get/
proxy_url = "http://xxxxx:@smartproxy.crawlbase.com:8012"
proxies = {"http": proxy_url, "https": proxy_url}
GeminiClient = Gemini(cookies=cookies, proxies=proxies, timeout=30)
GeminiClient.generate_content("Hello, Gemini. Give me a beautiful photo of Seoul's scenery.")
Reusable session object
You can continue the conversation using a reusable session. However, this feature is limited, and it is difficult for a package-level feature to perfectly maintain context. You can try to maintain the consistency of conversations same way as other LLM services, such as passing some sort of summary of past conversations to the DB.
from gemini import Gemini, HEADERS
import requests
cookies = {
"key": "value"
}
session = requests.Session()
session.headers = HEADERS
session.cookies.update(cookies)
GeminiClient = Gemini(session=session, timeout=30)
response = GeminiClient.generate_content("Hello, Gemini. What's the weather like in Seoul today?")
# Continued conversation without set new session
response = GeminiClient.generate_content("What was my last prompt?")
More features
- Chat Gemini
- Multi-language Gemini
- Export Conversation
- Export Code to Repl.it
- Executing Python code received as a response from Gemini
- Max_token, Max_sentences
How to use open-source Gemma
Gemma models are Google's lightweight, advanced text-to-text, decoder-only language models, derived from Gemini research. Available in English, they offer open weights and variants, ideal for tasks like question answering and summarization. Their small size enables deployment in resource-limited settings, broadening access to cutting-edge AI. For more infomation, visit Gemma-7b model card.
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("google/gemma-7b")
model = AutoModelForCausalLM.from_pretrained("google/gemma-7b")
input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(input_text, return_tensors="pt")
outputs = model.generate(**input_ids)
print(tokenizer.decode(outputs[0]))
Sponsor
Use Crawlbase API for efficient data scraping to train AI models, boasting a 98% success rate and 99.9% uptime. It's quick to start, GDPR/CCPA compliant, supports massive data extraction, and is trusted by 70k+ developers.
FAQ
You can find most help on the FAQ and Issue pages. Alternatively, utilize the official Gemini API at Google AI Studio.
Issues
Sincerely grateful for any reports on new features or bugs. Your valuable feedback on the code is highly appreciated. Frequent errors may occur due to changes in Google's service API interface. Both Issue reports and Pull requests contributing to improvements are always welcome. We strive to maintain an active and courteous open community.
Contributions
We would like to express our sincere gratitude to all the contributors.
Further development potential
-
refactoring
-
gemini/core
: httpx.session-
messages
-
content
-
text
-
parsing
-
-
image
-
parsing
-
-
-
response format structure class
-
tool_calls
-
-
third party
-
replit
-
google tools
-
-
-
gemini/client
: httpx.AsyncClient-
messages
-
content
-
text
-
parsing
-
-
image
-
parsing
-
-
-
response format structure class
-
tool_calls
-
-
third party
-
replit
-
google tools
-
-
Contacts
Core maintainers:
License
MIT license, 2024, Minwoo(Daniel) Park. We hereby strongly disclaim any explicit or implicit legal liability related to our works. Users are required to use this package responsibly and at their own risk. This project is a personal initiative and is not affiliated with or endorsed by Google. It is recommended to use Google's official API.
References
[1] Github acheong08/Bard
[2] Github dsdanielpark/Bard-API
[3] GitHub HanaokaYuzu/Gemini-API
[3] Github GoogleCloudPlatform/generative-ai
[4] Web Google AI Studio
Warning* Users bear all legal responsibilities when using the GeminiAPI package, which offers easy access to Google Gemini for developers. This unofficial Python package isn't affiliated with Google and may lead to Google account restrictions if used excessively or commercially due to its reliance on Google account cookies. Frequent changes in Google's interface, Google's API policies, and your country/region, as well as the status of your Google account, may affect functionality. Utilize the issue page and discussion page.
Copyright (c) 2024 Minwoo(Daniel) Park, South Korea
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file python-gemini-api-1.0.4.tar.gz
.
File metadata
- Download URL: python-gemini-api-1.0.4.tar.gz
- Upload date:
- Size: 36.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 711191526d1ac1fd7e302d44d6b515045d30f672bfd12d6e9e4f75501c641403 |
|
MD5 | d416df3a911ea0c85130c2112e0872dc |
|
BLAKE2b-256 | 9c72957d5e9a02d22a906d442c4d3ba246f1c2e83c50ca242952684182a6e556 |
File details
Details for the file python_gemini_api-1.0.4-py3-none-any.whl
.
File metadata
- Download URL: python_gemini_api-1.0.4-py3-none-any.whl
- Upload date:
- Size: 39.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9e0969c4980e3dd68f3917e23c633da02846166f7bc357ae15c18a746c86abfe |
|
MD5 | e911e0fff349e03f3362a2c542bc01bf |
|
BLAKE2b-256 | 17a2dd19f42c74f0dd2b0dc2e33a96add09937f39aa7834e20854cfffcd722a2 |