SDK for the T2G API
Project description
Lettria's Text-to-Graph SDK 🚀
Welcome to the official Python SDK for Lettria's Text-to-Graph (T2G) API! This SDK provides a convenient way to interact with the T2G API, allowing you to unlock the power of knowledge graphs from your text data directly within your Python applications.
🌟 Overview
Lettria's Text-to-Graph (T2G) technology transforms unstructured text into structured knowledge graphs. This SDK is designed to simplify the process of sending your data to the T2G API and retrieving the results, whether you are indexing a single document, a collection of files, or building complex ontologies.
This SDK is built with developers in mind, providing a clean, asynchronous client to handle API requests efficiently.
✨ Features
- Asynchronous Client: Built with
asyncioandaiohttpfor high-performance, non-blocking API calls. - Simple Interface: Easy-to-use methods for indexing files and managing jobs.
- Data Validation: Leverages
pydanticfor robust and reliable data modeling. - Neo4j Integration: Directly save your graph data to a Neo4j instance.
- Flexible Configuration: Configure the SDK via environment variables or directly in your code.
- Built-in Error Handling: Gracefully handles API errors with custom exceptions.
📦 Installation
You can install the SDK using pip:
pip install t2g-sdk==1.0.0-rc.9
🚀 Getting Started
To start using the SDK, you will need an API key from Lettria.
To create an API key, please visit our preview instance here.
Access to the API is managed by whitelisting. If you require access, please contact us at hello@lettria.com to request whitelisting.
Configuration
The SDK can be configured by setting the following environment variable:
LETTRIA_API_KEY: Your Lettria API key.
Alternatively, you can pass this value directly to the T2GClient constructor.
Quick Example: Indexing a File
This example demonstrates how to index a local text file and save the resulting graph to Neo4j.
import asyncio
from t2g_sdk.client import T2GClient
from t2g_sdk.exceptions import T2GException
from t2g_sdk.models import Job
async def main():
# It is recommended to use the client as a context manager
async with T2GClient() as client:
try:
# Index a file and save the result to Neo4j
job: Job = await client.index_file(
file_path="path/to/your/document.txt",
# You can also specify an ontology file
# ontology_path="path/to/your/ontology.ttl",
save_to_neo4j=True,
)
print("🎉 Job completed successfully!")
print("Job details:", job)
except T2GException as e:
print(f"An API error occurred: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
if __name__ == "__main__":
asyncio.run(main())
📚 API Reference
The main entry point of the SDK is the T2GClient class.
t2g_sdk.client.T2GClient
The asynchronous client for interacting with the T2G API.
Methods:
async def index_file(file_path: str, ontology_path: str = None, output_path: str = None, save_to_neo4j: bool = False) -> Job: Processes a file by uploading it, running a job, and downloading the output.file_path: Path to the file to process.ontology_path(optional): Path to an ontology file (e.g.,.ttl).output_path(optional): The path to save the output to. If not provided, a default path will be used.save_to_neo4j(optional): IfTrue, saves the result to your configured Neo4j instance.
Properties:
file->FileService: Provides access to file-related operations.job->JobService: Provides access to job-related operations.ontology->OntologyService: Provides access to ontology-related operations.neo4j->Neo4jService: Provides access to Neo4j-related operations.
Services
T2GClient.file -> FileService
The FileService handles file-related operations and is accessed via the file property of an active T2GClient instance.
Methods:
async def upload_file(file_path: str) -> File: Uploads a file to the T2G API.file_path: The path to the file to upload.
async def create_file(name: str, source_hash: str) -> Dict: Creates a file record and returns a presigned URL for uploading.name: The name of the file.source_hash: The SHA256 hash of the file's content.
async def find_files(ids: List[str] = None, source_hashes: List[str] = None) -> List[File]: Finds files by their IDs or source hashes.ids(optional): A list of file IDs to find.source_hashes(optional): A list of source hashes to find.
T2GClient.job -> JobService
The JobService handles job-related operations and is accessed via the job property of an active T2GClient instance.
Methods:
async def run_job(file_id: str, ontology_id: str = None, polling_interval: int = 5, timeout: int = 300) -> Job: Submits a job and waits for its completion.file_id: The ID of the file to process.ontology_id(optional): The ID of the ontology to use.polling_interval(optional): The interval in seconds to poll for job status.timeout(optional): The timeout in seconds for the job to complete.
async def submit_job(file_id: str, ontology_id: str = None) -> Job: Submits a job for processing.file_id: The ID of the file to process.ontology_id(optional): The ID of the ontology to use.
async def find_jobs(ids: List[str]) -> List[Job]: Finds jobs by their IDs.ids: A list of job IDs to find.
async def download_job_output(job_id: str, output_path: str = None) -> str: Downloads the output of a completed job.job_id: The ID of the job to download the output from.output_path(optional): The path to save the output to.
T2GClient.ontology -> OntologyService
The OntologyService handles ontology-related operations and is accessed via the ontology property of an active T2GClient instance.
Methods:
async def upload_ontology(ontology_path: str) -> Ontology: Uploads an ontology file to the T2G API.ontology_path: The path to the ontology file to upload.
async def create_ontology(name: str, source_hash: str) -> Dict: Creates an ontology record and returns a presigned URL for uploading.name: The name of the ontology.source_hash: The SHA256 hash of the ontology file's content.
async def find_ontologies(ids: List[str] = None, source_hashes: List[str] = None) -> List[Ontology]: Finds ontologies by their IDs or source hashes.ids(optional): A list of ontology IDs to find.source_hashes(optional): A list of source hashes to find.
T2GClient.neo4j -> Neo4jService
The Neo4jService handles operations related to Neo4j and is accessed via the neo4j property of an active T2GClient instance.
Methods:
async def save_output_to_neo4j(file_path: str): Saves the output of a job (in CQL format) to a Neo4j database.file_path: The path to the CQL file to save.
Models
The SDK uses pydantic models to represent the data returned by the API.
Job: Represents a job with itsidandstatus.File: Represents a file with itsid,name,status, andcreated_at.Ontology: Represents an ontology with itsid,name,status, andcreated_at.JobStatus: AnEnumfor the status of a job (PENDING,PROCESSING,SUCCEEDED,FAILED).FileStatus: AnEnumfor the status of a file (UPLOADED,PENDING).OntologyStatus: AnEnumfor the status of an ontology (UPLOADED,PENDING).
⚙️ Configuration Details
The SDK uses pydantic-settings for configuration management. You can configure the client by passing arguments to its constructor, or by setting environment variables.
| Argument | Environment Variable | Description |
|---|---|---|
api_key |
LETTRIA_API_KEY |
Required. Your Lettria API key. |
neo4j_uri |
NEO4J_URI |
(Optional) The URI for your Neo4j instance. |
neo4j_user |
NEO4J_USER |
(Optional) The username for your Neo4j instance. |
neo4j_password |
NEO4J_PASSWORD |
(Optional) The password for your Neo4j instance. |
Note: The Neo4j configuration options (neo4j_uri, neo4j_user, neo4j_password) are only required if you set save_to_neo4j=True when calling index_file.
🚨 Error Handling
The SDK defines custom exceptions to handle API errors.
t2g_sdk.exceptions.T2GException: The base exception for all API-related errors.t2g_sdk.exceptions.APIError: Raised for general API errors.t2g_sdk.exceptions.AuthenticationError: Raised for authentication-related errors (e.g., invalid API key).t2g_sdk.exceptions.JobError: Raised when a job fails.
It is recommended to wrap your API calls in a try...except block to handle these exceptions.
📂 Examples
You can find more examples in the examples/ directory. Each example includes a README.md with instructions on how to run it.
index_file/: A simple demonstration of how to index a file.simple-reporting/: An advanced example of how to generate a report from the indexed data.upload_file/: A basic example of how to upload a file.
🤝 Contributing
Contributions are welcome! Please feel free to submit a pull request or open an issue if you have any feedback or suggestions.
📧 Contact
For any feedback, questions, or support, please reach out to us at hello@lettria.com.
📄 License
This SDK is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file t2g_sdk-1.0.0rc9.tar.gz.
File metadata
- Download URL: t2g_sdk-1.0.0rc9.tar.gz
- Upload date:
- Size: 4.5 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
97ae88bc288329413f3929936f59bd2036f102f59e54d57b9e9d62664a65a4e5
|
|
| MD5 |
188bb64defde337073ad17555977b4ce
|
|
| BLAKE2b-256 |
4ef939280f918ae10195ffc303464e6d6b880183eb96f90344aef08a993398cb
|
File details
Details for the file t2g_sdk-1.0.0rc9-py3-none-any.whl.
File metadata
- Download URL: t2g_sdk-1.0.0rc9-py3-none-any.whl
- Upload date:
- Size: 15.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0814964d0f0c6c84e91a08c728735449be80e4fe5dd9c3ac504bfc098634eb5d
|
|
| MD5 |
c9c7eb49571a77899897792d6c3f4c5d
|
|
| BLAKE2b-256 |
428b075ae6110ad69ab2f2d0af571dead2b2f42354e834a540453ee14c34130f
|