Upload convo.py
Browse files
convo.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# convo.py
|
2 |
+
|
3 |
+
from llama_index.core.tools import FunctionTool
|
4 |
+
import os
|
5 |
+
from datetime import datetime
|
6 |
+
|
7 |
+
conversation_file = os.path.join("data", "conversation.txt")
|
8 |
+
|
9 |
+
# Function to save conversation to the file with timestamp
|
10 |
+
def save_conversation(prompt, response):
|
11 |
+
if not os.path.exists(conversation_file):
|
12 |
+
open(conversation_file, "w").close() # Create file if not exists
|
13 |
+
|
14 |
+
# Get current date and time
|
15 |
+
now = datetime.now()
|
16 |
+
timestamp = now.strftime("%Y-%m-%d %H:%M:%S")
|
17 |
+
|
18 |
+
with open(conversation_file, "a") as f:
|
19 |
+
f.write(f"Timestamp: {timestamp}\n")
|
20 |
+
f.write(f"Prompt: {prompt}\n")
|
21 |
+
f.write(f"Response: {response}\n")
|
22 |
+
f.write("=" * 40 + "\n") # Separator for readability
|
23 |
+
|
24 |
+
return "conversation saved"
|
25 |
+
|
26 |
+
# Create FunctionTool for conversation saving
|
27 |
+
conversation_tool = FunctionTool.from_defaults(
|
28 |
+
fn=save_conversation,
|
29 |
+
name="conversation_saver",
|
30 |
+
description="This tool can save the conversation (prompt and response) with timestamp to a file for the user.",
|
31 |
+
)
|