From 83c7c2e237f6db852ac0f623f32f422d1f5a2cdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Campion?= Date: Fri, 15 Nov 2024 16:28:16 +0100 Subject: [PATCH 1/2] Receipe for the cookbook to explain how to extract event details from message --- docs/cookbook/extract_event_details.md | 70 ++++++++++++++++++++++++++ mkdocs.yml | 1 + 2 files changed, 71 insertions(+) create mode 100644 docs/cookbook/extract_event_details.md diff --git a/docs/cookbook/extract_event_details.md b/docs/cookbook/extract_event_details.md new file mode 100644 index 000000000..facfec64f --- /dev/null +++ b/docs/cookbook/extract_event_details.md @@ -0,0 +1,70 @@ +This recipe demonstrates how to use the `outlines` library to extract structured event details from a text message. +We will extract the title, location, and start date and time from messages like the following: + +```plaintext +Hello Kitty, my grandmother will be here, I think it's better to postpone +our appointment to review math lessons to next Monday at 2pm at the same +place, 3 avenue des tanneurs, one hour will be enough see you 😘 +``` + +Let see how to extract the event details from the message. + +```python + +from datetime import datetime +from pydantic import Field, BaseModel +from outlines import models, generate + +# Load the model +model = models.mlxlm("mlx-community/Hermes-3-Llama-3.1-8B-8bit") + +# Define the event schema using Pydantic +class Event(BaseModel): + title: str = Field(description="title of the event") + location: str + start: datetime = Field(default=None, description="date of the event if available in iso format") + +# Get the current date and time +now = datetime.now().strftime("%d/%m/%Y %H:%M") + +# Define the prompt +prompt = f""" +Today's date and time are {datetime.now().strftime("%d/%m/%Y %H:%M")} +Given a user message, extract information of the event like date and time in iso format, location and title. +Here is the message: +""" + +# Sample message +message = """Hello Kitty, my grandmother will be here , I think it's better to postpone our +appointment to review math lessons to next Monday at 2pm at the same place, 3 avenue des tanneurs, I think that one hour will be enough +see you 😘 """ + +# Create the generator +generator = generate.json(model, Event) + +# Extract the event information +event = generator(prompt + message) + +# Print the current date and time +print("Today's date and time are", now) + +# Print the extracted event information in JSON format +print(event.json()) + +``` + +The output will be: + +```plaintext +Today's date and time are 15/11/2024 15:52 +``` + +and the extracted event information will be: + +```json +{ + "title":"Math lessons", + "location":"3 avenue des tanneurs", + "start":"2024-11-18T14:00:00Z" +} +``` diff --git a/mkdocs.yml b/mkdocs.yml index 7d48bf392..63145ecfd 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -123,6 +123,7 @@ nav: - Structured Generation from PDFs: cookbook/read-pdfs.md - Earnings reports to CSV: cookbook/earnings-reports.md - Digitizing receipts with vision models: cookbook/receipt-digitization.md + - Extract events details from text: cookbook/extract_event_details.md - Run on the cloud: - BentoML: cookbook/deploy-using-bentoml.md - Cerebrium: cookbook/deploy-using-cerebrium.md From dda211fd7fc1e918f1631c2e2bab7f749f842d2b Mon Sep 17 00:00:00 2001 From: Sebastien Campion Date: Sat, 16 Nov 2024 11:24:44 +0100 Subject: [PATCH 2/2] Cosmit and use of mkdocs snippets extension. Moving the python code in a dedicated file allow to benefit of the precommit hooks and homogenous codeMoving python code to a dedicated file lets you benefit from precommit hooks and homogeneous code In the future, it may also allow to test example dynamically gererate output to be sure that the documentation is consistant (like for the scikit learn documentation In the future, it could also be used to test examples and integrate output into the construction of documentation to ensure that it is valid and consistent (as with scikit learn documentation). ) --- docs/cookbook/extract_event_details.md | 54 +++++--------------------- docs/cookbook/extract_event_details.py | 46 ++++++++++++++++++++++ mkdocs.yml | 1 + 3 files changed, 56 insertions(+), 45 deletions(-) create mode 100644 docs/cookbook/extract_event_details.py diff --git a/docs/cookbook/extract_event_details.md b/docs/cookbook/extract_event_details.md index facfec64f..0f87d9586 100644 --- a/docs/cookbook/extract_event_details.md +++ b/docs/cookbook/extract_event_details.md @@ -7,64 +7,28 @@ our appointment to review math lessons to next Monday at 2pm at the same place, 3 avenue des tanneurs, one hour will be enough see you 😘 ``` -Let see how to extract the event details from the message. +Let see how to extract the event details from the message with the MLX +library dedicated to Apple Silicon processor (M series). ```python - -from datetime import datetime -from pydantic import Field, BaseModel -from outlines import models, generate - -# Load the model -model = models.mlxlm("mlx-community/Hermes-3-Llama-3.1-8B-8bit") - -# Define the event schema using Pydantic -class Event(BaseModel): - title: str = Field(description="title of the event") - location: str - start: datetime = Field(default=None, description="date of the event if available in iso format") - -# Get the current date and time -now = datetime.now().strftime("%d/%m/%Y %H:%M") - -# Define the prompt -prompt = f""" -Today's date and time are {datetime.now().strftime("%d/%m/%Y %H:%M")} -Given a user message, extract information of the event like date and time in iso format, location and title. -Here is the message: -""" - -# Sample message -message = """Hello Kitty, my grandmother will be here , I think it's better to postpone our -appointment to review math lessons to next Monday at 2pm at the same place, 3 avenue des tanneurs, I think that one hour will be enough -see you 😘 """ - -# Create the generator -generator = generate.json(model, Event) - -# Extract the event information -event = generator(prompt + message) - -# Print the current date and time -print("Today's date and time are", now) - -# Print the extracted event information in JSON format -print(event.json()) - +--8<-- "docs/cookbook/extract_event_details.py" ``` The output will be: ```plaintext -Today's date and time are 15/11/2024 15:52 +Today: Saturday 16 November 2024 and it's 10:55 ``` and the extracted event information will be: ```json { - "title":"Math lessons", + "title":"Math Review", "location":"3 avenue des tanneurs", - "start":"2024-11-18T14:00:00Z" + "start":"2024-11-22T14:00:00Z" } ``` + + +To find out more about this use case, we recommend the project developped by [Joseph Rudoler](https://x.com/JRudoler) the [ICS Generator](https://github.com/jrudoler/ics-generator) diff --git a/docs/cookbook/extract_event_details.py b/docs/cookbook/extract_event_details.py new file mode 100644 index 000000000..b51f8d921 --- /dev/null +++ b/docs/cookbook/extract_event_details.py @@ -0,0 +1,46 @@ +from datetime import datetime + +from pydantic import BaseModel, Field + +from outlines import generate, models + +# Load the model +model = models.mlxlm("mlx-community/Hermes-3-Llama-3.1-8B-8bit") + + +# Define the event schema using Pydantic +class Event(BaseModel): + title: str = Field(description="title of the event") + location: str + start: datetime = Field( + default=None, description="date of the event if available in iso format" + ) + + +# Get the current date and time +now = datetime.now().strftime("%A %d %B %Y and it's %H:%M") + +# Define the prompt +prompt = f""" +Today's date and time are {now} +Given a user message, extract information of the event like date and time in iso format, location and title. +If the given date is relative, think step by step to find the right date. +Here is the message: +""" + +# Sample message +message = """Hello Kitty, my grandmother will be here , I think it's better to postpone our +appointment to review math lessons to next Friday at 2pm at the same place, 3 avenue des tanneurs, I think that one hour will be enough +see you 😘 """ + +# Create the generator +generator = generate.json(model, Event) + +# Extract the event information +event = generator(prompt + message) + +# Print the current date and time +print(f"Today: {now}") + +# Print the extracted event information in JSON format +print(event.json()) diff --git a/mkdocs.yml b/mkdocs.yml index 63145ecfd..4888309b9 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -74,6 +74,7 @@ markdown_extensions: - pymdownx.emoji: emoji_index: !!python/name:material.extensions.emoji.twemoji emoji_generator: !!python/name:material.extensions.emoji.to_svg + - pymdownx.snippets: extra_css: