-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
186 lines (149 loc) · 5.65 KB
/
Copy pathProgram.cs
File metadata and controls
186 lines (149 loc) · 5.65 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using ParadeDB.EntityFrameworkCore.Extensions;
using Rag;
using Rag.Data;
using Shared;
var config = new ConfigurationBuilder().AddUserSecrets<ProductResult>().Build();
var openRouterApiKey =
Environment.GetEnvironmentVariable("OPENROUTER_API_KEY") ?? config["OpenRouter:ApiKey"];
var model = Environment.GetEnvironmentVariable("RAG_MODEL") ?? "anthropic/claude-3-haiku";
var options = new DbContextOptionsBuilder<AppDbContext>()
.UseNpgsql(ExampleSetup.ConnectionString, o => o.UseParadeDb())
.UseSnakeCaseNamingConvention()
.Options;
await using var dbContext = new AppDbContext(options);
Console.WriteLine(new string('=', 60));
Console.WriteLine("RAG with ParadeDB + OpenRouter");
Console.WriteLine(new string('=', 60));
Console.WriteLine($"Using model: {model}");
if (string.IsNullOrWhiteSpace(openRouterApiKey))
{
Console.WriteLine("OPENROUTER_API_KEY is not set; generation responses will be skipped.");
}
await ExampleSetup.SetupMockItemsAsync(dbContext);
var count = await dbContext.MockItems.CountAsync();
Console.WriteLine($"Loaded {count} products");
await Rag(dbContext, "What running shoes do you have?", openRouterApiKey, model);
await Rag(dbContext, "I need comfortable shoes for everyday use", openRouterApiKey, model);
await Rag(dbContext, "Do you have any wireless audio products?", openRouterApiKey, model);
Console.WriteLine();
Console.WriteLine(new string('=', 60));
Console.WriteLine("Done!");
return;
static async Task<List<ProductResult>> Retrieve(AppDbContext db, string query, int topK = 5)
{
return await db
.MockItems.Where(x => EF.Functions.Parse(x.Description, query, lenient: true))
.Select(x => new ProductResult
{
Id = x.Id,
Description = x.Description,
Category = x.Category,
Rating = x.Rating,
InStock = x.InStock,
Metadata = x.Metadata,
Score = EF.Functions.Score(x.Id),
})
.OrderByDescending(x => x.Score)
.Take(topK)
.ToListAsync();
}
static string FormatContext(List<ProductResult> items)
{
if (items.Count == 0)
{
return "No products found.";
}
var lines = new List<string>();
foreach (var item in items)
{
var stock = item.InStock ? "In Stock" : "Out of Stock";
var color = "N/A";
if (
item.Metadata is not null
&& item.Metadata.RootElement.TryGetProperty("color", out var colorElement)
)
{
color = colorElement.GetString() ?? "N/A";
}
lines.Add(
$"- {item.Description} | Category: {item.Category} | "
+ $"Rating: {item.Rating}/5 | {stock} | Color: {color}"
);
}
return string.Join("\n", lines);
}
static async Task<string> Generate(string query, string context, string? apiKey, string model)
{
if (string.IsNullOrWhiteSpace(apiKey))
return "(Set OPENROUTER_API_KEY to enable generation.)";
var prompt = $"""
You are a helpful product assistant. Answer the customer's question based only on the product information provided below.
Product Catalog:
{context}
Customer Question: {query}
Provide a helpful, concise answer. If the products don't match what the customer is looking for, say so.
""";
using var http = new HttpClient();
http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
var payload = new { model, messages = new[] { new { role = "user", content = prompt } } };
try
{
var json = JsonSerializer.Serialize(payload);
using var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await http.PostAsync(
"https://openrouter.ai/api/v1/chat/completions",
content
);
response.EnsureSuccessStatusCode();
var responseJson = await response.Content.ReadAsStringAsync();
using var doc = JsonDocument.Parse(responseJson);
return doc.RootElement.GetProperty("choices")[0]
.GetProperty("message")
.GetProperty("content")
.GetString()
?? "";
}
catch (Exception ex)
when (ex
is HttpRequestException
or JsonException
or KeyNotFoundException
or InvalidOperationException
)
{
return $"(OpenRouter error: {ex.Message}. Check your API key)";
}
}
static async Task Rag(AppDbContext db, string query, string? apiKey, string model)
{
Console.WriteLine($"\n{new string('=', 60)}");
Console.WriteLine($"Question: {query}");
Console.WriteLine(new string('=', 60));
var items = await Retrieve(db, query);
Console.WriteLine($"\nRetrieved {items.Count} products:");
foreach (var item in items)
Console.WriteLine($" • {item.Description} (score: {item.Score:F2})");
var context = FormatContext(items);
Console.WriteLine("\nAnswer:");
Console.WriteLine(new string('-', 40));
var answer = await Generate(query, context, apiKey, model);
Console.WriteLine(answer);
}
namespace Rag
{
public sealed class ProductResult
{
public int Id { get; set; }
public string Description { get; set; } = string.Empty;
public string Category { get; set; } = string.Empty;
public int Rating { get; set; }
public bool InStock { get; set; }
public JsonDocument? Metadata { get; set; }
public float Score { get; set; }
}
}