crewplus 0.2.36__tar.gz → 0.2.37__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of crewplus might be problematic. Click here for more details.

Files changed (22) hide show
  1. {crewplus-0.2.36 → crewplus-0.2.37}/PKG-INFO +1 -1
  2. {crewplus-0.2.36 → crewplus-0.2.37}/crewplus/services/model_load_balancer.py +24 -14
  3. {crewplus-0.2.36 → crewplus-0.2.37}/pyproject.toml +1 -1
  4. {crewplus-0.2.36 → crewplus-0.2.37}/LICENSE +0 -0
  5. {crewplus-0.2.36 → crewplus-0.2.37}/README.md +0 -0
  6. {crewplus-0.2.36 → crewplus-0.2.37}/crewplus/__init__.py +0 -0
  7. {crewplus-0.2.36 → crewplus-0.2.37}/crewplus/services/__init__.py +0 -0
  8. {crewplus-0.2.36 → crewplus-0.2.37}/crewplus/services/azure_chat_model.py +0 -0
  9. {crewplus-0.2.36 → crewplus-0.2.37}/crewplus/services/gemini_chat_model.py +0 -0
  10. {crewplus-0.2.36 → crewplus-0.2.37}/crewplus/services/init_services.py +0 -0
  11. {crewplus-0.2.36 → crewplus-0.2.37}/crewplus/services/tracing_manager.py +0 -0
  12. {crewplus-0.2.36 → crewplus-0.2.37}/crewplus/utils/__init__.py +0 -0
  13. {crewplus-0.2.36 → crewplus-0.2.37}/crewplus/utils/schema_action.py +0 -0
  14. {crewplus-0.2.36 → crewplus-0.2.37}/crewplus/utils/schema_document_updater.py +0 -0
  15. {crewplus-0.2.36 → crewplus-0.2.37}/crewplus/vectorstores/milvus/__init__.py +0 -0
  16. {crewplus-0.2.36 → crewplus-0.2.37}/crewplus/vectorstores/milvus/milvus_schema_manager.py +0 -0
  17. {crewplus-0.2.36 → crewplus-0.2.37}/crewplus/vectorstores/milvus/schema_milvus.py +0 -0
  18. {crewplus-0.2.36 → crewplus-0.2.37}/crewplus/vectorstores/milvus/vdb_service.py +0 -0
  19. {crewplus-0.2.36 → crewplus-0.2.37}/docs/GeminiChatModel.md +0 -0
  20. {crewplus-0.2.36 → crewplus-0.2.37}/docs/ModelLoadBalancer.md +0 -0
  21. {crewplus-0.2.36 → crewplus-0.2.37}/docs/VDBService.md +0 -0
  22. {crewplus-0.2.36 → crewplus-0.2.37}/docs/index.md +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: crewplus
3
- Version: 0.2.36
3
+ Version: 0.2.37
4
4
  Summary: Base services for CrewPlus AI applications
5
5
  Author-Email: Tim Liu <tim@opsmateai.com>
6
6
  License: MIT
@@ -1,6 +1,7 @@
1
1
  import json
2
2
  import random
3
3
  import logging
4
+ import threading
4
5
  from typing import Dict, List, Optional, Union
5
6
  from collections import defaultdict
6
7
  from langchain_openai import ChatOpenAI, AzureOpenAIEmbeddings
@@ -31,7 +32,7 @@ class ModelLoadBalancer:
31
32
  self.config_data = config_data
32
33
  self.logger = logger or logging.getLogger(__name__)
33
34
  self.models_config: List[Dict] = []
34
- self.models: Dict[int, Union[TracedAzureChatOpenAI, ChatOpenAI, AzureOpenAIEmbeddings, GeminiChatModel]] = {}
35
+ self.thread_local = threading.local()
35
36
  self._initialize_state()
36
37
  self._config_loaded = False # Flag to check if config is loaded
37
38
 
@@ -60,15 +61,6 @@ class ModelLoadBalancer:
60
61
 
61
62
  self.models_config = config['models']
62
63
 
63
- # Instantiate models
64
- for model_config in self.models_config:
65
- model_id = model_config['id']
66
- model_instance = self._instantiate_model(model_config)
67
- if model_instance is not None:
68
- self.models[model_id] = model_instance
69
- else:
70
- self.logger.warning(f"Model with id {model_id} was not loaded due to instantiation error.")
71
-
72
64
  self._config_loaded = True
73
65
  self.logger.debug("Model balancer: configuration loaded successfully.")
74
66
  except (FileNotFoundError, json.JSONDecodeError, ValueError) as e:
@@ -105,8 +97,7 @@ class ModelLoadBalancer:
105
97
  if deployment_name:
106
98
  for model_config in self.models_config:
107
99
  if model_config.get('deployment_name') == deployment_name:
108
- model_id = model_config['id']
109
- model = self.models[model_id]
100
+ model = self._get_or_create_model(model_config)
110
101
  if with_metadata:
111
102
  return model, deployment_name
112
103
  return model
@@ -130,14 +121,33 @@ class ModelLoadBalancer:
130
121
  self.logger.warning(f"Unsupported selection strategy: '{selection_strategy}'. Defaulting to 'random'.")
131
122
  selected_model_config = self._random_selection(candidates)
132
123
 
133
- model_id = selected_model_config['id']
134
- model = self.models[model_id]
124
+ model = self._get_or_create_model(selected_model_config)
135
125
  if with_metadata:
136
126
  return model, selected_model_config.get('deployment_name')
137
127
  return model
138
128
 
139
129
  raise ValueError("Either 'deployment_name' or both 'provider' and 'model_type' must be provided.")
140
130
 
131
+ def _get_thread_local_models_cache(self) -> Dict:
132
+ """Gets the model cache for the current thread, creating it if it doesn't exist."""
133
+ if not hasattr(self.thread_local, 'models_cache'):
134
+ self.thread_local.models_cache = {}
135
+ return self.thread_local.models_cache
136
+
137
+ def _get_or_create_model(self, model_config: Dict):
138
+ """
139
+ Gets a model instance from the thread-local cache. If it doesn't exist,
140
+ it instantiates, caches, and returns it.
141
+ """
142
+ model_id = model_config['id']
143
+ models_cache = self._get_thread_local_models_cache()
144
+
145
+ if model_id not in models_cache:
146
+ self.logger.debug(f"Creating new model instance for id {model_id} in thread {threading.get_ident()}")
147
+ models_cache[model_id] = self._instantiate_model(model_config)
148
+
149
+ return models_cache[model_id]
150
+
141
151
  def _instantiate_model(self, model_config: Dict):
142
152
  """Instantiate and return an LLM object based on the model configuration"""
143
153
  provider = model_config['provider']
@@ -6,7 +6,7 @@ build-backend = "pdm.backend"
6
6
 
7
7
  [project]
8
8
  name = "crewplus"
9
- version = "0.2.36"
9
+ version = "0.2.37"
10
10
  description = "Base services for CrewPlus AI applications"
11
11
  authors = [
12
12
  { name = "Tim Liu", email = "tim@opsmateai.com" },
File without changes
File without changes
File without changes
File without changes