Skip to content

Commit 1a8d175

Browse files
authored
Content Writing Example Rewrite (#3142)
1 parent 0560d87 commit 1a8d175

File tree

2 files changed

+87
-53
lines changed

2 files changed

+87
-53
lines changed

docs/docs.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@
227227
"examples/personal-travel-assistant",
228228
"examples/llama-index-mem0",
229229
"examples/chrome-extension",
230-
"examples/document-writing",
230+
"examples/memory-guided-content-writing",
231231
"examples/multimodal-demo",
232232
"examples/personalized-deep-research",
233233
"examples/mem0-agentic-tool",

docs/examples/document-writing.mdx renamed to docs/examples/memory-guided-content-writing.mdx

Lines changed: 86 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,133 @@
11
---
2-
title: Document Editing with Mem0
2+
title: Memory-Guided Content Writing
33
---
44
<Snippet file="security-compliance.mdx" />
55

6-
This guide demonstrates how to leverage **Mem0** to edit documents efficiently, ensuring they align with your unique writing style and preferences.
6+
This guide demonstrates how to leverage **Mem0** to streamline content writing by applying your unique writing style and preferences using persistent memory.
77

8-
## **Why Use Mem0?**
8+
## Why Use Mem0?
99

10-
By integrating Mem0 into your workflow, you can streamline your document editing process with:
10+
Integrating Mem0 into your writing workflow helps you:
1111

12-
1. **Persistent Writing Preferences**: Mem0 stores and recalls your style preferences, ensuring consistency across all documents.
13-
2. **Automated Enhancements**: Your stored preferences guide document refinements, making edits seamless and efficient.
14-
3. **Scalability & Reusability**: Your writing style can be applied to multiple documents, saving time and effort.
12+
1. **Store persistent writing preferences** ensuring consistent tone, formatting, and structure.
13+
2. **Automate content refinement** by retrieving preferences when rewriting or reviewing content.
14+
3. **Scale your writing style** so it applies consistently across multiple documents or sessions.
1515

16-
---
17-
## **Setup**
16+
## Setup
1817

1918
```python
2019
import os
20+
from openai import OpenAI
2121
from mem0 import MemoryClient
2222

23-
# Set up Mem0 client
2423
os.environ["MEM0_API_KEY"] = "your-mem0-api-key"
24+
os.environ["OPENAI_API_KEY"] = "your-openai-api-key"
25+
26+
27+
# Set up Mem0 and OpenAI client
2528
client = MemoryClient()
29+
openai = OpenAI()
2630

27-
# Define constants
2831
USER_ID = "content_writer"
2932
RUN_ID = "smart_editing_session"
3033
```
3134

32-
---
3335
## **Storing Your Writing Preferences in Mem0**
3436

3537
```python
3638
def store_writing_preferences():
3739
"""Store your writing preferences in Mem0."""
3840

39-
# Define writing preferences
4041
preferences = """My writing preferences:
4142
1. Use headings and sub-headings for structure.
42-
2. Keep paragraphs concise (8-10 sentences max).
43+
2. Keep paragraphs concise (810 sentences max).
4344
3. Incorporate specific numbers and statistics.
4445
4. Provide concrete examples.
4546
5. Use bullet points for clarity.
4647
6. Avoid jargon and buzzwords."""
4748

48-
# Store preferences in Mem0
49-
preference_message = [
50-
{"role": "user", "content": "Here are my writing style preferences"},
49+
messages = [
50+
{"role": "user", "content": "Here are my writing style preferences."},
5151
{"role": "assistant", "content": preferences}
5252
]
53-
54-
response = client.add(preference_message, user_id=USER_ID, run_id=RUN_ID, metadata={"type": "preferences", "category": "writing_style"})
55-
56-
print("Writing preferences stored successfully.")
53+
54+
response = client.add(
55+
messages,
56+
user_id=USER_ID,
57+
run_id=RUN_ID,
58+
metadata={"type": "preferences", "category": "writing_style"}
59+
)
60+
5761
return response
5862
```
5963

60-
---
61-
## **Editing Documents with Mem0**
64+
## **Editing Content Using Stored Preferences**
6265

6366
```python
64-
def edit_document_based_on_preferences(original_content):
65-
"""Edit a document using Mem0-based stored preferences."""
66-
67-
# Retrieve stored preferences
68-
query = "What are my writing style preferences?"
69-
preferences_results = client.search(query, user_id=USER_ID, run_id=RUN_ID)
70-
71-
if not preferences_results:
72-
print("No writing preferences found.")
67+
def apply_writing_style(original_content):
68+
"""Use preferences stored in Mem0 to guide content rewriting."""
69+
70+
results = client.search(
71+
query="What are my writing style preferences?",
72+
version="v2",
73+
filters={
74+
"AND": [
75+
{
76+
"user_id": USER_ID
77+
},
78+
{
79+
"run_id": RUN_ID
80+
}
81+
]
82+
},
83+
)
84+
85+
if not results:
86+
print("No preferences found.")
7387
return None
74-
75-
# Extract preferences
76-
preferences = ' '.join(memory["memory"] for memory in preferences_results)
77-
78-
# Apply stored preferences to refine the document
79-
edited_content = f"Applying stored preferences:\n{preferences}\n\nEdited Document:\n{original_content}"
80-
81-
return edited_content
88+
89+
preferences = "\n".join(r["memory"] for r in results)
90+
91+
system_prompt = f"""
92+
You are a writing assistant.
93+
94+
Apply the following writing style preferences to improve the user's content:
95+
96+
Preferences:
97+
{preferences}
98+
"""
99+
100+
messages = [
101+
{"role": "system", "content": system_prompt},
102+
{"role": "user", "content": f"""Original Content:
103+
{original_content}"""}
104+
]
105+
106+
response = openai.chat.completions.create(
107+
model="gpt-4o-mini",
108+
messages=messages
109+
)
110+
clean_response = response.choices[0].message.content.strip()
111+
112+
return clean_response
82113
```
83114

84-
---
85-
## **Complete Workflow: Document Editing**
115+
## **Complete Workflow: Content Editing**
86116

87117
```python
88-
def document_editing_workflow(content):
118+
def content_writing_workflow(content):
89119
"""Automated workflow for editing a document based on writing preferences."""
90120

91-
# Step 1: Store writing preferences (if not already stored)
92-
store_writing_preferences()
121+
# Store writing preferences (if not already stored)
122+
store_writing_preferences() # Ideally done once, or with a conditional check
93123

94-
# Step 2: Edit the document with Mem0 preferences
95-
edited_content = edit_document_based_on_preferences(content)
124+
# Edit the document with Mem0 preferences
125+
edited_content = apply_writing_style(content)
96126

97127
if not edited_content:
98128
return "Failed to edit document."
99129

100-
# Step 3: Display results
130+
# Display results
101131
print("\n=== ORIGINAL DOCUMENT ===\n")
102132
print(content)
103133

@@ -107,7 +137,6 @@ def document_editing_workflow(content):
107137
return edited_content
108138
```
109139

110-
---
111140
## **Example Usage**
112141

113142
```python
@@ -125,10 +154,9 @@ We plan to launch the campaign in July and continue through September.
125154
"""
126155

127156
# Run the workflow
128-
result = document_editing_workflow(original_content)
157+
result = content_writing_workflow(original_content)
129158
```
130159

131-
---
132160
## **Expected Output**
133161

134162
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
182210
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.
183211
```
184212

185-
Mem0 creates a seamless, intelligent document editing experience—perfect for content creators, technical writers, and businesses alike!
213+
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.
214+
215+
## Help & Resources
216+
217+
- [Mem0 Platform](https://app.mem0.ai/)
218+
219+
<Snippet file="get-help.mdx" />

0 commit comments

Comments
 (0)