From 527cd3e0cc9d529f9bd2f1d5053def13dd1a037b Mon Sep 17 00:00:00 2001
From: "Ankush Pala ankush@lastmileai.dev" <>
Date: Mon, 5 Feb 2024 17:28:31 -0500
Subject: [PATCH 1/3] [python] temporary Fix Gemini Event loop
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Gemini api under the hood saves a reference to the async event loop. This messes with the local editor because we create a new event loop on api/run.
This diff reinitializes the "client" at the run step for the Gemini model parser. I put client in quotes because its not called a client but its analogous to the openai client abstraction.
## Testplan
Run a gemini prompt twice inside local editor.
Before:
After:
run twice, no event loop error
https://github.com/lastmile-ai/gradio-workbook/assets/141073967/39d1d4b2-90c5-4ee1-b1a4-bc35d8528d60
---
python/src/aiconfig/default_parsers/gemini.py | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/python/src/aiconfig/default_parsers/gemini.py b/python/src/aiconfig/default_parsers/gemini.py
index d599cbae2..971964b8c 100644
--- a/python/src/aiconfig/default_parsers/gemini.py
+++ b/python/src/aiconfig/default_parsers/gemini.py
@@ -104,6 +104,7 @@ def __init__(self, id: str = "gemini-pro"):
super().__init__()
# TODO(?): @Ankush-lastmile Figure out how to make this model parser impl and init not depend on the model
self.model = genai.GenerativeModel(id)
+ self.model_id = id
# Note: we don't need to explicitly set the key as long as this is set
# as an env var genai.configure() will pick up the env var
@@ -329,6 +330,10 @@ async def run_inference(
if not self.api_key:
self.api_key = get_api_key_from_environment("GOOGLE_API_KEY", required=True).unwrap()
+
+ # Gemini api stores a reference to the currently executing async event loop.
+ # Reinitialize the "client" to avoid issues with the event loop.
+ self.model = genai.GenerativeModel(self.model_id)
genai.configure(api_key=self.api_key)
# TODO: check and handle api key here
From 50cada26b37152da6bec052e3fe9e7e872292dc7 Mon Sep 17 00:00:00 2001
From: "Ankush Pala ankush@lastmileai.dev" <>
Date: Mon, 5 Feb 2024 17:50:32 -0500
Subject: [PATCH 2/3] [easy] Set Gemini model parser id to the model id being
used
title
---
python/src/aiconfig/default_parsers/gemini.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/python/src/aiconfig/default_parsers/gemini.py b/python/src/aiconfig/default_parsers/gemini.py
index 971964b8c..d774cf94c 100644
--- a/python/src/aiconfig/default_parsers/gemini.py
+++ b/python/src/aiconfig/default_parsers/gemini.py
@@ -116,7 +116,7 @@ def id(self) -> str:
"""
Returns an identifier for the model (e.g. llama-2, gpt-4, etc.).
"""
- return "GeminiModelParser"
+ return self.model_id
async def serialize(
self,
From e5862672b1708e765ca450a7c577059422866d26 Mon Sep 17 00:00:00 2001
From: Ankush <141073967+Ankush-lastmile@users.noreply.github.com>
Date: Mon, 5 Feb 2024 18:06:38 -0500
Subject: [PATCH 3/3] comment nit
added comment
Co-authored-by: Sarmad Qadri
---
python/src/aiconfig/default_parsers/gemini.py | 1 +
1 file changed, 1 insertion(+)
diff --git a/python/src/aiconfig/default_parsers/gemini.py b/python/src/aiconfig/default_parsers/gemini.py
index d774cf94c..3432eb9c5 100644
--- a/python/src/aiconfig/default_parsers/gemini.py
+++ b/python/src/aiconfig/default_parsers/gemini.py
@@ -332,6 +332,7 @@ async def run_inference(
self.api_key = get_api_key_from_environment("GOOGLE_API_KEY", required=True).unwrap()
# Gemini api stores a reference to the currently executing async event loop.
+ # This causes issues on reruns for the model parser, so to alleviate this, we
# Reinitialize the "client" to avoid issues with the event loop.
self.model = genai.GenerativeModel(self.model_id)
genai.configure(api_key=self.api_key)