Skip to content

Commit cf286d6

Browse files
committed
resync code examples.
1 parent 4b92d4a commit cf286d6

File tree

1 file changed

+73
-44
lines changed

1 file changed

+73
-44
lines changed

docs/guides/deno/byo-deep-research.mdx

+73-44
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,7 @@ async function handleCreateQuery(message: CreateQueryTopicMessage) {
454454

455455
async function handleQuery(message: PerformQueryTopicMessage) {
456456
console.log(`[Research] Executing search query: ${message.query.query}`)
457-
458-
// Perform the given query using our search
457+
// perform the given query using our search
459458
const result = await search(message.query.query)
460459

461460
console.log(`[Research] Found ${result.length} results`)
@@ -465,26 +464,20 @@ async function handleQuery(message: PerformQueryTopicMessage) {
465464
html: result[0]?.html.substring(0, 200) + '...',
466465
})
467466

468-
// Clean and convert HTML to markdown
469-
console.log(
470-
`[Research] Converting HTML to Markdown for URL: ${result[0]?.url}`,
471-
)
472-
console.log(`[Research] Original HTML length: ${result[0]?.html.length}`)
473-
474-
const cleanedHtml = cleanHtml(result[0]?.html)
475-
console.log(`[Research] Cleaned HTML length: ${cleanedHtml.length}`)
467+
const content = result.reduce((acc, curr) => {
468+
const cleanedContent = cleanHtml(curr.html)
469+
return `${acc}
476470
477-
const markdown = turndownService.turndown(cleanedHtml)
478-
console.log(
479-
`[Research] Converted HTML to Markdown: ${markdown.substring(0, 200)}...`,
480-
)
481-
console.log(`[Research] Markdown content length: ${markdown.length}`)
471+
# ${curr.title}
472+
${turndownService.turndown(cleanedContent)}
473+
`
474+
}, '')
482475

483-
// Publish a summarize request to the research service
476+
// publish a summarize request to the research service
484477
await researchTopicPub.publish({
485478
...message,
486479
type: 'summarize',
487-
content: markdown,
480+
content: content,
488481
})
489482
}
490483

@@ -493,34 +486,31 @@ async function handleSummarize(message: SummarizeTopicMessage) {
493486
`[Research] Summarizing content for topic: ${message.topics[message.topics.length - 1]}`,
494487
)
495488

496-
// Append message content to previous summaries
497-
const summaries = [...message.summaries, message.content]
498-
499489
console.log(
500490
`[Research] Previous summaries count: ${message.summaries.length}`,
501491
)
502492
console.log(`[Research] Current content length: ${message.content.length}`)
503493

504-
// Reduce summaries into a single string
505-
const fullSummary = summaries.join('\n')
506-
console.log(`[Research] Combined summary length: ${fullSummary.length}`)
507-
508-
// Summarize the given content
494+
// Process the current content in isolation
509495
const completion = await OAI.chat.completions.create({
510496
model: MODEL,
511497
messages: [
512-
{ role: 'system', content: summarizerPrompt(message.topics) },
513-
{ role: 'user', content: fullSummary },
498+
{
499+
role: 'system',
500+
content: summarizerPrompt([message.topics[message.topics.length - 1]]),
501+
},
502+
{ role: 'user', content: message.content },
514503
],
515504
})
516505

517506
const summary = completion.choices[0].message.content!
518507
console.log(`[Research] Generated summary: ${summary}`)
519508

520-
// Publish a reflect request to the research service
509+
// publish a reflect request to the research service
521510
await researchTopicPub.publish({
522511
summaries: [
523-
// Reset to the newly compacted summary
512+
// Keep all previous summaries and add the new one
513+
...message.summaries,
524514
summary,
525515
],
526516
remainingIterations: message.remainingIterations,
@@ -542,19 +532,29 @@ async function handleReflect(message: ReflectTopicMessage) {
542532
console.log(
543533
`[Research] No iterations remaining. Writing final summary to bucket.`,
544534
)
545-
// Combine all summaries with clear topic separation
546-
const finalSummary = message.summaries
547-
.map(
548-
(summary, index) =>
549-
`## Research Topic: ${message.topics[index]}\n${summary}`,
550-
)
551-
.join('\n\n')
535+
// Create a more comprehensive final summary with proper structure
536+
const finalSummary = `# Research Summary: ${message.topics[0]}
537+
538+
## Introduction
539+
This document contains research findings on the topic "${message.topics[0]}". The research was conducted through multiple iterations of querying, analyzing, and synthesizing information.
540+
541+
## Research Findings
542+
${message.summaries
543+
.map(
544+
(summary, index) =>
545+
`### Research Topic: ${message.topics[index]}\n\n${summary}`,
546+
)
547+
.join('\n\n')}
548+
549+
## Conclusion
550+
This research provides a comprehensive overview of "${message.topics[0]}" and related topics. The findings are based on multiple sources and have been synthesized to provide a coherent understanding of the subject matter.
551+
`
552552

553553
await researchBucket.file(message.topics[0]).write(finalSummary)
554554
return
555555
}
556556

557-
// Use the reflection prompt to restart the research chain
557+
// Here I want to use the reflection prompt I have to restart the research chain
558558
const completion = await OAI.chat.completions.create({
559559
model: MODEL,
560560
messages: [
@@ -587,19 +587,48 @@ async function handleReflect(message: ReflectTopicMessage) {
587587
console.log(
588588
`[Research] No knowledge gaps found. Writing final summary to bucket: ${message.topics[message.topics.length - 1]}`,
589589
)
590-
// Combine all summaries with clear topic separation
591-
const finalSummary = message.summaries
592-
.map(
593-
(summary, index) =>
594-
`## Research Topic: ${message.topics[index]}\n${summary}`,
595-
)
596-
.join('\n\n')
590+
// Create a more comprehensive final summary with proper structure
591+
const finalSummary = `# Research Summary: ${message.topics[0]}
592+
593+
## Introduction
594+
This document contains research findings on the topic "${message.topics[0]}". The research was conducted through multiple iterations of querying, analyzing, and synthesizing information.
595+
596+
## Research Findings
597+
${message.summaries
598+
.map(
599+
(summary, index) =>
600+
`### Research Topic: ${message.topics[index]}\n\n${summary}`,
601+
)
602+
.join('\n\n')}
603+
604+
## Conclusion
605+
This research provides a comprehensive overview of "${message.topics[0]}" and related topics. The findings are based on multiple sources and have been synthesized to provide a coherent understanding of the subject matter.
606+
`
597607

598608
console.log(`[Research] Final summary length: ${finalSummary.length}`)
599609
await researchBucket.file(message.topics[0]).write(finalSummary)
600610
}
601611
}
602612

613+
researchApi.post('/query', async (ctx) => {
614+
const query = ctx.req.text()
615+
const remainingIterations = MAX_ITERATIONS
616+
617+
// Submit off start of research chain
618+
await researchTopicPub.publish({
619+
summaries: [],
620+
remainingIterations,
621+
type: 'create_query',
622+
date: new Date().toISOString(),
623+
topics: [],
624+
originalTopic: query,
625+
})
626+
627+
ctx.res.body = 'Query submitted'
628+
629+
return ctx
630+
})
631+
603632
// API endpoint to start research
604633
researchApi.post('/query', async (ctx) => {
605634
const query = ctx.req.text()

0 commit comments

Comments
 (0)