Skip to content

Commit

Permalink
Merge pull request #69 from ZackBradshaw/sweep/fix-gha-failure_e9743
Browse files Browse the repository at this point in the history
[Sweep GHA Fix] Fix failing GitHub Actions run
  • Loading branch information
ZackBradshaw authored Feb 27, 2024
2 parents cf5993b + 7410829 commit b0dde3b
Show file tree
Hide file tree
Showing 2 changed files with 216 additions and 9 deletions.
10 changes: 5 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ This project utilizes the [pre-commit](https://pre-commit.com/) tool to maintain
- Install pre-commit (https://pre-commit.com/)

```bash
pip install pre-commit
pip install pre-commit==2.15.0
```

- Check that it's installed

```bash
pre-commit --version
pre-commit --version && pre-commit install && pre-commit install -t pre-commit
```

Now when you make a git commit, the black code formatter and ruff linter will run.
Expand All @@ -76,20 +76,20 @@ Furthermore, we have integrated a pre-commit GitHub Action into our workflow. Th

To run the pre-commit tool, follow these steps:

1. Install pre-commit by running the following command: `poetry install`. It will not only install pre-commit but also install all the deps and dev-deps of project
1. Install pre-commit by running the following command: `poetry install && pre-commit install`. It will not only install pre-commit but also install all the deps and dev-deps of project

2. Once pre-commit is installed, navigate to the project's root directory.

3. Run the command `pre-commit run --all-files`. This will execute the pre-commit hooks configured for this project against the modified files. If any issues are found, the pre-commit tool will provide feedback on how to resolve them. Make the necessary changes and re-run the pre-commit command until all issues are resolved.

4. You can also install pre-commit as a git hook by execute `pre-commit install`. Every time you made `git commit` pre-commit run automatically for you.
4. You can also install pre-commit as a git hook by running the following command: `pre-commit install -t pre-commit`. This will automate the pre-commit checks on every commit, ensuring that the checks are run automatically before every commit is made.


### Docstrings

All new functions and classes in `swarms` should include docstrings. This is a prerequisite for any new functions and classes to be added to the library.

`swarms` adheres to the [Google Python docstring style](https://google.github.io/styleguide/pyguide.html#383-functions-and-methods). Please refer to the style guide while writing docstrings for your contribution.
`swarms` adheres to the [Google Python docstring style](https://sphinxcontrib-napoleon.readthedocs.io/en/latest/index.html). Please refer to the style guide while writing docstrings for your contribution.

### Type checking

Expand Down
215 changes: 211 additions & 4 deletions docs/swarms/memory/pg.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,216 @@ Attributes:

### 3.1 Attribute Validators

The class includes validators for the `connection_string` and `engine` attributes to ensure their proper usage. These validators help maintain consistency in attribute values.
#### 4.4 Querying Vectors - Method Overview <a name="querying-vectors"></a>

Before diving into the details of the querying vectors with the PgVectorVectorStore class, let's provide an overview of the purpose and functionality of the `query` method.

### 3.2 Initialization

During initialization, the class checks if an engine is provided. If an engine is not provided, it creates a new database connection using the `connection_string` and `create_engine_params`.
### 4.3 Loading Vector Entries <a name="loading-vector-entries"></a>

You can load vector entries from the collection using the `load_entry` and `load_entries` methods.

#### 4.3.1 Loading a Single Entry

The `load_entry` method allows you to load a specific vector entry based on its identifier and optional namespace.

```python
def load_entry(
self, vector_id: str, namespace: Optional[str] = None
) -> BaseVectorStore.Entry:
"""
Retrieves a specific vector entry from the collection based on its identifier and optional namespace.
Parameters:
- vector_id (str): The ID of the vector to retrieve.
- namespace (Optional[str]): An optional namespace for filtering. Default: None.
Returns:
- BaseVectorStore.Entry: The loaded vector entry.
"""
```

#### Example: Loading a Single Entry

```python
# Initialize the PgVectorVectorStore instance
vector_store = PgVectorVectorStore(connection_string="your-db-connection-string", table_name="your-table-name")

# Load a specific vector entry
loaded_entry = vector_store.load_entry(vector_id="unique-vector-id", namespace="your-namespace")

if loaded_entry is not None:
loaded_vector = loaded_entry.vector
loaded_meta = loaded_entry.meta
# Use the loaded vector and metadata as needed
else:
# Vector not found
```

#### 4.3.2 Loading Multiple Entries

The `load_entries` method allows you to load all vector entries from the collection, optionally filtering by namespace.

```python
def load_entries(
self, namespace: Optional[str] = None
) -> list[BaseVectorStore.Entry]:
"""
Retrieves all vector entries from the collection, optionally filtering to only those that match the provided namespace.
Parameters:
- namespace (Optional[str]): An optional namespace for filtering. Default: None.
Returns:
- list[BaseVectorStore.Entry]: A list of loaded vector entries.
"""
```

#### Example: Loading Multiple Entries

```python
# Initialize the PgVectorVectorStore instance
vector_store = PgVectorVectorStore(connection_string="your-db-connection-string", table_name="your-table-name")

# Load all vector entries in the specified namespace
entries = vector_store.load_entries(namespace="your-namespace")

# Process the loaded entries
for entry in entries:
vector_id = entry.id
vector = entry.vector
meta = entry.meta

# Handle the loaded entries as needed
```

### 4.4 Querying Vectors <a name="querying-vectors"></a>
### 4.4 Querying Vectors <a name="querying-vectors"></a>

You can perform vector queries to find vectors similar to a given query vector using the `query` method. You can specify the query string, the maximum number of results to return, and other options.

```python
def query(
self,
query: str,
count: Optional[int] = BaseVectorStore.DEFAULT_QUERY_COUNT,
namespace: Optional[str] = None,
include_vectors: bool = False,
distance_metric: str = "cosine_distance",
**kwargs
) -> list[BaseVectorStore.QueryResult]:
"""
Performs a search on the collection to find vectors similar to the provided input vector,
optionally filtering to only those that match the provided namespace.
Parameters:
- query (str): The query string to find similar vectors.
- count (Optional[int]): Maximum number of results to return. Default: BaseVectorStore.DEFAULT_QUERY_COUNT.
- namespace (Optional[str]): An optional namespace for filtering. Default: None.
- include_vectors (bool): If True, includes vectors in the query results. Default: False.
- distance_metric (str): The distance metric to use for similarity measurement.
Options: "cosine_distance", "l2_distance", "inner_product". Default: "cosine_distance".
- **kwargs: Additional keyword arguments.
Returns:
- list[BaseVectorStore.QueryResult]: A list of query results, each containing vector ID, vector (if included), score, and metadata.
"""
```

#### Example: Querying Vectors

```python
# Initialize the PgVectorVectorStore instance
vector_store = PgVectorVectorStore(connection_string="your-db-connection-string", table_name="your-table-name")

# Perform a vector query
query_string = "your-query-string"
count = 10 # Maximum number of results to return
namespace = "your-namespace"
include_vectors = False # Set to True to include vectors in results
distance_metric = "cosine_distance"

results = vector_store.query(
query=query_string,
count=count,
namespace=namespace,
include_vectors=include_vectors,
distance_metric=distance_metric
)

# Process the query results
for result in results:
vector_id = result.id
vector = result.vector
score = result.score
meta = result.meta

# Handle the results as needed
```
### 4.4 Querying Vectors <a name="querying-vectors"></a>

You can perform vector queries to find vectors similar to a given query vector using the `query` method. You can specify the query string, the maximum number of results to return, and other options.

```python
def query(
self,
query: str,
count: Optional[int] = BaseVectorStore.DEFAULT_QUERY_COUNT,
namespace: Optional[str] = None,
include_vectors: bool = False,
distance_metric: str = "cosine_distance",
**kwargs
) -> list[BaseVectorStore.QueryResult]:
"""
Performs a search on the collection to find vectors similar to the provided input vector,
optionally filtering to only those that match the provided namespace.
Parameters:
- query (str): The query string to find similar vectors.
- count (Optional[int]): Maximum number of results to return. Default: BaseVectorStore.DEFAULT_QUERY_COUNT.
- namespace (Optional[str]): An optional namespace for filtering. Default: None.
- include_vectors (bool): If True, includes vectors in the query results. Default: False.
- distance_metric (str): The distance metric to use for similarity measurement.
Options: "cosine_distance", "l2_distance", "inner_product". Default: "cosine_distance".
- **kwargs: Additional keyword arguments.
Returns:
- list[BaseVectorStore.QueryResult]: A list of query results, each containing vector ID, vector (if included), score, and metadata.
"""
```

#### Example: Querying Vectors

```python
# Initialize the PgVectorVectorStore instance
vector_store = PgVectorVectorStore(connection_string="your-db-connection-string", table_name="your-table-name")

# Perform a vector query
query_string = "your-query-string"
count = 10 # Maximum number of results to return
namespace = "your-namespace"
include_vectors = False # Set to True to include vectors in results
distance_metric = "cosine_distance"

results = vector_store.query(
query=query_string,
count=count,
namespace=namespace,
include_vectors=include_vectors,
distance_metric=distance_metric
)

# Process the query results
for result in results:
vector_id = result.id
vector = result.vector
score = result.score
meta = result.meta

# Handle the results as needed
```
---

## 4. Functionality and Usage <a name="functionality-and-usage"></a>
Expand All @@ -97,7 +301,7 @@ def setup(
install_vector_extension: bool = True,
) -> None:
"""
Provides a mechanism to initialize the database schema and extensions.
Provides a mechanism to initialize the database schema, install the necessary extensions, and configure the database setup.
Parameters:
- create_schema (bool): If True, creates the necessary database schema for vector storage. Default: True.
Expand All @@ -113,7 +317,7 @@ def setup(
vector_store = PgVectorVectorStore(connection_string="your-db-connection-string", table_name="your-table-name")

# Set up the database with default settings
vector_store.setup()
vector_store.setup(create_schema=True, install_uuid_extension=True, install_vector_extension=True)
```

#### Example 2: Customized Database Setup
Expand All @@ -123,7 +327,7 @@ vector_store.setup()
vector_store = PgVectorVectorStore(connection_string="your-db-connection-string", table_name="your-table-name")

# Set up the database with customized settings
vector_store.setup(create_schema=False, install_uuid_extension=True, install_vector_extension=True)
vector_store.setup(create_schema=True, install_uuid_extension=True, install_vector_extension=True)
```

### 4.2 Upserting Vectors <a name="upserting-vectors"></a>
Expand All @@ -137,6 +341,9 @@ def upsert_vector(
vector_id: Optional[str] = None,
namespace: Optional[str] = None,
meta: Optional[dict] = None,
vector_id: Optional[str] = None,
namespace: Optional[str] = None,
meta: Optional[dict] = None,
**kwargs
) -> str:
"""
Expand Down

0 comments on commit b0dde3b

Please sign in to comment.