Browse Source

config file handling

master
Hendrik Langer 2 years ago
parent
commit
3fbc6dd4cf
  1. 11
      matrix_pygmalion_bot/bot/ai/langchain.py
  2. 24
      matrix_pygmalion_bot/bot/core.py
  3. 3
      matrix_pygmalion_bot/bot/memory/chatlog.py
  4. 2
      matrix_pygmalion_bot/connections/matrix.py
  5. 4
      matrix_pygmalion_bot/main.py

11
matrix_pygmalion_bot/bot/ai/langchain.py

@ -5,7 +5,7 @@ from .langchain_memory import BotConversationSummerBufferWindowMemory
from langchain import PromptTemplate from langchain import PromptTemplate
from langchain import LLMChain, ConversationChain from langchain import LLMChain, ConversationChain
from langchain.memory import ConversationBufferMemory from langchain.memory import ConversationBufferMemory, ReadOnlySharedMemory
from langchain.chains.base import Chain from langchain.chains.base import Chain
from typing import Dict, List from typing import Dict, List
@ -94,7 +94,7 @@ class AI(object):
docs = text_splitter.split_documents(documents) docs = text_splitter.split_documents(documents)
db = Chroma(persist_directory=f'{self.memory_path}/chroma-db', embedding_function=embeddings) db = Chroma(persist_directory=os.path.join(self.memory_path, f'chroma-db'), embedding_function=embeddings)
print(f"Indexing {len(docs)} documents") print(f"Indexing {len(docs)} documents")
texts = [doc.page_content for doc in docs] texts = [doc.page_content for doc in docs]
@ -123,6 +123,7 @@ class AI(object):
async def generate_roleplay(self, message, reply_fn, typing_fn): async def generate_roleplay(self, message, reply_fn, typing_fn):
memory = self.get_memory(message) memory = self.get_memory(message)
readonlymemory = ReadOnlySharedMemory(memory=memory)
prompt = prompt_vicuna.partial( prompt = prompt_vicuna.partial(
ai_name=self.bot.name, ai_name=self.bot.name,
@ -137,7 +138,7 @@ class AI(object):
llm=self.llm_chat, llm=self.llm_chat,
prompt=prompt, prompt=prompt,
verbose=True, verbose=True,
memory=memory, memory=readonlymemory,
#stop=['<|endoftext|>', '\nYou:', f"\n{message.user_name}:"], #stop=['<|endoftext|>', '\nYou:', f"\n{message.user_name}:"],
) )
@ -148,6 +149,10 @@ class AI(object):
stop = ['<|endoftext|>', f"\n{message.user_name}:"] stop = ['<|endoftext|>', f"\n{message.user_name}:"]
print(f"Message is: \"{message.message}\"") print(f"Message is: \"{message.message}\"")
output = await chain.arun({"input":message.message, "stop": stop}) output = await chain.arun({"input":message.message, "stop": stop})
memory.chat_memory.add_user_message(message.message)
memory.chat_memory.add_ai_message(output)
output = output.strip()
memory.load_memory_variables({})
return output.strip() return output.strip()

24
matrix_pygmalion_bot/bot/core.py

@ -3,6 +3,7 @@ import os, sys
import time import time
import importlib import importlib
import re import re
import json
import logging import logging
from functools import partial from functools import partial
from .memory.chatlog import ChatLog from .memory.chatlog import ChatLog
@ -38,14 +39,26 @@ class ChatBot(object):
#self.example_dialogue = self.example_dialogue.replace('{{char}}', self.name) #self.example_dialogue = self.example_dialogue.replace('{{char}}', self.name)
def persist(self, data_dir): async def persist(self, data_dir):
self.chatlog_path = f"{data_dir}/chatlogs" self.chatlog_path = os.path.join(data_dir, "chatlogs/")
self.images_path = f"{data_dir}/images" self.images_path = os.path.join(data_dir, "images/")
self.memory_path = f"{data_dir}/memory" self.memory_path = os.path.join(data_dir, "memory/")
self.rooms_conf_file = os.path.join(data_dir, "rooms.conf")
os.makedirs(self.chatlog_path, exist_ok=True) os.makedirs(self.chatlog_path, exist_ok=True)
os.makedirs(self.images_path, exist_ok=True) os.makedirs(self.images_path, exist_ok=True)
os.makedirs(self.memory_path, exist_ok=True) os.makedirs(self.memory_path, exist_ok=True)
self.chatlog.enable_logging(self.chatlog_path) self.chatlog.enable_logging(self.chatlog_path)
self.rooms = await self.read_conf2()
async def read_conf2(self):
if not os.path.isfile(self.rooms_conf_file):
return {}
with open(self.rooms_conf_file, "r") as f:
return json.load(f)
async def write_conf2(self, data):
with open(self.rooms_conf_file, "w") as f:
json.dump(data, f)
async def connect(self): async def connect(self):
await self.connection.login() await self.connection.login()
@ -114,6 +127,7 @@ class ChatBot(object):
if not room.room_id in self.rooms: if not room.room_id in self.rooms:
self.rooms[room.room_id] = {} self.rooms[room.room_id] = {}
self.write_conf2(self.rooms)
# ToDo: set ticks 0 / start # ToDo: set ticks 0 / start
if not self.connection.synced: if not self.connection.synced:
@ -209,8 +223,10 @@ class ChatBot(object):
await reply_fn(self.greeting) await reply_fn(self.greeting)
elif message.message.startswith('!start'): elif message.message.startswith('!start'):
self.rooms[message.room_id]["disabled"] = False self.rooms[message.room_id]["disabled"] = False
self.write_conf2(self.rooms)
elif message.message.startswith('!stop'): elif message.message.startswith('!stop'):
self.rooms[message.room_id]["disabled"] = True self.rooms[message.room_id]["disabled"] = True
self.write_conf2(self.rooms)
elif message.message.startswith('!!'): elif message.message.startswith('!!'):
if self.chatlog.chat_history_len(message.room_id) > 2: if self.chatlog.chat_history_len(message.room_id) > 2:
for _ in range(2): for _ in range(2):

3
matrix_pygmalion_bot/bot/memory/chatlog.py

@ -1,4 +1,5 @@
from time import gmtime, localtime, strftime from time import gmtime, localtime, strftime
import os
from ..utilities.messages import Message from ..utilities.messages import Message
class ChatLog(object): class ChatLog(object):
@ -20,7 +21,7 @@ class ChatLog(object):
room_id_sanitized = "".join(c for c in message.room_id if c.isalnum() or c in keepcharacters).strip() room_id_sanitized = "".join(c for c in message.room_id if c.isalnum() or c in keepcharacters).strip()
time_suffix = strftime("%Y-%m", localtime()) time_suffix = strftime("%Y-%m", localtime())
time = strftime("%a, %d %b %Y %H:%M:%S", localtime(message.timestamp)) time = strftime("%a, %d %b %Y %H:%M:%S", localtime(message.timestamp))
with open(f"{self.directory}/{message.room_name}_{room_id_sanitized}_{time_suffix}.txt", "a") as f: with open(os.path.join(self.directory, f"{message.room_name}_{room_id_sanitized}_{time_suffix}.txt"), "a") as f:
f.write("{} | {}: {}\n".format(time, message.user_name, message.message)) f.write("{} | {}: {}\n".format(time, message.user_name, message.message))

2
matrix_pygmalion_bot/connections/matrix.py

@ -154,7 +154,7 @@ class ChatClient(object):
self.device_name = device_name self.device_name = device_name
self.synced = False self.synced = False
def persist(self, data_dir): async def persist(self, data_dir):
#self.data_dir = data_dir #self.data_dir = data_dir
self.config_file = f"{data_dir}/matrix_credentials.json" self.config_file = f"{data_dir}/matrix_credentials.json"
self.store_path = f"{data_dir}/store/" self.store_path = f"{data_dir}/store/"

4
matrix_pygmalion_bot/main.py

@ -33,9 +33,9 @@ async def main() -> None:
for section in config.sections(): for section in config.sections():
bot_config = config[section] bot_config = config[section]
connection = ChatClient(bot_config['matrix_homeserver'], bot_config['matrix_username'], bot_config['matrix_password'], bot_config.get('matrix_device_name', 'matrix-nio')) connection = ChatClient(bot_config['matrix_homeserver'], bot_config['matrix_username'], bot_config['matrix_password'], bot_config.get('matrix_device_name', 'matrix-nio'))
connection.persist(f"{DATA_DIR}/{section}/matrix") await connection.persist(f"{DATA_DIR}/{section}/matrix")
bot = ChatBot(section, connection) bot = ChatBot(section, connection)
bot.persist(f"{DATA_DIR}/{section}") await bot.persist(f"{DATA_DIR}/{section}")
bot.init_character( bot.init_character(
bot_config['persona'], bot_config['persona'],
bot_config['scenario'], bot_config['scenario'],

Loading…
Cancel
Save