Python SDK

Visit the GitHub Repo

You’ll need to Create an API Key to Authentication with Metal.

Installation

pip3 install metal-sdk

Retrieval

App Setup

ParamTypeDescription
api_keystringThe API Key for your Org. Learn more.
client_idstringThe Client ID for your Org. Learn more.
index_idstringThe ID of the index you want to connect with. Learn more.
from metal_sdk.metal import Metal

metal = Metal("api_key", "client_id", "index_id")

Adding an App

ParamTypeDescription
namestringName of the App.
indexesarrayAn array of the index connected to the app.
metal.add_app({"name": "My App", "indexes": ["index1"]})

Get One App

ParamTypeDescription
app_idstringID of the App.
metal.get_app("app_id")

Get All Apps

metal.get_apps()

Updating an App

ParamTypeDescription
app_idstringThe unique identifier of the app you want to update.
namestring (optional)The updated name for the App.
indexesarray (optional)An updated array of the index connected to the app.

metal.update_app("app_id", {"name": "Updated Metal App", "indexes": ["index1"]})

Adding an Index

Payload

ParamTypeDescription
modelstringIdentifier for the model being used, e.g., “text-embedding-ada-002”.
namestringName of the index.
datasourcestringUnique identifier for the Datasource.
indexTypestringType of index structure, e.g., “FLAT”, “HNSW”.
dimensionsnumberNumber of dimensions for the index. e.g.1536
filtersarrayAn array of filters specifying fields for advanced searching.

Filters

ParamTypeDescription
fieldstringName of the attribute you wish to filter by.
typestringType of the attribute (e.g., “string”, “number”).
metal = Metal("api_key", "client_id")

payload = {
    "model": "text-embedding-ada-002",
    "name": "my index",
    "datasource": "<datasource_id>",
    "indexType": "FLAT",
    "dimensions": 1536,
    "filters": [
      {
        "field": "name",
        "type": "string"
      },
      {
        "field": "age",
        "type": "number"
      }
    ]
}

metal.add_index(payload)

Get One Index

ParamTypeDescription
index_idstringThe unique identifier of the index you want to retrieve.
metal.get_index("index_id")

Indexing a Document

ParamTypeDescription
payloaddictDictionary with index parameters
index_idstringOptional index id where record will get indexed.

Payload

ParamTypeDescription
idstringOptional identifier for your embedding.
embeddingnumber[]Raw embeddings to be indexed.
imageUrlstringA URL for an image to be vectorized and indexed.
textstringThe text to be vectorized and indexed.
metadataobjectA flexible metdata object to be stored w/ the embedding.
metal.index({ "text": "text-to-index" }, "index_id")

Multi(Bulk) Indexing

You can index up to 100 records in a single request.

ParamTypeDescription
payloadBulkIndexItem[]Array of bulk items

BulkIndexItem

ParamTypeDescription
idstringOptional identifier for your embedding.
indexstringRequired - index id.
embeddingnumber[]Raw embeddings to be indexed.
imageUrlstringA URL for an image to be vectorized and indexed.
textstringThe text to be vectorized and indexed.
metadataobjectA flexible metdata object to be stored w/ the embedding.
metal.index_many([{ "text": "blacksabbath", "index": "index-id" }, { "text": "ironmaiden", "index": "index-id" }])

Searching

self, payload: SearchPayload = {}, index_id=None, ids_only=False, limit=1

ParamTypeDescription
payloaddictsearch payload.
index_idstringId of index to search.
ids_onlybooleanOptional return only the ids
limitnumberNumber of documents returned by the API (default=1).

Payload

ParamTypeDescription
embeddingnumber[]Raw embeddings to be searched.
imageUrlstringA URL for an image to be vectorized and searched.
textstringThe text to be vectorized and searched.
filtersdict[]List of filters to match in search.

Filter

ParamTypeDescription
anddictAnd clause, all members have to be satisfied.
ordictOr clause, at least one member has to be satisfied.

Filter Item

ParamTypeDescription
valuestring | numberValue to match.
fieldstringField to filter.
operatorstringPossible values: eq, gt, gte, lt, lte.
results = metal.search({ "text": "term-to-search", "filters": { "and": [{"field": "band", "value": "Black Sabbath", "operator":"eq"}]} }, index_id="indexID", limit=10)

Get One Document

ParamTypeDescription
idstringThe ID of the document to retrieve.
index_idstringOptional. Id of index to search.
document = metal.get_one("document_id_123")

Get Many Documents

ParamTypeDescription
idslist of strA list of document IDs to retrieve.
index_idstr, optionalThe ID of the index from which to retrieve documents. If not provided, the default index ID will be used.
document = metal.get_many("[document_id_123, document_id_456]")

Get Queries

ParamTypeDescription
index_idstringThe ID of the Index to retrieve.
queries = metal.get_queries("index_id")

Delete One

ParamTypeDescription
embedding_idstringThe ID of the document or embedding to delete.
index_idstringThe ID of the index containing the document.
metal.delete_one("embedding_id", "index_id")

Delete Many (Bulk)

ParamTypeDescription
embedding_idslistA list of IDs for the documents or embeddings to delete.
index_idstringThe ID of the index containing the documents.
embedding_ids_to_delete = ["id1", "id2", "id3"]
metal.delete_many(embedding_ids_to_delete, "index_id")

Memory (Motorhead)

App Setup

ParamTypeDescription
api_keystringFor Managed. The API Key for your Org. [details](- Create an API Key for Authentication.
client_idstringFor Managed.The Client ID for your Org. [details](- Create an API Key for Authentication.
base_urlstringFor Self-hosted. The URL path to your motorhead instance
from metal_sdk.motorhead import Motorhead

# Managed
motorhead = Motorhead({ "api_key": "api_key", "client_id": "client-id" })

# Self-hosted
motorhead = Motorhead({ "base_url": "https://motorhead.yourdomain.com" })

Create Memory

class Message:
    content: str
    role: str  # "AI" or "Human"

class Memory:
    messages: list  # List of Message instances
    context: str
ParamTypeDescription
session_idstringThe ID of the session. If the session does not exist, it will create one
memoryMemory[]A memory payload to update the session memory
memory_payload = {
  "messages": [
    {"role": "Human", "content": "Who is the best vocalist of all time?"},
    {"role": "AI", "content": "Ozzy!"},
  ],
  "context":
    "User ask what can he eat in Colombia. The AI responds arepas are really nice",
}


motorhead.add_memory("session-id", memory_payload)

Get Memory

ParamTypeDescription
session_idstringThe ID of the session to retrieve memory for
motorhead.get_memory("session-id")

Delete Memory

ParamTypeDescription
session_idstringThe ID of the session to delete memory for
motorhead.delete_memory("session-id")

Datasources

Add a Datasource

ParamTypeDescription
namestringName of the Datasource.
sourcetypestringType of the source. It can either be ‘File’ or ‘Text’.
autoExtractbooleanFlag indicating whether auto-extraction is enabled.
metadataFieldsarrayAn array of fields specifying which attributes you wish to extract. Each object should have name, type, and description properties.

Metadata Fields

ParamTypeDescription
namestringName of the attribute you wish to extract.
typestringType of the attribute (e.g., “String”, “Number”).
descriptionstringBrief description or example of the attribute.
from metal_sdk.metal import Metal

payload = {
    "name": "my datasource",
    "sourcetype": "File",
    "autoExtract": True,
    "metadataFields": [
      {
        "name": "band",
        "type": "string",
        "description": "Which heavy metal band is represented by the iconic mascot Eddie?"
      }
    ]
}

metal.add_datasource(payload)

Update Datasource

Parameters

ParamTypeDescription
dataSourceIdstringIdentifier of the Datasource you want to update.
namestringUpdated name of the Datasource.
sourcetypestringUpdated type of the source. Either ‘File’ or ‘Text’.
autoExtractbooleanUpdated flag indicating whether auto-extraction is enabled.
metadataFieldsarrayUpdated fields specifying which attributes you wish to extract.

Example

payload = {
    "name": "updated_datasource",
    "sourcetype": "Text",
    "autoExtract": False,
    "metadataFields": [
        {
            "name": "song",
            "type": "string",
            "description": "Which was Iron Maiden's first single?"
        }
    ]
}

metal.update_datasource("existing_datasourceId", payload)

Get Datasource

Parameters

ParamTypeDescription
dataSourceIdstringIdentifier of the Datasource you want to retrieve information about.

Example

metal.get_datasource("existing_datasourceId")
print(response)  # This will display detailed information about the Datasource

List Datasources

Parameters

ParamTypeDescription
limitint(Optional) The maximum number of Datasources to return. Default is 10 and a maximum of 100.
pageint(Optional) The page number for pagination. Should be a positive integer up to 100.
metal.get_all_datasources(page=1, limit=10)

Delete Datasource

Parameters

ParamTypeDescription
datasource_idstringThe ID of the Datasource you want to delete.

Example

response = metal.delete_datasource("datasourceID")
print(response)  # Expected: "Datasource successfully deleted."

Data Entities

Add a Data entity

.pdf, doc, .docx, .xlsx, and .csv are accepted.

Parameters

NameTypeDescription
datasourceIdstringThe ID of the datasource where the file belongs.
file_pathstringThe local file path of the file to upload.
metadataobjectAdditional Metadata that isn’t extracted.

Example

results = metal.add_data_entity("datasource_id", "./file_path.csv", metadata=metadata)

Get Data Entity

Parameters

ParamTypeDescription
idstringThe ID of the data entity you want to retrieve.
metal.get_data_entity("data_entity_id")

List Data Entities

Parameters

ParamTypeDescription
datasourceIDstringThe ID of the datasource for which you want to list the data entities.
limitint(Optional) The maximum number of data entities to return. Default is 10 and a maximum of 100.
pageint(Optional) The page number for pagination. Should be a positive integer up to 100.
metal.get_all_data_entities("datasource_id", page=1, limit=10)

Delete Data Entity

Parameters

ParamTypeDescription
idstringThe ID of the data entity you want to delete.
metal.delete_data_entity("data_entity_id")