1
+ import contextlib
1
2
import ipaddress
2
3
import json
3
4
import os
70
71
T = TypeVar ('T' )
71
72
72
73
73
- class BaseClient :
74
+ class BaseClient ( contextlib . AbstractContextManager ) :
74
75
def __init__ (
75
76
self ,
76
77
client ,
@@ -105,6 +106,15 @@ def __init__(
105
106
** kwargs ,
106
107
)
107
108
109
+ def __exit__ (self , exc_type , exc_val , exc_tb ):
110
+ self .close ()
111
+
112
+ async def __aenter__ (self ) -> Any :
113
+ return self
114
+
115
+ async def __aexit__ (self , exc_type , exc_val , exc_tb ):
116
+ await self .close ()
117
+
108
118
109
119
CONNECTION_ERROR_MESSAGE = 'Failed to connect to Ollama. Please check that Ollama is downloaded, running and accessible. https://ollama.com/download'
110
120
@@ -116,12 +126,6 @@ def __init__(self, host: Optional[str] = None, **kwargs) -> None:
116
126
def close (self ):
117
127
self ._client .close ()
118
128
119
- def __enter__ (self ):
120
- return self
121
-
122
- def __exit__ (self , exc_type , exc_val , exc_tb ):
123
- self .close ()
124
-
125
129
def _request_raw (self , * args , ** kwargs ):
126
130
try :
127
131
r = self ._client .request (* args , ** kwargs )
@@ -629,12 +633,6 @@ def __init__(self, host: Optional[str] = None, **kwargs) -> None:
629
633
async def close (self ):
630
634
await self ._client .aclose ()
631
635
632
- async def __aenter__ (self ):
633
- return self
634
-
635
- async def __aexit__ (self , exc_type , exc_val , exc_tb ):
636
- await self .close ()
637
-
638
636
async def _request_raw (self , * args , ** kwargs ):
639
637
try :
640
638
r = await self ._client .request (* args , ** kwargs )
0 commit comments