mle-core


Namemle-core JSON
Version 0.0.3 PyPI version JSON
download
home_pagehttps://github.com/RamailoTech/mle_core
SummaryCore modules necessary during application development
upload_time2024-08-24 14:25:31
maintainerNone
docs_urlNone
authorML Experts Team
requires_python>=3.9
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # MLE Core

## Overview

Welcome to the MLE Core repository, maintained by the ML Experts team. This repository contains core modules and utilities necessary for application development. It includes connectors for databases and language model services, a chat service for interacting with LLMs, and various utility functions to aid in development.

## Directory Structure

```
mle_core/
├── __init__.py
├── chat/
│   ├── __init__.py
│   └── chat_service.py
├── connectors/
│   ├── __init__.py
│   ├── base.py
│   ├── db/
│   │   ├── __init__.py
│   │   ├── postgres_connector.py
│   │   └── mongo_connector.py
│   └── llm/
│       ├── __init__.py
│       ├── base.py
│       ├── openai_connector.py
│       └── azure_connector.py
├── utils/
│   ├── __init__.py
│   ├── formatting.py
│   ├── logging.py
│   └── response_handling.py
├── config.py
└── main.py
```

## Modules

### Chat

The `chat` module provides a `ChatService` class that simplifies interaction with different language model (LLM) connectors.

- `chat_service.py`: Contains the `ChatService` class for interacting with LLMs.

### Connectors

The `connectors` module includes connectors for various databases and LLMs.

- `base.py`: Defines the abstract base class for connectors.
- `db/`: Contains database connectors.
  - `postgres_connector.py`: Connector for PostgreSQL.
  - `mongo_connector.py`: Connector for MongoDB.
- `llm/`: Contains LLM connectors.
  - `openai_connector.py`: Connector for OpenAI API.
  - `azure_connector.py`: Connector for Azure AI API.

### Utils

The `utils` module contains utility functions that are commonly used across different modules.

- `formatting.py`: Functions for formatting prompts.
- `logging.py`: Functions for setting up logging.
- `response_handling.py`: Functions for handling LLM responses.

### Config

The `config.py` file contains configuration logic to select the appropriate connectors based on the environment or other criteria.

### Installing the Repository

First, install the `prowritingaid-sdk` dependency for grammar checker.

```sh
pip install git+https://github.com/prowriting/prowritingaid.python.git
```
Then, install our package
```sh
pip install mle_core
```

## Usage

### Setting Up Environment Variables

Ensure you have the following environment variables set for database and LLM connectors:

For PostgreSQL:

```
DATABASE_USER=your_db_user
DATABASE_PASSWORD=your_db_password
DATABASE_HOST=your_db_host
DATABASE_PORT=your_db_port
DATABASE_NAME=your_db_name
```

For MongoDB:

```
MONGO_URI=your_mongo_uri
MONGO_DB_NAME=your_mongo_db_name
```

For OpenAI:

```
OPENAI_API_KEY=your_openai_api_key
```

For ChatAnthropic:
```
ANTHROPIC_API_KEY=your_anthropic_api_key
```

For Azure AI:

```
AZURE_ENDPOINT=your_azure_endpoint
AZURE_API_KEY=your_azure_api_key
AZURE_DEPLOYMENT_NAME=your_azure_deployment_name
```

### Using the Chat Service

```python
from mle_core.chat import ChatService

import asyncio
from dotenv import load_dotenv
from mle_core.chat.chat_service import ChatService

load_dotenv()


async def main():
    llm_type='openai' # or "azure" or "anthropic"
    chat_service = ChatService(llm_type)

    method = 'sync'  # or async
    response_method = 'invoke'  # or "batch" or "stream"
    system_message = 'You are a helpful assistant.'
    user_message = 'What is the weather like today?'
    model_name = "gpt-3.5-turbo"
    input = {
        "system_message": system_message,
        "user_message": user_message
    }
    if method == "sync":
        response = chat_service.get_sync_response(
        response_method, 
        input, 
        model_name=model_name, 
        temperature=0.2, 
        max_tokens=1000, 
        is_structured=False, 
        pydantic_model=None)
        print(response)

    elif method == "async":
        response = await chat_service.get_async_response(
        response_method, 
        input, 
        model_name=model_name, 
        temperature=0.2, 
        max_tokens=1000,
        is_structured=False, 
        pydantic_model=None)
        print(response)


asyncio.run(main())
```

### Using the Chat Service for structured output

```python
from mle_core.chat import ChatService
import asyncio
from dotenv import load_dotenv
from mle_core.chat.chat_service import ChatService
from langchain_core.pydantic_v1 import BaseModel, Field

load_dotenv()

#create a pydnatic model
class Joke(BaseModel):
    setup: str = Field(description="setup of the joke")
    punchline: str = Field(description="punchline of the joke")

async def main():
    llm_type='openai' # or "azure" or "anthropic"
    chat_service = ChatService(llm_type)
    
    method = 'sync'  # or async
    response_method = 'invoke'  # or "batch" or "stream"
    system_message = 'You are a helpful assistant.'
    user_message = 'What is the weather like today?'
    model_name = "gpt-3.5-turbo"
    input = {
        "system_message": system_message,
        "user_message": user_message
    }
    if method == "sync":
        response = chat_service.get_sync_response(
            response_method, 
            input, 
            model_name=model_name,
            temperature=0.2, 
            max_tokens=1000, 
            is_structured=True, 
            pydantic_model=Joke)
        print(response)

    elif method == "async":
        response = await chat_service.get_async_response(
            response_method, 
            input, 
            model_name=model_name, 
            temperature=0.2, 
            max_tokens=1000,
            is_structured=True, 
            pydantic_model=Joke)
        print(response)


asyncio.run(main())
```


### Note: Using Chat Service

1. If response_method is "batch" the input should be list of input. 

Example: 
```
system_message = 'You are a helpful assistant.'
input = [{'system_message': system_message, 'user_message': 'Tell me a bear joke.'}, {'system_message': system_message, 'user_message': 'Tell me a cat joke.'}]
```


### Using Database Connectors

```python
from mle_core.config import get_db_connector

def main():
    db_type = "postgres"  # or "mongo"
    db_connector = get_db_connector(db_type)
    db_connection = db_connector.get_connection()
    print(db_connection)

if __name__ == "__main__":
    main()
```

### Using Evaluators

```python
from mle_core.evaluators.tests_results_generation import Evaluator

def main():
    input_file_path = 'test_case.json'
    output_file_path = 'output_file.csv'
    output_file_type = 'csv'

    # assume your evaluator_function be f_eval_function 
    try:
        evaluator = Evaluator(input_file_path,f_eval_function, output_file_path, output_file_type.lower())
        evaluator.execute()
        print("Processing completed successfully.")
    except Exception as e:
        print(f"An error occurred: {str(e)}")

if __name__ == '__main__':
    main()

```
### Using Checkers

#### Fact checker and hyperbole detector
```python
from mle_core.checkers import f_hyperbole_detector, f_fact_checker

# The context basically refers to the knowledge base
# question is generally the user prompt to the system
# answer generally is the LLM generated output

fact = f_fact_checker(question, context, answer)
hyperbole = f_hyperbole_detector(question, context, answer)
```

#### Database consistency checker
```python
from mle_core.connectors.db import Neo4jConnector
from mle_core.checkers import Neo4jSanityCheck

uri = os.getenv('NEO4J_URI')
user = os.getenv('NEO4J_USERNAME')
password = os.getenv('NEO4J_PASSWORD')
if not uri or not user or not password:
    raise ValueError("Missing one or more required environment variables: NEO4J_URI, NEO4J_USERNAME, NEO4J_PASSWORD")

neo4j_connection = Neo4jConnector(uri=uri, user=user, password=password)


def check_database_consistency():
    try:
        neo4j_sanity_check = Neo4jSanityCheck(neo4j_connection)
        results = neo4j_sanity_check.run_checks()
        return results
    except Exception as e:
        print(f"An error occurred during database consistency check: {str(e)}")

```


#### Grammar checker

```python
# language-tool-python

from mle_core.checkers import JsonGrammarChecker

def check_grammar_language_tool(json):
    result = {"success": True, "error": []}
    keywords = ['a','b']          # these are the words not to run the grammar checker on
    json_grammar_checker = JsonGrammarChecker(json, keywords)
    errors = json_grammar_checker.check_json_for_errors()
    return errors

# Prowriter
def grammar_check_prowriter(prompt):
    try:
        result = check_grammar_prowriter(prompt)
        return result
    except Exception as e:
        print(f"An error occurred: {e}")
        return False


```


## Contributing

Feel free to contribute by making a pull request. Please ensure your code follows the style guidelines and includes appropriate tests.

## License

This repository is licensed under the MIT License. See the [LICENSE](LICENSE) file for more information.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/RamailoTech/mle_core",
    "name": "mle-core",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": null,
    "author": "ML Experts Team",
    "author_email": "mvp@ramailo.tech",
    "download_url": "https://files.pythonhosted.org/packages/b2/87/1154e4e57f8b381cccc7b70af80d6f68018a8a2a0fe237878d29abbb78ce/mle_core-0.0.3.tar.gz",
    "platform": null,
    "description": "# MLE Core\n\n## Overview\n\nWelcome to the MLE Core repository, maintained by the ML Experts team. This repository contains core modules and utilities necessary for application development. It includes connectors for databases and language model services, a chat service for interacting with LLMs, and various utility functions to aid in development.\n\n## Directory Structure\n\n```\nmle_core/\n\u251c\u2500\u2500 __init__.py\n\u251c\u2500\u2500 chat/\n\u2502   \u251c\u2500\u2500 __init__.py\n\u2502   \u2514\u2500\u2500 chat_service.py\n\u251c\u2500\u2500 connectors/\n\u2502   \u251c\u2500\u2500 __init__.py\n\u2502   \u251c\u2500\u2500 base.py\n\u2502   \u251c\u2500\u2500 db/\n\u2502   \u2502   \u251c\u2500\u2500 __init__.py\n\u2502   \u2502   \u251c\u2500\u2500 postgres_connector.py\n\u2502   \u2502   \u2514\u2500\u2500 mongo_connector.py\n\u2502   \u2514\u2500\u2500 llm/\n\u2502       \u251c\u2500\u2500 __init__.py\n\u2502       \u251c\u2500\u2500 base.py\n\u2502       \u251c\u2500\u2500 openai_connector.py\n\u2502       \u2514\u2500\u2500 azure_connector.py\n\u251c\u2500\u2500 utils/\n\u2502   \u251c\u2500\u2500 __init__.py\n\u2502   \u251c\u2500\u2500 formatting.py\n\u2502   \u251c\u2500\u2500 logging.py\n\u2502   \u2514\u2500\u2500 response_handling.py\n\u251c\u2500\u2500 config.py\n\u2514\u2500\u2500 main.py\n```\n\n## Modules\n\n### Chat\n\nThe `chat` module provides a `ChatService` class that simplifies interaction with different language model (LLM) connectors.\n\n- `chat_service.py`: Contains the `ChatService` class for interacting with LLMs.\n\n### Connectors\n\nThe `connectors` module includes connectors for various databases and LLMs.\n\n- `base.py`: Defines the abstract base class for connectors.\n- `db/`: Contains database connectors.\n  - `postgres_connector.py`: Connector for PostgreSQL.\n  - `mongo_connector.py`: Connector for MongoDB.\n- `llm/`: Contains LLM connectors.\n  - `openai_connector.py`: Connector for OpenAI API.\n  - `azure_connector.py`: Connector for Azure AI API.\n\n### Utils\n\nThe `utils` module contains utility functions that are commonly used across different modules.\n\n- `formatting.py`: Functions for formatting prompts.\n- `logging.py`: Functions for setting up logging.\n- `response_handling.py`: Functions for handling LLM responses.\n\n### Config\n\nThe `config.py` file contains configuration logic to select the appropriate connectors based on the environment or other criteria.\n\n### Installing the Repository\n\nFirst, install the `prowritingaid-sdk` dependency for grammar checker.\n\n```sh\npip install git+https://github.com/prowriting/prowritingaid.python.git\n```\nThen, install our package\n```sh\npip install mle_core\n```\n\n## Usage\n\n### Setting Up Environment Variables\n\nEnsure you have the following environment variables set for database and LLM connectors:\n\nFor PostgreSQL:\n\n```\nDATABASE_USER=your_db_user\nDATABASE_PASSWORD=your_db_password\nDATABASE_HOST=your_db_host\nDATABASE_PORT=your_db_port\nDATABASE_NAME=your_db_name\n```\n\nFor MongoDB:\n\n```\nMONGO_URI=your_mongo_uri\nMONGO_DB_NAME=your_mongo_db_name\n```\n\nFor OpenAI:\n\n```\nOPENAI_API_KEY=your_openai_api_key\n```\n\nFor ChatAnthropic:\n```\nANTHROPIC_API_KEY=your_anthropic_api_key\n```\n\nFor Azure AI:\n\n```\nAZURE_ENDPOINT=your_azure_endpoint\nAZURE_API_KEY=your_azure_api_key\nAZURE_DEPLOYMENT_NAME=your_azure_deployment_name\n```\n\n### Using the Chat Service\n\n```python\nfrom mle_core.chat import ChatService\n\nimport asyncio\nfrom dotenv import load_dotenv\nfrom mle_core.chat.chat_service import ChatService\n\nload_dotenv()\n\n\nasync def main():\n    llm_type='openai' # or \"azure\" or \"anthropic\"\n    chat_service = ChatService(llm_type)\n\n    method = 'sync'  # or async\n    response_method = 'invoke'  # or \"batch\" or \"stream\"\n    system_message = 'You are a helpful assistant.'\n    user_message = 'What is the weather like today?'\n    model_name = \"gpt-3.5-turbo\"\n    input = {\n        \"system_message\": system_message,\n        \"user_message\": user_message\n    }\n    if method == \"sync\":\n        response = chat_service.get_sync_response(\n        response_method, \n        input, \n        model_name=model_name, \n        temperature=0.2, \n        max_tokens=1000, \n        is_structured=False, \n        pydantic_model=None)\n        print(response)\n\n    elif method == \"async\":\n        response = await chat_service.get_async_response(\n        response_method, \n        input, \n        model_name=model_name, \n        temperature=0.2, \n        max_tokens=1000,\n        is_structured=False, \n        pydantic_model=None)\n        print(response)\n\n\nasyncio.run(main())\n```\n\n### Using the Chat Service for structured output\n\n```python\nfrom mle_core.chat import ChatService\nimport asyncio\nfrom dotenv import load_dotenv\nfrom mle_core.chat.chat_service import ChatService\nfrom langchain_core.pydantic_v1 import BaseModel, Field\n\nload_dotenv()\n\n#create a pydnatic model\nclass Joke(BaseModel):\n    setup: str = Field(description=\"setup of the joke\")\n    punchline: str = Field(description=\"punchline of the joke\")\n\nasync def main():\n    llm_type='openai' # or \"azure\" or \"anthropic\"\n    chat_service = ChatService(llm_type)\n    \n    method = 'sync'  # or async\n    response_method = 'invoke'  # or \"batch\" or \"stream\"\n    system_message = 'You are a helpful assistant.'\n    user_message = 'What is the weather like today?'\n    model_name = \"gpt-3.5-turbo\"\n    input = {\n        \"system_message\": system_message,\n        \"user_message\": user_message\n    }\n    if method == \"sync\":\n        response = chat_service.get_sync_response(\n            response_method, \n            input, \n            model_name=model_name,\n            temperature=0.2, \n            max_tokens=1000, \n            is_structured=True, \n            pydantic_model=Joke)\n        print(response)\n\n    elif method == \"async\":\n        response = await chat_service.get_async_response(\n            response_method, \n            input, \n            model_name=model_name, \n            temperature=0.2, \n            max_tokens=1000,\n            is_structured=True, \n            pydantic_model=Joke)\n        print(response)\n\n\nasyncio.run(main())\n```\n\n\n### Note: Using Chat Service\n\n1. If response_method is \"batch\" the input should be list of input. \n\nExample: \n```\nsystem_message = 'You are a helpful assistant.'\ninput = [{'system_message': system_message, 'user_message': 'Tell me a bear joke.'}, {'system_message': system_message, 'user_message': 'Tell me a cat joke.'}]\n```\n\n\n### Using Database Connectors\n\n```python\nfrom mle_core.config import get_db_connector\n\ndef main():\n    db_type = \"postgres\"  # or \"mongo\"\n    db_connector = get_db_connector(db_type)\n    db_connection = db_connector.get_connection()\n    print(db_connection)\n\nif __name__ == \"__main__\":\n    main()\n```\n\n### Using Evaluators\n\n```python\nfrom mle_core.evaluators.tests_results_generation import Evaluator\n\ndef main():\n    input_file_path = 'test_case.json'\n    output_file_path = 'output_file.csv'\n    output_file_type = 'csv'\n\n    # assume your evaluator_function be f_eval_function \n    try:\n        evaluator = Evaluator(input_file_path,f_eval_function, output_file_path, output_file_type.lower())\n        evaluator.execute()\n        print(\"Processing completed successfully.\")\n    except Exception as e:\n        print(f\"An error occurred: {str(e)}\")\n\nif __name__ == '__main__':\n    main()\n\n```\n### Using Checkers\n\n#### Fact checker and hyperbole detector\n```python\nfrom mle_core.checkers import f_hyperbole_detector, f_fact_checker\n\n# The context basically refers to the knowledge base\n# question is generally the user prompt to the system\n# answer generally is the LLM generated output\n\nfact = f_fact_checker(question, context, answer)\nhyperbole = f_hyperbole_detector(question, context, answer)\n```\n\n#### Database consistency checker\n```python\nfrom mle_core.connectors.db import Neo4jConnector\nfrom mle_core.checkers import Neo4jSanityCheck\n\nuri = os.getenv('NEO4J_URI')\nuser = os.getenv('NEO4J_USERNAME')\npassword = os.getenv('NEO4J_PASSWORD')\nif not uri or not user or not password:\n    raise ValueError(\"Missing one or more required environment variables: NEO4J_URI, NEO4J_USERNAME, NEO4J_PASSWORD\")\n\nneo4j_connection = Neo4jConnector(uri=uri, user=user, password=password)\n\n\ndef check_database_consistency():\n    try:\n        neo4j_sanity_check = Neo4jSanityCheck(neo4j_connection)\n        results = neo4j_sanity_check.run_checks()\n        return results\n    except Exception as e:\n        print(f\"An error occurred during database consistency check: {str(e)}\")\n\n```\n\n\n#### Grammar checker\n\n```python\n# language-tool-python\n\nfrom mle_core.checkers import JsonGrammarChecker\n\ndef check_grammar_language_tool(json):\n    result = {\"success\": True, \"error\": []}\n    keywords = ['a','b']          # these are the words not to run the grammar checker on\n    json_grammar_checker = JsonGrammarChecker(json, keywords)\n    errors = json_grammar_checker.check_json_for_errors()\n    return errors\n\n# Prowriter\ndef grammar_check_prowriter(prompt):\n    try:\n        result = check_grammar_prowriter(prompt)\n        return result\n    except Exception as e:\n        print(f\"An error occurred: {e}\")\n        return False\n\n\n```\n\n\n## Contributing\n\nFeel free to contribute by making a pull request. Please ensure your code follows the style guidelines and includes appropriate tests.\n\n## License\n\nThis repository is licensed under the MIT License. See the [LICENSE](LICENSE) file for more information.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Core modules necessary during application development",
    "version": "0.0.3",
    "project_urls": {
        "Homepage": "https://github.com/RamailoTech/mle_core",
        "Issues": "https://github.com/RamailoTech/mle_core/issues"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "06a0e8d3e7116c31ee38c1e3ce5b935e398f9dda96c5848f493de9a291f99431",
                "md5": "725bce632061d497cb443f585aa72797",
                "sha256": "73dee14cce873f9a0069e42e833dd50f79e3d91bdb948cd2da1b2db58ec9d97b"
            },
            "downloads": -1,
            "filename": "mle_core-0.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "725bce632061d497cb443f585aa72797",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 24064,
            "upload_time": "2024-08-24T14:25:28",
            "upload_time_iso_8601": "2024-08-24T14:25:28.944945Z",
            "url": "https://files.pythonhosted.org/packages/06/a0/e8d3e7116c31ee38c1e3ce5b935e398f9dda96c5848f493de9a291f99431/mle_core-0.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b2871154e4e57f8b381cccc7b70af80d6f68018a8a2a0fe237878d29abbb78ce",
                "md5": "9a228a9f9ff557f6bb1bd23f0c005552",
                "sha256": "44c4443593f3e117e2f7515343a9232aa3ef550f1f0db5d400aaf9d2e74b8b30"
            },
            "downloads": -1,
            "filename": "mle_core-0.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "9a228a9f9ff557f6bb1bd23f0c005552",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 20011,
            "upload_time": "2024-08-24T14:25:31",
            "upload_time_iso_8601": "2024-08-24T14:25:31.320169Z",
            "url": "https://files.pythonhosted.org/packages/b2/87/1154e4e57f8b381cccc7b70af80d6f68018a8a2a0fe237878d29abbb78ce/mle_core-0.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-24 14:25:31",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "RamailoTech",
    "github_project": "mle_core",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "mle-core"
}
        
Elapsed time: 0.33097s