Content Writing Example Rewrite (#3142)
This commit is contained in:
@@ -227,7 +227,7 @@
|
|||||||
"examples/personal-travel-assistant",
|
"examples/personal-travel-assistant",
|
||||||
"examples/llama-index-mem0",
|
"examples/llama-index-mem0",
|
||||||
"examples/chrome-extension",
|
"examples/chrome-extension",
|
||||||
"examples/document-writing",
|
"examples/memory-guided-content-writing",
|
||||||
"examples/multimodal-demo",
|
"examples/multimodal-demo",
|
||||||
"examples/personalized-deep-research",
|
"examples/personalized-deep-research",
|
||||||
"examples/mem0-agentic-tool",
|
"examples/mem0-agentic-tool",
|
||||||
|
|||||||
@@ -1,103 +1,133 @@
|
|||||||
---
|
---
|
||||||
title: Document Editing with Mem0
|
title: Memory-Guided Content Writing
|
||||||
---
|
---
|
||||||
<Snippet file="security-compliance.mdx" />
|
<Snippet file="security-compliance.mdx" />
|
||||||
|
|
||||||
This guide demonstrates how to leverage **Mem0** to edit documents efficiently, ensuring they align with your unique writing style and preferences.
|
This guide demonstrates how to leverage **Mem0** to streamline content writing by applying your unique writing style and preferences using persistent memory.
|
||||||
|
|
||||||
## **Why Use Mem0?**
|
## Why Use Mem0?
|
||||||
|
|
||||||
By integrating Mem0 into your workflow, you can streamline your document editing process with:
|
Integrating Mem0 into your writing workflow helps you:
|
||||||
|
|
||||||
1. **Persistent Writing Preferences**: Mem0 stores and recalls your style preferences, ensuring consistency across all documents.
|
1. **Store persistent writing preferences** ensuring consistent tone, formatting, and structure.
|
||||||
2. **Automated Enhancements**: Your stored preferences guide document refinements, making edits seamless and efficient.
|
2. **Automate content refinement** by retrieving preferences when rewriting or reviewing content.
|
||||||
3. **Scalability & Reusability**: Your writing style can be applied to multiple documents, saving time and effort.
|
3. **Scale your writing style** so it applies consistently across multiple documents or sessions.
|
||||||
|
|
||||||
---
|
## Setup
|
||||||
## **Setup**
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
import os
|
import os
|
||||||
|
from openai import OpenAI
|
||||||
from mem0 import MemoryClient
|
from mem0 import MemoryClient
|
||||||
|
|
||||||
# Set up Mem0 client
|
|
||||||
os.environ["MEM0_API_KEY"] = "your-mem0-api-key"
|
os.environ["MEM0_API_KEY"] = "your-mem0-api-key"
|
||||||
client = MemoryClient()
|
os.environ["OPENAI_API_KEY"] = "your-openai-api-key"
|
||||||
|
|
||||||
|
|
||||||
|
# Set up Mem0 and OpenAI client
|
||||||
|
client = MemoryClient()
|
||||||
|
openai = OpenAI()
|
||||||
|
|
||||||
# Define constants
|
|
||||||
USER_ID = "content_writer"
|
USER_ID = "content_writer"
|
||||||
RUN_ID = "smart_editing_session"
|
RUN_ID = "smart_editing_session"
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
|
||||||
## **Storing Your Writing Preferences in Mem0**
|
## **Storing Your Writing Preferences in Mem0**
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def store_writing_preferences():
|
def store_writing_preferences():
|
||||||
"""Store your writing preferences in Mem0."""
|
"""Store your writing preferences in Mem0."""
|
||||||
|
|
||||||
# Define writing preferences
|
|
||||||
preferences = """My writing preferences:
|
preferences = """My writing preferences:
|
||||||
1. Use headings and sub-headings for structure.
|
1. Use headings and sub-headings for structure.
|
||||||
2. Keep paragraphs concise (8-10 sentences max).
|
2. Keep paragraphs concise (8–10 sentences max).
|
||||||
3. Incorporate specific numbers and statistics.
|
3. Incorporate specific numbers and statistics.
|
||||||
4. Provide concrete examples.
|
4. Provide concrete examples.
|
||||||
5. Use bullet points for clarity.
|
5. Use bullet points for clarity.
|
||||||
6. Avoid jargon and buzzwords."""
|
6. Avoid jargon and buzzwords."""
|
||||||
|
|
||||||
# Store preferences in Mem0
|
messages = [
|
||||||
preference_message = [
|
{"role": "user", "content": "Here are my writing style preferences."},
|
||||||
{"role": "user", "content": "Here are my writing style preferences"},
|
|
||||||
{"role": "assistant", "content": preferences}
|
{"role": "assistant", "content": preferences}
|
||||||
]
|
]
|
||||||
|
|
||||||
response = client.add(preference_message, user_id=USER_ID, run_id=RUN_ID, metadata={"type": "preferences", "category": "writing_style"})
|
response = client.add(
|
||||||
|
messages,
|
||||||
print("Writing preferences stored successfully.")
|
user_id=USER_ID,
|
||||||
|
run_id=RUN_ID,
|
||||||
|
metadata={"type": "preferences", "category": "writing_style"}
|
||||||
|
)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
## **Editing Content Using Stored Preferences**
|
||||||
## **Editing Documents with Mem0**
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def edit_document_based_on_preferences(original_content):
|
def apply_writing_style(original_content):
|
||||||
"""Edit a document using Mem0-based stored preferences."""
|
"""Use preferences stored in Mem0 to guide content rewriting."""
|
||||||
|
|
||||||
# Retrieve stored preferences
|
results = client.search(
|
||||||
query = "What are my writing style preferences?"
|
query="What are my writing style preferences?",
|
||||||
preferences_results = client.search(query, user_id=USER_ID, run_id=RUN_ID)
|
version="v2",
|
||||||
|
filters={
|
||||||
if not preferences_results:
|
"AND": [
|
||||||
print("No writing preferences found.")
|
{
|
||||||
|
"user_id": USER_ID
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"run_id": RUN_ID
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
if not results:
|
||||||
|
print("No preferences found.")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Extract preferences
|
preferences = "\n".join(r["memory"] for r in results)
|
||||||
preferences = ' '.join(memory["memory"] for memory in preferences_results)
|
|
||||||
|
system_prompt = f"""
|
||||||
# Apply stored preferences to refine the document
|
You are a writing assistant.
|
||||||
edited_content = f"Applying stored preferences:\n{preferences}\n\nEdited Document:\n{original_content}"
|
|
||||||
|
Apply the following writing style preferences to improve the user's content:
|
||||||
return edited_content
|
|
||||||
|
Preferences:
|
||||||
|
{preferences}
|
||||||
|
"""
|
||||||
|
|
||||||
|
messages = [
|
||||||
|
{"role": "system", "content": system_prompt},
|
||||||
|
{"role": "user", "content": f"""Original Content:
|
||||||
|
{original_content}"""}
|
||||||
|
]
|
||||||
|
|
||||||
|
response = openai.chat.completions.create(
|
||||||
|
model="gpt-4o-mini",
|
||||||
|
messages=messages
|
||||||
|
)
|
||||||
|
clean_response = response.choices[0].message.content.strip()
|
||||||
|
|
||||||
|
return clean_response
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
## **Complete Workflow: Content Editing**
|
||||||
## **Complete Workflow: Document Editing**
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def document_editing_workflow(content):
|
def content_writing_workflow(content):
|
||||||
"""Automated workflow for editing a document based on writing preferences."""
|
"""Automated workflow for editing a document based on writing preferences."""
|
||||||
|
|
||||||
# Step 1: Store writing preferences (if not already stored)
|
# Store writing preferences (if not already stored)
|
||||||
store_writing_preferences()
|
store_writing_preferences() # Ideally done once, or with a conditional check
|
||||||
|
|
||||||
# Step 2: Edit the document with Mem0 preferences
|
# Edit the document with Mem0 preferences
|
||||||
edited_content = edit_document_based_on_preferences(content)
|
edited_content = apply_writing_style(content)
|
||||||
|
|
||||||
if not edited_content:
|
if not edited_content:
|
||||||
return "Failed to edit document."
|
return "Failed to edit document."
|
||||||
|
|
||||||
# Step 3: Display results
|
# Display results
|
||||||
print("\n=== ORIGINAL DOCUMENT ===\n")
|
print("\n=== ORIGINAL DOCUMENT ===\n")
|
||||||
print(content)
|
print(content)
|
||||||
|
|
||||||
@@ -107,7 +137,6 @@ def document_editing_workflow(content):
|
|||||||
return edited_content
|
return edited_content
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
|
||||||
## **Example Usage**
|
## **Example Usage**
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@@ -125,10 +154,9 @@ We plan to launch the campaign in July and continue through September.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
# Run the workflow
|
# Run the workflow
|
||||||
result = document_editing_workflow(original_content)
|
result = content_writing_workflow(original_content)
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
|
||||||
## **Expected Output**
|
## **Expected Output**
|
||||||
|
|
||||||
Your document will be transformed into a structured, well-formatted version based on your preferences.
|
Your document will be transformed into a structured, well-formatted version based on your preferences.
|
||||||
@@ -182,4 +210,10 @@ This proposal outlines our strategy for the Q3 marketing campaign. We aim to sig
|
|||||||
We believe this strategy will effectively increase our market share. To achieve these goals, we need your support and collaboration. Let’s work together to make this campaign a success. Please review the proposal and provide your feedback by the end of the week.
|
We believe this strategy will effectively increase our market share. To achieve these goals, we need your support and collaboration. Let’s work together to make this campaign a success. Please review the proposal and provide your feedback by the end of the week.
|
||||||
```
|
```
|
||||||
|
|
||||||
Mem0 creates a seamless, intelligent document editing experience—perfect for content creators, technical writers, and businesses alike!
|
Mem0 enables a seamless, intelligent content-writing workflow, perfect for content creators, marketers, and technical writers looking to scale their personal tone and structure across work.
|
||||||
|
|
||||||
|
## Help & Resources
|
||||||
|
|
||||||
|
- [Mem0 Platform](https://app.mem0.ai/)
|
||||||
|
|
||||||
|
<Snippet file="get-help.mdx" />
|
||||||
Reference in New Issue
Block a user