Skip to main content

Python client library for the Xmemory API

Project description

xmemory

Python client library for the Xmemory API.

Quick start

from xmemory import xmemory_instance

mem = xmemory_instance(
    url="https://api.xmemory.ai",   # or set XMEM_API_URL env var
    instance_id="<your-instance-id>",
    token="<your-token>",           # or set XMEM_AUTH_TOKEN env var
)

mem.write("Alice is a software engineer who loves Python.")
result = mem.read("What does Alice do?")
print(result.reader_result)

Configuration

Parameter Env var Default Description
url XMEM_API_URL https://api.xmemory.ai Base URL of the Xmemory API
instance_id None Instance to read/write against
token XMEM_AUTH_TOKEN None Bearer token for authentication
timeout 60 Default request timeout in seconds

Methods

check_health() → None

Verify that the Xmemory API is reachable. Raises XmemoryHealthCheckError on failure. Unlike the constructor, this method performs the actual connectivity check, so you can call it whenever you need to confirm the service is alive.

try:
    mem.check_health()
    print("API is up")
except XmemoryHealthCheckError as e:
    print(f"API is unreachable: {e}")

generate_schema(schema_description, *, old_schema_yml=None, timeout=None) → GenerateSchemaResponse

Ask the API to generate a YML schema from a plain-text description.

resp = mem.generate_schema("People with name, role, and location.")
print(resp.generated_schema)

Optionally pass old_schema_yml to refine an existing schema.

create_instance(schema_text, schema_type, *, timeout=None) → bool

Create a new instance with the given schema. On success, the new instance_id is saved automatically and used for subsequent calls.

write(text, *, timeout=None, extract_write_id=None) → WriteResponse

Extract structured objects from text and store them in the instance.

resp = mem.write("Bob joined the team on Monday as a designer.")
print(resp.status)          # "ok" or "error"
print(resp.cleaned_objects) # written objects

read(query, *, read_mode=ReadMode.SINGLE_ANSWER, timeout=None) → ReadResponse

Query the instance and get a natural-language answer.

resp = mem.read("Who is on the team?")
print(resp.reader_result)

You can select the response format with ReadMode:

from xmemory import ReadMode

resp = mem.read("Show people and companies", read_mode=ReadMode.XRESPONSE)
print(resp.reader_result)

extract(text, *, timeout=None, extract_write_id=None) → ExtractionResponse

Extract structured objects from text without writing them to the instance.

resp = mem.extract("Carol is a manager based in Berlin.")
print(resp.objects_extracted)
from xmemory import SchemaType

ok = mem.create_instance(schema_yml, SchemaType.YML)
ok = mem.create_instance(schema_json, SchemaType.JSON)

update_schema(schema_text, schema_type, *, timeout=None) → bool

Update the schema of the current instance.

ok = mem.update_schema(new_schema_yml, SchemaType.YML)

Error handling

All errors raise XmemoryAPIError (or its subclass XmemoryHealthCheckError for connectivity failures).

from xmemory import XmemoryAPIError, XmemoryHealthCheckError, xmemory_instance

mem = xmemory_instance(url="http://localhost:8000", instance_id="abc")

try:
    mem.check_health()
except XmemoryHealthCheckError as e:
    print(f"Could not reach the API: {e}")

try:
    resp = mem.read("something")
except XmemoryAPIError as e:
    print(f"API error (HTTP {e.status}): {e}")

Package publishing to pip

python -m pip install --upgrade build twine
python -m build

#test with test.pypi.org (separate account and API key required)
python -m twine upload --repository testpypi dist/*

#publish the real version when ready
python -m twine upload dist/*

#test the package
pip install xmemory

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xmemory_ai-0.2.1.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xmemory_ai-0.2.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file xmemory_ai-0.2.1.tar.gz.

File metadata

  • Download URL: xmemory_ai-0.2.1.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.5

File hashes

Hashes for xmemory_ai-0.2.1.tar.gz
Algorithm Hash digest
SHA256 16634cebc24d97e24f9d0cfb410e7df54bc0797291e904b9b40c1e375a34912b
MD5 bdf6677ec32eae0ad9680f13e016126f
BLAKE2b-256 f508c4ed0edc473439280f6c5fd3c029fe93cb4b476a8b87a69eed552d833a22

See more details on using hashes here.

File details

Details for the file xmemory_ai-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: xmemory_ai-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.5

File hashes

Hashes for xmemory_ai-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 467c16cc5d8824dfd9c122455724a7878e6510f6d427346cb1fa9eb8d70bc3d6
MD5 1c68f5c54e10516b35f96bdae6f71cba
BLAKE2b-256 1f1a8a45317817c68fd4e4e16d68eae661e882741fd99c12fa42899e5d61b679

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page