Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dsdanielpark committed Apr 27, 2024
2 parents 0455936 + c501950 commit df0515b
Showing 1 changed file with 55 additions and 51 deletions.
106 changes: 55 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,14 @@ pip install -q -U python-gemini-api
With browser open, try auto-collecting cookies first.
```python
from gemini import Gemini
GeminiClient = Gemini(auto_cookies=True)

client = Gemini(auto_cookies=True)

# Testing needed as cookies vary by region.
# GeminiClient = Gemini(auto_cookies=True, target_cookies=["__Secure-1PSID", "__Secure-1PSIDTS"])
# GeminiClient = Gemini(auto_cookies=True, target_cookies="all") # You can pass whole cookies
# client = Gemini(auto_cookies=True, target_cookies=["__Secure-1PSID", "__Secure-1PSIDTS"])
# client = Gemini(auto_cookies=True, target_cookies="all") # You can pass whole cookies

response = GeminiClient.generate_content("Hello, Gemini. What's the weather like in Seoul today?")
response = client.generate_content("Hello, Gemini. What's the weather like in Seoul today?")
print(response.payload)
```
2. *(Manually)* `F12` for browser console → `Session: Application``Cookies` → Copy the value of some working cookie sets. If it doesn't work, go to step 3.
Expand Down Expand Up @@ -149,21 +150,21 @@ pip install -q -U python-gemini-api
```python
from gemini import Gemini

cookies = {} # Cookies may vary by account or region. Consider sending the entire cookie file.
GeminiClient = Gemini(cookies=cookies) # You can use various args
cookies = {"key" : "value"} # Cookies may vary by account or region. Consider sending the entire cookie file.
client = Gemini(cookies=cookies) # You can use various args

response = GeminiClient.generate_content("Hello, Gemini. What's the weather like in Seoul today?")
response = client.generate_content("Hello, Gemini. What's the weather like in Seoul today?")
response.payload
```

**Generate content from image:** you can use image as input.
```python
from gemini import Gemini

cookies = {} # Cookies may vary by account or region. Consider sending the entire cookie file.
cookies = {"key" : "value"} # Cookies may vary by account or region. Consider sending the entire cookie file.
client = Gemini(cookies=cookies) # You can use various args

GeminiClient = Gemini(cookies=cookies) # You can use various args
response = GeminiClient.generate_content("What does the text in this image say?", image='folder/image.jpg')
response = client.generate_content("What does the text in this image say?", image='folder/image.jpg')
response.payload
```

Expand All @@ -180,6 +181,7 @@ Setting Gemini response language (Optional): Check supported languages [here](ht

```python
import os

os.environ["GEMINI_LANGUAGE"] = "KR" # Setting Gemini response language (Optional)
os.environ["GEMINI_ULTRA"] = "1" # Switch to Gemini-advanced response (Experimental, Optional)
# In some accounts, access to Gemini Ultra may not be available. If that's the case, please revert it back to "0".
Expand All @@ -206,9 +208,9 @@ cookies = {
# Cookies may vary by account or region. Consider sending the entire cookie file.
}

GeminiClient = Gemini(cookies=cookies)
# GeminiClient = Gemini(cookie_fp="folder/cookie_file.json") # (*.json, *.txt) are supported.
# GeminiClient = Gemini(auto_cookies=True) # Or use auto_cookies paprameter
client = Gemini(cookies=cookies)
# client = Gemini(cookie_fp="folder/cookie_file.json") # (*.json, *.txt) are supported.
# client = Gemini(auto_cookies=True) # Or use auto_cookies paprameter
```

##### Auto Cookie Update
Expand All @@ -224,7 +226,7 @@ Returns Gemini's response, but the first one might be empty.

```python
prompt = "Hello, Gemini. What's the weather like in Seoul today?"
response = GeminiClient.generate_content(prompt)
response = client.generate_content(prompt)
print(response.payload)
```

Expand Down Expand Up @@ -255,9 +257,9 @@ Send request: returns the request's payload and status_code, making debugging ea
from gemini import Gemini

cookies = {} # Cookies may vary by account or region. Consider sending the entire cookie file.
GeminiClient = Gemini(cookies=cookies) # You can use various args
client = Gemini(cookies=cookies) # You can use various args

response_text, response_status = GeminiClient.send_request("Hello, Gemini. What's the weather like in Seoul today?")
response_text, response_status = client.send_request("Hello, Gemini. What's the weather like in Seoul today?")
print(response_text)
```
You can track the total number of requests made by accessing the `request_count` property within the `Gemini` class.
Expand All @@ -268,7 +270,7 @@ You can track the total number of requests made by accessing the `request_count`
Returns text generated by Gemini.
```python
prompt = "Hello, Gemini. What's the weather like in Seoul today?"
response = GeminiClient.generate_content(prompt)
response = client.generate_content(prompt)
print(response.text)
```

Expand All @@ -281,7 +283,7 @@ Returns images generated by Gemini.
*Async downloader*

```python
response = GeminiClient.generate_content("Create illustrations of Seoul, South Korea.")
response = client.generate_content("Create illustrations of Seoul, South Korea.")

generated_images = response.generated_images # Check generated images [Dict]

Expand Down Expand Up @@ -312,7 +314,7 @@ await GeminiImage.save(generated_images, "save_dir", cookies=cookies)
```python
from gemini import Gemini, GeminiImage

response = GeminiClient.generate_content("Create illustrations of Seoul, South Korea.")
response = client.generate_content("Create illustrations of Seoul, South Korea.")
generated_images = response.generated_images # Check generated images [Dict]

GeminiImage.save_sync(generated_images, save_path="save_dir", cookies=cookies)
Expand Down Expand Up @@ -368,7 +370,7 @@ Returns images in response of Gemini.

*Async downloader*
```python
response = GeminiClient.generate_content("Create illustrations of Seoul, South Korea.")
response = client.generate_content("Create illustrations of Seoul, South Korea.")

response_images = response.web_images # Check generated images [Dict]

Expand All @@ -384,7 +386,7 @@ await GeminiImage.save(response_images, "save_dir")
```python
from gemini import Gemini, GeminiImage

response = GeminiClient.generate_content("Please recommend a travel itinerary for Seoul.")
response = client.generate_content("Please recommend a travel itinerary for Seoul.")
response_images = response.web_images # Check response images [Dict]

GeminiImage.save_sync(response_images, save_path="save_dir")
Expand Down Expand Up @@ -436,7 +438,7 @@ Takes an image as input and returns a response.
image = 'folder/image.jpg'
# image = open('folder/image.jpg', 'rb').read() # (jpg, jpeg, png, webp) are supported.

response = GeminiClient.generate_content("What does the text in this image say?", image=image)
response = client.generate_content("What does the text in this image say?", image=image)
response.response_dict
```

Expand All @@ -450,7 +452,7 @@ To begin, you must link Google Workspace to activate this extension via the [Gem
@Gmail, @Google Drive, @Google Docs, @Google Maps, @Google Flights, @Google Hotels, @YouTube
```
```python
response = GeminiClient.generate_content("@YouTube Search clips related with Google Gemini")
response = client.generate_content("@YouTube Search clips related with Google Gemini")
response.response_dict
```
<details><summary>Extension description</summary>
Expand Down Expand Up @@ -489,15 +491,15 @@ You can specify a particular response by setting its Response Candidate ID(RCID)

```python
# Generate content for the prompt "Give me some information about the USA."
response1 = GeminiClient.generate_content("Give me some information about the USA.")
response1 = client.generate_content("Give me some information about the USA.")
# After reviewing the responses, choose the one you prefer and copy its RCID.
GeminiClient.rcid = "rc_xxxx"
client.rcid = "rc_xxxx"

# Now, generate content for the next prompt "How long does it take from LA to New York?"
response2 = GeminiClient.generate_content("How long does it take from LA to New York?")
response2 = client.generate_content("How long does it take from LA to New York?")

# However, RCID may not persist. If parsing fails, reset `GeminiClient.rcid` to None.
# GeminiClient.rcid = None
# However, RCID may not persist. If parsing fails, reset `client.rcid` to None.
# client.rcid = None
```


Expand All @@ -510,7 +512,7 @@ In Gemini, generate_content returns the first response. This may vary depending
from gemini import GeminiModelOutput

GeminiModelOutput.chosen = 1 # default is 0
response_choice_1 = GeminiClient.generate_content("Give me some information about the USA.")
response_choice_1 = client.generate_content("Give me some information about the USA.")

# If not all Gemini returns are necessarily plural, revert back to 0 in case of errors.
# GeminiModelOutput.chosen = 0
Expand All @@ -526,10 +528,10 @@ Using `Gemini.generate_custom_content`, specify custom parsing to extract specif

```python
# You can create a parser method that takes response_text as the input for custom_parser.
response_text, response_status = GeminiClient.send_request("Give me some information about the USA.")
response_text, response_status = client.send_request("Give me some information about the USA.")

# Use custom_parser function or class inheriting from BaseParser
response = GeminiClient.generate_custom_content("Give me some information about the USA.", *custom_parser)
response = client.generate_custom_content("Give me some information about the USA.", *custom_parser)
```

https://github.com/dsdanielpark/Gemini-API/blob/31b842488bbc5429ad9c74b1d8b00e20d94e8cb1/gemini/client.py#L323
Expand All @@ -546,8 +548,8 @@ If you want to **avoid blocked requests** and bans, then use [Smart Proxy by Cra
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.")
client = Gemini(cookies=cookies, proxies=proxies, timeout=30)
client.generate_content("Hello, Gemini. Give me a beautiful photo of Seoul's scenery.")
```

### Reusable session object
Expand All @@ -563,8 +565,8 @@ session.headers = Headers.MAIN
for key, value in cookies.items():
session.cookies.update({key: value})

GeminiClient = Gemini(session=session) # You can use various args
response = GeminiClient.generate_content("Hello, Gemini. What's the weather like in Seoul today?")
client = Gemini(session=session) # You can use various args
response = client.generate_content("Hello, Gemini. What's the weather like in Seoul today?")
```


Expand Down Expand Up @@ -636,13 +638,13 @@ OpenRouter offers temporary free inference for select models. Obtain an API key
from gemini import OpenRouter

OPENROUTER_API_KEY = "<your_open_router_api_key>"
GemmaClient = OpenRouter(api_key=OPENROUTER_API_KEY, model="google/gemma-7b-it:free")
gemma_client = OpenRouter(api_key=OPENROUTER_API_KEY, model="google/gemma-7b-it:free")

prompt = "Do you know UCA academy in Korea? https://blog.naver.com/ulsancoding"
response = GemmaClient.create_chat_completion(prompt)
response = gemma_client.create_chat_completion(prompt)
print(response)

# payload = GemmaClient.generate_content(prompt)
# payload = gemma_client.generate_content(prompt)
# print(payload.json())
```

Expand Down Expand Up @@ -713,19 +715,21 @@ Core maintainers:


## References
[1]: Paper - [Introducing GEMINI: Multimodal Generative Models](https://arxiv.org/abs/2312.11805) <br>
[2]: Website - [Google DeepMind :: GEMINI Introduction](https://deepmind.google/technologies/gemini/#introduction) <br>
[3]: Paper - [GEMMA: A Unified Language Model for Text Generation, Understanding, Translation, Coding, and Math.]() <br>
[4]: Website - [AI at Google :: GEMS Documentation](https://ai.google.dev/gemma/docs) <br>
[5]: Report - [CodeGMMA: Large Language Models Can Write Realistic Programming Assignments](https://storage.googleapis.com/deepmind-media/gemma/codegemma_report.pdf?utm_source=substack&utm_medium=email) <br>
[6]: Blog Post - [Announcing CodeGen: Building Better Developers’ Tools Using LLMs](https://huggingface.co/blog/codegen) <br>
[7]: Collection - [Google :: CodeGen Release](https://huggingface.co/collections/google/codegen-release-5d0f4c4eaedbc5cefcfdcbdf) <br>
[8]: Github - [acheong08/Bard](https://github.com/acheong08/Bard) <br>
[9]: GitHub - [HanaokaYuzu/Gemini-API](https://github.com/HanaokaYuzu/Gemini-API) <br>
[10]: Github - [dsdanielpark/Bard-API](https://github.com/dsdanielpark/Bard-API) <br>
[11]: Github - [GoogleCloudPlatform/generative-ai](https://github.com/GoogleCloudPlatform/generative-ai) <br>
[12]: Github - [OpenRouter](https://github.com/OpenRouterTeam/openrouter-runner) <br>
[13]: WebSite - [Google AI Studio](https://ai.google.dev/tutorials/ai-studio_quickstart) <br>
1. [Introducing GEMINI: Multimodal Generative Models](https://arxiv.org/abs/2312.11805)
2. [Google DeepMind: GEMINI Introduction](https://deepmind.google/technologies/gemini/#introduction)
3. [GEMMA: A Unified Language Model for Text Generation, Understanding, Translation, Coding, and Math](https://arxiv.org/abs/2403.08295)
4. [AI at Google: GEMS Documentation](https://ai.google.dev/gemma/docs)
5. [CodeGMMA: Large Language Models Can Write Realistic Programming Assignments](https://storage.googleapis.com/deepmind-media/gemma/codegemma_report.pdf?utm_source=substack&utm_medium=email)
6. [Announcing CodeGen: Building Better Developers' Tools Using LLMs](https://huggingface.co/blog/codegen)
7. [Google: CodeGen Release](https://huggingface.co/collections/google/codegen-release-5d0f4c4eaedbc5cefcfdcbdf)
8. [acheong08/Bard](https://github.com/acheong08/Bard)
9. [dsdanielpark/Bard-API](https://github.com/dsdanielpark/Bard-API)
10. [HanaokaYuzu/Gemini-API](https://github.com/HanaokaYuzu/Gemini-API)
11. [GoogleCloudPlatform/generative-ai](https://github.com/GoogleCloudPlatform/generative-ai)
12. [OpenRouter](https://github.com/OpenRouterTeam/openrouter-runner)
13. [Google AI Studio](https://ai.google.dev/tutorials/ai-studio_quickstart)

<br>


> *Warning*
Expand Down

0 comments on commit df0515b

Please sign in to comment.