Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • CreatiCode
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

CreatiCode Scratch Forum

  1. CreatiCode Forum
  2. Knowledge Base
  3. Tutorials
  4. AI - Chat About a PDF File (Difficulty: 3)

AI - Chat About a PDF File (Difficulty: 3)

Scheduled Pinned Locked Moved Tutorials
1 Posts 1 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CreatiCodeI Offline
    CreatiCodeI Offline
    CreatiCode
    wrote on last edited by admin
    #1

    Introduction

     

    One of the most useful applications of AI (large language models like ChatGPT) is to analyze and process PDF files. For example, we might need help summarizing a file or understanding its content.

    In this tutorial, you will build a relatively simple tool, which allows the user to load any PDF file from the computer, and chat with the AI (LLM model) about the content of that file.

     
     

    Step 1 - Start a New Project

     

    In the CreatiCode playground, create a new project named “Chat about a PDF file”. Delete the sprite with the dog, as we will only write code in the Empty1 sprite.

     
     

    Step 2 - Add a Backdrop

     

    To make our project look nice, we can generate a backdrop image using the AI tool. For example, if we use the prompt “a light-colored background with a book, 2D illustration”, we will get a simple image like this:

    alt text

     
     

    Step 3 - Add a Button for Loading the PDF File

     

    The user will always start by loading a new PDF file, which will be attached to the user’s chat with the AI. We will provide a button for loading the file from the computer like this:

    290d4d0b-4885-46a0-8d43-878fc5f86370-image.png

     
    The button will look like this:

    cad1f1d0-7766-45ae-9da5-1896ac710239-image.png

     
     

    Step 4 - Attach the PDF File to Chat

     

    When the user clicks the button, we will run the reporter block “attach files to chat”. It will open a new dialog window for the user to select a PDF file from the computer. The return value of this reporter block will be the file name, and we will store that information in a new variable called “pdf name”:

    1044e2be-6d8e-4eb8-9977-8a097600eca8-image.png

     
    This is how it works:

    loadbook

     
    As an example, we will be using the chapter 1 of a free book “US History” from OpenStax. Since that book is too big, we have extracted the first chapter of it into a standalone PDF file. You can download it for testing your program, or you can prepare any other PDF file. Make sure it is not too big, since it will take longer to process a big file.

     
     

    Step 5 - Add a Chat Window with the First Message

     

    Next, we will add a chat window to the stage. We will also let the user know we have received the file by adding a chat message first. Note that this message is fixed, and we have not started using the AI yet.

    a2dba15d-a289-4b1f-b47d-952e1dd76bd9-image.png

     
    The result looks like this:

    6ed5762f-beee-4862-9afb-c8163bd7360a-image.png

     
     

    Step 6 - Add User Message to Chat Window

     

    When the user inputs a message and then presses the ENTER key or clicks the SEND button, we will first append that message to the chat window as the user. Note that the “value of chat1” block contains the input from the user.

    41127687-9c1f-4dbf-8d98-13ca1b94280e-image.png

     
     

    Step 7 - Send User Message to AI

     

    Next, we will send the user’s message to the AI:

    eb099110-96ed-4f97-bbab-d9823d8ac709-image.png

     
    We will use the “LLM” block instead of the ChatGPT block, since only the LLM block can read the files attached to the chat. Some notes on the parameters:

    • The “small” model is faster, so for most use cases it is good enough;
    • The request is simply the user’s input;
    • The AI’s response will be stored in the “result” variable;
    • We will be waiting for the response to come back before running the next block below;
    • The response will be cut off if it is longer than 1000 tokens;
    • The AI will use a temperature of 1 for maximum creativity;
    • The session type is “continue”: When the first time this block is used after the green flag button is clicked, it will be treated as “new chat”, and all past chat messages and file attachments will be removed; after the chat session has already started, we will continue the same chat, so the AI can “see” previous messages and files.

     
     

    Step 8 - Add AI’s Response to Chat

     

    Lastly, when we receive the response from AI, we will append it to the chat window as well:

    f60842b2-30e7-4ef5-88c1-45baf42e441e-image.png

     
    Here is the final demo of the project.

    chatpdf

     
    Note that it might take some time for the PDF to be uploaded to the AI server before it can generate a response, so it may take longer than usual. You can clearly see that the response of the AI is based on the content of the file.

     
     

    Additional Challenges

     

    Here are some additional challenges for you:

    • Display a message like “Please wait while I upload the PDF file …” in the chat window while waiting for the AI’s response, so the user would wait patiently.

    • The “attach files to chat” block is actually very powerful. You can attach multiple files so long as their total size is less than 10MB. Besides PDF files, it can also accept Text, Image, CSV, MP3, and WAVE files. Can you try to build another tool to make use of this capability?

    1 Reply Last reply
    0
    • CreatiCodeI CreatiCode pinned this topic on

    Hello! It looks like you're interested in this conversation, but you don't have an account yet.

    Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

    With your input, this post could be even better 💗

    Register Login
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode