Skip to main content

Library for generating consistent Gemini API outputs and wrapping them with json and python objects

Project description

GeminiWrapper

GeminiWrapper is a library for generating consistent Gemini API outputs and wrapping them with json and python objects. It takes python dictionaries(for json format) or python objects as few-shot examples to output valid json or python objects.

Getting Started

Installation

pip install geminiwrapper

Basic Usage

  1. Import:
import geminiwrapper
  1. Obtain Gemini AI API key: Get Gemini AI API key

  2. Create example objects:

  • For json output, create a few python dictionaries. For example,
json1 = {
    "name": "England",
    "capital": "London",
    "population": "56.3 million",
    "neighbouring_countries": ["Scotland", "Wales"],
}
json2 = {
    "name": "Thailand",
    "capital": "Bangkok",
    "population": "70.4 million",
    "neighbouring_countries": ["Myanmar (Burma)", "Laos", "Cambodia", "Malaysia"],
}
  • For python object output, create a class and a few objects. For example,
class Country:
    def __init__(self, name, capital, population, neighbouring_countries) -> None:
        self.name = name
        self.capital = capital
        self.population = population
        self.neighbouring_countries = neighbouring_countries
country1 = Country("England", "London", "56.3 million", ["Scotland", "Wales"])
country2 = Country(
    "Thailand",
    "Bangkok",
    "70.4 million",
    ["Myanmar (Burma)", "Laos", "Cambodia", "Malaysia"],
)
  1. Instantiate the wrapper:
  • For json output,
json_model = JsonWrapper(api_key, [json1, json2])

api_key is the Gemini API key obtained from this link

  • For python object output,
object_model = ObjectWrapper(api_key, __name__, [country1, country2])

api_key is the Gemini API key obtained from this link __name__ is to pass the name of the module.

  1. Generate Content:
  • For json output,
prompt = "Write about Myanmar, Singapore, United States and Germany"
response = json_model.generate_content(prompt)

response is a list of dictionaries that can be processed as follow.

for country in response:
    print(f"The name of the country: {country['name']}")
    print(f"The capital of the country: {country['capital']}")
    print(f"The population of the country: {country['population']}")
    print(f"The neighbouring countries: {country['neighbouring_countries']}")
  • For python object output,
prompt = "Write about Myanmar, Singapore, United States and Germany"
response: List[Country] = object_model.generate_content(prompt)

response is a list of Country objects that can be processed as follow.

for country in response:
    print(f"The name of the country: {country.name}")
    print(f"The capital of the country: {country.capital}")
    print(f"The population of the country: {country.population}")
    print(f"The neighbouring countries: {country.neighbouring_countries}")

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

geminiwrapper-0.1.0.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

geminiwrapper-0.1.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: geminiwrapper-0.1.0.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.4

File hashes

Hashes for geminiwrapper-0.1.0.tar.gz
Algorithm Hash digest
SHA256 206d9f32748896422f9d460b3b359ad5417689ccb92189c74bd320a67a8c2a3b
MD5 e329d0c85a2f2f6c03b121d6f49008fd
BLAKE2b-256 8aa2ff46a51be35a26028f7f7191cebde86f742ea2e4b858bbced3a99b82d114

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for geminiwrapper-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5944408865da83f1e709403c1861e98479bf09faa75c9439854a4f40046e46fb
MD5 eeca99be47cab1ae5aed4023ea18f5eb
BLAKE2b-256 e0d243d779412684714a9d5f5fcf38d26d574439dd6b7f5bb39351c3f104f49b

See more details on using hashes here.

Supported by

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