Skip to content

Commit 5eda54d

Browse files
Update gpt-3.5-turbo model
1 parent 38c5fa0 commit 5eda54d

File tree

7 files changed

+44
-29
lines changed

7 files changed

+44
-29
lines changed

.env.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
OPENAI_API =
2-
OPENAI_MODEL_ENGINE = 'text-davinci-003'
3-
OPENAI_MAX_TOKENS = 1024
2+
OPENAI_MODEL_ENGINE = 'gpt-3.5-turbo'
3+
SYSTEM_MESSAGE = 'You are a helpful assistant.'
44
LINE_CHANNEL_SECRET =
55
LINE_CHANNEL_ACCESS_TOKEN =

README.en.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
[![license](https://img.shields.io/pypi/l/ansicolortags.svg)](LICENSE) [![Release](https://img.shields.io/github/v/release/TheExplainthis/ChatGPT-Line-Bot)](https://github.com/TheExplainthis/ChatGPT-Line-Bot/releases/)
66

7+
8+
## Update
9+
- 2023/03/03 Model change to chat completion: `gpt-3.5-turbo`
10+
11+
712
## Introduction
813
Import the ChatGPT bot to Line and start interacting with it by simply typing text in the input box. In addition to ChatGPT, the model for DALL·E 2 is also integrated. Enter `/imagine + text` to return the corresponding image, as shown in the figure below:
914

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
[![license](https://img.shields.io/pypi/l/ansicolortags.svg)](LICENSE) [![Release](https://img.shields.io/github/v/release/TheExplainthis/ChatGPT-Line-Bot)](https://github.com/TheExplainthis/ChatGPT-Line-Bot/releases/)
66

7+
8+
## 更新
9+
- 2023/03/03 模型換成 chat completion: `gpt-3.5-turbo`
10+
11+
712
## 介紹
813
在 Line 中去導入 ChatGPT Bot,只要在輸入框直接輸入文字,即可與 ChatGPT 開始互動,除了 ChatGPT 以外,也直接串上了 DALL·E 2 的模型,輸入 `/imagine + 文字`,就會回傳相對應的圖片,如下圖所示:
914

main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
line_bot_api = LineBotApi(os.getenv('LINE_CHANNEL_ACCESS_TOKEN'))
2323
handler = WebhookHandler(os.getenv('LINE_CHANNEL_SECRET'))
2424

25-
models = OpenAIModel(api_key=os.getenv('OPENAI_API'), model_engine=os.getenv('OPENAI_MODEL_ENGINE'), max_tokens=int(os.getenv('OPENAI_MAX_TOKENS')))
25+
models = OpenAIModel(api_key=os.getenv('OPENAI_API'), model_engine=os.getenv('OPENAI_MODEL_ENGINE'))
2626

27-
memory = Memory()
27+
memory = Memory(system_message=os.getenv('SYSTEM_MESSAGE'))
2828
chatgpt = ChatGPT(models, memory)
2929
dalle = DALLE(models)
3030

src/chatgpt.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33

44

55
class ChatGPT:
6-
def __init__(self, model: ModelInterface, memory: MemoryInterface = None):
6+
def __init__(self, model: ModelInterface, memory: MemoryInterface):
77
self.model = model
88
self.memory = memory
99

1010
def get_response(self, user_id: str, text: str) -> str:
11-
prompt = text if self.memory is None else f'{self.memory.get(user_id)}\n\n{text}'
12-
response = self.model.text_completion(f'{prompt} <|endoftext|>')
13-
if self.memory is not None:
14-
self.memory.append(user_id, prompt)
15-
self.memory.append(user_id, response)
16-
return response
11+
self.memory.append(user_id, {'role': 'user', 'content': text})
12+
response = self.model.chat_completion(self.memory.get(user_id))
13+
role = response['choices'][0]['message']['role']
14+
content = response['choices'][0]['message']['content']
15+
self.memory.append(user_id, {'role': role, 'content': content})
16+
return content
1717

1818
def clean_history(self, user_id: str) -> None:
1919
self.memory.remove(user_id)

src/memory.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
from typing import Dict
12
from collections import defaultdict
23

34

45
class MemoryInterface:
5-
def append(self, user_id: str, text: str) -> None:
6+
def append(self, user_id: str, message: Dict) -> None:
67
pass
78

89
def get(self, user_id: str) -> str:
@@ -13,15 +14,23 @@ def remove(self, user_id: str) -> None:
1314

1415

1516
class Memory(MemoryInterface):
16-
def __init__(self):
17+
def __init__(self, system_message):
1718
self.storage = defaultdict(list)
19+
self.system_message = system_message
1820

19-
def append(self, user_id: str, text: str) -> None:
20-
self.storage[user_id].append(text)
21+
def initialize(self, user_id: str):
22+
self.storage[user_id] = [{
23+
'role': 'system', 'content': self.system_message
24+
}]
25+
26+
def append(self, user_id: str, message: Dict) -> None:
27+
print(user_id)
28+
if self.storage[user_id] == []:
29+
self.initialize(user_id)
30+
self.storage[user_id].append(message)
2131

2232
def get(self, user_id: str) -> str:
23-
HISTORY_MESSAGE_COUNT = 3
24-
return '\n\n'.join(self.storage.get(user_id, [])[-HISTORY_MESSAGE_COUNT:])
33+
return self.storage[user_id]
2534

2635
def remove(self, user_id: str) -> None:
2736
self.storage[user_id] = []

src/models.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
1+
from typing import List, Dict
12
import openai
23

34

45
class ModelInterface:
5-
def text_completion(self, prompt: str) -> str:
6+
def chat_completion(self, messages: List[Dict]) -> str:
67
pass
78

89
def image_generation(self, prompt: str) -> str:
910
pass
1011

1112

1213
class OpenAIModel(ModelInterface):
13-
def __init__(self, api_key: str, model_engine: str, max_tokens: int = 128, image_size: str = '512x512'):
14+
def __init__(self, api_key: str, model_engine: str, image_size: str = '512x512'):
1415
openai.api_key = api_key
1516
self.model_engine = model_engine
16-
self.max_tokens = max_tokens
1717
self.image_size = image_size
1818

19-
def text_completion(self, prompt: str) -> str:
20-
response = openai.Completion.create(
21-
engine=self.model_engine,
22-
prompt=prompt,
23-
max_tokens=self.max_tokens,
24-
stop=None,
25-
temperature=0.5,
19+
def chat_completion(self, messages) -> str:
20+
response = openai.ChatCompletion.create(
21+
model=self.model_engine,
22+
messages=messages
2623
)
27-
text = response.choices[0].text.strip()
28-
return text
24+
return response
2925

3026
def image_generation(self, prompt: str) -> str:
3127
response = openai.Image.create(

0 commit comments

Comments
 (0)