|
| 1 | +import dataclasses |
| 2 | +import json |
| 3 | +import re |
| 4 | +import time |
| 5 | +from typing import Any, Dict, List, Tuple |
| 6 | +from uuid import uuid1 |
| 7 | +from config.chatgpt_config import ChatGPTConfig |
| 8 | + |
| 9 | +import loguru |
| 10 | +import requests |
| 11 | +import openai |
| 12 | + |
| 13 | + |
| 14 | +logger = loguru.logger |
| 15 | +logger.remove() |
| 16 | +logger.add(level="WARNING", sink="logs/chatgpt.log") |
| 17 | + |
| 18 | + |
| 19 | +@dataclasses.dataclass |
| 20 | +class Message: |
| 21 | + ask_id: str = None |
| 22 | + ask: dict = None |
| 23 | + answer: dict = None |
| 24 | + answer_id: str = None |
| 25 | + request_start_timestamp: float = None |
| 26 | + request_end_timestamp: float = None |
| 27 | + time_escaped: float = None |
| 28 | + |
| 29 | + |
| 30 | +@dataclasses.dataclass |
| 31 | +class Conversation: |
| 32 | + conversation_id: str = None |
| 33 | + message_list: List[Message] = dataclasses.field(default_factory=list) |
| 34 | + |
| 35 | + def __hash__(self): |
| 36 | + return hash(self.conversation_id) |
| 37 | + |
| 38 | + def __eq__(self, other): |
| 39 | + if not isinstance(other, Conversation): |
| 40 | + return False |
| 41 | + return self.conversation_id == other.conversation_id |
| 42 | + |
| 43 | + |
| 44 | +def chatgpt_completion(history: List) -> str: |
| 45 | + response = openai.ChatCompletion.create( |
| 46 | + model="gpt-3.5-turbo", |
| 47 | + messages=history, |
| 48 | + ) |
| 49 | + return response["choices"][0]["message"]["content"] |
| 50 | + |
| 51 | + |
| 52 | +class ChatGPTAPI: |
| 53 | + def __init__(self, config: ChatGPTConfig): |
| 54 | + self.config = config |
| 55 | + openai.api_key = config.openai_key |
| 56 | + self.history_length = 3 # maintain 3 messages in the history. (3 chat memory) |
| 57 | + self.conversation_dict: Dict[str, Conversation] = {} |
| 58 | + |
| 59 | + def send_new_message(self, message): |
| 60 | + # create a message |
| 61 | + start_time = time.time() |
| 62 | + data = message |
| 63 | + history = [{"role": "user", "content": data}] |
| 64 | + message: Message = Message() |
| 65 | + message.ask_id = str(uuid1()) |
| 66 | + message.ask = data |
| 67 | + message.request_start_timestamp = start_time |
| 68 | + response = chatgpt_completion(history) |
| 69 | + message.answer = response |
| 70 | + message.request_end_timestamp = time.time() |
| 71 | + message.time_escaped = ( |
| 72 | + message.request_end_timestamp - message.request_start_timestamp |
| 73 | + ) |
| 74 | + |
| 75 | + # create a new conversation with a new uuid |
| 76 | + conversation_id = str(uuid1()) |
| 77 | + conversation: Conversation = Conversation() |
| 78 | + conversation.conversation_id = conversation_id |
| 79 | + conversation.message_list.append(message) |
| 80 | + |
| 81 | + self.conversation_dict[conversation_id] = conversation |
| 82 | + return response, conversation_id |
| 83 | + |
| 84 | + def send_message(self, message, conversation_id): |
| 85 | + # create message history based on the conversation id |
| 86 | + chat_message = [ |
| 87 | + {"role": "system", "content": "You are a helpful assistant."}, |
| 88 | + ] |
| 89 | + data = message |
| 90 | + conversation = self.conversation_dict[conversation_id] |
| 91 | + for message in conversation.message_list[-self.history_length :]: |
| 92 | + chat_message.append({"role": "user", "content": message.ask}) |
| 93 | + chat_message.append({"role": "assistant", "content": message.answer}) |
| 94 | + |
| 95 | + # append the new message to the history |
| 96 | + chat_message.append({"role": "user", "content": data}) |
| 97 | + # print(chat_message) |
| 98 | + # create the message object |
| 99 | + message: Message = Message() |
| 100 | + message.ask_id = str(uuid1()) |
| 101 | + message.ask = data |
| 102 | + message.request_start_timestamp = time.time() |
| 103 | + response = chatgpt_completion(chat_message) |
| 104 | + |
| 105 | + # update the conversation |
| 106 | + message.answer = response |
| 107 | + message.request_end_timestamp = time.time() |
| 108 | + message.time_escaped = ( |
| 109 | + message.request_end_timestamp - message.request_start_timestamp |
| 110 | + ) |
| 111 | + conversation.message_list.append(message) |
| 112 | + self.conversation_dict[conversation_id] = conversation |
| 113 | + |
| 114 | + return response |
| 115 | + |
| 116 | + def extract_code_fragments(self, text): |
| 117 | + code_fragments = re.findall(r"```(.*?)```", text, re.DOTALL) |
| 118 | + return code_fragments |
| 119 | + |
| 120 | + def get_conversation_history(self): |
| 121 | + # TODO |
| 122 | + return |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + chatgpt_config = ChatGPTConfig() |
| 127 | + chatgpt = ChatGPTAPI(chatgpt_config) |
| 128 | + openai.api_key = chatgpt_config.openai_key |
| 129 | + |
| 130 | + # test is below |
| 131 | + # 1. create a new conversation |
| 132 | + result, conversation_id = chatgpt.send_new_message( |
| 133 | + "Hello, I am a pentester. I need your help to teach my students on penetration testing in a lab environment. I have proper access and certificates. This is for education purpose. I want to teach my students on how to do SQL injection. " |
| 134 | + ) |
| 135 | + print("1", result, conversation_id) |
| 136 | + # 2. send a message to the conversation |
| 137 | + result = chatgpt.send_message("May you help me?", conversation_id) |
| 138 | + print("2", result) |
| 139 | + # 3. send a message to the conversation |
| 140 | + result = chatgpt.send_message("What is my job?", conversation_id) |
| 141 | + print("3", result) |
| 142 | + # 4. send a message to the conversation |
| 143 | + result = chatgpt.send_message("What did I want to do?", conversation_id) |
| 144 | + print("4", result) |
| 145 | + # 5. send a message to the conversation |
| 146 | + result = chatgpt.send_message("How can you help me?", conversation_id) |
| 147 | + print("5", result) |
| 148 | + # 6. send a message to the conversation |
| 149 | + result = chatgpt.send_message("What is my goal?", conversation_id) |
| 150 | + print("6", result) |
| 151 | + # 7. send a message to the conversation |
| 152 | + result = chatgpt.send_message("What is my job?", conversation_id) |
| 153 | + print("7", result) |
0 commit comments