-
Notifications
You must be signed in to change notification settings - Fork 3
/
chatgpt.py
168 lines (146 loc) · 5.64 KB
/
chatgpt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# -*- coding: utf-8 -*-
# file: chatgpt.py
# time: 13:09 16/05/2023
# author: YANG, HENG <[email protected]> (杨恒)
# github: https://github.com/yangheng95
# huggingface: https://huggingface.co/yangheng
# google scholar: https://scholar.google.com/citations?user=NPq5a_0AAAAJ&hl=en
# Copyright (C) 2019-2023. All Rights Reserved.
import json
import logging
import os
import time
from hashlib import sha256
import openai
class Chatbot:
def __init__(self, system_prompt=None, model='gpt-3.5-turbo', log_dir='logs', api_key=None, **kwargs):
print(os.environ)
try:
assert os.environ.get("OPENAI_API_KEY"), "Please set OPENAI_API_KEY environment variable."
openai.api_key = os.environ.get("OPENAI_API_KEY")
except Exception as e:
print("Please set OPENAI_API_KEY environment variable.")
openai.api_key = api_key
self._chatbot = None
self._model = model
if system_prompt:
self.system_prompts = [
{'role': 'system', 'content': system_prompt},
]
else:
self.system_prompts = [
{'role': 'system', 'content': 'You are ChatGPT, a very helpful assistant.'},
]
self.messages = []
if not os.path.exists(log_dir):
os.makedirs(log_dir)
self._logger = logging.getLogger(__name__)
self._logger.addHandler(
logging.FileHandler('{}/chatbot.log'.format(
log_dir)
)
)
self._logger.setLevel(logging.INFO)
def chat(self,
prompt,
max_tokens=500,
temperature=1,
top_p=1,
frequency_penalty=0,
presence_penalty=0.6,
stop=['<EndofConversation>'],
**kwargs
):
if kwargs.get('system_prompt'):
self.system_prompts = [
{'role': 'system', 'content': kwargs.get('system_prompt')},
]
_messages = [
{"role": "user", "content": prompt
}]
_response = openai.ChatCompletion.create(
model=self._model,
messages=self.system_prompts + _messages,
max_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
frequency_penalty=frequency_penalty,
presence_penalty=presence_penalty,
stop=stop
)
info = json.dumps(
{
'tag': kwargs.get('tag', 'Debug'),
'prompt': prompt,
'response': _response.choices[0].message.content,
'timestamp': time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
}
)
# print(info)
self._logger.info(info)
return _response.choices[0].message.content
def chat_with_history(self,
prompt,
max_tokens=500,
temperature=1,
top_p=1,
frequency_penalty=0,
presence_penalty=0.6,
stop=['<EndofConversation>'],
**kwargs
):
if kwargs.get('system_prompt'):
self.system_prompts = [
{'role': 'system', 'content': kwargs.get('system_prompt')},
]
self.messages.append({"role": "user", "content": prompt})
_response = openai.ChatCompletion.create(
model=self._model,
messages=self.system_prompts + self.messages,
max_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
frequency_penalty=frequency_penalty,
presence_penalty=presence_penalty,
stop=stop
)
self.messages.append(dict(_response.choices[0].message))
info = json.dumps(
{
'tag': kwargs.get('tag', 'Debug'),
'prompt': prompt,
'response': _response.choices[0].message.content,
'messages': self.messages,
'timestamp': time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
}
)
# print(info)
self._logger.info(info)
return _response.choices[0].message.content
if __name__ == '__main__':
import flask
from flask import request, jsonify
app = flask.Flask(__name__)
chatbot = Chatbot(system_prompt=None, model='gpt-3.5-turbo-0613')
@app.route('/chat', methods=['POST'])
def chat():
# try:
if not request.args.get('password'):
return jsonify({"error": "No password"})
if sha256(request.args.get(
'password').encode()).hexdigest() != "01cb92dfff4091c2bee0f343b2af049fb39b45c08a1e5132b834e12e037d919d":
return jsonify({"error": "Wrong password"})
prompt = request.args.get('prompt')
print('message_history', request.args.getlist('message_history'))
if request.args.getlist('message_history'):
chatbot.messages = eval(request.args.get('message_history'))
print(chatbot.messages)
else:
chatbot.messages = []
response = chatbot.chat_with_history(prompt, tag=request.args.get('tag'), max_tokens=2000)
return jsonify({"response": response, "message_history": json.dumps(chatbot.messages)})
# except Exception as e:
# return jsonify({"error": str(e)})
app.run(port=6789, debug=False)
# TEST FLASK API
# curl -i -H "Content-Type: application/json" -X POST -d '{"prompt":"Hello, my name is"}' http://localhost:6789/chat