Spaces:
Sleeping
Sleeping
Commit
·
e8fba33
1
Parent(s):
db491a7
Add chat history
Browse files
app.py
CHANGED
|
@@ -8,7 +8,8 @@ from chainlit.prompt import Prompt, PromptMessage # importing prompt tools
|
|
| 8 |
from chainlit.playground.providers import ChatOpenAI # importing ChatOpenAI tools
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
|
| 11 |
-
load_dotenv()
|
|
|
|
| 12 |
|
| 13 |
# ChatOpenAI Templates
|
| 14 |
system_template = """You are a helpful assistant who always speaks in a pleasant tone!
|
|
@@ -30,17 +31,25 @@ async def start_chat():
|
|
| 30 |
"presence_penalty": 0,
|
| 31 |
}
|
| 32 |
|
|
|
|
|
|
|
| 33 |
cl.user_session.set("settings", settings)
|
|
|
|
| 34 |
|
| 35 |
|
| 36 |
@cl.on_message # marks a function that should be run each time the chatbot receives a message from a user
|
| 37 |
async def main(message: cl.Message):
|
| 38 |
settings = cl.user_session.get("settings")
|
|
|
|
| 39 |
|
| 40 |
client = AsyncOpenAI()
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
print(message.content)
|
| 43 |
|
|
|
|
| 44 |
prompt = Prompt(
|
| 45 |
provider=ChatOpenAI.id,
|
| 46 |
messages=[
|
|
@@ -63,15 +72,22 @@ async def main(message: cl.Message):
|
|
| 63 |
|
| 64 |
msg = cl.Message(content="")
|
| 65 |
|
| 66 |
-
# Call OpenAI
|
| 67 |
async for stream_resp in await client.chat.completions.create(
|
| 68 |
-
messages=
|
| 69 |
):
|
| 70 |
token = stream_resp.choices[0].delta.content
|
| 71 |
if not token:
|
| 72 |
token = ""
|
| 73 |
await msg.stream_token(token)
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
# Update the prompt object with the completion
|
| 76 |
prompt.completion = msg.content
|
| 77 |
msg.prompt = prompt
|
|
|
|
| 8 |
from chainlit.playground.providers import ChatOpenAI # importing ChatOpenAI tools
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
|
| 11 |
+
load_dotenv(override=True)
|
| 12 |
+
print("OPENAI_API_KEY:", os.getenv("OPENAI_API_KEY"))
|
| 13 |
|
| 14 |
# ChatOpenAI Templates
|
| 15 |
system_template = """You are a helpful assistant who always speaks in a pleasant tone!
|
|
|
|
| 31 |
"presence_penalty": 0,
|
| 32 |
}
|
| 33 |
|
| 34 |
+
# Initialize conversation history in the user session
|
| 35 |
+
conversation_history = [{"role": "system", "content": system_template}]
|
| 36 |
cl.user_session.set("settings", settings)
|
| 37 |
+
cl.user_session.set("conversation_history", conversation_history)
|
| 38 |
|
| 39 |
|
| 40 |
@cl.on_message # marks a function that should be run each time the chatbot receives a message from a user
|
| 41 |
async def main(message: cl.Message):
|
| 42 |
settings = cl.user_session.get("settings")
|
| 43 |
+
conversation_history = cl.user_session.get("conversation_history")
|
| 44 |
|
| 45 |
client = AsyncOpenAI()
|
| 46 |
|
| 47 |
+
# Append user input to the conversation history
|
| 48 |
+
conversation_history.append({"role": "user", "content": message.content})
|
| 49 |
+
|
| 50 |
print(message.content)
|
| 51 |
|
| 52 |
+
# Prepare Chainlit prompt for visualization/debugging (optional)
|
| 53 |
prompt = Prompt(
|
| 54 |
provider=ChatOpenAI.id,
|
| 55 |
messages=[
|
|
|
|
| 72 |
|
| 73 |
msg = cl.Message(content="")
|
| 74 |
|
| 75 |
+
# Call OpenAI with conversation history
|
| 76 |
async for stream_resp in await client.chat.completions.create(
|
| 77 |
+
messages=conversation_history, stream=True, **settings
|
| 78 |
):
|
| 79 |
token = stream_resp.choices[0].delta.content
|
| 80 |
if not token:
|
| 81 |
token = ""
|
| 82 |
await msg.stream_token(token)
|
| 83 |
|
| 84 |
+
# Update the assistant's reply in the conversation history
|
| 85 |
+
assistant_reply = msg.content
|
| 86 |
+
conversation_history.append({"role": "assistant", "content": assistant_reply})
|
| 87 |
+
|
| 88 |
+
# Save updated conversation history back to the user session
|
| 89 |
+
cl.user_session.set("conversation_history", conversation_history)
|
| 90 |
+
|
| 91 |
# Update the prompt object with the completion
|
| 92 |
prompt.completion = msg.content
|
| 93 |
msg.prompt = prompt
|