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