File size: 1,986 Bytes
291adda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import datetime
import json
import os


class LLM:
    def __init__(self, client, save_dir="outputs"):
        self.client = client
        self.history = []
        self.save_dir = save_dir
        if save_dir:
            os.makedirs(save_dir, exist_ok=True)

    def prompt(self, p):
        # Add user message to history
        user_message = {"role": "user", "content": [{"type": "text", "text": p}]}
        self.history.append(user_message)

        # Create messages list with history
        messages = self.history.copy()

        response = self.client.chat.completions.create(
            model="o1-mini-2024-09-12",
            messages=self.history,
            store=False,
        )

        # Extract assistant response
        assistant_response = response.choices[0].message.content

        # Add assistant response to history
        assistant_message = {"role": "assistant", "content": [{"type": "text", "text": assistant_response}]}
        self.history.append(assistant_message)

        return assistant_response

    def save_history(self, filename="conversation_history.json"):
        """Save conversation history to a JSON file"""
        if not self.save_dir:
            return

        # Add timestamp to filename
        timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
        base_name, extension = os.path.splitext(filename)
        timestamped_filename = f"{base_name}_{timestamp}{extension}"

        save_path = os.path.join(self.save_dir, timestamped_filename)
        try:
            with open(save_path, "w", encoding="utf-8") as f:
                json.dump(self.history, f, indent=4, ensure_ascii=False)
            print(f"Conversation history saved to: {save_path}")
        except Exception as e:
            print(f"Failed to save conversation history: {e}")

    def __del__(self):
        """Destructor: Saves conversation history when the object is destroyed"""
        if self.save_dir:
            self.save_history()