From 4b2b094ea6727368137cdf4a00aa907308132dc0 Mon Sep 17 00:00:00 2001 From: Siddhesh Agarwal Date: Thu, 31 Oct 2024 17:08:14 +0530 Subject: [PATCH] moved to cached_property from property --- simplemind/providers/_base.py | 9 +++++---- simplemind/providers/anthropic.py | 5 +++-- simplemind/providers/gemini.py | 5 +++-- simplemind/providers/groq.py | 5 +++-- simplemind/providers/ollama.py | 5 +++-- simplemind/providers/openai.py | 5 +++-- simplemind/providers/xai.py | 6 ++++-- 7 files changed, 24 insertions(+), 16 deletions(-) diff --git a/simplemind/providers/_base.py b/simplemind/providers/_base.py index a8d4a4c..42ffb9b 100644 --- a/simplemind/providers/_base.py +++ b/simplemind/providers/_base.py @@ -1,5 +1,6 @@ from abc import ABC, abstractmethod -from typing import Type, TypeVar +from functools import cached_property +from typing import Any, Type, TypeVar from instructor import Instructor from pydantic import BaseModel @@ -13,13 +14,13 @@ class BaseProvider(ABC): NAME: str DEFAULT_MODEL: str - @property + @cached_property @abstractmethod - def client(self): + def client(self) -> Any: """The instructor client for the provider.""" raise NotImplementedError - @property + @cached_property @abstractmethod def structured_client(self) -> Instructor: """The structured client for the provider.""" diff --git a/simplemind/providers/anthropic.py b/simplemind/providers/anthropic.py index 38cdbc3..4a79dce 100644 --- a/simplemind/providers/anthropic.py +++ b/simplemind/providers/anthropic.py @@ -1,3 +1,4 @@ +from functools import cached_property from typing import Type, TypeVar import anthropic @@ -22,14 +23,14 @@ class Anthropic(BaseProvider): def __init__(self, api_key: str | None = None): self.api_key = api_key or settings.get_api_key(PROVIDER_NAME) - @property + @cached_property def client(self): """The raw Anthropic client.""" if not self.api_key: raise ValueError("Anthropic API key is required") return anthropic.Anthropic(api_key=self.api_key) - @property + @cached_property def structured_client(self): """A client patched with Instructor.""" return instructor.from_anthropic(self.client) diff --git a/simplemind/providers/gemini.py b/simplemind/providers/gemini.py index ec21edc..1e7a932 100644 --- a/simplemind/providers/gemini.py +++ b/simplemind/providers/gemini.py @@ -1,3 +1,4 @@ +from functools import cached_property from typing import Type, TypeVar import google.generativeai as genai @@ -22,7 +23,7 @@ class Gemini(BaseProvider): self.api_key = api_key or settings.get_api_key(PROVIDER_NAME) self.model_name = DEFAULT_MODEL - @property + @cached_property def client(self, model_name: str = DEFAULT_MODEL): """The raw Gemini client.""" if not self.api_key: @@ -30,7 +31,7 @@ class Gemini(BaseProvider): self.model_name = model_name return genai.GenerativeModel(model_name=self.model_name) - @property + @cached_property def structured_client(self): """A Gemini client patched with Instructor.""" return instructor.from_gemini(self.client) diff --git a/simplemind/providers/groq.py b/simplemind/providers/groq.py index 574ff10..d1a3c8e 100644 --- a/simplemind/providers/groq.py +++ b/simplemind/providers/groq.py @@ -1,3 +1,4 @@ +from functools import cached_property from typing import Type, TypeVar import groq @@ -20,14 +21,14 @@ class Groq(BaseProvider): def __init__(self, api_key: str | None = None): self.api_key = api_key or settings.get_api_key(PROVIDER_NAME) - @property + @cached_property def client(self): """The raw Groq client.""" if not self.api_key: raise ValueError("Groq API key is required") return groq.Groq(api_key=self.api_key) - @property + @cached_property def structured_client(self): """A client patched with Instructor.""" return instructor.from_groq(self.client) diff --git a/simplemind/providers/ollama.py b/simplemind/providers/ollama.py index 9950db9..7d1ac22 100644 --- a/simplemind/providers/ollama.py +++ b/simplemind/providers/ollama.py @@ -1,3 +1,4 @@ +from functools import cached_property from typing import Type, TypeVar import instructor @@ -24,14 +25,14 @@ class Ollama(BaseProvider): def __init__(self, host_url: str | None = None): self.host_url = host_url or settings.OLLAMA_HOST_URL - @property + @cached_property def client(self): """The raw Ollama client.""" if not self.host_url: raise ValueError("No ollama host url provided") return ol.Client(timeout=self.TIMEOUT, host=self.host_url) - @property + @cached_property def structured_client(self) -> instructor.Instructor: """A client patched with Instructor.""" return instructor.from_openai( diff --git a/simplemind/providers/openai.py b/simplemind/providers/openai.py index cc660be..b6fa568 100644 --- a/simplemind/providers/openai.py +++ b/simplemind/providers/openai.py @@ -1,3 +1,4 @@ +from functools import cached_property from typing import Type, TypeVar import instructor @@ -20,14 +21,14 @@ class OpenAI(BaseProvider): def __init__(self, api_key: str | None = None): self.api_key = api_key or settings.get_api_key(PROVIDER_NAME) - @property + @cached_property def client(self): """The raw OpenAI client.""" if not self.api_key: raise ValueError("OpenAI API key is required") return oa.OpenAI(api_key=self.api_key) - @property + @cached_property def structured_client(self): """A OpenAI client with Instructor.""" return instructor.from_openai(self.client) diff --git a/simplemind/providers/xai.py b/simplemind/providers/xai.py index 78cb096..dd32e0b 100644 --- a/simplemind/providers/xai.py +++ b/simplemind/providers/xai.py @@ -1,3 +1,5 @@ +from functools import cached_property + import instructor import openai as oa @@ -17,7 +19,7 @@ class XAI(BaseProvider): def __init__(self, api_key: str | None = None): self.api_key = api_key or settings.get_api_key(PROVIDER_NAME) - @property + @cached_property def client(self): """The raw OpenAI client.""" if not self.api_key: @@ -27,7 +29,7 @@ class XAI(BaseProvider): base_url=BASE_URL, ) - @property + @cached_property def structured_client(self): """A client patched with Instructor.""" return instructor.from_openai(self.client)