19
19
20
20
import logging
21
21
import warnings
22
- from functools import cached_property
23
22
from typing import TYPE_CHECKING , Any
24
23
25
24
import cohere
@@ -65,6 +64,7 @@ def __init__(
65
64
self .timeout = timeout
66
65
self .max_retries = max_retries
67
66
self .request_options = request_options
67
+ self ._client : cohere .ClientV2 | None = None
68
68
69
69
if self .max_retries :
70
70
warnings .warn (
@@ -77,20 +77,23 @@ def __init__(
77
77
else :
78
78
self .request_options .update ({"max_retries" : self .max_retries })
79
79
80
- @cached_property
81
- def get_conn (self ) -> cohere .ClientV2 : # type: ignore[override]
82
- conn = self .get_connection (self .conn_id )
83
- return cohere .ClientV2 (
84
- api_key = conn .password ,
85
- timeout = self .timeout ,
86
- base_url = conn .host or None ,
87
- )
80
+ def get_conn (self ) -> cohere .ClientV2 :
81
+ """Return a new or cached Cohere client instance."""
82
+ if self ._client is None :
83
+ # create a new client instance if there is no existing client
84
+ conn = self .get_connection (self .conn_id )
85
+ self ._client = cohere .ClientV2 (
86
+ api_key = conn .password ,
87
+ timeout = self .timeout ,
88
+ base_url = conn .host or None ,
89
+ )
90
+ return self ._client
88
91
89
92
def create_embeddings (
90
93
self , texts : list [str ], model : str = "embed-multilingual-v3.0"
91
94
) -> EmbedByTypeResponseEmbeddings :
92
95
logger .info ("Creating embeddings with model: embed-multilingual-v3.0" )
93
- response = self .get_conn .embed (
96
+ response = self .get_conn () .embed (
94
97
texts = texts ,
95
98
model = model ,
96
99
input_type = "search_document" ,
@@ -117,7 +120,7 @@ def test_connection(
117
120
try :
118
121
if messages is None :
119
122
messages = [UserChatMessageV2 (role = "user" , content = "hello world!" )]
120
- self .get_conn .chat (model = model , messages = messages )
123
+ self .get_conn () .chat (model = model , messages = messages )
121
124
return True , "Connection successfully established."
122
125
except Exception as e :
123
126
return False , f"Unexpected error: { str (e )} "
0 commit comments