I created a locally running AI bulletin board
I developed a simple AI bulletin board using WebSocket.
With this setup, users can experience a virtual bulletin board through AI interactions.
Here are the main features:
-
AI-Generated Responses:
Using a local LLM (2b), AI-generated responses are created based on different user personas. Since these personas are automatically generated, you can set the number of participants to increase the number of people in the bulletin board simulation. -
User Post Moderation by AI:
Sometimes, users may post emotionally charged messages. By running messages through the AI, users can adjust their content to a more neutral tone before posting. This feature is optional.
The full code is available on GitHub. Below, I’ll provide a brief code overview.
WebSocket
Since multiple AIs are responding simultaneously, I used WebSocket for communication. The server and client are built with FastAPI. The client uses HTML for message exchange.
from fastapi import FastAPI, WebSocket
from fastapi.responses import HTMLResponse
app = FastAPI()
html = """
<!DOCTYPE html>
<html>
<head>
...
</body>
</html>
"""
@app.get("/")
async def get():
return HTMLResponse(html)
On the server side, two tasks are running: one to “receive messages from the client” and another to “generate responses and send them back to the client.”
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
receive_task = asyncio.create_task(receive_message(websocket, llm_manager))
send_task = asyncio.create_task(send_response(websocket, llm_manager, response_queue))
await websocket.accept()
await asyncio.gather(receive_task, send_task)
Using the LLM
The LLM is used in three processes:
- Persona creation
- Response generation
- User post moderation
Persona Creation
Multiple AIs are participating in the bulletin board, so several personas are generated when the server starts. We use the following prompt to create each persona:
Create a brief persona for a fictional character with the following attributes:
1. **Name and Age** - Basic identity of the character.
2. **Occupation** - Their job or social role, impacting their perspective and expertise.
3. **Hobbies and Interests** - Topics they enjoy discussing or exploring.
Please focus on creating a consistent and realistic persona based on these points. Output only the completed persona. Output less than 512 letters.
persona:
Response Generation
For response generation, the AI uses the initial persona and the conversation history to create a reply. The following prompt is used:
You have the following persona.
# Persona
{persona}
You are chatting on a message board. The other person is anonymous, and you only know their ID. Below is the conversation history on the message board.
# Conversation History
{chat_history}
# Task
Create a reply that continues this conversation history. Careful **not to reveal personal information**, this is anonymous bulletin board.
Create a reply less than 30 words.
Output **only the reply**.
Now, please begin the task.
response:
This setup allows us to switch personas in a single LLM by using the persona as a formatted string.
User Post Moderation
When users post emotionally charged messages, AI can revise them to a more neutral tone. To avoid accuracy issues with 2b’s LLM when performing this in a single step, we first check if revision is needed. The following prompt determines if revision is required:
Check whether user message contains any of the following:
1. Emotional expressions
2. A strong preference for a particular stance
3. Language that may cause discomfort to others
If Contains, Output: "yes"
If Not Contains, Output: "no"
# user message
{user_message}
Contains:
If revision is necessary, we use the following prompt:
Convert the user's message to a calm and polite expression that avoids inappropriate or overly emotional language while maintaining the original intent. Ensure that the essential facts or requests are preserved so the core message remains clear without causing discomfort to the reader.
# user message
{user_message}
Converted message:
This approach helps prevent potential conflicts within the bulletin board.
Final Thoughts
While many people now gather information from social media, they may occasionally encounter distressing posts. AI moderation could be an effective way to maintain peace of mind.
In contrast to traditional chatbots, I am also interested in developing mutual, interactive communication with AI. Currently, replies are randomly determined, but by deciding whether to respond in real time, I believe we can enable more natural communication with AI.
Thank you for reading through to the end.
コメント
コメントを投稿