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.
- Provider-agnostic completion, embeddings, and reranking APIs
- Chat history and tool calling
- Document loaders, retrieval, and vector stores
- PHP 8.3 or later
- Composer 2
supported providers:
| Provider | Link | Features |
|---|---|---|
| OpenAI | openai.com | Completion, Embeddings |
| Mistral | mistral.ai | Completion, Embeddings |
| ai.google | Completion, Embeddings | |
| GitHub | github.com | Completion, Embeddings |
| Anthropic | anthropic.com | Completion |
| Voyager.ai | voyageai.com | Embeddings, Reranking |
| Ollama | ollama.com | Completion, Embeddings |
| Deepseek | deepseek.com | completion |
composer require ngandu-dev/lughaEmbeddings 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 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 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;composer install
composer format
composer qualitySee CONTRIBUTING.md before opening an issue or pull request.
Report vulnerabilities privately as described in SECURITY.md.
Released under the MIT License.