Skip to content

Commit

Permalink
Rename conversation with generated title
Browse files Browse the repository at this point in the history
  • Loading branch information
njbbaer committed May 1, 2024
1 parent d12cc07 commit 9bf5192
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
27 changes: 22 additions & 5 deletions src/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ def reset_conversation(self):

def new_conversation(self):
self._data["active_conversation"] = self._new_conversation_name()
self._load_conversation_by_name(self._data["active_conversation"])
self._load_conversation_by_name(self.active_conversation)

def load_conversation(self):
if "active_conversation" in self._data:
self._load_conversation_by_name(self._data["active_conversation"])
self._load_conversation_by_name(self.active_conversation)
else:
self.new_conversation()

Expand Down Expand Up @@ -69,6 +69,10 @@ def vars(self):
def name(self):
return self._data["name"]

@property
def active_conversation(self):
return self._data.get("active_conversation")

@property
def model(self):
model = self._data.get("model")
Expand All @@ -85,9 +89,22 @@ def instruction_template(self):
def api_provider(self):
return self._data.get("api_provider") or "openai"

@property
def _total_cost(self):
return self._data["total_cost"]
def has_title(self):
return len(self.active_conversation.split("_")) >= 3

def apply_conversation_title(self, title):
name_parts = self.active_conversation.split("_")
if len(name_parts) >= 3:
name_parts[2] = title
else:
name_parts.append(title)
new_name = "_".join(name_parts)
os.rename(
f"{self.dir}/conversations/{self.active_conversation}.yml",
f"{self.dir}/conversations/{new_name}.yml",
)
self._data["active_conversation"] = new_name
self.load_conversation()

def _new_conversation_name(self):
timestamp = datetime.now().replace(microsecond=0).isoformat()
Expand Down
6 changes: 3 additions & 3 deletions src/lm_executors/title_executor_template.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
---
- role: system
content: |-
You are categorizing transcripts. Your goal is to provide a short, factual kebab-case slug to make the conversation easy to identify.
You are categorizing transcripts. Your goal is to provide a short, factual kebab-case slug to make the conversation easy to identify. Provide only the slug and nothing else.
{% for message in messages %}
- role: {{ message.role }}
content: |-
{{ message.content | indent(4) }}
{% endfor %}
- role: assistant
content: |-
You are categorizing transcripts. Your goal is to provide a short, factual kebab-case slug to make the conversation easy to identify.
You are categorizing transcripts. Your goal is to provide a short, factual kebab-case slug to make the conversation easy to identify. Provide only the slug and nothing else.
Title:
Kebab-case slug:
7 changes: 6 additions & 1 deletion src/simulacrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ async def chat(self, user_input, user_name, image_url):
return speech

async def new_conversation(self):
if not self.context.has_title():
await self.generate_title()
self.context.load()
self.context.new_conversation()
self.context.save()
Expand Down Expand Up @@ -67,7 +69,10 @@ def get_conversation_cost(self):
async def generate_title(self):
self.context.load()
completion = await TitleExecutor(self.context).execute()
return completion.content
title = completion.content.strip()
self.context.apply_conversation_title(title)
self.context.save()
return title

def _filter_hidden(self, response):
response = re.sub(r"<THINK>.*?</>", "", response, flags=re.DOTALL)
Expand Down

0 comments on commit 9bf5192

Please sign in to comment.