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.
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
For OpenAI API example usage look at the following scripts:
- openai_chat, a simple chat use case;
- openai_image, generate an image using
dall-e-3
model; - openai_speech, text-to-speech example using
tts-1
model; - openai_moderation, moderation using
text-moderation-latest
model; - openai_function, function calling example;
For LLPhant examples you can see the following scripts:
- llphant_chat, a simple chat use case;
- llphant_tool, the function calling tool in LLPhant;
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 (C) 2024 by Enrico Zimuel