• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

feihoo87 / waveforms / 4722979957

pending completion
4722979957

push

github

feihoo87
update

10 of 10 new or added lines in 2 files covered. (100.0%)

0 of 12140 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/waveforms/sys/chat.py
1
import pickle
×
2
import time
×
3
from datetime import datetime
×
4
from pathlib import Path
×
5

6
import openai
×
7
from IPython import get_ipython
×
8
from IPython.display import Markdown, display
×
9

10
DEFAULT_SYSTEM_PROMPT = 'You are a helpful assistant. Respond using markdown.'
×
11
DEFAULT_MODEL = "gpt-3.5-turbo"
×
12

13

14
class Completion():
×
15

16
    def __init__(self,
×
17
                 system_prompt=DEFAULT_SYSTEM_PROMPT,
18
                 model=DEFAULT_MODEL):
19
        self.messages = [{"role": "system", "content": system_prompt}]
×
20
        self.title = 'untitled'
×
21
        self.last_time = datetime.now()
×
22
        self.completion = None
×
23
        self.total_tokens = 0
×
24
        self.prompt_tokens = 0
×
25
        self.completion_tokens = 0
×
26
        self.model = model
×
27

28
    def make_title(self):
×
29

30
        text = [
×
31
            f'{d["role"]} :\n"""\n{d["content"]}\n"""'
32
            for d in self.messages[1:]
33
        ]
34

35
        messages = [{
×
36
            "role": "system",
37
            "content": 'You are a helpful assistant.'
38
        }, {
39
            'role':
40
            "user",
41
            'content': ("总结以下对话的内容并为其取个标题以概括对话的内容,标题长度不超过100个字符。"
42
                        "返回的结果除了标题本身,不要包含额外的内容,省略结尾的句号。\n" + '\n\n'.join(text))
43
        }]
44
        completion = openai.ChatCompletion.create(model=self.model,
×
45
                                                  messages=messages)
46
        content = completion.choices[0].message['content']
×
47
        return f"{content} {time.asctime()}"
×
48

49
    def say(self, msg):
×
50
        self.last_time = datetime.now()
×
51
        self.messages.append({"role": "user", "content": msg})
×
52
        self.completion = openai.ChatCompletion.create(model=self.model,
×
53
                                                       messages=self.messages)
54
        self.total_tokens += self.completion.usage.total_tokens
×
55
        self.completion_tokens += self.completion.usage.completion_tokens
×
56
        self.prompt_tokens += self.completion.usage.prompt_tokens
×
57
        message = self.completion.choices[0].message
×
58
        self.messages.append({
×
59
            "role": message['role'],
60
            "content": message['content']
61
        })
62
        return message['content']
×
63

64
    def save(self):
×
65
        if self.title == 'untitled':
×
66
            self.title = self.make_title()
×
67

68
        filepath = Path.home() / 'chatGPT' / f"{self.title}.completion"
×
69
        filepath.parent.mkdir(parents=True, exist_ok=True)
×
70
        with open(filepath, 'wb') as f:
×
71
            pickle.dump(self, f)
×
72

73

74
ipy = get_ipython()
×
75

76
current_completion = Completion()
×
77

78

79
def chat(line, cell):
×
80
    global current_completion
81
    if line:
×
82
        current_completion.save()
×
83
        current_completion = Completion()
×
84
    if line in ['end', 'save']:
×
85
        return
×
86
    content = current_completion.say(cell)
×
87
    display(Markdown(content))
×
88
    ipy.set_next_input('%%chat\n')
×
89

90

91
def autosave_completion():
×
92
    if (datetime.now() - current_completion.last_time).seconds > 300 and len(
×
93
            current_completion.messages) >= 3:
94
        current_completion.save()
×
95
    elif len(current_completion.messages) > 7:
×
96
        current_completion.save()
×
97

98

99
ipy.register_magic_function(chat, 'cell', magic_name='chat')
×
100
ipy.events.register('post_run_cell', autosave_completion)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc