Skip to content

Commit a003d14

Browse files
fix: generate structured objects and images with NEAR AI (elizaOS#3644)
* fix: nearai generate object and image model * fix: require structured output if schema is specified * feat: update warning msg * style: update comments * style: update comments * fix: env typo
1 parent b6d944c commit a003d14

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ NEARAI_MODEL=
366366
SMALL_NEARAI_MODEL= # Default: fireworks::accounts/fireworks/models/llama-v3p2-3b-instruct
367367
MEDIUM_NEARAI_MODEL= # Default: fireworks::accounts/fireworks/models/llama-v3p1-70b-instruct
368368
LARGE_NEARAI_MODEL= # Default: fireworks::accounts/fireworks/models/llama-v3p1-405b-instruct
369+
IMAGE_NEARAI_MODEL= # Default: fireworks::accounts/fireworks/models/playground-v2-5-1024px-aesthetic
369370

370371
# Remaining Provider Configurations
371372
GOOGLE_GENERATIVE_AI_API_KEY= # Gemini API key

packages/core/src/generation.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ import { fal } from "@fal-ai/client";
5353

5454
import BigNumber from "bignumber.js";
5555
import { createPublicClient, http } from "viem";
56+
import fs from "fs";
57+
import os from "os";
58+
import path from "path";
5659

5760
type Tool = CoreTool<any, any>;
5861
type StepResult = AIStepResult<any>;
@@ -1734,6 +1737,15 @@ export const generateImage = async (
17341737
return runtime.getSetting("LIVEPEER_GATEWAY_URL");
17351738
case ModelProviderName.SECRETAI:
17361739
return runtime.getSetting("SECRET_AI_API_KEY");
1740+
case ModelProviderName.NEARAI:
1741+
try {
1742+
// Read auth config from ~/.nearai/config.json if it exists
1743+
const config = JSON.parse(fs.readFileSync(path.join(os.homedir(), '.nearai/config.json'), 'utf8'));
1744+
return JSON.stringify(config?.auth);
1745+
} catch (e) {
1746+
elizaLogger.warn(`Error loading NEAR AI config. The environment variable NEARAI_API_KEY will be used. ${e}`);
1747+
}
1748+
return runtime.getSetting("NEARAI_API_KEY");
17371749
default:
17381750
// If no specific match, try the fallback chain
17391751
return (
@@ -2279,6 +2291,8 @@ export async function handleProvider(
22792291
return await handleLivepeer(options);
22802292
case ModelProviderName.SECRETAI:
22812293
return await handleSecretAi(options);
2294+
case ModelProviderName.NEARAI:
2295+
return await handleNearAi(options);
22822296
default: {
22832297
const errorMessage = `Unsupported provider: ${provider}`;
22842298
elizaLogger.error(errorMessage);
@@ -2661,6 +2675,34 @@ async function handleSecretAi({
26612675
});
26622676
}
26632677

2678+
/**
2679+
* Handles object generation for NEAR AI models.
2680+
*
2681+
* @param {ProviderOptions} options - Options specific to NEAR AI.
2682+
* @returns {Promise<GenerateObjectResult<unknown>>} - A promise that resolves to generated objects.
2683+
*/
2684+
async function handleNearAi({
2685+
model,
2686+
apiKey,
2687+
schema,
2688+
schemaName,
2689+
schemaDescription,
2690+
mode = "json",
2691+
modelOptions,
2692+
}: ProviderOptions): Promise<GenerateObjectResult<unknown>> {
2693+
const nearai = createOpenAI({ apiKey, baseURL: models.nearai.endpoint });
2694+
// Require structured output if schema is provided
2695+
const settings = schema ? { structuredOutputs: true } : undefined;
2696+
return await aiGenerateObject({
2697+
model: nearai.languageModel(model, settings),
2698+
schema,
2699+
schemaName,
2700+
schemaDescription,
2701+
mode,
2702+
...modelOptions,
2703+
});
2704+
}
2705+
26642706
// Add type definition for Together AI response
26652707
interface TogetherAIImageResponse {
26662708
data: Array<{

packages/core/src/models.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,9 @@ export const models: Models = {
12161216
maxOutputTokens: 8192,
12171217
temperature: 0.6,
12181218
},
1219+
[ModelClass.IMAGE]: {
1220+
name: settings.IMAGE_NEARAI_MODEL || "fireworks::accounts/fireworks/models/playground-v2-5-1024px-aesthetic",
1221+
},
12191222
},
12201223
},
12211224
};

0 commit comments

Comments
 (0)