@@ -17,6 +17,50 @@ import { generateObject, generateText } from 'ai';
17
17
import { type TiktokenModel , encodingForModel } from 'js-tiktoken' ;
18
18
import { z } from 'zod' ;
19
19
20
+ /**
21
+ * Gets the Cloudflare Gateway base URL for a specific provider if enabled
22
+ * @param runtime The runtime environment
23
+ * @param provider The model provider name
24
+ * @returns The Cloudflare Gateway base URL if enabled, undefined otherwise
25
+ */
26
+ function getCloudflareGatewayBaseURL ( runtime : IAgentRuntime , provider : string ) : string | undefined {
27
+ const isCloudflareEnabled = runtime . getSetting ( 'CLOUDFLARE_GW_ENABLED' ) === 'true' ;
28
+ const cloudflareAccountId = runtime . getSetting ( 'CLOUDFLARE_AI_ACCOUNT_ID' ) ;
29
+ const cloudflareGatewayId = runtime . getSetting ( 'CLOUDFLARE_AI_GATEWAY_ID' ) ;
30
+
31
+ logger . debug ( 'Cloudflare Gateway Configuration:' , {
32
+ isEnabled : isCloudflareEnabled ,
33
+ hasAccountId : ! ! cloudflareAccountId ,
34
+ hasGatewayId : ! ! cloudflareGatewayId ,
35
+ provider : provider ,
36
+ } ) ;
37
+
38
+ if ( ! isCloudflareEnabled ) {
39
+ logger . debug ( 'Cloudflare Gateway is not enabled' ) ;
40
+ return undefined ;
41
+ }
42
+
43
+ if ( ! cloudflareAccountId ) {
44
+ logger . warn ( 'Cloudflare Gateway is enabled but CLOUDFLARE_AI_ACCOUNT_ID is not set' ) ;
45
+ return undefined ;
46
+ }
47
+
48
+ if ( ! cloudflareGatewayId ) {
49
+ logger . warn ( 'Cloudflare Gateway is enabled but CLOUDFLARE_AI_GATEWAY_ID is not set' ) ;
50
+ return undefined ;
51
+ }
52
+
53
+ const baseURL = `https://gateway.ai.cloudflare.com/v1/${ cloudflareAccountId } /${ cloudflareGatewayId } /${ provider . toLowerCase ( ) } ` ;
54
+ logger . info ( 'Using Cloudflare Gateway:' , {
55
+ provider,
56
+ baseURL,
57
+ accountId : cloudflareAccountId ,
58
+ gatewayId : cloudflareGatewayId ,
59
+ } ) ;
60
+
61
+ return baseURL ;
62
+ }
63
+
20
64
/**
21
65
* Asynchronously tokenizes the given text based on the specified model and prompt.
22
66
*
@@ -62,11 +106,11 @@ async function detokenizeText(model: ModelTypeName, tokens: number[]) {
62
106
* Defines the Groq plugin with its name, description, and configuration options.
63
107
* @type {Plugin }
64
108
*
65
- * elizaLogger .debug(
109
+ * logger .debug(
66
110
"Initializing Groq model with Cloudflare check"
67
111
);
68
112
const baseURL = getCloudflareGatewayBaseURL(runtime, "groq");
69
- elizaLogger .debug("Groq baseURL result:", { baseURL });
113
+ logger .debug("Groq baseURL result:", { baseURL });
70
114
const groq = createGroq({
71
115
apiKey,
72
116
fetch: runtime.fetch,
@@ -91,7 +135,7 @@ async function detokenizeText(model: ModelTypeName, tokens: number[]) {
91
135
});
92
136
93
137
response = groqResponse;
94
- elizaLogger .debug("Received response from Groq model.");
138
+ logger .debug("Received response from Groq model.");
95
139
break;
96
140
}
97
141
async function handleGroq({
@@ -104,9 +148,9 @@ async function detokenizeText(model: ModelTypeName, tokens: number[]) {
104
148
modelOptions,
105
149
runtime,
106
150
}: ProviderOptions): Promise<GenerationResult> {
107
- elizaLogger .debug("Handling Groq request with Cloudflare check");
151
+ logger .debug("Handling Groq request with Cloudflare check");
108
152
const baseURL = getCloudflareGatewayBaseURL(runtime, "groq");
109
- elizaLogger .debug("Groq handleGroq baseURL:", { baseURL });
153
+ logger .debug("Groq handleGroq baseURL:", { baseURL });
110
154
111
155
const groq = createGroq({
112
156
apiKey,
@@ -213,6 +257,8 @@ export const groqPlugin: Plugin = {
213
257
try {
214
258
const baseURL = getCloudflareGatewayBaseURL ( runtime , 'groq' ) ;
215
259
260
+ const model = runtime . getSetting ( 'EMBEDDING_GROQ_MODEL' ) ?? 'text-embedding-3-small' ;
261
+
216
262
// Call the OpenAI API
217
263
const response = await fetch ( `${ baseURL } /embeddings` , {
218
264
method : 'POST' ,
@@ -568,7 +614,7 @@ export const groqPlugin: Plugin = {
568
614
} ,
569
615
} ,
570
616
{
571
- name : 'openai_test_text_embedding ' ,
617
+ name : 'groq_test_text_embedding ' ,
572
618
fn : async ( runtime ) => {
573
619
try {
574
620
const embedding = await runtime . useModel ( ModelType . TEXT_EMBEDDING , {
0 commit comments