Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle google exceptions and add backoff #154

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions elleelleaime/evaluate/strategies/google/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def _evaluate_impl(self, bug: Bug, sample: dict) -> Optional[List[dict]]:

for generation in sample["generation"]:
for candidate in generation["candidates"]:
if "content" not in candidate:
evaluation.append(None)
continue
candidate_patch = candidate["content"]["parts"][0]["text"]
candidate_patch = self.extract_patch_from_message(candidate_patch)
evaluation.append(
Expand Down
24 changes: 17 additions & 7 deletions elleelleaime/generate/strategies/models/google/google.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import google.api_core
import google.api_core.exceptions
from elleelleaime.generate.strategies.strategy import PatchGenerationStrategy

from dotenv import load_dotenv
from typing import Any, List

import os
import tqdm
import google.generativeai as genai
import google
import backoff

import google.api


class GoogleModels(PatchGenerationStrategy):
def __init__(self, model_name: str, **kwargs) -> None:
self.model_name = model_name
self.model = genai.GenerativeModel(self.model_name)
self.temperature = kwargs.get("temperature", 0.0)
self.n_samples = kwargs.get("n_samples", 1)

Expand All @@ -21,18 +29,20 @@ def __get_config(self):
temperature=self.temperature,
)

@backoff.on_exception(backoff.expo, google.api_core.exceptions.ResourceExhausted)
def __generate_with_backoff(self, prompt: str) -> dict:
completion = self.model.generate_content(
prompt, generation_config=self.__get_config()
)
return completion.to_dict()

def _generate_impl(self, chunk: List[str]) -> Any:
result = []

model = genai.GenerativeModel(self.model_name)

for prompt in chunk:
for prompt in tqdm.tqdm(chunk, "Generating patches for prompt..."):
p_results = []
for _ in range(self.n_samples):
completion = model.generate_content(
prompt, generation_config=self.__get_config()
)
p_results.append(completion.to_dict())
p_results.append(self.__generate_with_backoff(prompt))
result.append(p_results)

return result
Loading