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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 3

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