Skip to main content

Gemini AI module (using Gemini AI API through requests lib)

Project description

GeminiRequests

GeminiRequests is a Python library (and code example) designed for interacting with Google’s Gemini AI text generation model using the requests library.
This code provides the Gen class, which allows you to store dialogue history, send requests to the API, load/save conversation history, and more.
Note: The last message in the dialogue history must always be from the user when generating text.

Features

  1. Easy integration with Gemini AI:
    Uses the standard requests library to send HTTP requests to Google’s Gemini AI API.

  2. Dialogue history management:
    The Gen class keeps track of conversation history (a list of messages with roles and text), making it highly useful for context-dependent tasks.
    Important: Ensure the last message in the history is from the user before calling generate().

  3. Convenient export and import methods:

    • export_history(filename) / import_history(filename)
    • import_history_anyway(filename) (attempts to load history and creates a file if it doesn’t exist)
  4. API key management:
    On the first run, it automatically creates a gemini_api_key file (in the GeminiRequests folder) where you can save your API key.


Installation and Setup

Installing from PyPI

pip install GeminiRequests

GitHub

  1. Clone the repository (or copy this code into your project):

    git clone https://github.com/Python-shik/GeminiRequests.git
    
  2. Install dependencies:

    pip install -r requirements.txt
    

    If you’re only using this module, you can simply install:

    pip install requests
    

Set up the API key:

  • On the first run, the script will ask you to enter your Gemini AI API key.
  • The key will be saved in the GeminiRequests/gemini_api_key file.

Usage

Assume the file with the Gen class is stored in the GeminiRequests folder. Example:

from GeminiRequests import Gen  # Assuming the file is named gen.py

# Create an instance of the class. 
# On the first run, you’ll be prompted to enter your API key if the gemini_api_key file is missing.
ai = Gen()

# Add some messages to the dialogue history
ai.history_add(role="user", content="Hello, Gemini! Tell me a joke.")
ai.history_add(role="system", content="For context generation.")

# Ensure the last message in the history is from the user
ai.history_add(role="user", content="What else can you do?")

# Generate a response
response = ai.generate()
print("Response from Gemini AI:", response)

# Save the history
ai.export_history("dialog_history.pkl")

# Clear local history (from memory)
ai.history = []

# Load history again if needed
ai.import_history("dialog_history.pkl")

The Gen Class and Its Methods

  • Gen(history=[], system_instructions=None)
    Constructor. On the first run, it creates a gemini_api_key file if it doesn’t exist.

    • history: a list of dictionaries like {"role": ..., "parts": [{"text": ...}]}, describing the current conversation history.
    • system_instructions: a list of instructions sent as part of the request (essentially "prompts" or "context" from the system).
  • history_add(role, content)
    Adds a new message (role and text) to the dialogue history.

  • generate()
    Sends the dialogue history (and system instructions, if any) to the API and retrieves the generated text.
    Important: Ensure the last message in the history is from the user before calling this method.

  • export_history(filename) / import_history(filename)
    Save/load the dialogue history to/from a file using pickle.

  • import_history_anyway(filename)
    Attempts to load the history from a file. If the file doesn’t exist, it creates one and retries.

  • clear_history(filename)
    Deletes the history file and clears the history from memory.


License

This project is distributed under the MIT License.
You are free to modify and use the code in your projects, including commercial ones.


Author

Developed by Illya Lazarev
Email: sbdt.israel@gmail.com

If you have ideas or suggestions for improvements, feel free to submit pull requests or open an Issue.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

geminirequests-0.1.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

GeminiRequests-0.1.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file geminirequests-0.1.0.tar.gz.

File metadata

  • Download URL: geminirequests-0.1.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.2

File hashes

Hashes for geminirequests-0.1.0.tar.gz
Algorithm Hash digest
SHA256 aee35371c86d23e782322b722c4015eba86246cb73a7f1045f798358211df64c
MD5 d1e6c4a9a51e6c9e7153e588d94636d1
BLAKE2b-256 1916ecbf1c5e90d26839b2f210df701e873ad076ffb1783e96788a8d7b81ecc5

See more details on using hashes here.

File details

Details for the file GeminiRequests-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: GeminiRequests-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.2

File hashes

Hashes for GeminiRequests-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9e3d2482fd6e4d9db570ea12e21ad9cd3b3cbb6ce1c977f3066953c80372443e
MD5 ef1624fd594a855c399fdbad51703b13
BLAKE2b-256 d64377ce5a1648298f505e2dffd92e34b24136d80359a24fc8ca3c2dc1caabf8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page