Skip to content

Commit

Permalink
fix prompt not include history issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin021 committed Feb 3, 2023
1 parent f4aa037 commit 3576d63
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
8 changes: 5 additions & 3 deletions server/chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ def ask(self, user_request: str) -> dict:
}
"""
prompt = self.prompt.construct_prompt(user_request)

completion = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
temperature=self.temprature,
max_tokens=1024,
stop=["\n\n\n"],
stop=["\n"],
)
if completion.get("choices") is None:
raise Exception("ChatGPT API returned no choices")
Expand All @@ -56,11 +57,12 @@ def ask(self, user_request: str) -> dict:
self.prompt.add_to_chat_history(
"You: "
+ user_request
+ "\n\n\n"
+ "\n"
+ "ChatGPT: "
+ completion["choices"][0]["text"]
+ "\n\n\n",
+ "\n",
)
print(self.prompt.history())
return completion

def rollback(self, num: int) -> None:
Expand Down
2 changes: 1 addition & 1 deletion server/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def history(self) -> str:
"""
Return chat history
"""
return "\n\n\n\n".join(self.chat_history)
return "\n".join(self.chat_history)

def construct_prompt(self, new_prompt: str) -> str:
"""
Expand Down
7 changes: 4 additions & 3 deletions server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
quit(0)

app = Flask(__name__)
chatbot = Chatbot(api_key=OPEN_AI_KEY,
temprature=TEMPRATURE, base_prompt=base_prompt)

@app.route("/chat", methods=["GET"])
@cross_origin()
Expand Down Expand Up @@ -50,15 +52,14 @@ def chatbot_commands(cmd: str) -> bool:
user_request = request.args.get('q')
# decode the `q` parameter from UTF-8 encoding
user_request = urllib.parse.unquote(user_request)
chatbot = Chatbot(api_key=OPEN_AI_KEY,
temprature=TEMPRATURE, base_prompt=base_prompt)

# Start chat
PROMPT = user_request
if PROMPT.startswith("!"):
if chatbot_commands(PROMPT):
print("continue")
response = chatbot.ask(PROMPT)
print("ChatGPT: " + response["choices"][0]["text"])
# print("ChatGPT: " + response["choices"][0]["text"])
message = response["choices"][0]["text"]
message = message.replace("\n\n", "")
# print(message)
Expand Down

0 comments on commit 3576d63

Please sign in to comment.