File size: 3,719 Bytes
6161c48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51e6285
6161c48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51e6285
6161c48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51e6285
6161c48
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import weaviate
from weaviate.connect import ConnectionParams
from weaviate.classes.init import AdditionalConfig, Timeout

from sentence_transformers import SentenceTransformer
from langchain_community.document_loaders import BSHTMLLoader
from pathlib import Path
from lxml import html
import logging
from semantic_text_splitter import HuggingFaceTextSplitter
from tokenizers import Tokenizer
import json
import os
import re
import logging

import llama_cpp
from llama_cpp import Llama

import ipywidgets as widgets
from IPython.display import display, clear_output


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)



######################################################################
# MAINLINE
#
logger.info("#### MAINLINE ENTERED.")

#####################################################################
# Create UI widgets.
output_widget = widgets.Output()
with output_widget:
    print("### Create widgets entered.")

systemTextArea = widgets.Textarea(
    value='',
    placeholder='Enter System Prompt.',
    description='Sys Prompt: ',
    disabled=False,
    layout=widgets.Layout(width='300px', height='80px')
)

userTextArea = widgets.Textarea(
    value='',
    placeholder='Enter User Prompt.',
    description='User Prompt: ',
    disabled=False,
    layout=widgets.Layout(width='435px', height='110px')
)

ragPromptTextArea = widgets.Textarea(
    value='',
    placeholder='App generated prompt with RAG information.',
    description='RAG Prompt: ',
    disabled=False,
    layout=widgets.Layout(width='580px', height='180px')
)

responseTextArea = widgets.Textarea(
    value='',
    placeholder='LLM generated response.',
    description='LLM Resp: ',
    disabled=False,
    layout=widgets.Layout(width='780px', height='200px')
)

selectRag = widgets.Checkbox(
    value=False,
    description='Use RAG',
    disabled=False
)

submitButton = widgets.Button(
    description='Run Model.',
    disabled=False,
    button_style='', # 'success', 'info', 'warning', 'danger' or ''
    tooltip='Click',
    icon='check' # (FontAwesome names without the `fa-` prefix)
)


# Display UI
logger.debug("### Before displaying UI: ")
display(systemTextArea)
display(userTextArea)
display(ragPromptTextArea)
display(responseTextArea)
display(selectRag)
display(submitButton)

def runLLM(prompt):
    max_tokens = 1000
    temperature = 0.3
    top_p = 0.1
    echo = True
    stop = ["Q", "\n"]

    modelOutput = llm(
       prompt,
       max_tokens=max_tokens,
       temperature=temperature,
       top_p=top_p,
       echo=echo,
       stop=stop,
    )
    result = modelOutput["choices"][0]["text"].strip()
    return(result)

def setPrompt(pprompt,ragFlag):
    logger.info("\n### setPrompt() entered. ragFlag: ",ragFlag)
    if ragFlag:
        ragPrompt = getRagData(pprompt)
        userPrompt = pprompt + "\n" + ragPrompt
        prompt = userPrompt
        userPrompt = "Using this information: " + ragPrompt \
             + "process the following statement or question and produce a a response" \
             + intialPrompt
    else:
        userPrompt = pprompt
    #prompt = f""" <s> [INST] <<SYS>> {systemTextArea.value} </SYS>> Q: {userPrompt} A: [/INST]"""
    return userPrompt


def on_submitButton_clicked(b):
    with output_widget:
        clear_output(wait=True)
        ragPromptTextArea.value = ""
        responseTextArea.value = ""
        log.debug(f"### selectRag: {selectRag.value}")
        prompt = setPrompt(userTextArea.value,selectRag.value)
        log.debug("### prompt: " + prompt)
        runLLM(prompt)

logger.info("\n### Before calling submitButton.on_click().")
submitButton.on_click(on_submitButton_clicked)
display(output_widget)