Hello! Welcome back to our project on automating social media content.
In our last lesson, we built a crucial filtering mechanism. By creating an LLM "judge," your system can now sift through trending topics and rank them based on relevance and substance, ensuring we only focus on high-quality signals. We have a list of approved trends, each backed by a cluster of source articles.
Today, we'll tackle the next essential step: transforming that cluster of raw articles into a single, rich, and structured story. The learning outcome for this lesson is to design prompts to synthesize multiple sources into a coherent trend narrative. This narrative will serve as the "single source of truth" from which all our future content—be it a LinkedIn article, an X thread, or an Instagram carousel—will be derived.
From Multiple Sources to a Single Story
Imagine you're a journalist tasked with writing about a new tech trend. You wouldn't just staple together summaries of your source articles. You would read them all, identify the core ideas, connect the dots, note disagreements, and then write a new, cohesive piece that tells the whole story.
Our goal is to automate that process. We need to design a prompt that instructs an LLM to perform this multi-document synthesis. The challenge lies in creating a prompt that is detailed enough to produce a high-quality, factual, and structured output.
Let's start by looking at the fundamental building blocks of any good prompt.
Advanced Prompt Engineering: OpenAI Hackathon
The video 'Advanced Prompt Engineering' from Microsoft DevRadio provides a great breakdown of the core elements of a prompt. Understanding these is the first step to mastering prompt design.
Please watch the segment from 5:15 to 8:52. The speaker outlines the five key elements of a prompt: Instruction: What the model should do. Context: Background information to frame the task. Examples: (Optional but powerful) To guide the style. Input Data: The raw material to work with. Output Indicator: The desired format for the response.
For our synthesis task, these elements will map directly to our needs:
- Instruction: "Synthesize these articles into a trend narrative."
- Context: "You are a senior tech analyst preparing a briefing document."
- Input Data: The collection of articles, titles, and summaries for a single ranked trend.
- Output Indicator: A request for a structured JSON object.
Structuring the Thought Process: Chain of Thought (CoT)
Simply asking an LLM to "synthesize" a dozen articles can lead to a messy, unstructured output. A more robust approach is to guide the model's reasoning process step-by-step. This is the core idea behind Chain of Thought (CoT) prompting.
Instead of one complex command, we give the LLM a sequence of simpler, related tasks.
Prompt Engineering Techniques - A Guide to Prompt Engineering ...
The article 'Prompt Engineering Techniques' provides a clear explanation of Chain of Thought and other valuable methods. We'll focus on a few key techniques from this guide.
Please read the section titled 'Chain of thought (CoT) prompting'. Notice how it breaks a complex query into a linear sequence of prompts to explore a topic systematically.
Applying CoT to our synthesis task, we can structure our prompt to instruct the model as follows:
- First, read all the provided articles. For each one, identify the main claim and key supporting points.
- Second, group the key points from all articles into 3-5 common themes.
- Third, identify any conflicting information or differing perspectives among the sources.
- Finally, using these themes and conflicts, write a cohesive narrative summarizing the trend.
This step-by-step guidance significantly improves the structure and quality of the final narrative. There's even a simplified version called Zero-shot CoT, where just adding the phrase "Let's think step by step" to your prompt can encourage more deliberate, reasoned outputs.
Grounding the Narrative: An Application of RAG
One of the biggest risks with LLMs is "hallucination"—inventing facts. Since our goal is to create authoritative content, our narrative must be strictly based on the provided source articles.
The architecture we are building is a form of Retrieval-Augmented Generation (RAG). We are retrieving our own curated data (the articles) to augment the LLM's generation process. This grounds the model's output in our specific context, rather than its general pre-trained knowledge.
Advanced Prompt Engineering: OpenAI Hackathon
The 'Advanced Prompt Engineering' video also includes an excellent, in-depth explanation of RAG. Understanding this will clarify how our entire system fits into modern AI architectures.
Please watch the final major section on 'Retrieval Augmented Generation (RAG)' from 56:09 to 1:12:44. This is a longer segment, but it's invaluable. Focus on: The high-level concept: combining a 'retriever' and a 'generator'. The practical workflow: chunking documents, creating embeddings, searching for relevant chunks, and feeding them to the LLM. The key benefit: citation and verifiability. This is what allows us to trust the output.
The key takeaway for our prompt design is the need for grounding and citation. Our prompt must explicitly instruct the LLM to:
- Base its narrative only on the provided source texts.
- Identify which source articles support which parts of the narrative.

Advanced Synthesis: Chain of Density (CoD)
A good trend narrative should be both comprehensive and concise. We want maximum insight in a compact format. A powerful technique for this is Chain of Density (CoD) prompting.
CoD works by iteratively refining a summary. You start with a basic summary and then repeatedly ask the model to add more specific, salient entities from the source text while keeping the word count the same. This forces the model to compress, fuse, and rewrite the text to become more information-rich.
Prompt Engineering Techniques - A Guide to Prompt Engineering ...
Let's return to the 'Prompt Engineering Techniques' guide to understand this advanced method.
Read the final section on 'Chain of Density (CoD) prompting'. Grasp the core concept: starting sparse and iteratively adding 'missing entities' to create a denser summary without increasing length.
We can incorporate a simplified version of this into our main synthesis prompt to ensure the final narrative is potent and packed with value.
Activity: Building the Synthesis Prompt in TypeScript
Now, let's combine these techniques—CoT, RAG principles, and CoD—into a single, powerful prompt within our TypeScript project. We'll design a function that takes the raw content of a trend's articles and produces a structured narrative.
Harnessing Generative AI for Proactive Trend Forecasting
The article 'Harnessing Generative AI for Proactive Trend Forecasting' provides many excellent, practical examples of prompts for synthesis and analysis, which will inspire our implementation.
Skim through section III, 'Prompt Engineering for Trend Discovery'. Pay attention to subsections A, B, and C. Note the example prompts for summarizing information across sources and identifying recurring themes. These are directly applicable to what we're about to build.
Here is a TypeScript code skeleton for a synthesizeTrend.ts service. This follows the same pattern as our rankTrend.ts service from the last lesson.
import { z } from 'zod';
import zodToJsonSchema from 'zod-to-json-schema';
import OpenAI from 'openai';
// Let's assume this is the input structure for our articles
interface SourceArticle {
id: string; // e.g., 'hackernews-12345'
title: string;
content: string; // The scraped content of the article
}
// 1. Define the Zod schema for our structured narrative output
const TrendNarrativeSchema = z.object({
headline: z.string().describe("A compelling, single-sentence headline for the trend."),
narrative: z.string().describe("A dense, 150-200 word narrative synthesizing the articles. It should explain what the trend is, why it's happening now, and its potential impact."),
keyPoints: z.array(z.string()).describe("A bulleted list of 3-5 most important takeaways or facts about the trend."),
conflictingViews: z.string().optional().describe("A brief summary of any notable disagreements or counter-arguments found in the sources."),
sourceIds: z.array(z.string()).describe("An array of the source article IDs used to construct this narrative.")
});
// Type alias for the output
type TrendNarrative = z.infer<typeof TrendNarrativeSchema>;
// Initialize your LLM client
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
// 2. Create the main synthesis function
export async function synthesizeTrend(articles: SourceArticle[]): Promise<TrendNarrative | null> {
const articlesString = articles
.map(a => `--- ARTICLE START ---\nID: ${a.id}\nTITLE: ${a.title}\nCONTENT: ${a.content}\n--- ARTICLE END ---`)
.join('\n\n');
const systemPrompt = `
You are an expert tech analyst and synthesizer. Your task is to process a collection of articles about a single technology trend and generate a structured, coherent, and dense narrative.
**Instructions (Chain of Thought):**
1. **Analyze all sources:** Read every article provided below, paying close attention to their main arguments, evidence, and perspectives.
2. **Identify Core Themes:** Identify the 3-5 central themes that recur across the articles. What is the trend? Why is it significant? What are its key components?
3. **Detect Nuance:** Note any conflicting viewpoints, counter-arguments, or potential risks mentioned in the sources.
4. **Synthesize Narrative (Chain of Density approach):**
a. First, draft a ~150 word summary explaining the trend.
b. Then, review your summary and identify 2-3 specific entities (people, technologies, companies) from the articles that are missing.
c. Rewrite the summary to the same length, but integrate these new entities to make it more specific and information-dense. This final version is your 'narrative'.
5. **Extract Key Facts:** Pull out the 3-5 most critical, non-obvious facts or takeaways.
6. **Format Output:** Present your complete analysis as a single JSON object matching the required schema.
**Crucial Guardrails (RAG principles):**
- **Grounding:** You MUST base your entire output *exclusively* on the information present in the provided articles. Do not introduce any outside knowledge.
- **Citation:** In the 'sourceIds' field, you MUST list the IDs of all articles you used for the synthesis.
`;
try {
const response = await openai.chat.completions.create({
model: "gpt-4-turbo",
messages: [
{ role: "system", content: systemPrompt },
{ role: "user", content: `Please synthesize the following articles:\n\n${articlesString}` }
],
response_format: { type: "json_object" },
});
const content = response.choices[0].message.content;
if (!content) {
throw new Error("Empty response from LLM.");
}
// 3. Parse and validate the response against our Zod schema
const narrative = TrendNarrativeSchema.parse(JSON.parse(content));
console.log("Trend Narrative Synthesis Complete:", narrative);
return narrative;
} catch (error) {
console.error("Error during trend synthesis:", error);
return null;
}
}
This synthesizeTrend function is the heart of our analysis module. It takes the output of our previous steps (ranked trends and their associated articles) and transforms it into a structured, high-quality asset ready for the next phase: content creation.
Conclusion
Excellent work. You've now moved from being a data analyst who can spot a trend to a storyteller who can explain it. This synthesis step is what turns raw information into actionable insight.
Key Takeaways:
- Synthesis is a creative act: It's about building a new, integrated story from disparate pieces of information, not just summarizing them.
- Chain of Thought (CoT) is a vital technique for guiding an LLM through complex multi-step reasoning tasks like synthesis.
- The system we are building is a practical example of Retrieval-Augmented Generation (RAG), where we ground the LLM's output in our own curated data to ensure accuracy and verifiability.
- Chain of Density (CoD) offers an advanced method for creating concise yet highly informative narratives, perfect for a thought leadership context.
- Combining these prompt engineering techniques with Zod schemas creates a robust and reliable pipeline for turning raw data into structured, usable intelligence.
Preview of the next lesson:
We now have a rich, factual narrative about a trend. But facts alone don't always make for compelling content. To truly establish thought leadership, we need a unique perspective. In our next lesson, "Generate unique or contrarian angles on identified trends for thought leadership," we will explore prompts that push the LLM from being a synthesizer to a creative partner, helping us brainstorm fresh and interesting takes on the trends we've identified.