-
-
Notifications
You must be signed in to change notification settings - Fork 925
feat(core): add NEAR AI model provider #1291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PierreLeGuen
wants to merge
2
commits into
VoltAgent:main
Choose a base branch
from
PierreLeGuen:nearai-provider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@voltagent/core": patch | ||
| --- | ||
|
|
||
| Add NEAR AI Cloud to the model provider registry. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
packages/core/src/registries/model-provider-registry-nearai.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| let createOpenAICompatibleCalls: unknown[][] = []; | ||
|
|
||
| vi.mock("@voltagent/internal", () => ({ | ||
| safeStringify: (value: unknown) => JSON.stringify(value), | ||
| })); | ||
|
|
||
| vi.mock("@ai-sdk/openai-compatible", () => ({ | ||
| createOpenAICompatible: (...args: unknown[]) => { | ||
| createOpenAICompatibleCalls.push(args); | ||
| const mockModel = { | ||
| modelId: "mock-model", | ||
| specificationVersion: "v1", | ||
| provider: "nearai", | ||
| }; | ||
| return { | ||
| languageModel: () => mockModel, | ||
| chatModel: () => mockModel, | ||
| textEmbeddingModel: () => mockModel, | ||
| }; | ||
| }, | ||
| })); | ||
|
|
||
| describe("NEAR AI provider registry", () => { | ||
| const originalEnv = { ...process.env }; | ||
|
|
||
| beforeEach(() => { | ||
| vi.resetModules(); | ||
| createOpenAICompatibleCalls = []; | ||
| (globalThis as Record<string, unknown>).___voltagent_model_provider_registry = undefined; | ||
| process.env = { ...originalEnv, NODE_ENV: "production" }; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| process.env = originalEnv; | ||
| (globalThis as Record<string, unknown>).___voltagent_model_provider_registry = undefined; | ||
| }); | ||
|
|
||
| it("should list nearai as a registered provider", async () => { | ||
| const { ModelProviderRegistry } = await import("./model-provider-registry"); | ||
| const registry = ModelProviderRegistry.getInstance(); | ||
|
|
||
| expect(registry.listProviders()).toContain("nearai"); | ||
| }); | ||
|
|
||
| it("should load nearai through the OpenAI-compatible adapter", async () => { | ||
| process.env.NEARAI_API_KEY = "test-nearai-key"; | ||
|
|
||
| const { ModelProviderRegistry } = await import("./model-provider-registry"); | ||
| const registry = ModelProviderRegistry.getInstance(); | ||
| const model = await registry.resolveLanguageModel("nearai/zai-org/GLM-5.1-FP8"); | ||
|
|
||
| expect(model).toBeDefined(); | ||
| expect(createOpenAICompatibleCalls.length).toBeGreaterThan(0); | ||
|
|
||
| const lastCall = createOpenAICompatibleCalls[createOpenAICompatibleCalls.length - 1]; | ||
| const config = lastCall[0] as Record<string, unknown>; | ||
| expect(config.name).toBe("nearai"); | ||
| expect(config.baseURL).toBe("https://cloud-api.near.ai/v1"); | ||
| expect(config.apiKey).toBe("test-nearai-key"); | ||
| expect(config.supportsStructuredOutputs).toBe(false); | ||
| expect(config.transformRequestBody).toBeTypeOf("function"); | ||
| }); | ||
|
|
||
| it("should support NEARAI_BASE_URL override", async () => { | ||
| process.env.NEARAI_API_KEY = "test-nearai-key"; | ||
| process.env.NEARAI_BASE_URL = "https://custom.near.ai/v1"; | ||
|
|
||
| const { ModelProviderRegistry } = await import("./model-provider-registry"); | ||
| const registry = ModelProviderRegistry.getInstance(); | ||
| await registry.resolveLanguageModel("nearai/zai-org/GLM-5.1-FP8"); | ||
|
|
||
| const lastCall = createOpenAICompatibleCalls[createOpenAICompatibleCalls.length - 1]; | ||
| const config = lastCall[0] as Record<string, unknown>; | ||
| expect(config.baseURL).toBe("https://custom.near.ai/v1"); | ||
| }); | ||
|
|
||
| it("should sanitize unsupported NEAR AI request fields", async () => { | ||
| process.env.NEARAI_API_KEY = "test-nearai-key"; | ||
|
|
||
| const { ModelProviderRegistry } = await import("./model-provider-registry"); | ||
| const registry = ModelProviderRegistry.getInstance(); | ||
| await registry.resolveLanguageModel("nearai/zai-org/GLM-5.1-FP8"); | ||
|
|
||
| const lastCall = createOpenAICompatibleCalls[createOpenAICompatibleCalls.length - 1]; | ||
| const config = lastCall[0] as Record<string, unknown>; | ||
| const transformRequestBody = config.transformRequestBody as ( | ||
| args: Record<string, unknown>, | ||
| ) => Record<string, unknown>; | ||
|
|
||
| const transformed = transformRequestBody({ | ||
| messages: [{ role: "developer", content: "Use concise answers." }], | ||
| reasoning_effort: "medium", | ||
| store: true, | ||
| response_format: { | ||
| type: "json_schema", | ||
| json_schema: { name: "result", strict: true }, | ||
| }, | ||
| tools: [ | ||
| { | ||
| type: "function", | ||
| function: { name: "lookup", strict: true }, | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| expect(transformed).not.toHaveProperty("reasoning_effort"); | ||
| expect(transformed).not.toHaveProperty("store"); | ||
| expect(transformed.messages).toEqual([{ role: "system", content: "Use concise answers." }]); | ||
| expect(transformed.response_format).toEqual({ | ||
| type: "json_schema", | ||
| json_schema: { name: "result" }, | ||
| }); | ||
| expect(transformed.tools).toEqual([ | ||
| { | ||
| type: "function", | ||
| function: { name: "lookup" }, | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it("should throw if NEARAI_API_KEY is not set", async () => { | ||
| process.env = Object.fromEntries( | ||
| Object.entries(process.env).filter(([key]) => key !== "NEARAI_API_KEY"), | ||
| ); | ||
|
|
||
| const { ModelProviderRegistry } = await import("./model-provider-registry"); | ||
| const registry = ModelProviderRegistry.getInstance(); | ||
|
|
||
| await expect(registry.resolveLanguageModel("nearai/zai-org/GLM-5.1-FP8")).rejects.toThrow( | ||
| /NEARAI_API_KEY/, | ||
| ); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| --- | ||
| title: NEAR AI Cloud | ||
| --- | ||
|
|
||
| # NEAR AI Cloud | ||
|
|
||
| Use `nearai/<model>` with VoltAgent's model router. | ||
|
|
||
| NEAR AI Cloud provides OpenAI-compatible TEE inference at `https://cloud-api.near.ai/v1`. | ||
|
|
||
| ## Quick start | ||
|
|
||
| ```ts | ||
| import { Agent } from "@voltagent/core"; | ||
|
|
||
| const agent = new Agent({ | ||
| name: "nearai-agent", | ||
| instructions: "You are a helpful assistant", | ||
| model: "nearai/zai-org/GLM-5.1-FP8", | ||
| }); | ||
| ``` | ||
|
|
||
| ## Environment variables | ||
|
|
||
| - `NEARAI_API_KEY` | ||
|
|
||
| ## Provider package | ||
|
|
||
| `@ai-sdk/openai-compatible` | ||
|
|
||
| This provider uses the OpenAI-compatible adapter. | ||
|
|
||
| ## Default base URL | ||
|
|
||
| `https://cloud-api.near.ai/v1` | ||
|
|
||
| You can override the base URL by setting `NEARAI_BASE_URL`. | ||
|
|
||
| ## Provider docs | ||
|
|
||
| - https://docs.near.ai/ | ||
|
|
||
| ## Models | ||
|
|
||
| <details> | ||
| <summary>Show models (33)</summary> | ||
|
|
||
| - Qwen/Qwen3-30B-A3B-Instruct-2507 | ||
| - Qwen/Qwen3-Embedding-0.6B | ||
| - Qwen/Qwen3-Reranker-0.6B | ||
| - Qwen/Qwen3-VL-30B-A3B-Instruct | ||
| - Qwen/Qwen3.5-122B-A10B | ||
| - anthropic/claude-haiku-4-5 | ||
| - anthropic/claude-opus-4-6 | ||
| - anthropic/claude-opus-4-7 | ||
| - anthropic/claude-sonnet-4-5 | ||
| - anthropic/claude-sonnet-4-6 | ||
| - black-forest-labs/FLUX.2-klein-4B | ||
| - google/gemini-2.5-flash | ||
| - google/gemini-2.5-flash-lite | ||
| - google/gemini-2.5-pro | ||
| - google/gemini-3.1-flash-lite | ||
| - openai/gpt-4.1 | ||
| - openai/gpt-4.1-mini | ||
| - openai/gpt-4.1-nano | ||
| - openai/gpt-5 | ||
| - openai/gpt-5-mini | ||
| - openai/gpt-5-nano | ||
| - openai/gpt-5.1 | ||
| - openai/gpt-5.2 | ||
| - openai/gpt-5.4 | ||
| - openai/gpt-5.4-mini | ||
| - openai/gpt-5.4-nano | ||
| - openai/gpt-5.5 | ||
| - openai/gpt-oss-120b | ||
| - openai/o3 | ||
| - openai/o3-mini | ||
| - openai/o4-mini | ||
| - openai/whisper-large-v3 | ||
| - zai-org/GLM-5.1-FP8 | ||
|
|
||
| </details> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.