Skip to main content

A Python client library for Aiklyra API

Project description

Aiklyra Python Package

Aiklyra is a Python client library that provides a simple interface to your FastAPI-powered conversation analysis API. It allows developers to easily submit conversation data for clustering, analysis, and graph processing using an asynchronous workflow.

License


Table of Contents


Features

  • Asynchronous Analysis: Submit conversation data for analysis and later retrieve detailed results via job status.
  • Graph Processing: Construct, filter, and visualize directed graphs representing conversation flows.
  • Customizable Filters: Apply graph filtering strategies like thresholding, top-K filtering, and advanced reconnecting filters.
  • Custom Exceptions: Detailed exception classes (InvalidAPIKeyError, InsufficientCreditsError, etc.) for better error handling.
  • Pydantic Models: Uses Pydantic for data validation and serialization.
  • Easy Integration: Designed to integrate seamlessly with existing Python codebases.

Installation

  1. Clone or Download the Repository:

    git clone https://github.com/AiklyraData/aiklyra.git
    cd Aiklyra
    
  2. Install via setup.py:

    pip install .
    

    or

  3. Editable Installation (recommended for development):

    pip install -e .
    

Requirements


Usage

Basic Setup

  1. Configure the Client
    The client now uses asynchronous endpoints and does not require an API key. Simply specify the API's base URL.

  2. Import the Client

    from aiklyra.client import AiklyraClient
    
  3. Initialize the Client

    client = AiklyraClient(base_url="http://your-api-base-url")
    
  4. Submit Analysis & Retrieve Results
    The analysis workflow now occurs in two steps:

    • Submit your conversation data for analysis. This call returns a job ID.
    • Check the job status using the returned job ID to obtain the full analysis results.
    # Submit conversation data for analysis
    submission = client.submit_analysis(
        conversation_data=your_conversation_data,
        min_clusters=5,
        max_clusters=10,
        top_k_nearest_to_centroid=10
    )
    
    # Later, check the job status to retrieve the analysis result:
    job_status = client.check_job_status(submission.job_id)
    # The actual analysis is available in the "result" field.
    analysis = job_status.result
    

Example

Below is an example script that demonstrates the complete workflow:

from aiklyra.client import AiklyraClient
from aiklyra.exceptions import AnalysisError, AiklyraAPIError
import time

def main():
    # Replace with your API's base URL
    base_url = "http://your-api-base-url"

    # Initialize the client (no API key required)
    client = AiklyraClient(base_url=base_url)

    # Example conversation data
    conversation_data = {
        "conversation_1": [
            {"role": "user", "content": "Hi, I need help with my account."},
            {"role": "assistant", "content": "Sure, please provide your account ID."},
            {"role": "user", "content": "It's 12345."}
        ],
        "conversation_2": [
            {"role": "user", "content": "Can I change my subscription plan?"},
            {"role": "assistant", "content": "Yes, you can change it from your settings."},
            {"role": "user", "content": "Great, thank you!"}
        ]
    }

    try:
        # Submit analysis job
        submission = client.submit_analysis(
            conversation_data=conversation_data,
            min_clusters=5,
            max_clusters=10,
            top_k_nearest_to_centroid=10
        )
        print(f"Job submitted. Job ID: {submission.job_id}")

        # Optionally, wait before checking job status (or poll periodically)
        time.sleep(2)

        # Check job status and retrieve analysis result
        job_status = client.check_job_status(submission.job_id)
        if job_status.status == "completed":
            analysis = job_status.result
            print("Analysis Result:")
            print("Transition Matrix:", analysis.transition_matrix)
            print("Intent by Cluster:", analysis.intent_by_cluster)
        else:
            print(f"Job status: {job_status.status}")

    except AnalysisError as e:
        print(f"Analysis failed: {e}")
    except AiklyraAPIError as e:
        print(f"API Error: {e}")

if __name__ == "__main__":
    main()

Run the script:

python example_usage.py

Graph Processing and Visualization

Graph Construction

After obtaining the analysis result (a ConversationFlowAnalysisResponse object), you can process it into a directed graph:

from aiklyra.graph.processor import GraphProcessor

# Assuming 'analysis' is your ConversationFlowAnalysisResponse instance
graph_processor = GraphProcessor(analysis)
graph = graph_processor.graph  # This is a NetworkX graph

Graph Filtering

Apply filters to refine the graph. For example, you can use various filters to remove low-weight edges or retain only the top-K connections:

from aiklyra.graph.filters import ThresholdFilter, TopKFilter

# Apply a threshold filter to remove edges below a certain weight.
threshold_filter = ThresholdFilter(threshold=0.3)
filtered_graph = graph_processor.filter_graph(threshold_filter)

# Or apply a top-K filter.
topk_filter = TopKFilter(top_k=3)
filtered_graph = graph_processor.filter_graph(topk_filter)

Visualization

Visualize the graph using PyVis or NetworkX:

# PyVis visualization (creates an HTML file)
graph_processor.plot_graph_html(file_name="conversation_flow.html")

# Alternatively, visualize using NetworkX's built-in methods
graph_processor.visualize_graph()

Testing

  1. Install Development Dependencies:

    pip install -r requirements.txt
    
  2. Run Tests:

    python -m unittest discover tests
    

    or

    pytest
    

Development

  • Branching: Use feature branches for new features or bug fixes.
  • Pull Requests: Open a PR with a clear description and pass all tests before merging.
  • Coding Standards: Follow PEP 8 style guidelines.

License

This project is licensed under the Apache License 2.0.
You are free to use, distribute, and modify the library under the terms of this license.


Contributing

We welcome contributions! To get started:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/my-feature)
  3. Commit your changes (git commit -m "Add my feature")
  4. Push to the branch (git push origin feature/my-feature)
  5. Open a Pull Request on GitHub

Please ensure your contributions include tests, documentation, and follow the coding standards described above.


Contact

If you have questions, suggestions, or issues, feel free to open an issue on the GitHub repository or reach out by email!


Thank you for using Aiklyra! We look forward to seeing how you integrate it into your projects.

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

Aiklyra-1.1.0.tar.gz (26.5 kB view details)

Uploaded Source

Built Distribution

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

Aiklyra-1.1.0-py3-none-any.whl (33.5 kB view details)

Uploaded Python 3

File details

Details for the file Aiklyra-1.1.0.tar.gz.

File metadata

  • Download URL: Aiklyra-1.1.0.tar.gz
  • Upload date:
  • Size: 26.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.4

File hashes

Hashes for Aiklyra-1.1.0.tar.gz
Algorithm Hash digest
SHA256 13472af3783e17e25780b05371403b4c8ae9d1a957d94dfdd1d6304945263806
MD5 129c3c7ebf7c025edfa2e5c853d54daf
BLAKE2b-256 e52c762cf7b48e119a0822fdf6ba3d2245b42f3adfc5bb2ad2542fc899d139ee

See more details on using hashes here.

File details

Details for the file Aiklyra-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: Aiklyra-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 33.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.4

File hashes

Hashes for Aiklyra-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 01e14ed939a8326323604f55086a294059931a7aa2cfaab1c4f85a1d41a600a4
MD5 317c1b881c9de5facc206a3e94eabcae
BLAKE2b-256 ebabfe6da0a329e35d11dccfd5678236cf47c03df3ca191cb1b6866aeb6b989b

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