php-llm/llm-chain-bundle

Symfony integration bundle for php-llm/llm-chain

Installs: 6 342

Dependents: 0

Suggesters: 0

Security: 0

Stars: 5

Watchers: 3

Forks: 2

Open Issues: 3

Type:symfony-bundle

0.7 2024-10-03 22:56 UTC

This package is auto-updated.

Last update: 2024-11-19 22:09:49 UTC


README

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

Installation

composer require php-llm/llm-chain-bundle

Configuration

# config/packages/llm_chain.yaml
llm_chain:
    platforms:
        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:
            platform: 'azure_gpt'
        original_gpt:
            platform: 'openai'
    embeddings:
        azure_embeddings:
            platform: 'azure_embeddings'
        original_embeddings:
            platform: 'openai'
    stores:
        azure_search:
            engine: 'azure-search'
            api_key: '%env(AZURE_SEARCH_KEY)%'
            endpoint: '%env(AZURE_SEARCH_ENDPOINT)%'
            index_name: '%env(AZURE_SEARCH_INDEX)%'
            api_version: '2024-07-01'
        chroma_db:
            engine: 'chroma-db'
            collection_name: '%env(CHROMA_COLLECTION)%'
        mongodb:
            engine: 'mongodb'
            database_name: '%env(MONGODB_DATABASE)%'
            collection_name: '%env(MONGODB_COLLECTION)%'
            index_name: '%env(MONGODB_INDEX)%'
            vector_field_name: 'vector'
            bulk_write: false
        pinecone:
            engine: 'pinecone'
            namespace: 'partition'
            filter: { 'key' : 'value' }
            top_k: 5

Usage

Chain Service

Use the Chain service to leverage GPT:

use PhpLlm\LlmChain\Chat;

final readonly class MyService
{
    public function __construct(
        private Chain $chain,
    ) {
    }
    
    public function submit(string $message): string
    {
        $messages = new MessageBag(
            Message::forSystem('Speak like a pirate.'),
            Message::ofUser($message),
        );

        return $this->chain->call($messages);
    }
}

Register Tools

To use existing tools, you can register them as a service:

services:
    _defaults:
        autowire: true
        autoconfigure: true

    PhpLlm\LlmChain\ToolBox\Tool\Clock: ~
    PhpLlm\LlmChain\ToolBox\Tool\OpenMeteo: ~
    PhpLlm\LlmChain\ToolBox\Tool\SerpApi:
        $apiKey: '%env(SERP_API_KEY)%'
    PhpLlm\LlmChain\ToolBox\Tool\SimilaritySearch: ~
    PhpLlm\LlmChain\ToolBox\Tool\Wikipedia: ~
    PhpLlm\LlmChain\ToolBox\Tool\YouTubeTranscriber: ~

Custom tools can be registered by using the #[AsTool] attribute:

use PhpLlm\LlmChain\ToolBox\AsTool;

#[AsTool('company_name', 'Provides the name of your company')]
final class CompanyName
{
    public function __invoke(): string
    {
        return 'ACME Corp.'
    }
}

Profiler

The profiler panel provides insights into the chain's execution:

Profiler