@ragpipe/plugin-cloudflare 0.3.0-alpha.1 → 0.3.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +5 -14
- package/dist/index.js +5 -14
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -66,7 +66,7 @@ function cloudflareEmbedding(options) {
|
|
|
66
66
|
// src/generation.ts
|
|
67
67
|
function cloudflareGeneration(options) {
|
|
68
68
|
const { model } = options;
|
|
69
|
-
const baseUrl = `https://api.cloudflare.com/client/v4/accounts/${options.accountId}/ai/
|
|
69
|
+
const baseUrl = `https://api.cloudflare.com/client/v4/accounts/${options.accountId}/ai/v1/chat/completions`;
|
|
70
70
|
function buildMessages(question, context, opts) {
|
|
71
71
|
const systemPrompt = opts?.systemPrompt ?? options.systemPrompt ?? "Answer based on the provided context.";
|
|
72
72
|
let userPrompt = `Context:
|
|
@@ -95,7 +95,7 @@ ${userPrompt}`;
|
|
|
95
95
|
Authorization: `Bearer ${options.apiToken}`,
|
|
96
96
|
"Content-Type": "application/json"
|
|
97
97
|
},
|
|
98
|
-
body: JSON.stringify({ messages, stream: false })
|
|
98
|
+
body: JSON.stringify({ model, messages, stream: false })
|
|
99
99
|
});
|
|
100
100
|
if (!res.ok) {
|
|
101
101
|
throw new Error(
|
|
@@ -103,16 +103,7 @@ ${userPrompt}`;
|
|
|
103
103
|
);
|
|
104
104
|
}
|
|
105
105
|
const data = await res.json();
|
|
106
|
-
|
|
107
|
-
throw new Error(
|
|
108
|
-
"Cloudflare generation error: API returned success=false"
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
if (data.result.choices?.length) {
|
|
112
|
-
const message = data.result.choices[0].message;
|
|
113
|
-
return message.content ?? message.reasoning_content ?? "";
|
|
114
|
-
}
|
|
115
|
-
return data.result.response ?? "";
|
|
106
|
+
return data.choices?.[0]?.message?.content ?? "";
|
|
116
107
|
},
|
|
117
108
|
async *generateStream(question, context, opts) {
|
|
118
109
|
const messages = buildMessages(question, context, opts);
|
|
@@ -122,7 +113,7 @@ ${userPrompt}`;
|
|
|
122
113
|
Authorization: `Bearer ${options.apiToken}`,
|
|
123
114
|
"Content-Type": "application/json"
|
|
124
115
|
},
|
|
125
|
-
body: JSON.stringify({ messages, stream: true })
|
|
116
|
+
body: JSON.stringify({ model, messages, stream: true })
|
|
126
117
|
});
|
|
127
118
|
if (!res.ok) {
|
|
128
119
|
throw new Error(
|
|
@@ -146,7 +137,7 @@ ${userPrompt}`;
|
|
|
146
137
|
if (payload === "[DONE]") return;
|
|
147
138
|
try {
|
|
148
139
|
const data = JSON.parse(payload);
|
|
149
|
-
const chunk = data.choices?.[0]?.delta?.content
|
|
140
|
+
const chunk = data.choices?.[0]?.delta?.content;
|
|
150
141
|
if (chunk) yield chunk;
|
|
151
142
|
} catch {
|
|
152
143
|
}
|
package/dist/index.js
CHANGED
|
@@ -39,7 +39,7 @@ function cloudflareEmbedding(options) {
|
|
|
39
39
|
// src/generation.ts
|
|
40
40
|
function cloudflareGeneration(options) {
|
|
41
41
|
const { model } = options;
|
|
42
|
-
const baseUrl = `https://api.cloudflare.com/client/v4/accounts/${options.accountId}/ai/
|
|
42
|
+
const baseUrl = `https://api.cloudflare.com/client/v4/accounts/${options.accountId}/ai/v1/chat/completions`;
|
|
43
43
|
function buildMessages(question, context, opts) {
|
|
44
44
|
const systemPrompt = opts?.systemPrompt ?? options.systemPrompt ?? "Answer based on the provided context.";
|
|
45
45
|
let userPrompt = `Context:
|
|
@@ -68,7 +68,7 @@ ${userPrompt}`;
|
|
|
68
68
|
Authorization: `Bearer ${options.apiToken}`,
|
|
69
69
|
"Content-Type": "application/json"
|
|
70
70
|
},
|
|
71
|
-
body: JSON.stringify({ messages, stream: false })
|
|
71
|
+
body: JSON.stringify({ model, messages, stream: false })
|
|
72
72
|
});
|
|
73
73
|
if (!res.ok) {
|
|
74
74
|
throw new Error(
|
|
@@ -76,16 +76,7 @@ ${userPrompt}`;
|
|
|
76
76
|
);
|
|
77
77
|
}
|
|
78
78
|
const data = await res.json();
|
|
79
|
-
|
|
80
|
-
throw new Error(
|
|
81
|
-
"Cloudflare generation error: API returned success=false"
|
|
82
|
-
);
|
|
83
|
-
}
|
|
84
|
-
if (data.result.choices?.length) {
|
|
85
|
-
const message = data.result.choices[0].message;
|
|
86
|
-
return message.content ?? message.reasoning_content ?? "";
|
|
87
|
-
}
|
|
88
|
-
return data.result.response ?? "";
|
|
79
|
+
return data.choices?.[0]?.message?.content ?? "";
|
|
89
80
|
},
|
|
90
81
|
async *generateStream(question, context, opts) {
|
|
91
82
|
const messages = buildMessages(question, context, opts);
|
|
@@ -95,7 +86,7 @@ ${userPrompt}`;
|
|
|
95
86
|
Authorization: `Bearer ${options.apiToken}`,
|
|
96
87
|
"Content-Type": "application/json"
|
|
97
88
|
},
|
|
98
|
-
body: JSON.stringify({ messages, stream: true })
|
|
89
|
+
body: JSON.stringify({ model, messages, stream: true })
|
|
99
90
|
});
|
|
100
91
|
if (!res.ok) {
|
|
101
92
|
throw new Error(
|
|
@@ -119,7 +110,7 @@ ${userPrompt}`;
|
|
|
119
110
|
if (payload === "[DONE]") return;
|
|
120
111
|
try {
|
|
121
112
|
const data = JSON.parse(payload);
|
|
122
|
-
const chunk = data.choices?.[0]?.delta?.content
|
|
113
|
+
const chunk = data.choices?.[0]?.delta?.content;
|
|
123
114
|
if (chunk) yield chunk;
|
|
124
115
|
} catch {
|
|
125
116
|
}
|