File size: 3,366 Bytes
bca04f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from silence_tensorflow import silence_tensorflow # import and call silence_tensor_flow to make tensorflow shutup about files it thinks I need but don't
silence_tensorflow()
import logging
logging.disable(logging.WARNING) # disable logging warnings to get rid of warnings about things that aren't really errors
from langchain import HuggingFacePipeline,PromptTemplate # import the stuff for setting up langchain with huggingface
from langchain.memory import ConversationBufferMemory # import stuff langchain uses to remember stuff
from langchain.chains import ConversationChain # import stuff it uses in relation to conversations
from transformers import pipeline # import the main pipeline from transformers
import readline # import readline for a slightly nicer and slightly easier to us interface
from transformers import GenerationConfig # import stuff to configure for text generation
import re # import re to match regular expression

modelPath = "LittleMKIA" # local path to the language model
mode1 = "text2text-generation" # task we want the transformers pipeline to perform

config = GenerationConfig.from_pretrained(modelPath) # set up the configuration object

pipe = pipeline(task= mode1, model=modelPath,min_length = 20,max_new_tokens = 200,temperature = 0.7,early_stopping = True,
no_repeat_ngram_size=3,do_sample = True,top_k = 150,generation_config=config) # set up the pipeline

llm = HuggingFacePipeline(pipeline=pipe) # make transformers pipeline usable by langchain

# create a template for the prompt
template  = '''
{history}
You are MKIA an intelligent companion and assistent.
User: {input}'''

# create the prompt from the template
prompt = PromptTemplate(
    input_variables=[ "input","history"],
    template=template)

# set up a memory object
mem = ConversationBufferMemory(k = 1000,memory_key = "history",return_messase = False,ai_prefix = "MKIA")

# make a conversation chain and pass all the necessary parameters to it, tell it we don't want verbose so we only get regular output
chat_chain = ConversationChain(
    llm=llm,
    prompt = prompt,
    memory= mem,
    verbose=False
)
#create a function that will act as the program's main loop
def loop():
    while 1: # python is optimize for while 1: not while True: so I will use while 1:
        In = input('User > ') # ask for input
        if re.match('think[:] (.*)|think[:](.*)|Think[:] (.*)|Think[:](.*)',In) != None:
            # if the input text matches the pattern then we will bypass langchain

            In2 = re.sub('think[:]|Think[:]','',In).strip()
            # remove the prefix at the begining

            out= pipe(In2)[0]['generated_text']
            # get the output directly from the language model

            print(out)

        elif In == 'quit':
            break

        else:
           out1 = llm.predict(In)
           mem.chat_memory.add_user_message(In)
           #mem.chat_history.add_ai_message(out1)

           print(f'MKIA-model > {out1}\n')
           out2 = chat_chain.run(input=In+' '+out1)      # we feed the input to langchain and get the result
           mem.chat_memory.add_ai_message(out1+ ' '+out2)
           #mem.chat_memory.add_ai_message(out2)

           print(f'MKIA-bot > {out2}\n') # let the user know what MKIA said and that she said it
           print('\n\n') # print 2 newlines to help output be prettier

loop()