-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestLangChaingoExtras.js
More file actions
117 lines (105 loc) · 3.74 KB
/
testLangChaingoExtras.js
File metadata and controls
117 lines (105 loc) · 3.74 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
import BosBase from "bosbase";
const baseUrl = process.env.BOSBASE_BASE_URL ?? "http://127.0.0.1:8090";
const authEmail =
process.env.BOSBASE_EMAIL ??
process.env.BOSBASE_SUPERUSER_EMAIL ??
"try@bosbase.com";
const authPassword =
process.env.BOSBASE_PASSWORD ??
process.env.BOSBASE_SUPERUSER_PASSWORD ??
"bosbasepass";
const runLangChaingo = process.env.BOSBASE_RUN_LANGCHAINGO === "1";
const modelProvider = process.env.BOSBASE_LANGCHAINGO_PROVIDER;
const modelName = process.env.BOSBASE_LANGCHAINGO_MODEL;
async function main() {
if (!runLangChaingo) {
console.log(
"[SKIP] LangChaingo extras skipped (set BOSBASE_RUN_LANGCHAINGO=1 to enable live calls).",
);
return;
}
let pb;
let collectionName;
try {
console.log("[INFO] LANGCHAINGO extras doc test starting...");
pb = new BosBase(baseUrl);
await pb.collection("_superusers").authWithPassword(authEmail, authPassword);
console.log("[SUCCESS] Authenticated as superuser");
if (!pb.langchaingo) {
throw new Error("LangChaingo service is missing from the client");
}
collectionName = `langchaingo_rag_${Date.now()}`;
await pb.llmDocuments.createCollection(collectionName, { domain: "test" });
await pb.llmDocuments.insert(
{
content: "The sky is blue because of Rayleigh scattering.",
metadata: { topic: "physics" },
},
{ collection: collectionName },
);
await pb.llmDocuments.insert(
{
content: "Photosynthesis lets plants convert sunlight into energy.",
metadata: { topic: "biology" },
},
{ collection: collectionName },
);
console.log(`[SUCCESS] LLM docs ready in collection "${collectionName}"`);
const modelConfig =
modelProvider || modelName
? { provider: modelProvider, model: modelName }
: undefined;
const rag = await pb.langchaingo.rag({
collection: collectionName,
question: "Why is the sky blue?",
topK: 2,
returnSources: true,
model: modelConfig,
});
console.log("[SUCCESS] RAG answer:", rag?.answer || "(no answer)");
console.log("[INFO] RAG sources:", (rag?.sources || []).length);
const docQuery = await pb.langchaingo.queryDocuments({
collection: collectionName,
query: "Give one fact about plant energy.",
topK: 2,
returnSources: true,
model: modelConfig,
});
console.log("[SUCCESS] Document query answer:", docQuery?.answer || "(no answer)");
console.log("[INFO] Query sources:", (docQuery?.sources || []).length);
const sql = await pb.langchaingo.sql({
query: "Count the number of superusers and describe the result.",
tables: ["_superusers"],
topK: 1,
model: modelConfig,
});
console.log("[SUCCESS] SQL generated:", sql?.sql || "(no sql)");
console.log("[INFO] SQL answer summary:", sql?.answer || "(no answer)");
console.log("\n========== LANGCHAINGO extras doc test completed ==========");
} catch (error) {
console.error("[ERROR] LangChaingo extras doc test failed:");
if (error?.response) {
console.error("Status:", error.response.status);
console.error("Data:", JSON.stringify(error.response.data, null, 2));
if (error.response.data?.message) {
console.error("Message:", error.response.data.message);
}
} else {
console.error(error);
}
process.exit(1);
} finally {
if (pb && collectionName) {
try {
await pb.llmDocuments.deleteCollection(collectionName);
console.log(`[CLEANUP] Deleted test LLM collection "${collectionName}"`);
} catch (cleanupError) {
console.warn(
"[WARN] Failed to delete LLM test collection:",
cleanupError?.message || cleanupError,
);
}
}
}
}
main();