Individual Seed Blog
AP Exam Preparation
Feature
Chat API Assistant Overview
Step 1: Create an Assistant An Assistant represents an entity that can be configured to respond to users’ Messages.
Step 2: Create a Thread A Thread represents a conversation. OpenAI recommends creating one Thread per user as soon as the user initiates the conversation.
Step 3: Add a Message to a Thread A Message contains text, and optionally any files that you allow the user to upload. Messages need to be added to a specific Thread. Adding images via message objects like in Chat Completions using GPT-4 with Vision is not supported today.
Step 4: Run the Assistant For the Assistant to respond to the user message, you need to create a Run. This makes the Assistant read the Thread and decide whether to call tools (if they are enabled) or simply use the model to best answer the query. As the run progresses, the assistant appends Messages to the thread with the role="assistant". The Assistant will also automatically decide what previous Messages to include in the context window for the model.
Step 5: Check the Run status By default, a Run goes into the queued state. You can periodically retrieve the Run to check on its status to see if it has moved to completed.
Step 6: Display the Assistant's Response Once the Run completes, you can list the Messages added to the Thread by the Assistant. There can be many messages, including previous conversations. The latest message is in the first_id message.
Backend
Backend is composed of a SpringBoot Rest Controller to communicate with ChatGPT. Chat history is persisted in DB using JPA. Chat.java Java POJO used as Entity. Fields
- ID – Long Primary Key Autogenerate
- chatMessage – String – User’s query / message to chatbot
- chatReponse – String – Chatbot’s reponse to user’s query
- timestamp – Datetime – time of user’s query
- personId – Long – Foreign key to Person table – To be implemented
ChatRepository.java JpaRepository for Chat entity Methods
- List<Chat> findAllChatsForPerson(Long id); Query - SELECT * FROM chat WHERE person_id = ?1 Returns all chats for a person with id.
- save(chat) Inherited method provided by Jpa. Save chat entity to the underlying DB table.
AIChatbotController.java RestController to communicate to the AI engine, in this case ChatGPT, and retrieve responses to user’s queries. URL Mappings
- URL: (GET) /aichatbot – provides a generic welcome message Responds with: “Hello From Chatbot AI”
- URL: (GET) /aichatbot/chat – provides respones to user’s queries. Request Param – message Example: http://localhost:port/aichatbot/chat?message=What is 2 times 3
Uses the ChatGPT 3.5 turbo AI Model and follows the process outlined before to talk to ChatGPT. Persists the message, reponse, timestamp, and person_id to the database table.
Frontend
Frontend Frontend is composed of HTML, CSS, and JavaScript. It is based on: https://codepen.io/sajadhsm/pen/odaBdd which provides a chat UI HTML. When a user types in a message and hits Send button, the Controller is invoked. The controller’s reponse is then shown to the user in a conversational style.
Key Commits
- Implementing Chat History and Deletion: https://github.com/CoolCodingPeople/place-backend/commit/7426d7b9bd63e8add4879dd25a63035ebc2d433a
- Added JPA to save chat history: https://github.com/CoolCodingPeople/place-backend/commit/35620e593661d631f9591a2b75e8a00889d57af5
- Updated AI Chatbot Controller: https://github.com/CoolCodingPeople/place-backend/commit/e4ed3713e8a7ee4b742313b49e03dabce32a295f
Github Analytics
https://github.com/DeadshotLegend
AP Work
- FRQ1: https://deadshotlegend.github.io/shivansh/2024/02/24/question1IPYNB_2.html
- FRQ2: https://deadshotlegend.github.io/shivansh/2024/02/26/question2IPYNB_2.html
- FRQ3: https://deadshotlegend.github.io/shivansh/2024/02/26/question3IPYNB_2.html
- FRQ4: https://deadshotlegend.github.io/shivansh/2024/02/26/question4IPYNB_2.html
- Assosciations: https://github.com/DeadshotLegend/shivansh/issues/7