memorizz 0.0.1__py3-none-any.whl
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.
- memorizz/__init__.py +6 -0
- memorizz/database/__init__.py +5 -0
- memorizz/database/mongodb/__init__.py +5 -0
- memorizz/database/mongodb/mongodb_tools.py +166 -0
- memorizz-0.0.1.dist-info/METADATA +118 -0
- memorizz-0.0.1.dist-info/RECORD +8 -0
- memorizz-0.0.1.dist-info/WHEEL +4 -0
- memorizz-0.0.1.dist-info/licenses/LICENCE.txt +0 -0
memorizz/__init__.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
print("Starting memorizz __init__.py")
|
|
2
|
+
from .database.mongodb import MongoDBTools, MongoDBToolsConfig, get_embedding, get_mongodb_toolbox
|
|
3
|
+
print("Finished importing from mongodb in memorizz __init__.py")
|
|
4
|
+
|
|
5
|
+
__all__ = ['MongoDBTools', 'MongoDBToolsConfig', 'get_embedding', 'get_mongodb_toolbox']
|
|
6
|
+
print("Exiting memorizz __init__.py")
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import json
|
|
3
|
+
import getpass
|
|
4
|
+
import inspect
|
|
5
|
+
from functools import wraps
|
|
6
|
+
from typing import get_type_hints, List, Dict, Any, Optional
|
|
7
|
+
import openai
|
|
8
|
+
import pymongo
|
|
9
|
+
import logging
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
|
|
12
|
+
logging.basicConfig(level=logging.INFO)
|
|
13
|
+
logger = logging.getLogger(__name__)
|
|
14
|
+
|
|
15
|
+
@dataclass
|
|
16
|
+
class MongoDBToolsConfig:
|
|
17
|
+
mongo_uri: Optional[str] = None
|
|
18
|
+
db_name: str = 'function_calling_db'
|
|
19
|
+
collection_name: str = 'tools'
|
|
20
|
+
embedding_model: str = "text-embedding-3-small"
|
|
21
|
+
vector_search_candidates: int = 150
|
|
22
|
+
vector_index_name: str = "vector_index"
|
|
23
|
+
|
|
24
|
+
def get_embedding(text: str, model: str = "text-embedding-3-small") -> List[float]:
|
|
25
|
+
text = text.replace("\n", " ")
|
|
26
|
+
try:
|
|
27
|
+
return openai.OpenAI().embeddings.create(input=[text], model=model).data[0].embedding
|
|
28
|
+
except Exception as e:
|
|
29
|
+
logger.error(f"Error generating embedding: {str(e)}")
|
|
30
|
+
raise
|
|
31
|
+
|
|
32
|
+
class MongoDBTools:
|
|
33
|
+
def __init__(self, config: MongoDBToolsConfig = MongoDBToolsConfig()):
|
|
34
|
+
self.config = config
|
|
35
|
+
if self.config.mongo_uri is None:
|
|
36
|
+
self.config.mongo_uri = os.getenv('MONGO_URI') or getpass.getpass("Enter MongoDB URI: ")
|
|
37
|
+
try:
|
|
38
|
+
self.mongo_client = pymongo.MongoClient(self.config.mongo_uri)
|
|
39
|
+
self.db = self.mongo_client[self.config.db_name]
|
|
40
|
+
self.tools_collection = self.db[self.config.collection_name]
|
|
41
|
+
except Exception as e:
|
|
42
|
+
logger.error(f"Error connecting to MongoDB: {str(e)}")
|
|
43
|
+
raise
|
|
44
|
+
|
|
45
|
+
def mongodb_toolbox(self, collection: Optional[pymongo.collection.Collection] = None):
|
|
46
|
+
if collection is None:
|
|
47
|
+
collection = self.tools_collection
|
|
48
|
+
|
|
49
|
+
def decorator(func):
|
|
50
|
+
@wraps(func)
|
|
51
|
+
def wrapper(*args, **kwargs):
|
|
52
|
+
return func(*args, **kwargs)
|
|
53
|
+
|
|
54
|
+
signature = inspect.signature(func)
|
|
55
|
+
docstring = inspect.getdoc(func) or ""
|
|
56
|
+
|
|
57
|
+
if not docstring:
|
|
58
|
+
raise ValueError(f"Error registering tool {func.__name__}: Docstring is missing. Please provide a docstring for the function.")
|
|
59
|
+
|
|
60
|
+
type_hints = get_type_hints(func)
|
|
61
|
+
|
|
62
|
+
tool_def = {
|
|
63
|
+
"name": func.__name__,
|
|
64
|
+
"description": docstring.strip(),
|
|
65
|
+
"parameters": {
|
|
66
|
+
"type": "object",
|
|
67
|
+
"properties": {},
|
|
68
|
+
"required": []
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
for param_name, param in signature.parameters.items():
|
|
73
|
+
if param.kind in (inspect.Parameter.VAR_POSITIONAL, inspect.Parameter.VAR_KEYWORD):
|
|
74
|
+
continue
|
|
75
|
+
|
|
76
|
+
param_type = type_hints.get(param_name, type(None))
|
|
77
|
+
json_type = "string"
|
|
78
|
+
if param_type in (int, float):
|
|
79
|
+
json_type = "number"
|
|
80
|
+
elif param_type == bool:
|
|
81
|
+
json_type = "boolean"
|
|
82
|
+
|
|
83
|
+
tool_def["parameters"]["properties"][param_name] = {
|
|
84
|
+
"type": json_type,
|
|
85
|
+
"description": f"Parameter {param_name}"
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if param.default == inspect.Parameter.empty:
|
|
89
|
+
tool_def["parameters"]["required"].append(param_name)
|
|
90
|
+
|
|
91
|
+
tool_def["parameters"]["additionalProperties"] = False
|
|
92
|
+
|
|
93
|
+
try:
|
|
94
|
+
vector = get_embedding(tool_def["description"], self.config.embedding_model)
|
|
95
|
+
tool_doc = {
|
|
96
|
+
**tool_def,
|
|
97
|
+
"embedding": vector
|
|
98
|
+
}
|
|
99
|
+
collection.update_one({"name": func.__name__}, {"$set": tool_doc}, upsert=True)
|
|
100
|
+
logger.info(f"Successfully registered tool: {func.__name__}")
|
|
101
|
+
except Exception as e:
|
|
102
|
+
logger.error(f"Error registering tool {func.__name__}: {str(e)}")
|
|
103
|
+
raise
|
|
104
|
+
|
|
105
|
+
return wrapper
|
|
106
|
+
return decorator
|
|
107
|
+
|
|
108
|
+
def _vector_search(self, user_query: str, collection: Optional[pymongo.collection.Collection] = None, limit: int = 2) -> List[Dict[str, Any]]:
|
|
109
|
+
if collection is None:
|
|
110
|
+
collection = self.tools_collection
|
|
111
|
+
|
|
112
|
+
try:
|
|
113
|
+
query_embedding = get_embedding(user_query, self.config.embedding_model)
|
|
114
|
+
except Exception as e:
|
|
115
|
+
logger.error(f"Error generating embedding for query: {str(e)}")
|
|
116
|
+
raise
|
|
117
|
+
|
|
118
|
+
vector_search_stage = {
|
|
119
|
+
"$vectorSearch": {
|
|
120
|
+
"index": self.config.vector_index_name,
|
|
121
|
+
"queryVector": query_embedding,
|
|
122
|
+
"path": "embedding",
|
|
123
|
+
"numCandidates": self.config.vector_search_candidates,
|
|
124
|
+
"limit": limit
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
unset_stage = {
|
|
129
|
+
"$unset": "embedding"
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
pipeline = [vector_search_stage, unset_stage]
|
|
133
|
+
|
|
134
|
+
try:
|
|
135
|
+
results = collection.aggregate(pipeline)
|
|
136
|
+
return list(results)
|
|
137
|
+
except Exception as e:
|
|
138
|
+
logger.error(f"Error performing vector search: {str(e)}")
|
|
139
|
+
raise
|
|
140
|
+
|
|
141
|
+
def populate_tools(self, user_query: str, num_tools: int = 2) -> List[Dict[str, Any]]:
|
|
142
|
+
try:
|
|
143
|
+
search_results = self._vector_search(user_query, limit=num_tools)
|
|
144
|
+
tools = []
|
|
145
|
+
for result in search_results:
|
|
146
|
+
print(result)
|
|
147
|
+
tool = {
|
|
148
|
+
"type": "function",
|
|
149
|
+
"function": {
|
|
150
|
+
"name": result["name"],
|
|
151
|
+
"description": result["description"],
|
|
152
|
+
"parameters": result["parameters"]
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
tools.append(tool)
|
|
156
|
+
logger.info(f"Successfully populated {len(tools)} tools")
|
|
157
|
+
return tools
|
|
158
|
+
except Exception as e:
|
|
159
|
+
logger.error(f"Error populating tools: {str(e)}")
|
|
160
|
+
raise
|
|
161
|
+
|
|
162
|
+
__all__ = ['MongoDBTools', 'MongoDBToolsConfig', 'get_embedding']
|
|
163
|
+
|
|
164
|
+
# You can create a function to get the mongodb_toolbox decorator:
|
|
165
|
+
def get_mongodb_toolbox(config: MongoDBToolsConfig = MongoDBToolsConfig()):
|
|
166
|
+
return MongoDBTools(config).mongodb_toolbox
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: memorizz
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A memory management library for Python
|
|
5
|
+
Project-URL: Homepage, https://github.com/RichmondAlake/memorizz
|
|
6
|
+
Project-URL: Bug Tracker, https://github.com/yourusername/memorizz/issues
|
|
7
|
+
Author-email: Richmond Alake <richmond.alake@gmail.com>
|
|
8
|
+
License-File: LICENCE.txt
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Requires-Python: >=3.7
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
MemoRizz is a comprehensive library for AI-assisted tools and functionalities. It currently includes MongoDB tools for storing function definitions and performing customizable vector searches.
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
pip install memorizz
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
MemoRizz can be used with either a custom configuration or the default settings. Here are examples of both approaches:
|
|
26
|
+
|
|
27
|
+
### Using Custom Configuration
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
from memorizz.databases.mongodb import MongoDBTools, MongoDBToolsConfig, mongodb_toolbox
|
|
31
|
+
|
|
32
|
+
# Create a custom configuration
|
|
33
|
+
custom_config = MongoDBToolsConfig(
|
|
34
|
+
mongo_uri="mongodb://custom-uri:27017",
|
|
35
|
+
db_name="my_custom_db",
|
|
36
|
+
collection_name="my_custom_tools",
|
|
37
|
+
embedding_model="text-embedding-ada-002",
|
|
38
|
+
vector_search_candidates=200,
|
|
39
|
+
vector_index_name="my_custom_index"
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
# Initialize MongoDBTools with custom configuration
|
|
43
|
+
custom_mongo_tools = MongoDBTools(custom_config)
|
|
44
|
+
|
|
45
|
+
# Use the custom mongodb_toolbox decorator
|
|
46
|
+
@custom_mongo_tools.mongodb_toolbox()
|
|
47
|
+
def custom_function(param1: str, param2: int) -> str:
|
|
48
|
+
"""
|
|
49
|
+
This is a custom function using a custom configuration.
|
|
50
|
+
"""
|
|
51
|
+
return f"Custom processed {param1} with {param2}"
|
|
52
|
+
|
|
53
|
+
# Use the custom tools
|
|
54
|
+
user_query = "How do I process something custom?"
|
|
55
|
+
custom_tools = custom_mongo_tools.populate_tools(user_query, num_tools=3)
|
|
56
|
+
|
|
57
|
+
print("Custom tools:", custom_tools)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Using Default Configuration
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
from memorizz.databases.mongodb import mongodb_toolbox, MongoDBTools
|
|
64
|
+
|
|
65
|
+
# Use the default mongodb_toolbox decorator
|
|
66
|
+
@mongodb_toolbox()
|
|
67
|
+
def default_function(param1: str, param2: int) -> str:
|
|
68
|
+
"""
|
|
69
|
+
This is a default function using the default configuration.
|
|
70
|
+
"""
|
|
71
|
+
return f"Default processed {param1} with {param2}"
|
|
72
|
+
|
|
73
|
+
# Use the default tools
|
|
74
|
+
default_mongo_tools = MongoDBTools() # This uses the default configuration
|
|
75
|
+
user_query = "How do I process something with default settings?"
|
|
76
|
+
default_tools = default_mongo_tools.populate_tools(user_query, num_tools=2)
|
|
77
|
+
|
|
78
|
+
print("Default tools:", default_tools)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
In both cases, the `mongodb_toolbox` decorator is used to register functions, and the `populate_tools` method is used to retrieve relevant tools based on a user query. The main difference is in the setup and configuration.
|
|
82
|
+
|
|
83
|
+
The custom configuration approach allows you to specify your own MongoDB URI, database name, collection name, and other settings. This is useful when you need to connect to a specific MongoDB instance or customize the behavior of the tools.
|
|
84
|
+
|
|
85
|
+
The default configuration approach is simpler and requires less setup. It's suitable for quick starts or when the default settings meet your needs.
|
|
86
|
+
|
|
87
|
+
Choose the approach that best fits your project requirements and structure.
|
|
88
|
+
|
|
89
|
+
## Features
|
|
90
|
+
|
|
91
|
+
- MongoDB Tools:
|
|
92
|
+
- Automatic tool registration with MongoDB
|
|
93
|
+
- Vector search based on user queries
|
|
94
|
+
- Customizable number of tools returned
|
|
95
|
+
- Configurable embedding model and search parameters
|
|
96
|
+
- Error handling and logging
|
|
97
|
+
- Extensible structure for future database support
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
## Feature Roadmap
|
|
101
|
+
|
|
102
|
+
- [x] Implement basic MongoDB tools functionality
|
|
103
|
+
- [x] Add customizable vector search
|
|
104
|
+
- [x] Implement error handling and logging
|
|
105
|
+
- [x] Create PyPI package
|
|
106
|
+
- [x] Restructure package for extensibility to other databases
|
|
107
|
+
- [ ] Add support for multiple embedding models to make MongoDBToolsConfig embedding model agnostic
|
|
108
|
+
- [ ] Implement MemScore logic for improved tool ranking and memory component retrival
|
|
109
|
+
- [ ] Add async support for improved performance
|
|
110
|
+
- [ ] Implement caching mechanism for embeddings and search results
|
|
111
|
+
- [ ] Create comprehensive API documentation
|
|
112
|
+
- [ ] Implement automated testing suite with high coverage
|
|
113
|
+
- [ ] Create example projects and use cases
|
|
114
|
+
- [ ] Add support for additional databases (e.g., PostgreSQL, Redis)
|
|
115
|
+
|
|
116
|
+
## License
|
|
117
|
+
|
|
118
|
+
This project is licensed under the MIT License.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
memorizz/__init__.py,sha256=FxpzM-4NJ0PdO5r8rE-a1eT3hUCDapCNDcU9rWX-Ow0,330
|
|
2
|
+
memorizz/database/__init__.py,sha256=tApVKs5G8WB-mCl7qEOWrFqgLxMFuxtWlD-HRkLVqX8,256
|
|
3
|
+
memorizz/database/mongodb/__init__.py,sha256=zszN974hb2ejKuCMou0GKayhzglb5FSI_sb0sL72U5I,260
|
|
4
|
+
memorizz/database/mongodb/mongodb_tools.py,sha256=jkx1DtIJk-ZBEw-QUN0OvVeVEZIZirwXkUFnrngnaj8,6110
|
|
5
|
+
memorizz-0.0.1.dist-info/METADATA,sha256=-LibnPjLChsMYwS5mT7mAMtcXDUW7czNSHqiv5qlOS8,4356
|
|
6
|
+
memorizz-0.0.1.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
|
|
7
|
+
memorizz-0.0.1.dist-info/licenses/LICENCE.txt,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
+
memorizz-0.0.1.dist-info/RECORD,,
|
|
File without changes
|