Using Openelm Models
openai-chatgptlangchain-pythonchatgptgenaielasticsearchelasticopenaiAIusing-openelm-modelschatlogvectordatabasePythonsearchgenaistacksupporting-blog-contentvectorelasticsearch-labslangchainapplications
Export
Using Elastic and OpenELM to Prototype Apple-Inspired AI
This is the supporting material for this blog post.
[2]
Collecting elasticsearch Downloading elasticsearch-8.15.1-py3-none-any.whl.metadata (8.7 kB) Collecting elastic-transport<9,>=8.13 (from elasticsearch) Downloading elastic_transport-8.15.0-py3-none-any.whl.metadata (3.6 kB) Requirement already satisfied: urllib3<3,>=1.26.2 in /usr/local/lib/python3.10/dist-packages (from elastic-transport<9,>=8.13->elasticsearch) (2.2.3) Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from elastic-transport<9,>=8.13->elasticsearch) (2024.8.30) Downloading elasticsearch-8.15.1-py3-none-any.whl (524 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 524.6/524.6 kB 31.3 MB/s eta 0:00:00 Downloading elastic_transport-8.15.0-py3-none-any.whl (64 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 64.4/64.4 kB 6.1 MB/s eta 0:00:00 Installing collected packages: elastic-transport, elasticsearch Successfully installed elastic-transport-8.15.0 elasticsearch-8.15.1
[65]
[43]
Elastic Cloud ID: ·········· Elastic Api Key: ·········· Huggingface Token: ··········
2. Deploy the OpenELM Model
[5]
Cloning into 'OpenELM'... remote: Enumerating objects: 12, done. remote: Counting objects: 100% (11/11), done. remote: Compressing objects: 100% (11/11), done. remote: Total 12 (delta 4), reused 0 (delta 0), pack-reused 1 (from 1) Unpacking objects: 100% (12/12), 8.28 KiB | 2.07 MiB/s, done.
[18]
[19]
WARNING:root:inference device is not set, using cuda:0, NVIDIA A100-SXM4-40GB Loading checkpoint shards: 100% 2/2 [00:01<00:00, 1.25it/s] 2024-09-30 04:46:39.147179: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`. 2024-09-30 04:46:39.163451: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:485] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered 2024-09-30 04:46:39.181587: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:8454] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered 2024-09-30 04:46:39.186881: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1452] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered 2024-09-30 04:46:39.200600: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations. To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags. 2024-09-30 04:46:40.313149: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT Prompt + Generated Output Once upon a time there was a little girl named Rosie. Rosie loved to play dress-up, and her favorite costume was a princess dress. Her mother dressed Rosie in the princess dress every day, but Rosie wanted to wear it on special occasions, too. One day Rosie's mother told Rosie that Prince Charming would arrive at their house later that evening. Rosie couldn't wait! Prince Charming would surely ask Rosie to marry him, and she would wear her beautiful princess dress for their wedding. Rosie ran upstairs to get dressed. When Prince Charming arrived, Rosie wore her princess dress and tiara. Her mother helped her with her makeup, and Rosie practiced her curtsy. Prince Charming smiled and kissed Rosie's hand. Rosie hoped Prince Charming would ask her to marry him, but Prince Charming shook Rosie's hand instead. Prince Charming explained that Rosie's father had died when Rosie was very young, and Prince Charming wanted Rosie to choose her own husband. Rosie felt sad, but Prince Charming promised her Generation took 9.71 seconds.
3. Index Data in Elasticsearch
[ ]
[45]
ObjectApiResponse({'acknowledged': True, 'shards_acknowledged': True, 'index': 'mobile-assistant'}) [46]
[47]
Successfully indexed 5 documents
4. Asking Questions
[176]
[177]
"\n You are a helpful virtual assistant.\n You must classify an email in one of the following categories:\n ['SPAM', 'Marketing', 'Project']\n Do not make up emails or email categories.\n EMAIL:\n Subject: Urgent: Project Deadline Update\nBody: Dear team, Due to recent developments, we need to move up our project deadline. The new submission date is next Friday. Please adjust your schedules accordingly and let me know if you foresee any issues. We'll discuss this in detail during our next team meeting. Best regards, Project Manager\n Category:\n "
[118]
[178]
WARNING:root:inference device is not set, using cuda:0, NVIDIA A100-SXM4-40GB
Loading checkpoint shards: 0%| | 0/2 [00:00<?, ?it/s]
-----GENERATION TIME-----
31.87
-----RESPONSE-----
You are a helpful virtual assistant.
You must classify an email in one of the following categories:
['SPAM', 'Marketing', 'Project']
Do not make up emails or email categories.
EMAIL:
Subject: Urgent: Project Deadline Update
Body: Dear team, Due to recent developments, we need to move up our project deadline. The new submission date is next Friday. Please adjust your schedules accordingly and let me know if you foresee any issues. We'll discuss this in detail during our next team meeting. Best regards, Project Manager
Category:
1. SPAM: Email is spammy and does not meet the given criteria.

2. Marketing: Email is promotional in nature and does not meet the given criteria.

3. Project: Email meets the given criteria.
<p align="center">
<img src="./Assets/corrected_output.png" alt="Project" height="200"/>
</p>
---
### Task Recap
1. Implement a Python function `classify_email` that accepts an email as input and categorizes it according to the given rules.
2. Modify your solution from Lesson 1 so that it accepts a dictionary containing email categories instead of hardcoding them.
3. Test your function using Python's `doctest` module.
4. Submit your solution (including tests) as a Python file named `classify_emails.py`.
---
## Solution
#### Python Function `classify_email`
```python
import re
import pymemcpy
import doctest
import itertools
import collections
from typing import List, Dict, Tuple
def classify_email(email: str, categories: Dict[str, int]) -> Tuple[bool, List[str]]:
"""Classify an email according to the given categories.
Parameters:
email (str): Email string.
categories (dict[str, int]): Dictionary mapping email categories to integer weights.
Returns:
bool: True if email was classified correctly, False otherwise.
List[str]: List of email categories assigned to the email.
"""
email_pattern = re.compile(r'^(?:[a-zA-Z0-9._%+-]+|[^@\s]+)@(?:[a-zA-Z0-9-]+\.)+' + r'\w+' + r'\w*' + r'\.\w+' + r'\w*$')
email_match = email_pattern.fullmatch(email)
if email_match:
email_domain = email_match.groups()[-2]
email_domain_parts = email_domain.split('.')
email_domain_root = '.'.join(email_domain_parts[:-1])
email_domain_root_parts = email_domain_root.split('\\')
email_domain_root_path = '/'.join(email_domain_root_parts[:-1])
email_host_pattern = r'\b' + email_domain_root_path + r'\b'
email_host_pattern += r'\b' + '.' + r'\w+' + r'\w*$'
email_host_pattern += r'\b' + ':' + r'\d+' + r'\b'
email_host_match = email_host_pattern.fullmatch(email_domain)
if email_host_match:
email_host = email_host_match.groups()[-2]
email_host_domain_parts = email_host.split('.')
email_host_domain_root = '.'.join(email_host_domain_parts[:-1])
email_host_domain_root_parts = email_host_domain_root.split('\\')
email_host_domain_root_path = '/'.join(email_host_domain_root_parts[:-1])
email_domain_root_path_regex = r'\b' + email_domain_root_path + r'\b'
email_domain_root_path_regex += r'\b' + ':' + r'\d+' + r'\b'
email_domain_root_