Skip to content

Commit 334a6d8

Browse files
committed
Merge branch 'main' into semantic-kernel-python-import-fix
2 parents 4d15c4b + a4fb780 commit 334a6d8

File tree

8 files changed

+129
-32
lines changed

8 files changed

+129
-32
lines changed

.evergreen/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ buildvariants:
205205
display_name: Autogen RHEL
206206
expansions:
207207
DIR: autogen
208-
REPO_NAME: autogen # TODO [PYTHON-4506] Switch to https://github.com/microsoft/autogen.git one merged
209-
CLONE_URL: https://github.com/ranfysvalle02/autogen.git
208+
REPO_NAME: autogen
209+
CLONE_URL: https://github.com/microsoft/autogen.git
210210
DATABASE: autogen_test_db
211211
run_on:
212212
- rhel87-small

.evergreen/provision-atlas.sh

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,13 @@ PYTHON_BINARY=$(find_python3)
88
EVERGREEN_PATH=$(pwd)/.evergreen
99
TARGET_DIR=$(pwd)/$DIR
1010
PING_ATLAS=$EVERGREEN_PATH/ping_atlas.py
11-
SCAFFOLD_SCRIPT=$EVERGREEN_PATH/scaffold_atlas.py
1211
DEPLOYMENT_NAME=$DIR
1312

14-
# Download the mongodb tar and extract the binary into the atlas directory
1513
set -ex
16-
curl https://fastdl.mongodb.org/mongocli/mongodb-atlas-cli_1.18.0_linux_x86_64.tar.gz -o atlas.tgz
17-
tar zxf atlas.tgz
18-
mv mongodb-atlas-cli_1.18.0* atlas
14+
mkdir atlas
1915

20-
# Create a local atlas deployment and store the connection string as an env var
21-
$atlas deployments setup $DIR --type local --force --debug
22-
$atlas deployments start $DIR
23-
CONN_STRING=$($atlas deployments connect $DIR --connectWith connectionString)
16+
setup_local_atlas
2417

25-
# Make the atlas directory hold the virtualenv for provisioning
2618
cd atlas
2719

2820
$PYTHON_BINARY -m venv .
@@ -33,17 +25,10 @@ $PYTHON_BINARY -m pip install pymongo
3325
CONN_STRING=$CONN_STRING \
3426
$PYTHON_BINARY -c "from pymongo import MongoClient; import os; MongoClient(os.environ['CONN_STRING']).db.command('ping')"
3527

36-
# Add database configuration
28+
# Add database and index configurations
3729
DATABASE=$DATABASE \
3830
CONN_STRING=$CONN_STRING \
3931
REPO_NAME=$REPO_NAME \
4032
DIR=$DIR \
4133
TARGET_DIR=$TARGET_DIR \
4234
$PYTHON_BINARY $SCAFFOLD_SCRIPT
43-
44-
# If a search index configuration can be found, create the index
45-
if [ -d "$TARGET_DIR/indexes" ]; then
46-
for file in $TARGET_DIR/indexes/*.json; do
47-
$atlas deployments search indexes create --file $file --deploymentName $DIR
48-
done
49-
fi

.evergreen/scaffold_atlas.py

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from pymongo import MongoClient
1010
from pymongo.database import Database
11+
from pymongo.operations import SearchIndexModel
1112
from pymongo.results import InsertManyResult
1213

1314
logging.basicConfig()
@@ -20,6 +21,7 @@
2021
DIR = os.environ.get("DIR")
2122
TARGET_DIR = os.environ.get("TARGET_DIR")
2223
DB_PATH = "database"
24+
INDEX_PATH = "indexes"
2325

2426

2527
def upload_data(db: Database, filename: Path) -> None:
@@ -49,20 +51,49 @@ def upload_data(db: Database, filename: Path) -> None:
4951
db.create_collection(collection_name)
5052

5153

52-
def walk_collection_directory() -> list[str]:
54+
def create_index(client: MongoClient, filename: Path) -> None:
55+
"""Create indexes based on the JSONs provided from the index_json files
56+
57+
Args:
58+
client (MongoClient): MongoClient
59+
filename (Path): Index configuration filepath
60+
"""
61+
with filename.open() as f:
62+
loaded_index_configuration = json.load(f)
63+
64+
collection_name = loaded_index_configuration.pop("collectionName")
65+
database_name = loaded_index_configuration.pop("database")
66+
index_name = loaded_index_configuration.pop("name")
67+
index_type = loaded_index_configuration.pop("type", None)
68+
69+
collection = client[database_name][collection_name]
70+
71+
search_index = SearchIndexModel(
72+
loaded_index_configuration, name=index_name, type=index_type
73+
)
74+
collection.create_search_index(search_index)
75+
76+
77+
def walk_directory(filepath) -> list[str]:
5378
"""Return all *.json filenames in the DB_PATH directory"""
54-
database_dir = Path(TARGET_DIR).joinpath(DB_PATH)
79+
database_dir = Path(TARGET_DIR).joinpath(filepath)
5580
return (
5681
[file for file in database_dir.iterdir() if file.suffix == ".json"]
5782
if database_dir.exists()
5883
else []
5984
)
6085

6186

62-
def main() -> None:
63-
database = MongoClient(CONN_STRING)[DATABASE_NAME]
64-
collection_jsons = walk_collection_directory()
65-
logger.debug("%s files found: %s", len(collection_jsons), collection_jsons)
87+
def generate_collections(database: Database, collection_jsons: list[Path]) -> None:
88+
"""Generate collections based on the collection_json filepaths
89+
90+
Args:
91+
database (Database): Mongo Database
92+
collection_jsons (list[Path]): List of collection filepaths
93+
"""
94+
logger.debug(
95+
"%s collection files found: %s", len(collection_jsons), collection_jsons
96+
)
6697
if not collection_jsons:
6798
return logger.warning(
6899
"No collections found in %s check if database folder exists", TARGET_DIR
@@ -71,5 +102,30 @@ def main() -> None:
71102
upload_data(database, collection_json)
72103

73104

105+
def generate_indexes(client: MongoClient, index_jsons: list[Path]) -> None:
106+
"""_summary_
107+
108+
Args:
109+
client (MongoClient): MongoClient
110+
index_jsons (list[Path]): List of index configuration filepaths
111+
"""
112+
logger.debug("%s index files found: %s", len(index_jsons), index_jsons)
113+
if not index_jsons:
114+
return logger.warning(
115+
"No indexes found in %s check if indexes folder exists", TARGET_DIR
116+
)
117+
for index_json in index_jsons:
118+
create_index(client, index_json)
119+
120+
121+
def main() -> None:
122+
client = MongoClient(CONN_STRING)
123+
database = client[DATABASE_NAME]
124+
collection_jsons = walk_directory(DB_PATH)
125+
index_jsons = walk_directory(INDEX_PATH)
126+
generate_collections(database, collection_jsons)
127+
generate_indexes(client, index_jsons)
128+
129+
74130
if __name__ == "__main__":
75131
main()

.evergreen/utils.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,56 @@ is_python_310() {
5050
return 1
5151
fi
5252
}
53+
54+
55+
# start mongodb-atlas-local container, because of a bug in podman we have to define the healtcheck ourselves (is the same as in the image)
56+
# stores the connection string in .local_atlas_uri file
57+
setup_local_atlas() {
58+
echo "Starting the container"
59+
CONTAINER_ID=$(podman run --rm -d -e DO_NOT_TRACK=1 -P --health-cmd "/usr/local/bin/runner healthcheck" mongodb/mongodb-atlas-local:latest)
60+
61+
echo "waiting for container to become healthy..."
62+
function wait() {
63+
CONTAINER_ID=$1
64+
echo "waiting for container to become healthy..."
65+
podman healthcheck run "$CONTAINER_ID"
66+
for _ in $(seq 600); do
67+
STATE=$(podman inspect -f '{{ .State.Health.Status }}' "$CONTAINER_ID")
68+
69+
case $STATE in
70+
healthy)
71+
echo "container is healthy"
72+
return 0
73+
;;
74+
unhealthy)
75+
echo "container is unhealthy"
76+
podman logs "$CONTAINER_ID"
77+
stop
78+
exit 1
79+
;;
80+
*)
81+
echo "Unrecognized state $STATE"
82+
sleep 1
83+
esac
84+
done
85+
86+
echo "container did not get healthy within 120 seconds, quitting"
87+
podman logs mongodb_atlas_local
88+
stop
89+
exit 2
90+
}
91+
92+
wait "$CONTAINER_ID"
93+
EXPOSED_PORT=$(podman inspect --format='{{ (index (index .NetworkSettings.Ports "27017/tcp") 0).HostPort }}' "$CONTAINER_ID")
94+
export CONN_STRING="mongodb://127.0.0.1:$EXPOSED_PORT/?directConnection=true"
95+
echo "CONN_STRING=mongodb://127.0.0.1:$EXPOSED_PORT/?directConnection=true" > $workdir/src/.evergreen/.local_atlas_uri
96+
}
97+
98+
fetch_local_atlas_uri() {
99+
. $workdir/src/.evergreen/.local_atlas_uri
100+
101+
export CONN_STRING=$CONN_STRING
102+
echo "$CONN_STRING"
103+
}
104+
105+

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,12 @@ The general layout of this repo looks like this:
5151

5252
### Configuring a Atlas CLI for testing
5353

54-
Each test subdirectory will automatically have its own local Atlas deployment. As a result, database and collection names will not conflict between different AI/ML integrations. To connect to your local Atlas using a connection string, call `$atlas` from the `run.sh` script within your subdirectory. This exposes the Atlas CLI binary. For example:
54+
Each test subdirectory will automatically have its own local Atlas deployment. As a result, database and collection names will not conflict between different AI/ML integrations. To connect to your local Atlas using a connection string, `utils.sh` has a `fetch_local_atlas_uri` that you can call from the `run.sh` script within your subdirectory. For example:
5555

5656
```bash
57-
CONN_STRING=$($atlas deployments connect $DIR --connectWith connectionString)
57+
. $workdir/src/.evergreen/utils.sh
58+
59+
CONN_STRING=$(fetch_local_atlas_uri)
5860
```
5961

6062
Stores the local Atlas URI within the `CONN_STRING` var. The script can then pass `CONN_STRING` as an environment variable to the test suite.
@@ -67,7 +69,7 @@ You can pre-populate a test's local Atlas deployment before running the `run.sh`
6769

6870
To create a search index, provide the search index configuration in the `indexes` dir; the file needs to be a json and for each individual index you wish to create, you must make a separate json file; for easier readability, please name each index configuration after the collection the index will be defined on or the name of the index itself. If multiple indexes will define one collection or multiples collections within a database share the name index name, append a name "purpose" (test_collection_vectorsearch_index) to favor readability. The evergreen script will then create the search index before the `run.sh` script is executed. See the ["Create an Atlas Search Index and Run a Query"](https://www.mongodb.com/docs/atlas/cli/stable/atlas-cli-deploy-fts/#create-an-atlas-search-index-and-run-a-query) documentation instructions on how to create a search index using Atlas CLI.
6971

70-
If you need more customized behavior when populating your database or configuring your local Atlas deployment, include that behavior in your `run.sh` script. The path to the `$atlas` binary is provided to that script. You can view more ways to configure local Atlas by visiting the [Atlas CLI local deployments documentation](https://www.mongodb.com/docs/atlas/cli/stable/atlas-cli-local-cloud/).
72+
If you need more customized behavior when populating your database or configuring your local Atlas deployment, include that behavior in your `run.sh` script.
7173

7274
### Unpacking the Evergreen config file
7375

langchain-python/run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pip install poetry
1616

1717
poetry install --with test
1818

19-
export MONGODB_ATLAS_URI=$($atlas deployments connect $DIR --connectWith connectionString)
19+
export MONGODB_ATLAS_URI=$(fetch_local_atlas_uri)
2020

2121
make test
2222

semantic-kernel-csharp/run.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
set -x
44

5+
. $workdir/src/.evergreen/utils.sh
56
# WORKING_DIR = src/semantic-kernel-csharp/semantic-kernel
67

78
# Install .NET
@@ -18,5 +19,5 @@ sed -i -e 's/"MongoDB Atlas cluster is required"/null/g' dotnet/src/IntegrationT
1819

1920
# Run tests
2021
echo "Running MongoDBMemoryStoreTests"
21-
MongoDB__ConnectionString=$($atlas deployments connect $DIR --connectWith connectionString) \
22+
MongoDB__ConnectionString=$(fetch_local_atlas_uri) \
2223
$DOTNET_SDK_PATH/dotnet test dotnet/src/IntegrationTests/IntegrationTests.csproj --filter SemanticKernel.IntegrationTests.Connectors.MongoDB.MongoDBMemoryStoreTests

semantic-kernel-python/run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -x
44

55
. $workdir/src/.evergreen/utils.sh
66

7-
CONN_STRING=$($atlas deployments connect $DIR --connectWith connectionString)
7+
CONN_STRING=$(fetch_local_atlas_uri)
88
PYTHON_BINARY=$(find_python3)
99

1010

0 commit comments

Comments
 (0)