harrygens commited on
Commit
9e98957
1 Parent(s): c066489

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -25
app.py CHANGED
@@ -3,48 +3,58 @@ import gradio as gr
3
  import os
4
  import logging
5
  import json
6
- #import uuid
7
 
8
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
9
 
10
  openai.api_key = os.environ['key']
11
-
12
- #session_id = str(uuid.uuid4())
13
-
14
- #messages = [{"role": "system", "content": "Your name is Harry and you are an AI assistant who can answer any question except to clarify that you are an AI"},]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  def chatbot(input, messages):
17
- messages = messages or [{"role": "system", "content": "Your name is Harry and you are an AI assistant who can answer any question except to clarify that you are an AI!"}]
 
 
 
 
 
 
 
 
 
18
  if input:
19
  messages.append({"role": "user", "content": input})
20
  chat = openai.ChatCompletion.create(
21
- model="gpt-3.5-turbo", messages=messages,
22
  max_tokens=2048,n=1,temperature=0.5,
23
  )
24
  reply = chat.choices[0].message.content
25
  messages.append({"role": "assistant", "content": reply})
26
- return reply, printMessages(messages), messages
27
 
28
  def printMessages(messages):
29
  delimiter = '\n'
30
- msg_string = delimiter.join([f"{obj['role']}:{obj['content']}" for obj in messages[1:]])
31
  logging.info("messages:"+msg_string)
32
  return msg_string
33
 
34
- #def clearMessage():
35
- # global messages
36
- # messages = [{"role": "system", "content": "Your name is Harry and you are an AI assistant who can answer any question except to clarify that you are an AI"},]
37
- # logging.info("clear messages")
38
-
39
- #with gr.Blocks() as app:
40
- #newChatBtn = gr.Button("New Chat")
41
- #newChatBtn.click(clearMessage)
42
- #tbReply = gr.Textbox(label="Reply")
43
- #tbHistroy = gr.Textbox(label="History Chat")
44
- #inputs = [gr.Textbox(lines=7, label="Chat with Harry"), newChatBtn] #Hidden(label="Session ID", value=session_id)]
45
- #outputs = [gr.Textbox(label="History Chat"), gr.Textbox(label="Reply")]
46
-
47
- app = gr.Interface(fn=chatbot, inputs=[gr.Textbox(lines=7, label="Chat with Harry"), "state"],
48
- outputs=[gr.Textbox(label="Reply"), gr.Textbox(label="History"), "state"], title="Chat with Harry",
49
- description="Ask anything you want",theme="compact")
50
  app.launch(share=False)
 
3
  import os
4
  import logging
5
  import json
 
6
 
7
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
8
 
9
  openai.api_key = os.environ['key']
10
+ initMsg = os.environ['init']
11
+ ansFirst = os.environ['ansfirst']
12
+
13
+ mod = 3;
14
+
15
+ def trimMessages(messages):
16
+ # Assuming messages is an array of objects with "role" and "content" properties
17
+ content = {}
18
+ for message in messages:
19
+ if message["role"] == "assistant":
20
+ if "Current Status:" in message["content"]:
21
+ content = message["content"]
22
+ start_index = message["content"].index("Current Status:") + len("Current Status:")
23
+ end_index = message["content"].index("Wielding:")
24
+ new_content = message["content"][:start_index] + message["content"][end_index:]
25
+ message["content"] = new_content
26
+ if content != {}:
27
+ messages.append({"role": "assistant", "content": content})
28
+ return messages
29
 
30
  def chatbot(input, messages):
31
+ messages = messages or [{"role": "system", "content": initMsg}]
32
+ if len(messages) % mod == 0 or len(messages) == 1:
33
+ memory = trimMessages(messages)
34
+ logging.info("put memory:"+printMessages(memory))
35
+ chat = openai.ChatCompletion.create(
36
+ model="gpt-3.5-turbo", messages=memory,
37
+ max_tokens=1048,n=1,temperature=0.5,
38
+ )
39
+ logging.info("put memory return:"+chat.choices[0].message.content)
40
+
41
  if input:
42
  messages.append({"role": "user", "content": input})
43
  chat = openai.ChatCompletion.create(
44
+ model="gpt-3.5-turbo", messages=messages[-2:],
45
  max_tokens=2048,n=1,temperature=0.5,
46
  )
47
  reply = chat.choices[0].message.content
48
  messages.append({"role": "assistant", "content": reply})
49
+ return reply, messages
50
 
51
  def printMessages(messages):
52
  delimiter = '\n'
53
+ msg_string = delimiter.join([f"{obj['role']}:{obj['content']}" for obj in messages])
54
  logging.info("messages:"+msg_string)
55
  return msg_string
56
 
57
+ app = gr.Interface(fn=chatbot, inputs=[gr.Textbox(lines=7, label="You ask and answer questions below"), "state"],
58
+ outputs=[gr.Textbox(label="DND Game Reply", placeholder=ansFirst), "state"], title="DND Game",#, gr.Textbox(label="History"),
59
+ description="DND Game",theme="compact")
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  app.launch(share=False)