Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: task type sagemaker #338

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/sagemaker/run-nomic-embed-text.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
}
],
"source": [
"response = embed_text(texts, endpoint_name, region_name=region_name, batch_size=32, dimensionality=128)\n",
"response = embed_text(texts, endpoint_name, region_name=region_name, batch_size=32, dimensionality=128, task_type=\"search_document\")\n",
"embeddings = response[\"embeddings\"]\n",
"np.array(embeddings).shape"
]
Expand Down
29 changes: 8 additions & 21 deletions nomic/aws/sagemaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,6 @@ def parse_sagemaker_response(response):
return resp["embeddings"]


def preprocess_texts(texts: List[str], task_type: str = "search_document"):
"""
Preprocess a list of texts for embedding using a sagemaker model.

Args:
texts: List of texts to be embedded.
task_type: The task type to use when embedding. One of `search_query`, `search_document`, `classification`, `clustering`

Returns:
List of texts formatted for sagemaker embedding.
"""
assert task_type in [
"search_query",
"search_document",
"classification",
"clustering",
], f"Invalid task type: {task_type}"
return [f"{task_type}: {text}" for text in texts]


def batch_transform_text(
s3_input_path: str,
s3_output_path: str,
Expand Down Expand Up @@ -157,7 +137,13 @@ def embed_text(
logger.warning("No texts to embed.")
return None

texts = preprocess_texts(texts, task_type)
assert task_type in [
zanussbaum marked this conversation as resolved.
Show resolved Hide resolved
"search_query",
"search_document",
"classification",
"clustering",
], f"Invalid task type: {task_type}"

assert dimensionality in (
64,
128,
Expand All @@ -175,6 +161,7 @@ def embed_text(
"texts": texts[i : i + batch_size],
"binary": binary,
"dimensionality": dimensionality,
"task_type": task_type,
}
)
response = client.invoke_endpoint(EndpointName=sagemaker_endpoint, Body=batch, ContentType="application/json")
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"pylint",
"pytest",
"isort",
"pyright",
"pyright<=pyright-1.1.377",
zanussbaum marked this conversation as resolved.
Show resolved Hide resolved
"myst-parser",
"mkdocs-material",
"mkautodoc",
Expand Down