Skip to content

Commit d5704f7

Browse files
author
mike dupont
committed
working
1 parent 5699cb6 commit d5704f7

File tree

1 file changed

+52
-6
lines changed

1 file changed

+52
-6
lines changed

packages/plugin-groq/src/index.ts

+52-6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,50 @@ import { generateObject, generateText } from 'ai';
1717
import { type TiktokenModel, encodingForModel } from 'js-tiktoken';
1818
import { z } from 'zod';
1919

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+
2064
/**
2165
* Asynchronously tokenizes the given text based on the specified model and prompt.
2266
*
@@ -62,11 +106,11 @@ async function detokenizeText(model: ModelTypeName, tokens: number[]) {
62106
* Defines the Groq plugin with its name, description, and configuration options.
63107
* @type {Plugin}
64108
*
65-
* elizaLogger.debug(
109+
* logger.debug(
66110
"Initializing Groq model with Cloudflare check"
67111
);
68112
const baseURL = getCloudflareGatewayBaseURL(runtime, "groq");
69-
elizaLogger.debug("Groq baseURL result:", { baseURL });
113+
logger.debug("Groq baseURL result:", { baseURL });
70114
const groq = createGroq({
71115
apiKey,
72116
fetch: runtime.fetch,
@@ -91,7 +135,7 @@ async function detokenizeText(model: ModelTypeName, tokens: number[]) {
91135
});
92136
93137
response = groqResponse;
94-
elizaLogger.debug("Received response from Groq model.");
138+
logger.debug("Received response from Groq model.");
95139
break;
96140
}
97141
async function handleGroq({
@@ -104,9 +148,9 @@ async function detokenizeText(model: ModelTypeName, tokens: number[]) {
104148
modelOptions,
105149
runtime,
106150
}: ProviderOptions): Promise<GenerationResult> {
107-
elizaLogger.debug("Handling Groq request with Cloudflare check");
151+
logger.debug("Handling Groq request with Cloudflare check");
108152
const baseURL = getCloudflareGatewayBaseURL(runtime, "groq");
109-
elizaLogger.debug("Groq handleGroq baseURL:", { baseURL });
153+
logger.debug("Groq handleGroq baseURL:", { baseURL });
110154
111155
const groq = createGroq({
112156
apiKey,
@@ -213,6 +257,8 @@ export const groqPlugin: Plugin = {
213257
try {
214258
const baseURL = getCloudflareGatewayBaseURL(runtime, 'groq');
215259

260+
const model = runtime.getSetting('EMBEDDING_GROQ_MODEL') ?? 'text-embedding-3-small';
261+
216262
// Call the OpenAI API
217263
const response = await fetch(`${baseURL}/embeddings`, {
218264
method: 'POST',
@@ -568,7 +614,7 @@ export const groqPlugin: Plugin = {
568614
},
569615
},
570616
{
571-
name: 'openai_test_text_embedding',
617+
name: 'groq_test_text_embedding',
572618
fn: async (runtime) => {
573619
try {
574620
const embedding = await runtime.useModel(ModelType.TEXT_EMBEDDING, {

0 commit comments

Comments
 (0)