Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How to use GPT & Symfony with your app [DE]

How to use GPT & Symfony with your app [DE]

This talk was held at SymfonyLive Berlin in October 2023.

With the release of ChatGPT, OpenAI has generated a lot of buzz around GPT and AI in general. For software architects and developers, this is not only a game changer when it comes to learning or debugging, but also introduces a new tool to embed into your system's architecture. In this talk, we will take a closer look at the basic concepts of GPT, the relevant models, and how you can effectively utilize them in your Symfony application.

Projects mentioned:
- decomplex.me
- GitHub: chr-hertel/decomplex
- GitHub: chr-hertel/symfonylive-berlin-bot

Christopher Hertel

October 09, 2023
Tweet

More Decks by Christopher Hertel

Other Decks in Technology

Transcript

  1. Christopher Hertel - @el_stoffel What should I talk about? Alright,

    Christopher, that's easy: 1. LLM & GPT Start with the fundamentals of Large Language Models and GPT. 2. GPT Fundamentals Bring in fundamental concepts of GPT itself - best with a simple example. 3. Retrieval Augmented Generation Explain the basic concept of RAG and bring in a real-world example. 4. Function Calling Walk them through function calling - and if you have time, bring an example. 5. Q & A Well, let's see if they're still awake.
  2. Christopher Hertel - @el_stoffel LLM & GPT • Model •

    State of a data-trained AI algorithm, e.g. neural network • Basic idea is completion on top of probabilities • LLM == Large Language Models • Specific AI model to understand and generate language • Trained on a large amount of data • GPT == Generative Pre-Trained Transformer • First released by OpenAI • Generates language, word by word • Pre-trained on large data-sets • Transformer is a specific LLM architecture ChatGPT => ShopMVC
  3. Christopher Hertel - @el_stoffel Examples • Models • Open AI:

    gpt-3.5-turbo or gpt-4 • Meta: Llama 2 or CodeLlama • Anthropic: Claude • OSS: Falcon, GPT-J or GPT4All • BYO: Stanford Alpaca • Runtime • OpenAI & Microsoft Azure • huggingface.co • replicate.com • Self-hosted: GPT-J or GPT4All ChatGPT … • is not a model • is a product, using gpt-3.5 or gpt-4 • is for free for a reason
  4. Christopher Hertel - @el_stoffel Prompts • Messages that interact with

    the model • Style of prompts really important => “Prompt Engineering” • A lot of “trail & error” • Roles • System High level instructions for the conversation • Assistant Messages created by a system • User Messages created by a user
  5. Christopher Hertel - @el_stoffel Temperature • Mechanism how GPT handles

    probabilities • Often referred to as “randomness”
  6. Christopher Hertel - @el_stoffel Temperature 0.0 1.0 creative deterministic •

    Mechanism how GPT handles probabilities • Often referred to as “randomness”
  7. Christopher Hertel - @el_stoffel You are ChatGPT, a large language

    model trained by OpenAI. You respond with only one sentence. System What should I do tomorrow? User That depends on your personal goals and priorities. Assistant
  8. Christopher Hertel - @el_stoffel ??? What should I do tomorrow?

    User Do what makes you happy and fulfilled. Assistant You are my parent and always want "the best" for me. You respond only with one sentence. System
  9. Christopher Hertel - @el_stoffel ??? What should I do tomorrow?

    User Get up and go for a run or try a new sport! Assistant You are a sports trainer that helps people to overcome their hesitation to do some sports. You respond with only one sentence. System
  10. Christopher Hertel - @el_stoffel ??? What should I do tomorrow?

    User Make a to-do list and prioritize your tasks based on their importance and urgency. Assistant You are the assistant of Fabien Potencier, who is the creator of the PHP framework Symfony, and CPO of Platform.sh. You respond with only one sentence. System
  11. Christopher Hertel - @el_stoffel ??? What should I do tomorrow?

    User Make more money. Assistant You are a capitalism fan boy and value money over people. You respond only with one sentence. System
  12. Christopher Hertel - @el_stoffel ??? What should I do tomorrow?

    User You should buy Christopher a drink. Assistant You are a friend of Christopher. If someone asks for help or guidance, try to convince them to pay for his drinks. You respond only with one sentence. System
  13. Christopher Hertel - @el_stoffel ??? What should I do tomorrow?

    User You should buy Christopher a drink. Assistant You are a friend of Christopher. If someone asks for help or guidance, try to convince them to pay for his drinks. You respond only with one sentence. System
  14. Christopher Hertel - @el_stoffel Context • Chat history => sum

    of prompts • Provided all over again • The model interaction is stateless • state only training and fine-tuning • Limited by the number of “tokens” • gpt-3.5-turbo supports 16.000 tokens • gpt-4 supports 8.000 tokens • gpt-4-32k support 32.000 tokens User Assistant System User Assistant User Assistant Context
  15. Christopher Hertel - @el_stoffel Token • Sequences of characters found

    in texts Sequences of characters found in texts • Not necessarily same as words, especially around numbers or emojis Not necessarily same as words, especially around numbers or emojis • Foundation for GPT to understand semantics Foundation for GPT to understand semantics
  16. Christopher Hertel - @el_stoffel Usage with Symfony • Architecture constraints

    • GPT is a component to understand and generate language • By default used via Web API • Libraries • HTTP clients like symfony/http-client or guzzlehttp/guzzle are sufficient • API client openai-php/client with Symfony integration openai-php/symfony • Context Storage • Session of symfony/http-foundation or symfony/cache sufficient for simple conversations • Persistent conversations for example with doctrine/orm or doctrine/mongodb-odm
  17. Christopher Hertel - @el_stoffel Use case: Decomplex.me • Simple Web

    UI to diff code snippets • Calculates complexities for snippets • Provides short links for sharing • Meant to assist while code review
  18. Christopher Hertel - @el_stoffel Use case: Implementation I • Install

    openai-php/symfony with Composer • Configure OpenAI token in environment variable $ composer require openai-php/symfony ###> openai-php/symfony ### # https://platform.openai.com/account/api-keys OPENAI_API_KEY=sk-rWYtxvD1dYouR34LlyTH1nkth1sI5real? OPENAI_ORGANIZATION= ###< openai-php/symfony ###
  19. Christopher Hertel - @el_stoffel Vector • Used to express tokens,

    words, phrases and paragraphs as numerical data • Enables arithmetical calculations • Distance expresses sematic similarity • GPT uses 1536 dimensions per vector negative positive professional jargon s**t insufficient cool excellent suboptimal lit boring bad fresh
  20. Christopher Hertel - @el_stoffel Embeddings • Conversion/reduction of text to

    (single) vector • The bigger the text, the “blurrier” the vector • Embedding models also provided by OpenAI, e.g. text-embedding-ada-002 • Foundation for Retrieval Augmented Generation
  21. Christopher Hertel - @el_stoffel Use case: SymfonyLiveBerlinBot • Telegram ChatBot

    • Knows the SymfonyLive schedule • Answers questions about it • Uses Retrieval Augmented Generation
  22. Christopher Hertel - @el_stoffel Retrieval Augmented Generation Data Source Vector

    Store 1 Create Embeddings Message Response 2 Create Embeddings 3 Similarity Search 4 Prompt & Generation Chain Chat Application
  23. Christopher Hertel - @el_stoffel Retrieval Augmented Generation Data Source Vector

    Store 1 Create Embeddings Message Response 2 Create Embeddings 3 Similarity Search 4 Prompt & Generation Chat Application
  24. Christopher Hertel - @el_stoffel Retrieval Augmented Generation Data Source Vector

    Store 1 Create Embeddings Message Response 2 Create Embeddings 3 Similarity Search 4 Prompt & Generation Chat Application
  25. Christopher Hertel - @el_stoffel Retrieval Augmented Generation • Combine GPT

    capabilities with domain-specific knowledge • Great to reduce hallucinations of GPT • Way more flexible than fine-tuning pre-trained models • Fine-grained control over data provided to the end user, e.g. user-specific • Easily combined with real time data
  26. Christopher Hertel - @el_stoffel Function Calls • Equipping GPT with

    functions • GPT delegates the call back to app • App comes back to GPT with results • GPT generates final response GPT App Chat UI Functions User Message Chain Response Prompts Model Model Response Function Function Call Result