moved to cached_property from property

This commit is contained in:
Siddhesh Agarwal
2024-10-31 17:08:14 +05:30
parent 33e4046ac3
commit 4b2b094ea6
7 changed files with 24 additions and 16 deletions
+5 -4
View File
@@ -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."""
+3 -2
View File
@@ -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)
+3 -2
View File
@@ -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)
+3 -2
View File
@@ -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)
+3 -2
View File
@@ -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(
+3 -2
View File
@@ -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)
+4 -2
View File
@@ -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)