Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
127 lines (111 loc) · 3.91 KB
/
Copy pathProgram.cs
File metadata and controls
127 lines (111 loc) · 3.91 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
// AI Research Agent: gather multi-source context for LLM/RAG pipelines.
// Use case: Feed real-time search data into an AI assistant or RAG system.
// Fans out across web, news, and scholar — returns structured context.
using SerpApi;
using System.Text.Json;
var apiKey = args.Length > 0 ? args[0] : Environment.GetEnvironmentVariable("SERPAPI_KEY");
if (string.IsNullOrEmpty(apiKey))
{
Console.WriteLine("Usage: dotnet run -- <API_KEY>");
Console.WriteLine(" or: SERPAPI_KEY=... dotnet run");
return;
}
using var client = new SerpApiClient(apiKey);
var topic = args.Length > 1 ? args[1] : "retrieval augmented generation";
Console.WriteLine($"Research topic: \"{topic}\"\n");
// Fan-out: web + news + academic papers in parallel (RAG context gathering)
var web = client.SearchAsync(new Dictionary<string, string>
{
["engine"] = "google_light",
["q"] = topic,
["num"] = "5"
});
var news = client.SearchAsync(new Dictionary<string, string>
{
["engine"] = "google_news_light",
["q"] = topic
});
var scholar = client.SearchAsync(new Dictionary<string, string>
{
["engine"] = "google_scholar",
["q"] = topic,
["as_ylo"] = DateTime.UtcNow.Year.ToString()
});
var sources = new List<(string Type, string Title, string Url, string Snippet)>();
try
{
await Task.WhenAll(web, news, scholar);
// Collect web results
var webResult = await web;
if (webResult.OrganicResults is { } webItems)
{
foreach (var r in webItems.EnumerateArray().Take(5))
{
sources.Add((
"web",
r.TryGetProperty("title", out var t) ? t.GetString() ?? "" : "",
r.TryGetProperty("link", out var l) ? l.GetString() ?? "" : "",
r.TryGetProperty("snippet", out var s) ? s.GetString() ?? "" : ""
));
}
}
// Collect news
var newsResult = await news;
var newsItems = newsResult["news_results"];
if (newsItems is { } ni)
{
foreach (var r in ni.EnumerateArray().Take(3))
{
sources.Add((
"news",
r.TryGetProperty("title", out var t) ? t.GetString() ?? "" : "",
r.TryGetProperty("link", out var l) ? l.GetString() ?? "" : "",
r.TryGetProperty("snippet", out var s) ? s.GetString() ?? "" : ""
));
}
}
// Collect academic papers
var scholarResult = await scholar;
if (scholarResult.OrganicResults is { } papers)
{
foreach (var r in papers.EnumerateArray().Take(3))
{
sources.Add((
"scholar",
r.TryGetProperty("title", out var t) ? t.GetString() ?? "" : "",
r.TryGetProperty("link", out var l) ? l.GetString() ?? "" : "",
r.TryGetProperty("snippet", out var s) ? s.GetString() ?? "" : ""
));
}
}
// Output structured context (ready for LLM prompt injection)
Console.WriteLine($"Gathered {sources.Count} sources for RAG context:\n");
foreach (var group in sources.GroupBy(s => s.Type))
{
Console.WriteLine($"=== {group.Key.ToUpper()} ===");
foreach (var (type, title, url, snippet) in group)
{
Console.WriteLine($" • {title}");
if (!string.IsNullOrEmpty(snippet))
Console.WriteLine($" {snippet[..Math.Min(100, snippet.Length)]}...");
Console.WriteLine($" {url}\n");
}
}
// In a real RAG pipeline, you'd format these as context and pass to an LLM:
// var prompt = $"Based on these sources:\n{FormatSources(sources)}\n\nAnswer: {topic}";
}
catch (SerpApiException ex)
{
Console.WriteLine($"Search error: {ex.Message}");
}
finally
{
DisposeCompleted(web);
DisposeCompleted(news);
DisposeCompleted(scholar);
}
static void DisposeCompleted(Task<SerpApiResponse> task)
{
if (task.Status == TaskStatus.RanToCompletion)
task.Result.Dispose();
}