INTRODUCTION

GPT SIER

BACKGROUND

The GPT model is a natural language processing model based on deep learning, which has unprecedented powerful language generation capabilities. This model can process a large amount of text data, and can learn language rules and patterns from it, and then generate text content with grammatical correctness, context coherence and logic. Compared with previous natural language processing technologies, the GPT model is closer to the understanding and expression of human language.

When the GPT model came out, it represented a major milestone in AI technology, as it showed that AI could interact and communicate with humans through natural language. This is an unprecedented challenge to human intelligence, and it also means that the knowledge accumulated by humans over thousands of years can become the basis for further training of AI. At the same time, this knowledge is more structured and logical, which opens a new door for the application of artificial intelligence.

MOTIVATION & GOALS

This work deploys a personal chatbot based on the GPT-3.5 Turbo API. Multiple rounds of dialogue are possible (I set up 5 rounds), as well as streaming responses. In July, I will be experimenting with the GPT-4 model, along with speech recognition and image generation.

METHOD

Front End

VUE3 + VITE4

	
export default {
	data() {
		return {
		id: Math.floor(Math.random() * 100) + 1,
		isTalking: false,
		messageContent: "",
		roleAlias: { user: "ME", assistant: "GPT SIER", system: "System" },
		md: md,
		messageList: [
			{
			role: "assistant",
			content: `Hi,I am an AI natural language model, I can provide some common services and information.`,
			},
		],
		};
	},
	watch: {
		messageList : {
		handler(newVal, oldVal) {
			this.scrollToBottom()
		},
		deep: true,
		immediate: true
		}
	},
	methods: {
		send() {
		if (!this.messageContent.length) return;
		this.sendChatMessage();
		},
		appendLastMessageContent(content: string){
		this.messageList[this.messageList.length - 1].content += content;
		},
		sendChatMessage() {
		try {
			const question = this.messageContent;
			this.isTalking = true;
			if (this.messageList.length === 2) {
			this.messageList.pop();
			}
			this.messageList.push({ role: "user", content: question});
			this.clearMessageContent();
			this.messageList.push({ role: "assistant", content: "" });
			
			fetch('/api', {
				method: 'POST',
				body: JSON.stringify({"id": this.id, question: question}),
				headers: {'Content-Type': 'application/json'}
			}).then(response => {
				const reader = response.body.getReader();
				const processChunk = async () => {
				const { done, value } = await reader.read();
				if (done) {return;}
				this.appendLastMessageContent(new TextDecoder('utf-8').decode(value))
				processChunk();
				};
	
				processChunk();
				}
			).catch(error => {
				console.error('Error:', error);
			});
			this.isTalking = false;
		} catch (error: any) {
			this.appendLastMessageContent(error);
		} finally {
			this.isTalking = false;
		}
		},
	
		clearMessageContent() {
		this.messageContent = ""
		},
		scrollToBottom() {
		let scrollElem = this.$refs.chatListDom;
		if (!scrollElem) return
		console.log("scroll", scrollElem.scrollHeight)
		window.scrollTo({
			top: scrollElem.scrollHeight, behavior: "smooth"
		})
		}
	}
	
	}
	

Back End

Python

	
		
import openai
from http.server import HTTPServer, BaseHTTPRequestHandler
import json
import time
import threading

# Set the context round to 5 round
quantity = 10

# Init user infomation
user_status = [{"id": 0, "start":time.time(), "time": time.time(), "messages": [{"role": "system", "content": "You are a intelligent assistant."}]}]

class RequestHandler(BaseHTTPRequestHandler):
    def do_POST(self):

        print(self.path)
        content_length = int(self.headers['Content-Length'])
        post_data = self.rfile.read(content_length)

        # Get the quetsion and save
        id = json.loads(post_data)['id']
        question = json.loads(post_data)['question']

        # Query historical records,
        # if the user already exists, accumulate questions and update the time,
        # if the user does not exist, add and insert.
        if id in [item.get("id") for item in user_status]:
            data = [item for item in user_status if item["id"] == id]

            # If greater than 5 rounds, delete the oldest.
            if len(data[0]['messages']) > quantity:
                data[0]['messages'] = data[0]['messages'][-quantity:]

            # If the interval exceeds 10 minutes, clear the user's history.
            if time.time() - data[0]['time'] >= 300:
                data[0]['messages'] = [{"role": "user", "content": question}]
            else:
                data[0]['messages'].append({"role": "user", "content": question})

            data[0]['time'] = time.time()
        else:
            user_status.append({"id": id, 
                                "start": time.time(), 
                                "time": time.time(), 
                                "messages": [{"role": "system", "content": "You are a intelligent assistant."}, {"role": "user", "content": question}]})

        # Set response headers, enable streaming
        self.send_response(200)
        self.send_header('Content-Type', 'text/plain')
        self.send_header('Transfer-Encoding', 'chunked')
        self.end_headers()

        # Call openai API
        openai.api_key = ""
        messages = [item for item in user_status if item["id"] == id][0]['messages']
        
        # Send the data stream chunk by chunk to the client
        print(messages)
        response_chunck_list = []
        for chunk in openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages, stream=True):
            # Determine if it is the last block
            if chunk["choices"][0]['finish_reason'] == "stop": 
                self.wfile.write(b'0\r\n\r\n')
                self.wfile.flush()
            else:
                chunk_content = chunk["choices"][0]['delta']['content']
                response_chunck_list.append(chunk_content)

                if chunk_content != "":
                    chunk_content = chunk_content.encode('utf-8')
                    self.wfile.write(b'%X\r\n%s\r\n' % (len(chunk_content), chunk_content))
                    self.wfile.flush()

        if id in [item.get("id") for item in user_status]:
            data = [item for item in user_status if item["id"] == id]
            data[0]['messages'].append({"role": "assistant", "content": ''.join(response_chunck_list)})

if __name__ == '__main__':
        print("# start sier server 8998")
        server = HTTPServer(("0.0.0.0", 8998), RequestHandler)
        server.serve_forever()   
  
	

KEYWORDS

Microservice, Modularization, Sensor, Visualization, Data Processing, Machine Learning, and Video Streaming.

EXPERIMENT

WEB UI

Figure 1. Assist in writing code.

Figure 2. With context memory function.