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
- Import:
import geminiwrapper
-
Obtain Gemini AI API key: Get Gemini AI API key
-
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"],
)
- 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.
- 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
Built Distribution
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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 206d9f32748896422f9d460b3b359ad5417689ccb92189c74bd320a67a8c2a3b |
|
MD5 | e329d0c85a2f2f6c03b121d6f49008fd |
|
BLAKE2b-256 | 8aa2ff46a51be35a26028f7f7191cebde86f742ea2e4b858bbced3a99b82d114 |
File details
Details for the file geminiwrapper-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: geminiwrapper-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5944408865da83f1e709403c1861e98479bf09faa75c9439854a4f40046e46fb |
|
MD5 | eeca99be47cab1ae5aed4023ea18f5eb |
|
BLAKE2b-256 | e0d243d779412684714a9d5f5fcf38d26d574439dd6b7f5bb39351c3f104f49b |