Skip to main content

A package for translating Natural language using NLP processing with Ollama and Gemini into actions.

Project description

NLP Interpreter Documentation

Description

The NLP Interpreter is a system designed to leverage the power of natural language processing (NLP) to classify textual data into user-defined categories. It offers a flexible and dynamic approach to text analysis, allowing users to specify the categories or actions they are interested in. This system is particularly useful for applications that require the categorization of text into predefined options, enhancing the automation and understanding of textual data.

Installation

  • Install the library
pip3 install nlp_interpreter
  • Add the GEMINI API key to the .env file, if you want to use the GEMINI API for text processing and classification.
GEMINI_API_KEY=your_gemini_api_key

Usage

  • The output of all the functions would be json

  • NLP Processing Function to extract custom parameters from the user input.

def nlp_process(data):
    json_schema = '''{  "type": "object",  "properties": {    "name": {      "type": "string"  },    "emotion": {      "type": "string"    }  },  "required": [    "name",    "emotion"  ]}'''
    return process_prompt(data, model_name = "llama3", json_schema = json_schema)
  • NLP Processing Function with GEMINI API
def nlp_process(data):
    json_schema = '''{  "type": "object",  "properties": {    "name": {      "type": "string"  },    "emotion": {      "type": "string"    }  },  "required": [    "name",    "emotion"  ]}'''
    return process_prompt(data, model_name = "gemini", json_schema = json_schema)
  • NLP Classification Function to classify the user input into a single category.
def nlp_classify(data):
    options = ["create", "delete", "edit"]
    return classify_prompt(data, model_name = "llama3", options = options)
  • NLP Classification for Multiple Categories Function to classify the user input into multiple categories.
def nlp_classify_into_multiple(data):
    options = ["create", "delete", "edit"]
    return classify_multiple_prompt(data, model_name = "llama3", options = options)

Practical Use Case Example: Smart Home Assistant

Scenario: Enhancing a Smart Home Assistant with NLP Interpretation

Background: Imagine a smart home ecosystem, HomeSmart, that allows users to control various IoT devices through a central assistant. Users can give voice commands to perform actions like adjusting the thermostat, turning lights on or off, playing music, or getting weather updates. However, the assistant's current command recognition system is rigid, requiring users to memorize specific phrases for each action, leading to a less user-friendly experience.

Solution: To make the assistant more intuitive and user-friendly, HomeSmart decides to integrate the NLP Interpreter. This integration aims to allow the assistant to understand commands given in natural language, eliminating the need for users to remember specific phrases and making the interaction more conversational.

Implementation:

  1. Command Definition: HomeSmart defines a set of possible actions that the assistant can perform, such as "Adjust Temperature", "Control Lights", "Play Music", and "Weather Update".
  2. NLP Interpreter Integration: The assistant is integrated with the NLP Interpreter, which is configured to classify user commands into one of the predefined actions based on natural language input.
  3. Dynamic Interaction: When a user gives a command, the assistant uses the NLP Interpreter to understand the intent and context of the command, mapping it to the corresponding action.
  4. Action Execution: Once the command is classified, the assistant executes the associated action, such as adjusting the thermostat or turning on the lights.

Benefits:

  • Improved User Experience: Users can interact with the assistant in a more natural and intuitive way, without needing to remember specific command phrases.
  • Flexibility: The system can easily adapt to new commands or actions as the product evolves, thanks to the dynamic classification capabilities of the NLP Interpreter.
  • Scalability: The NLP Interpreter can handle a wide range of commands and variations in natural language, making the system robust and scalable.
  • Efficiency: Automating command interpretation reduces the need for manual updates to the command recognition system, saving time and resources.

Outcome: After integrating the NLP Interpreter, HomeSmart notices a significant improvement in user satisfaction. Users find the assistant more responsive and easier to use, leading to increased engagement and usage of the smart home ecosystem. The assistant's ability to understand a wide range of natural language commands also reduces user frustration and enhances the overall smart home experience.

This practical use case demonstrates how the NLP Interpreter can be used to parse natural language input and provide applications with the context needed to take appropriate actions, acting as a bridge between human language and machine-executable commands.

Use Cases

  • Dynamic Text Classification: Allows users to define their own categories or options for classification, making the system highly adaptable to various needs.
  • GEMINI API Support: Offers an alternative to the Ollama model for users who prefer or require GEMINI API for text processing and classification.
  • Content Moderation: Analyze user-generated content to identify and categorize based on predefined criteria, aiding in moderation efforts.
  • Data Extraction: Extract specific information from unstructured text inputs for data analysis or entry into structured databases.

Functionalities

  • User-defined Classification: Classifies text based on categories or options provided by the user.
  • Multiple Model Support: Supports both the GEMINI API and the Ollama model for text classification, providing flexibility in choosing the underlying NLP technology.
  • Error Handling: Implements robust error handling to ensure accurate and reliable classification results.

Advantages

  • Customizability: Users can define their own categories, making the system highly adaptable.
  • Flexibility: Supports multiple NLP backends (GEMINI API and Ollama model), allowing users to choose the one that best fits their needs.
  • Ease of Integration: The Flask-based API facilitates easy integration into existing systems or workflows.

Error Handling

The system includes comprehensive error handling to ensure the accuracy of classifications. It checks the NLP model's response to ensure it aligns with the user-defined criteria. In cases where the response does not meet these criteria, the system can modify the prompt and retry or provide informative errors, enhancing reliability even in complex scenarios.

Types of Cases It Can Handle:

  • Valid Responses: Directly returns the model's response if it aligns with the user-defined criteria.
  • Invalid Responses: Attempts to guide the model towards a valid output by modifying the prompt and retrying.
  • Error Conditions: Provides detailed error messages if issues arise during model initialization or if the response does not meet the criteria after retries.

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

nlp_interpreter-1.0.0.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

nlp_interpreter-1.0.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file nlp_interpreter-1.0.0.tar.gz.

File metadata

  • Download URL: nlp_interpreter-1.0.0.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.12

File hashes

Hashes for nlp_interpreter-1.0.0.tar.gz
Algorithm Hash digest
SHA256 6b3f3c1039964f62e80cff78b2cbdd8279d027e8fefbf1227831d60a0ce56449
MD5 c0df1da0fe67be073759aa1768826ab7
BLAKE2b-256 b1fd796f7f821ffe06609ddaca1885ed704f2e1d9fdcf0943a8dea1b2ca9959d

See more details on using hashes here.

File details

Details for the file nlp_interpreter-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for nlp_interpreter-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 767bbfcc0e212657d1994ddefbca8608eefdb293edadd2e0de3db4acd934e9c3
MD5 28af3b3ae20185a70f5927f9a3a60d6c
BLAKE2b-256 fad03f095401c028e2316736ab132c7024ab5739f48469ec7e6d54487b970094

See more details on using hashes here.

Supported by

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