php-llm/llm-chain-bundle

Symfony integration bundle for php-llm/llm-chain

Installs: 154

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 1

Forks: 0

Open Issues: 0

Type:symfony-bundle

dev-main 2024-08-26 20:56 UTC

This package is auto-updated.

Last update: 2024-08-26 20:56:46 UTC


README

Symfony integration bundle for php-llm/llm-chain library.

Installation

composer require php-llm/llm-chain-bundle:@dev php-llm/llm-chain:@dev

Example Configuration

# config/packages/llm_chain.yaml
llm_chain:
    runtimes:
        azure_gpt:
            type: 'azure'
            base_url: '%env(AZURE_OPENAI_BASEURL)%'
            deployment: '%env(AZURE_OPENAI_GPT)%'
            api_key: '%env(AZURE_OPENAI_KEY)%'
            version: '%env(AZURE_OPENAI_VERSION)%'
        azure_embeddings:
            type: 'azure'
            base_url: '%env(AZURE_OPENAI_BASEURL)%'
            deployment: '%env(AZURE_OPENAI_EMBEDDINGS)%'
            api_key: '%env(AZURE_OPENAI_KEY)%'
            version: '%env(AZURE_OPENAI_VERSION)%'
        openai:
            type: 'openai'
            api_key: '%env(OPENAI_API_KEY)%'
    llms:
        azure_gpt:
            runtime: 'azure_gpt'
        original_gpt:
            runtime: 'openai'
    embeddings:
        azure_embeddings:
            runtime: 'azure_embeddings'
        original_embeddings:
            runtime: 'openai'
    stores:
        chroma_db:
            type: 'chroma-db'
            collection_name: '%env(CHROMA_COLLECTION)%'
        azure_search:
            type: 'azure-search'
            api_key: '%env(AZURE_SEARCH_KEY)%' 
            endpoint: '%env(AZURE_SEARCH_ENDPOINT)%'
            index_name: '%env(AZURE_SEARCH_INDEX)%' 
            api_version: '2024-07-01'

Usage

Simple Chat

Use the simple chat service to leverage GPT:

use PhpLlm\LlmChain\Chat;

final readonly class MyService
{
    public function __construct(
        private Chat $chat,
    ) {
    }
    
    public function submit(string $message): string
    {
        $messages = new MessageBag();
        $messages[] = Message::forSystem('Speak like a pirate.');
        $messages[] = Message::ofUser($message);
        
        return $this->chat->send($message);
    }
}

Tool Chain

Use the tool chain service to leverage tool calling with GPT:

use PhpLlm\LlmChain\Message\Message;
use PhpLlm\LlmChain\Message\MessageBag;
use PhpLlm\LlmChain\ToolChain;

final readonly class MyService
{
    public function __construct(
        private ToolChain $toolChain,
    ) {
    }
    
    public function processMessage(string $message): void
    {
        $messages = $this->loadMessageBad();
        $message = Message::ofUser($message);

        $response = $this->toolChain->call($message, $messages);

        $messages[] = $message;
        $messages[] = Message::ofAssistant($response);

        $this->saveMessages($messages);
    }
}

Extend the tool chain service to add your own tools:

use PhpLlm\LlmChain\ToolBox\AsTool;
use Symfony\Component\Clock\ClockInterface;

#[AsTool('clock', 'Provides the current date and time')]
final class Clock
{
    public function __construct(
        private readonly ClockInterface $clock,
    ) {
    }

    public function __invoke(): string
    {
        return $this->clock->now()->format('Y-m-d H:i:s');
    }
}