For the complete documentation index, see llms.txt. This page is also available as Markdown.

AI Provider

Interact with LLM providers or extend the framework to implement new ones.

With Neuron you can switch between LLM providers with just one line of code, without any impact on your agent implementation.

Anthropic

namespace App\Neuron;

use NeuronAI\Agent\Agent;
use NeuronAI\Chat\Messages\UserMessage;
use NeuronAI\Providers\AIProviderInterface;
use NeuronAI\Providers\Anthropic\Anthropic;

class MyAgent extends Agent
{
    protected function provider(): AIProviderInterface
    {
        return new Anthropic(
            key: 'ANTHROPIC_API_KEY',
            model: 'ANTHROPIC_MODEL',
            parameters: [], // Add custom params (temperature, logprobs, etc)
        );
    }
}

$message = MyAgent::make()
    ->chat(new UserMessage("Hi!"))
    ->getMessage();

echo $message->getContent();
// Hi, how can I help you today?

Anthropic Prompt Cache

Anthropic provider expose a dedicated method systemPromptBlocks() to leverage system prompt cache. Instead of using the instructions() method in the Agent class, you can pass prompts definition directly to the provider instance with cache type definition.

Anthropic On Google Vertex AI

To use this provider you need to install the goole auth composer package:

Below the syntax to use AnthropicVertex in your agent.

OpenAIResponses

This component uses the most recent OpenAI responses API:

OpenAI

This component uses the old OpenAI completions API:

AzureOpenAI

This provider allows you to connect with OpenAI models provided in the Azure cloud platform.

OpenAILike

This class simplify the connection with providers offering the same data format of the official OpenAI API.

Ollama

Gemini

Gemini on Vertex AI

To use this provider you need to install the goole auth composer package:

Below the syntax to use GeminiVertex in your agent.

Mistral

ZAI

HuggingFace

Deepseek

Grok (X-AI)

AWS Bedrock Runtime

To use The BedrockRuntime provider you need to install the aws/aws-sdk-php package.

Below you can find the syntax to use it in your agent.

Cohere

Alibaba DashScope

Routing

Official Neuron Router adds a reliability and management layer between the Agent session and providers API, giving you and your appliaction several key benefits.

Provider Failover for High Availability

Providers API occasionally experiences outages or rate limiting. Using the RouterProvider, your requests automatically fail over between multiple underlying providers. If one provider is unavailable or rate-limited, the router seamlessly routes to another, keeping your sessions uninterrupted.

Routing logic control

You can use routing logic like RoundRobin as a load balancer, ContentRule to route the request based on the content blocks inside the message (images, files, audio, video), or DifficultyRule to determine which model has the best capabilities to handle the incoming prompt.

First install the package:

Now use the RouterProvider class as any other provider in your agent class:

In the example above we use the RoundRobinRule making the router act as a load balancer between the attached AI providers. The package ships with several built-in rules including an LLM classifier to route calls to the appropriate model based on the promp difficulty score: https://github.com/neuron-core/router#difficultyrule

Custom Http Client

Providers use an HTTP client to communicate with the remote service. You can customize the configuration of the HTTP client explicitly passing an instance with custom constructor parameters, like timeout, custom headers, etc.

Implement a custom provider

If you want to create a new provider you have to implement the AIProviderInterface interface:

The chat method should contains the call the underlying LLM. If the provider doesn't support tools and function calls, you can implement it with a placeholder.

This is the basic template for a new AI provider implementation.

After creating your own implementation you can use it in the agent:

Last updated