Skip to content

ezimuel/php-llm-examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Examples of using LLM in PHP

In this repository, I collected some PHP examples about the usage of Generative AI and Large Language Model (LLM) in PHP.

For the PHP code, I used LLPhant and openai-php/client projects. For the LLM models I used OpenAI and Llama 3. For semantic search, I used Elasticsearch as vector database.

Configure the environment

To execute the examples you need to set some environment variables:

export OPENAI_API_KEY=xxx
export ELASTIC_URL=https://yyy
export ELASTIC_API_KEY=zzz

If you want to run Llama 3 locally you can install Ollama running the following command (in this case you don't need OPENAI_API_KEY):

ollama pull llama3

This will install Llama3 and the model will be available through HTTP API at http://localhost:11434/api/.

If you want to interact with LLama 3 using a chat interface, you can execute the following command:

ollama run llama3

Examples

For OpenAI API example usage look at the following scripts:

For LLPhant examples you can see the following scripts:

The Retrieval-Augmented Generation examples are in the src/rag folder.

I divided the folderusing different embedding models: ELSER, Llama3 using ollama and GPT-3.5-turbo by OpenAI.

For the RAG examples I used a simple PDF document that contains the AI act regulation proposed by the European Union in July 2023. This document is not part of the knowledge of GPT-3.5-turbo that is fixed to 2022. In the examples we store the document in the vector database (Elasticsearch) using the embedding.php script, than we use the qa.php to ask for the question "What is the AI act?". Using the RAG architecture we can expand the knowledge of the LLM, without fine-tuning the model, providing also the sources (chunks) used to answer the question.

Copyright

Copyright (C) 2024 by Enrico Zimuel

About

PHP examples about the usage of GenAI and LLM

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published