getzero11 commited on
Commit
4bedc08
·
verified ·
1 Parent(s): e70f810

Create src/llm/callLLM.js

Browse files
Files changed (1) hide show
  1. src/llm/callLLM.js +57 -0
src/llm/callLLM.js ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { getProviderConfig } from "./providers.js";
2
+
3
+ export async function callLLM({ provider, model, prompt }) {
4
+ const cfg = getProviderConfig(provider, model);
5
+
6
+ let body;
7
+
8
+ switch (cfg.name) {
9
+ case "gemini":
10
+ body = {
11
+ contents: [{ parts: [{ text: prompt }] }]
12
+ };
13
+ break;
14
+
15
+ case "dashscope":
16
+ body = {
17
+ model: cfg.model,
18
+ input: { prompt }
19
+ };
20
+ break;
21
+
22
+ default:
23
+ body = {
24
+ model: cfg.model,
25
+ messages: [
26
+ { role: "system", content: "You are a market research analyst." },
27
+ { role: "user", content: prompt }
28
+ ],
29
+ temperature: 0.4
30
+ };
31
+ }
32
+
33
+ const res = await fetch(cfg.baseUrl, {
34
+ method: "POST",
35
+ headers: cfg.headers,
36
+ body: JSON.stringify(body)
37
+ });
38
+
39
+ const text = await res.text();
40
+
41
+ if (!res.ok) {
42
+ throw new Error(`LLM error ${res.status}: ${text}`);
43
+ }
44
+
45
+ const json = JSON.parse(text);
46
+
47
+ // Normalize output
48
+ if (cfg.name === "gemini") {
49
+ return json.candidates?.[0]?.content?.parts?.[0]?.text || "";
50
+ }
51
+
52
+ if (cfg.name === "dashscope") {
53
+ return json.output?.text || "";
54
+ }
55
+
56
+ return json.choices?.[0]?.message?.content || "";
57
+ }