Chatbot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
4.8 KiB

2 years ago
import asyncio
import time
from .prompts import *
from .langchain_memory import BotConversationSummerBufferWindowMemory
from langchain import PromptTemplate
2 years ago
from langchain import LLMChain, ConversationChain
from langchain.memory import ConversationBufferMemory
2 years ago
2 years ago
from langchain.chains.base import Chain
from typing import Dict, List
from langchain.document_loaders import TextLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import SentenceTransformerEmbeddings
from langchain.vectorstores import Chroma
2 years ago
import logging
logger = logging.getLogger(__name__)
2 years ago
class RoleplayChain(Chain):
llm_chain: LLMChain
character_name: str
persona: str
scenario: str
ai_name_chat: str
human_name_chat: str
output_key: str = "output_text" #: :meta private:
@property
def input_keys(self) -> List[str]:
return ["character_name", "persona", "scenario", "ai_name_chat", "human_name_chat", "llm_chain"]
@property
def output_keys(self) -> List[str]:
return [self.output_key]
def _call(self, inputs: Dict[str, str]) -> Dict[str, str]:
other_keys = {k: v for k, v in inputs.items() if k not in self.input_keys}
result = self.llm_chain.predict(**other_keys)
return {self.output_key: result}
2 years ago
class AI(object):
2 years ago
def __init__(self, bot, text_wrapper, image_wrapper, memory_path: str):
2 years ago
self.name = bot.name
self.bot = bot
2 years ago
self.memory_path = memory_path
2 years ago
from ..wrappers.langchain_koboldcpp import KoboldCpp
self.llm_chat = KoboldCpp(temperature=self.bot.temperature, endpoint_url="http://172.16.85.10:5001/api/latest/generate", stop=['<|endoftext|>'])
self.llm_summary = KoboldCpp(temperature=0.2, endpoint_url="http://172.16.85.10:5001/api/latest/generate", stop=['<|endoftext|>'])
self.text_wrapper = text_wrapper
self.image_wrapper = image_wrapper
2 years ago
#self.memory = BotConversationSummerBufferWindowMemory(llm=self.llm_summary, max_token_limit=1200, min_token_limit=200)
self.memory = ConversationBufferMemory(memory_key="chat_history", human_prefix="You", ai_prefix=self.bot.name)
async def generate(self, message, reply_fn, typing_fn):
2 years ago
2 years ago
embeddings = SentenceTransformerEmbeddings()
#embeddings = SentenceTransformerEmbeddings(model="all-MiniLM-L6-v2")
loader = TextLoader('./germany.txt')
documents = loader.load()
text_splitter = RecursiveCharacterTextSplitter(
# Set a really small chunk size, just to show.
chunk_size = 600,
chunk_overlap = 100,
length_function = len,
)
2 years ago
2 years ago
docs = text_splitter.split_documents(documents)
2 years ago
2 years ago
db = Chroma(persist_directory=f'{self.memory_path}/chroma-db', embedding_function=embeddings)
print(f"Indexing {len(docs)} documents")
texts = [doc.page_content for doc in docs]
metadatas = [doc.metadata for doc in docs]
#db.add_texts(texts=texts, metadatas=metadatas, ids=None)
#db.persist()
query = "How is climate in Germany?"
output_docs = db.similarity_search_with_score(query)
print(query)
print('###')
for doc, score in output_docs:
print("-" * 80)
print("Score: ", score)
print(doc.page_content)
print("-" * 80)
2 years ago
prompt_template = "{input}"
chain = LLMChain(
llm=self.llm_chat,
prompt=PromptTemplate.from_template(prompt_template),
)
2 years ago
output = chain.run(message.message)
2 years ago
return output.strip()
async def generate_roleplay(self, message, reply_fn, typing_fn):
2 years ago
self.memory.human_prefix = message.user_name
prompt = prompt_vicuna.partial(
ai_name=self.bot.name,
persona=self.bot.persona,
scenario=self.bot.scenario,
human_name=message.user_name,
ai_name_chat=self.bot.name,
2 years ago
)
2 years ago
chain = ConversationChain(
2 years ago
llm=self.llm_chat,
2 years ago
prompt=prompt,
2 years ago
verbose=True,
memory=self.memory,
#stop=['<|endoftext|>', '\nYou:', f"\n{message.user_name}:"],
)
2 years ago
# output = llm_chain(inputs={"ai_name": self.bot.name, "persona": self.bot.persona, "scenario": self.bot.scenario, "human_name": message.user_name, "ai_name_chat": self.bot.name, "chat_history": "", "input": message.message})['results'][0]['text']
#roleplay_chain = RoleplayChain(llm_chain=chain, character_name=self.bot.name, persona=self.bot.persona, scenario=self.bot.scenario, ai_name_chat=self.bot.name, human_name_chat=message.user_name)
output = chain.run({"input":message.message, "stop": ['<|endoftext|>', f"\n{message.user_name}:"]})
2 years ago
return output.strip()
2 years ago
def estimate_num_tokens(input_text: str):
return len(input_text)//4+1