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

llm exception handling #711

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Format
  • Loading branch information
cmungall committed Mar 11, 2024

Verified

This commit was signed with the committer’s verified signature.
vikman90 Vikman Fernandez-Castro
commit 1f4792fad3fe83034d7a8ece97d6da83fc030685
13 changes: 10 additions & 3 deletions src/oaklib/implementations/llm_implementation.py
Original file line number Diff line number Diff line change
@@ -8,7 +8,12 @@
from typing import TYPE_CHECKING, Dict, Iterable, Iterator, List, Optional, Tuple

from sssom_schema import Mapping
from tenacity import retry, retry_if_exception, stop_after_attempt, wait_random_exponential
from tenacity import (
retry,
retry_if_exception,
stop_after_attempt,
wait_random_exponential,
)

from oaklib import BasicOntologyInterface
from oaklib.datamodels.obograph import DefinitionPropertyValue
@@ -83,6 +88,7 @@ def is_rate_limit_error(exception):
logger.warning(f"Exception: {exception}")
return exception_full_name in rate_limit_errors


@retry(
retry=retry_if_exception(is_rate_limit_error),
wait=wait_random_exponential(multiplier=1, max=40),
@@ -91,6 +97,7 @@ def is_rate_limit_error(exception):
def query_model(model, *args, **kwargs):
return model.prompt(*args, **kwargs)


@dataclass
class LLMImplementation(
OboGraphInterface,
@@ -149,14 +156,14 @@ def __post_init__(self):
self.wrapped_adapter = get_adapter(slug)
if self.model_id is not None:
import llm

self.model = llm.get_model(self.model_id)
if "claude" in self.model_id or "openrouter" in self.model_id:
# TODO: claude API seems to have its own rate limiting
# TODO: openrouter just seems very flaky
# but it is too conservative
self.throttle_time = 10


def entities(self, **kwargs) -> Iterator[CURIE]:
"""Return all entities in the ontology."""
yield from self.wrapped_adapter.entities(**kwargs)
@@ -417,7 +424,7 @@ def _get_description(entity: CURIE) -> Optional[str]:
extra += "Please try again, WITH VALID JSON."
extra += "Do not apologize or give more verbiage, JUST JSON."
logger.info(f"New Prompt: {main_prompt + extra}")
response = query_model(model,main_prompt + extra, system=system_prompt).text()
response = query_model(model, main_prompt + extra, system=system_prompt).text()
try:
obj = json.loads(response)
except json.JSONDecodeError as e: