devscast/lugha

Lugha is a PHP Generative AI framework to build chatbot, RAG systems and AI-powered applications

dev-master 2024-12-01 18:23 UTC

This package is auto-updated.

Last update: 2024-12-10 01:01:41 UTC


README

Lint Test Latest Stable Version Total Downloads License

Note

Work in progress.

Lugha from Swahili meaning "Language" is a PHP Generative AI Framework that provides a simple and easy way to interact with various AI providers. The main idea is to provide a unified provider-agnostic API for AI models, making it easier to switch between providers.

This project is highly inspired by LangChain and LLPhant, designed for Chatbot, RAG (Retrieval-Augmented Generation) based applications with integration of Embeddings, Completion and Reranking models.

supported providers:

Installation

composer require devscast/lugha

Usage

Embeddings

Embeddings are a type of word representation that allows words with similar meaning to have a similar representation. they can be used to find the similarity between words, phrases, or sentences. useful for document classification, clustering, and information retrieval.

$client = ClientFactory::create(Provider::GOOGLE);
$embeddings = $client->embeddings(
    prompt: 'Hello, world!', 
    config: new EmbeddingsConfig(
        model: 'text-embedding-004',
        dimensions: 512
    )
)->embedding;

Completion

Completion models are designed to generate human-like text based on the input prompt.

$client = ClientFactory::create(Provider::OPENAI);

// from a prompt
$completion = $client->completion(
    input: 'Hello, world!', 
    config: new CompletionConfig(
        model: 'gpt-3.5-turbo',
        temperature: 0.5,
        maxTokens: 100,
        frequencyPenalty: 0.5,
        presencePenalty: 0.5
    )
)->completion;

// from a chat history
$chat = $client->chat(
    input: History::fromMessages([
        new Message('You are a chatbot, expert in philosophy', Role::SYSTEM),
        new Message('what is the meaning of life ?', Role::USER)
    ]),
    config: new ChatConfig(
        model: 'gpt-4-turbo',
        temperature: 0.5,
        maxTokens: 100,
        frequencyPenalty: 0.5,
        presencePenalty: 0.5
    )
)->completion;

Reranking

Reranking models are designed to re-rank a list of documents based on the input prompt. useful for search engines, recommendation systems, and information retrieval.

$client = ClientFactory::create(Provider::VOYAGER);

$ranking = $client->reranking(
    prompt: 'What is the meaning of life ?',
    documents: [
        new Document('The best way to predict the future is to create it.')
        new Document('The only way to do great work is to love what you do.')
        new Document('Life is short, smile while you still have teeth.'),
        new Document('The best time to plant a tree was 20 years ago. The second best time is now.')
    ],
    config: new RerankingConfig(
        model: 'voyager-1.0',
        topK: 3
    )
)->documents;

Contributors

contributors