Skip to content

Repository files navigation

Lugha - yet another PHP Generative AI Framework

Latest Stable VersionTotal DownloadsQualityTestsLicense

Important

Lugha is abandoned in favor of Symfony AI - github.com/symfony/ai. Please use the new repository for all future development, issues, and contributions.

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.

Features

  • Provider-agnostic completion, embeddings, and reranking APIs
  • Chat history and tool calling
  • Document loaders, retrieval, and vector stores

Requirements

  • PHP 8.3 or later
  • Composer 2

supported providers:

ProviderLinkFeatures
OpenAIopenai.comCompletion, Embeddings
Mistralmistral.aiCompletion, Embeddings
Googleai.googleCompletion, Embeddings
GitHubgithub.comCompletion, Embeddings
Anthropicanthropic.comCompletion
Voyager.aivoyageai.comEmbeddings, Reranking
Ollamaollama.comCompletion, Embeddings
Deepseekdeepseek.comcompletion

Installation

composer require ngandu-dev/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: newEmbeddingsConfig(
model: 'text-embedding-004',
dimensions: 512
)
)->embeddings;

Completion

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

$client = ClientFactory::create(Provider::OPENAI);
  • completion from a single prompt
$completion = $client->completion(
input: 'Hello, world!', config: newCompletionConfig(
model: 'gpt-3.5-turbo',
temperature: 0.5,
maxTokens: 100,
frequencyPenalty: 0.5,
presencePenalty: 0.5
)
)->completion;
  • completion from a chat history (conversation)
$completion = $client->chat(
input: History::fromMessages([
newMessage('You are a chatbot, expert in philosophy', Role::SYSTEM),
newMessage('what is the meaning of life ?', Role::USER)
]),
config: newChatConfig(model: 'gpt-4-turbo')
)->completion;
  • completion with tool calling
#[ToolDefinition(
name: 'get_weather',
description: 'Get the weather for a location on a specific date.',
parameters: [
newToolParameter('location', 'string', 'The location to get the weather for.', required: true),
newToolParameter('date', 'string', 'The date to get the weather for.', required: true),
],
strict: true
)]
class WeatherProvider
{
publicfunction__invoke(string$location, string$date): string
{
return"The weather in $location on $date is sunny.";
}
}
$completion = $client->completion(
input: 'What is the weather in Lubumbashi on January 16th ?',
config: newCompletionConfig(model: 'gpt-4-turbo'),
tools: [newWeatherProvider()] )->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);
$reRankedDocuments = $client->reranking(
prompt: 'What is the meaning of life ?',
documents: [
newDocument('The best way to predict the future is to create it.'),
newDocument('The only way to do great work is to love what you do.'),
newDocument('Life is short, smile while you still have teeth.'),
newDocument('The best time to plant a tree was 20 years ago. The second best time is now.')
],
config: newRerankingConfig(model: 'voyager-1.0', topK: 3)
)->documents;

Development

composer install
composer format
composer quality

Contributing

See CONTRIBUTING.md before opening an issue or pull request.

Security

Report vulnerabilities privately as described in SECURITY.md.

License

Released under the MIT License.

Contributors

Contributors

About

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

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

30 stars

Watchers

0 watching

Forks

Releases

Used by

Contributors

Languages