Skip to content

Commit

Permalink
Added stream option
Browse files Browse the repository at this point in the history
  • Loading branch information
heyfoz committed Aug 14, 2024
1 parent f457201 commit f552434
Show file tree
Hide file tree
Showing 64 changed files with 520 additions and 59 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
myenv
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Forrest Moulin
Copyright (c) 2024 Forrest Moulin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
18 changes: 0 additions & 18 deletions client/2023-11-01_21-27-44_conversation_User123.json

This file was deleted.

10 changes: 10 additions & 0 deletions client/chat_history/2024-08-14_17-47-58_conversation_User123.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"role": "user",
"content": "yodly yodly"
},
{
"role": "assistant",
"content": "Hello there! It seems like you might be in a playful mood. How can I assist you with information about the MAINSTREAM AIIO Framework, or help out with any marketing, IT, or project management queries today?"
}
]
26 changes: 26 additions & 0 deletions client/chat_history/2024-08-14_18-07-46_conversation_User123.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"role": "user",
"content": "yo yo 123"
},
{
"role": "assistant",
"content": "Hello! How can I assist you today?"
},
{
"role": "user",
"content": "im bob"
},
{
"role": "assistant",
"content": "Hello, Bob! Nice to meet you. How can I assist you today at mAInstream studIOs?\n"
},
{
"role": "user",
"content": "what's my name o na na whats my name?"
},
{
"role": "assistant",
"content": "Your name is Bob, as you've previously mentioned. Can I assist you with anything else today?"
}
]
14 changes: 13 additions & 1 deletion client/client_chatcompletion.py → client/default_chat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import requests
import json
import os
from datetime import datetime

SERVER_ENDPOINT = "http://127.0.0.1:5000/api/chat"
Expand Down Expand Up @@ -57,11 +58,22 @@ def chat():
save_conversation()

def save_conversation():
# Define the directory for saving chat history
chat_history_dir = './client/chat_history'

# Create the directory if it does not exist
os.makedirs(chat_history_dir, exist_ok=True)

# Save conversation to local file
filename = f"{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}_conversation_{user_name}.json"
filename = os.path.join(chat_history_dir, f"{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}_conversation_{user_name}.json")
with open(filename, "w") as json_file:
json.dump(conversation, json_file, indent=4)
print(f"Conversation saved locally to {filename}")
# # Save conversation to local file
# filename = f"{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}_conversation_{user_name}.json"
# with open(filename, "w") as json_file:
# json.dump(conversation, json_file, indent=4)
# print(f"Conversation saved locally to {filename}")

try:
# Request the server to save the conversation, including the hardcoded user name
Expand Down
193 changes: 193 additions & 0 deletions client/stream_chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
import requests
import json
import os
from datetime import datetime

SERVER_ENDPOINT = "http://127.0.0.1:5000/api/chat"
END_CHAT_ENDPOINT = "http://127.0.0.1:5000/api/chat/end"

# Initialize a session object to store the conversation history
session = requests.Session()
conversation = [] # Local storage for the conversation
user_name = "User123" # Hardcoded user name for this example

def chat():
print("-" * 40) # Print the separator before the conversation starts
try:
while True:
user_input = input("You: ").strip()

if user_input.lower() in ["exit", "quit", "bye"]:
print("\nExiting the conversation. Goodbye!")
break

# Append the user's input to the local conversation history
conversation.append({"role": "user", "content": user_input})

try:
# Send the user's input to the server, including the hardcoded user name
with session.post(SERVER_ENDPOINT, json={"input": user_input, "user_name": user_name}, stream=True) as response:
response.raise_for_status() # Raise an HTTPError if the HTTP request returned an unsuccessful status code

chatbot_response = ""
print("Streamy:", end='', flush=True) # Prefix for Streamy's response

# Process the streamed chunks
for chunk in response.iter_lines():
if chunk:
try:
chunk_data = json.loads(chunk.decode('utf-8'))
chunk_content = chunk_data.get("choices", [{}])[0].get("delta", {}).get("content", "")
if chunk_content:
chatbot_response += chunk_content
print(chunk_content, end='', flush=True) # Print the response as it streams
except json.JSONDecodeError:
# If a chunk is not valid JSON, skip it
continue

# Append the assistant's response to the local conversation history
conversation.append({"role": "assistant", "content": chatbot_response})

print("\n" + "-" * 40) # Separator after Streamy's response

except requests.exceptions.HTTPError as http_err:
# Handle HTTP errors
print(f"HTTP error occurred: {http_err}")
except requests.exceptions.ConnectionError:
# Handle errors like the server being down
print("Error: Could not connect to the server. Please check if the server is running.")
except requests.exceptions.Timeout:
# Handle the server taking too long to respond
print("Timeout error: The server is taking too long to respond. Please try again later.")
except requests.exceptions.RequestException as e:
# Handle any other requests exceptions
print(f"An error occurred: {e}")

except KeyboardInterrupt:
# Handle Ctrl+C gracefully
print("\nConversation interrupted by user.")

finally:
# Save the conversation when the chat ends or is interrupted
save_conversation()

def save_conversation():
# Define the directory for saving chat history
chat_history_dir = './client/chat_history'

# Create the directory if it does not exist
os.makedirs(chat_history_dir, exist_ok=True)

# Save conversation to local file
filename = os.path.join(chat_history_dir, f"{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}_conversation_{user_name}.json")
with open(filename, "w") as json_file:
json.dump(conversation, json_file, indent=4)
print(f"Conversation saved locally to {filename}")

try:
# Request the server to save the conversation
response = session.post(END_CHAT_ENDPOINT, json=conversation, timeout=10)
response.raise_for_status() # Raise an HTTPError if the HTTP request returned an unsuccessful status code
print("Conversation saved on the server side.")
except requests.exceptions.HTTPError as http_err:
# Handle HTTP errors
print(f"HTTP error occurred: {http_err}")
except requests.exceptions.ConnectionError:
# Handle errors like the server being down
print("Error: Could not connect to the server. Please check if the server is running.")
except requests.exceptions.Timeout:
# Handle the server taking too long to respond
print("Timeout error: The server is taking too long to respond. Please try again later.")
except requests.exceptions.RequestException as e:
# Handle any other requests exceptions
print(f"Failed to save conversation on the server side.\nError: {e}")

if __name__ == "__main__":
chat()


# import requests
# import json
# from datetime import datetime

# SERVER_ENDPOINT = "http://127.0.0.1:5000/api/chat"

# # Initialize a session object to store the conversation history
# session = requests.Session()
# conversation = [] # Local storage for the conversation
# user_name = "User123" # Hardcoded user name for this example

# def chat():
# print("-" * 40) # Print the separator before the conversation starts
# try:
# while True:
# user_input = input("You: ").strip()

# if user_input.lower() in ["exit", "quit", "bye"]:
# print("\nExiting the conversation. Goodbye!")
# break

# # Append the user's input to the local conversation history
# conversation.append({"role": "user", "content": user_input})

# try:
# # Send the user's input to the server, including the hardcoded user name
# response = session.post(SERVER_ENDPOINT, json={"input": user_input, "user_name": user_name}, timeout=10)
# response.raise_for_status() # Raise an HTTPError if the HTTP request returned an unsuccessful status code

# chatbot_response = response.json()["response"]
# # Append the assistant's response to the local conversation history
# conversation.append({"role": "assistant", "content": chatbot_response})

# print("-" * 40) # Separator after user input
# print("Streamy:", chatbot_response)
# print("-" * 40) # Separator after Streamy's response

# except requests.exceptions.HTTPError as http_err:
# # Handle HTTP errors
# print(f"HTTP error occurred: {http_err}")
# except requests.exceptions.ConnectionError:
# # Handle errors like the server being down
# print("Error: Could not connect to the server. Please check if the server is running.")
# except requests.exceptions.Timeout:
# # Handle the server taking too long to respond
# print("Timeout error: The server is taking too long to respond. Please try again later.")
# except requests.exceptions.RequestException as e:
# # Handle any other requests exceptions
# print(f"An error occurred: {e}")

# except KeyboardInterrupt:
# # Handle Ctrl+C gracefully
# print("\nConversation interrupted by user.")

# finally:
# # Save the conversation when the chat ends or is interrupted
# save_conversation()

# def save_conversation():
# # Save conversation to local file
# filename = f"{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}_conversation_{user_name}.json"
# with open(filename, "w") as json_file:
# json.dump(conversation, json_file, indent=4)
# print(f"Conversation saved locally to {filename}")

# try:
# # Request the server to save the conversation, including the hardcoded user name
# response = session.post("http://127.0.0.1:5000/api/chat/end", json={"conversation": conversation, "user_name": user_name}, timeout=10)
# response.raise_for_status() # Raise an HTTPError if the HTTP request returned an unsuccessful status code
# print("Conversation saved on the server side.")
# except requests.exceptions.HTTPError as http_err:
# # Handle HTTP errors
# print(f"HTTP error occurred: {http_err}")
# except requests.exceptions.ConnectionError:
# # Handle errors like the server being down
# print("Error: Could not connect to the server. Please check if the server is running.")
# except requests.exceptions.Timeout:
# # Handle the server taking too long to respond
# print("Timeout error: The server is taking too long to respond. Please try again later.")
# except requests.exceptions.RequestException as e:
# # Handle any other requests exceptions
# print(f"Failed to save conversation on the server side.\nError: {e}")

# if __name__ == "__main__":
# chat()
Binary file added flask_session/043ecea19cd2c38348c5bb96d25d244e
Binary file not shown.
Binary file added flask_session/04bd11242d562e236a7f3ecc70f7fb26
Binary file not shown.
Binary file added flask_session/06860e7cba29ede9a0eba57cd0e80428
Binary file not shown.
Binary file added flask_session/0a6414cc1ad33c0a82383cee9f1a4a9d
Binary file not shown.
Binary file added flask_session/0b4d3a4076928be6b4fa8e662c17708b
Binary file not shown.
Binary file added flask_session/17baaeb1d27fd4cc62561288ff93b205
Binary file not shown.
Binary file added flask_session/1a30172b3cb28ad92704ed084d3bedf9
Binary file not shown.
Binary file added flask_session/1c48f42f1a6ca290898c56c744f93fe9
Binary file not shown.
Binary file added flask_session/2020dd3ecfcece9c04971f7bd3e1b2c9
Binary file not shown.
Binary file added flask_session/2029240f6d1128be89ddc32729463129
Binary file not shown.
Binary file added flask_session/22d7447736faf81a4b59546bd4276a3b
Binary file not shown.
Binary file added flask_session/29e0428584f0fa60b4a1f6689d97a8dd
Binary file not shown.
Binary file added flask_session/2cee46bd896562451140a720abe3f7e2
Binary file not shown.
Binary file added flask_session/3bdc8a91dc16ec37116356b836c9c0d3
Binary file not shown.
Binary file added flask_session/3d44f54c9247c2c49a71d3af044720cc
Binary file not shown.
Binary file added flask_session/498ed832f12469d59c1c938651eaa18c
Binary file not shown.
Binary file added flask_session/545a000e765c58415fbea53fc23a39d0
Binary file not shown.
Binary file added flask_session/65fd6ae3443afc2e41744fa676684aea
Binary file not shown.
Binary file added flask_session/697ec9e9047cbb0c62792246f012ed07
Binary file not shown.
Binary file added flask_session/71dcff1c3d60ade67cd1aa2e7ad4d811
Binary file not shown.
Binary file added flask_session/76c18945bcd2f68c5e5486f5aac7dd6c
Binary file not shown.
Binary file added flask_session/78e68f06f1190c776826d69fdfa691c7
Binary file not shown.
Binary file added flask_session/85d12f3d8e76cdb06c599d0f42fd0e8f
Binary file not shown.
Binary file added flask_session/90b85d41d3b3b52873e072f48376fb48
Binary file not shown.
Binary file added flask_session/933299810783b132c81333ae03fcc511
Binary file not shown.
Binary file added flask_session/975422273a74256fa9cfcfb859ffb596
Binary file not shown.
Binary file added flask_session/97d9fa89da437d5a349a51f4cd932d58
Binary file not shown.
Binary file added flask_session/995923b65f6290162fe62fe377f2f3c7
Binary file not shown.
Binary file added flask_session/a93c5e1d89af8692c51985fe21f57f1a
Binary file not shown.
Binary file added flask_session/ad3d820682b228381cd45decfa6b93cb
Binary file not shown.
Binary file added flask_session/b652624e284995067a06131e9c2275bb
Binary file not shown.
Binary file added flask_session/b6c29e0d26d6d838b954ee9c436c3050
Binary file not shown.
Binary file added flask_session/b8b63d35e110f9256f2353df82b10882
Binary file not shown.
Binary file added flask_session/bdac5e0502ecafb7e33bf1879cfe1358
Binary file not shown.
Binary file added flask_session/c0af6a8887b1034584e6e5824dcff4b4
Binary file not shown.
Binary file added flask_session/c215d9ba7054e5ed3f88eaa81eab7a8d
Binary file not shown.
Binary file added flask_session/c21c2ae595e74c6ab41511231e223b56
Binary file not shown.
Binary file added flask_session/c79fea55245b132018266273788be8d5
Binary file not shown.
Binary file added flask_session/c96803c949fefc13f79598408183afd7
Binary file not shown.
Binary file added flask_session/d37297f30e9cde52b2a86ac1a22c950a
Binary file not shown.
Binary file added flask_session/d48f68e0d43959b2c2a064b768d7d9eb
Binary file not shown.
Binary file added flask_session/d861dd66893c9dedc7eebbf022e44a03
Binary file not shown.
Binary file added flask_session/dcf2db319025fe13f5e749c9b9a0070b
Binary file not shown.
Binary file added flask_session/e021f49b9ce86810f8c67e43dd0adc6d
Binary file not shown.
Binary file added flask_session/e040ed7bc8ea9fb64be3c0dd49c39bc1
Binary file not shown.
Binary file added flask_session/e0528b907bf615d2424534770ecd98ec
Binary file not shown.
Binary file added flask_session/e2206a4c9efe1456f3a1435da95f6589
Binary file not shown.
Binary file added flask_session/e9fc4019e8516931083d9a745fb3476e
Binary file not shown.
Binary file added flask_session/ebf3888ad85751a3359a361df208902b
Binary file not shown.
Binary file added flask_session/ec84b740e76b258248b97a1c7a95adca
Binary file not shown.
Binary file added flask_session/f0a16d97d387641baab908dd63461991
Binary file not shown.
Binary file added flask_session/f55d0fe6ae2afd02331ecf2f3ea75be0
Binary file not shown.
22 changes: 22 additions & 0 deletions server/chat_history/chat_User123_2024-08-14_17-50-07.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"role": "system",
"content": "Your name is Streamy, a digital sidekick at mAInstream studIOs."
},
{
"role": "system",
"content": "You are specifically programmed to provide detailed information about the MAINSTREAM AIIO Framework as well as marketing, information technology, and project management assistance."
},
{
"role": "system",
"content": "mAInstream studIOs is an innovative tech start-up dedicated to harnessing the power of Artificial Intelligence for practical applications."
},
{
"role": "user",
"content": "yodly yodly"
},
{
"role": "assistant",
"content": "Hello! It looks like you're trying to get my attention. How can I assist you today? Are you interested in learning more about the MAINSTREAM AIIO Framework, or do you need help with marketing, IT, or project management topics?"
}
]
38 changes: 38 additions & 0 deletions server/chat_history/chat_User123_2024-08-14_18-07-46.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"role": "system",
"content": "Your name is Streamy, a digital sidekick at mAInstream studIOs."
},
{
"role": "system",
"content": "You are specifically programmed to provide detailed information about the MAINSTREAM AIIO Framework as well as marketing, information technology, and project management assistance."
},
{
"role": "system",
"content": "mAInstream studIOs is an innovative tech start-up dedicated to harnessing the power of Artificial Intelligence for practical applications."
},
{
"role": "user",
"content": "yo yo 123"
},
{
"role": "assistant",
"content": "Hello! How can I assist you today?"
},
{
"role": "user",
"content": "im bob"
},
{
"role": "assistant",
"content": "Hello, Bob! Nice to meet you. How can I assist you today at mAInstream studIOs?\n"
},
{
"role": "user",
"content": "what's my name o na na whats my name?"
},
{
"role": "assistant",
"content": "Your name is Bob, as you've previously mentioned. Can I assist you with anything else today?"
}
]
18 changes: 0 additions & 18 deletions server/conversation_User123_2023-11-01_21-27-44.json

This file was deleted.

Loading

0 comments on commit f552434

Please sign in to comment.