Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[63415] Neural API support for Python SDK #163

Merged

Conversation

mrashed-dev
Copy link
Collaborator

@mrashed-dev mrashed-dev commented Jul 17, 2021

Description

This PR brings support for the Nylas Neural API. The Neural API exposes endpoints that perform intelligent analysis on items in your Nylas account. This change will enable users to access the features of the Nylas Neural API and provides some utility functions to help improve the user's workflow.

Usage

To use Sentiment Analysis:

# To perform sentiment analysis on a message, pass in the list of message ID:
message_analysis = nylas.neural.sentiment_analysis_message([MESSAGE_ID])

# To perform sentiment analysis on just text, pass in a string:
text_analysis = nylas.neural.sentiment_analysis_text("Hi, thank you so much for reaching out! We can catch up tomorrow.")

To use Signature Extraction:

const signature = nylas.neural.extract_signature([MESSAGE_ID])

# The method also accepts two optional parameters
# parseContact, a boolean for whether Nylas should parse the contact from the signature (API defaults to true)
# options, an object of options that can be enabled for the Neural endpoint, of type NeuralMessageOptions:
options = NeuralMessageOptions(
    ignore_links=False,
    ignore_images=False,
    ignore_tables=False,
    remove_conclusion_phrases=False,
    images_as_markdowns=False
)

signature = nylas.neural.extract_signature([MESSAGE_ID], true, options)

and to parse the contact and convert it to the standard Nylas contact object:

contact = signature[0].contacts.to_contact_object()

To use Clean Conversations:

convo = nylas.neural.clean_conversation([MESSAGE_ID])

# You can also pass in an object of options that can be enabled for the Neural endpoint, of type NeuralMessageOptions
convo = nylas.neural.clean_conversation([MESSAGE_ID], options);

and to extract images from the result:

convo[0].extract_images()

To use Optical Character Recognition:

ocr = nylas.neural.ocr_request(FILE_ID)

# This endpoint also supports a second, optional parameter for an array specifying the pages that the user wants analyzed:
ocr = nylas.neural.ocr_request(FILE_ID, [2, 3])

To use Categorizer

cat = nylas.neural.categorize([MESSAGE_ID])

# You can also send a request to recategorize the message:
cat = cat[0].recategorize("conversation")

License

I confirm that this contribution is made under the terms of the MIT license and that I have the authority necessary to make this contribution on behalf of its copyright owner.

…eate

In preparation for the Neural API. Many Neural objects are objects that extend an existing class (such as `Message`), but unlike `Drafts` the server payload still denotes the `object` key as `message` instead of - say 'signature'
We do this because the Neural API series of endpoints don't support the same operations as the typical Nylas API objects. Thus this allows us to extend the minimum required functionality shared by both the typical Nylas API objects and Neural API objects
@shortcut-integration
Copy link

This pull request has been linked to Clubhouse Story #63415: Neural API support for Python SDK.

object_type
and object_type != cls_object_type
and object_type != "account"
and not _is_subclass(cls, object_type)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many Neural objects are objects that extend an existing class (such as Message), but unlike Drafts the server payload still denotes the object key as message instead of - say 'signature'. To prevent this from breaking existing functionality, we check if the class being created is the subclass of the inbound payload's object value.

return False


class RestfulModel(dict):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do this because the Neural API series of endpoints don't support the same operations as the typical Nylas API objects. Thus this allows us to extend the minimum required functionality shared by both the typical Nylas API objects and Neural API objects.


result = _validate(response).json()
# The Neural API will always return only one item
# but sometimes it returns it in the form of a 'singleton' array

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the restriction of the python SDK? conversation and signature can actually return list of objects.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the endpoints get more than 1 message id, it will return multiple messages

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I did not know that. I thought from the documentation it was always 1:1. I will update the code to reflect this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@egginsect I have pushed a change to reflect your comments. Does the same apply to OCR? Or does OCR only support one file ID at a time? Thanks!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it takes only 1 file_id per request

Copy link
Contributor

@jhatch28 jhatch28 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't review for potential bugs, just for the interface, which looks great

@mrashed-dev mrashed-dev merged commit aad9b8b into main Jul 21, 2021
@mrashed-dev mrashed-dev deleted the mostafarashed/ch63415/neural-api-support-for-python-sdk branch July 21, 2021 13:49
mrashed-dev added a commit that referenced this pull request Jul 21, 2021
New `nylas` v5.0.0 release bringing in the following additions:
* Add support for the Nylas Neural API (#163)
* Add `metadata` support (#152)
* Add new Room Resource fields  (#156)
* Add `Nylas-API-Version` header support (#157, #151)

as well as the following changes:
* Transitioned from `app_id` and `app_secret` naming to `client_id` and `client_secret` (#159, #86)
* Fix adding a tracking object to an existing `draft` (#153)
* Fix issue when converting offset-aware `datetime` objects to `timestamp` (#154, #143)
* Fix `limit` value in filter not being used when making `.all()` call (#155)
* Fix `from_` field set by attribute on draft ignored (#162, #160)
* Remove `bumpversion` from a required dependency to an extra dependency (#158, #144)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants