-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
134 lines (123 loc) · 3.87 KB
/
index.ts
File metadata and controls
134 lines (123 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { Langbase, Workflow } from 'langbase';
import dotenv from 'dotenv';
dotenv.config();
async function journalistWorkflow(input: string) {
const langbase = new Langbase({
apiKey: process.env.LANGBASE_API_KEY!
});
const { step } = new Workflow({
debug: true
});
// Step 1: Research the topic by searching the web
const searchResults = await step({
id: 'research_topic',
run: async () => {
return await langbase.tools.webSearch({
service: 'exa',
query: input,
totalResults: 5,
apiKey: process.env.EXA_API_KEY!
});
}
});
// Step 2: Retrieve journalism guidelines from memory
const journalismGuidelines = await step({
id: 'retrieve_guidelines',
run: async () => {
return await langbase.memories.retrieve({
query: 'journalism style guidelines and best practices',
memory: [{ name: 'journalism-guidelines-1747869545619' }]
});
}
});
// Step 3: Generate article outline
const articleOutline = await step({
id: 'generate_outline',
run: async () => {
const { output } = await langbase.agent.run({
model: 'openai:gpt-4.1-mini',
apiKey: process.env.OPENAI_API_KEY!,
instructions: `You are a professional journalist creating an article outline.
Based on the research provided, create a detailed outline for an article on the topic.
Include a compelling headline, introduction, 3-5 main sections, and conclusion.`,
input: [
{
role: 'user',
content: `Topic: ${input}\n\nResearch:\n${searchResults
.map(
result =>
`Source: ${result.url}\n${result.content}\n`
)
.join('\n')}`
}
],
stream: false
});
return output;
}
});
// Step 4: Write the full article
const draftArticle = await step({
id: 'write_article',
run: async () => {
const { output } = await langbase.agent.run({
model: 'openai:gpt-4.1-mini',
apiKey: process.env.OPENAI_API_KEY!,
instructions: `You are a professional journalist writing a high-quality article.
Follow the outline provided and incorporate information from the research.
Write in a clear, engaging, and informative style.
Include proper citations and quotes from sources where appropriate.
The article should be comprehensive, accurate, and well-structured.`,
input: [
{
role: 'user',
content: `Write a complete article based on this outline:\n${articleOutline}\n\nResearch sources:\n${searchResults
.map(
result =>
`Source: ${result.url}\n${result.content}\n`
)
.join('\n')}`
}
],
stream: false
});
return output;
}
});
// Step 5: Edit and refine the article
const finalArticle = await step({
id: 'edit_article',
run: async () => {
const { output } = await langbase.agent.run({
model: 'openai:gpt-4.1-mini',
apiKey: process.env.OPENAI_API_KEY!,
instructions: `You are a professional editor for the New York Times.
Review and refine the article to ensure it meets the highest journalistic standards.
Apply the journalism guidelines provided.
Check for accuracy, clarity, coherence, and proper citation of sources.
Improve the language, flow, and structure where needed.
Ensure the article is balanced, objective, and free of bias.
The final article should be publication-ready.`,
input: [
{
role: 'user',
content: `Edit and refine this article to meet New York Times standards:\n\n${draftArticle}\n\nJournalism Guidelines:\n${journalismGuidelines.map(item => item.text).join('\n')}`
}
],
stream: false
});
return output;
}
});
return {
article: finalArticle,
sources: searchResults.map(result => result.url)
};
}
async function main() {
const topic = 'The latest news on the stock market';
console.log('📰 Topic:', topic);
const result = await journalistWorkflow(topic);
return result;
}
main();