Codemaxxers / codemaxxerBackend

https://codemaxxers.stu.nighthawkcodingsociety.com/
0 stars 26 forks source link

implement chat ai feature into our project #63

Open Jyustin opened 6 months ago

Jyustin commented 6 months ago

Working on using shivansh chat ai project as a building block to add into our project.

Place Backend Code Link Place Frontend

these links are to project from last tri shivansh worked on. plan is to implement the chat ai feature into our project with Shivansh, Justin, some input from Rachit.

DeadshotLegend commented 6 months ago

Chat API Assistant Overview Ref: https://platform.openai.com/docs/assistants/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

  1. ID – Long Primary Key Autogenerate
  2. chatMessage – String – User’s query / message to chatbot
  3. chatReponse – String – Chatbot’s reponse to user’s query
  4. timestamp – Datetime – time of user’s query
  5. personId – Long – Foreign key to Person table – To be implemented

ChatRepository.java JpaRepository for Chat entity Methods

  1. List findAllChatsForPerson(Long id); Query - SELECT * FROM chat WHERE person_id = ?1 Returns all chats for a person with id.
  2. 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

  1. URL: (GET) /aichatbot – provides a generic welcome message Responds with: “Hello From Chatbot AI”
  2. 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 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.

DeadshotLegend commented 6 months ago

Future Changes to the Chat AI Feature:

Jyustin commented 5 months ago

plan for algorithmic incorporation

Jyustin commented 5 months ago

plan for websocket implementation use websockets to: store user chat history monitor chat history and block messages that exceed a certain token amount ...