Create CustomTrioGPT.py
Browse files- CustomTrioGPT.py +93 -0
CustomTrioGPT.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from llama_index import SimpleDirectoryReader, GPTListIndex, GPTVectorStoreIndex, LLMPredictor, PromptHelper
|
2 |
+
from llama_index import StorageContext, load_index_from_storage
|
3 |
+
from langchain.chat_models import ChatOpenAI
|
4 |
+
import gradio as gr
|
5 |
+
import os
|
6 |
+
import openai
|
7 |
+
from gradio.themes.utils import colors, fonts, sizes
|
8 |
+
|
9 |
+
openai.api_key = os.environ.get('openai_key')
|
10 |
+
|
11 |
+
messages = [
|
12 |
+
{"role": "system", "content": "follow the 4 instructions below for your outputs:"},
|
13 |
+
{"role": "system", "content": "1. make sure all expressions are compatible with Polish"},
|
14 |
+
{"role": "system", "content": "2. use Polish only for outputs"},
|
15 |
+
{"role": "system", "content": "3. if you cannot answer, reply that you do not have enough information"},
|
16 |
+
{"role": "system", "content": "4. do not make up any answer if you do know the answer"},
|
17 |
+
]
|
18 |
+
|
19 |
+
def construct_index(directory_path):
|
20 |
+
max_input_size = 4096
|
21 |
+
num_outputs = 512
|
22 |
+
max_chunk_overlap = 20
|
23 |
+
chunk_size_limit = 600
|
24 |
+
temperature = 0.1
|
25 |
+
|
26 |
+
prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
|
27 |
+
llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=temperature, model_name="gpt-3.5-turbo", max_tokens=num_outputs))
|
28 |
+
documents = SimpleDirectoryReader(directory_path).load_data()
|
29 |
+
#index = GPTVectorStoreIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
|
30 |
+
index = GPTVectorStoreIndex.from_documents(documents, urls=[
|
31 |
+
'https://trio.house/',
|
32 |
+
'https://trio.house/kontakt/',
|
33 |
+
'https://trio.house/o-nas/',
|
34 |
+
'https://trio.house/zainwestuj-z-nami/',
|
35 |
+
'https://trio.house/potrzebujesz-konsultacji-rynku-nieruchomosci/',
|
36 |
+
'https://trio.house/potrzebujesz-remontu/',
|
37 |
+
'https://trio.house/potrzebujesz-projektu-wnetrza/',
|
38 |
+
'https://trio.house/potrzebujesz-mebli-na-wymiar/',
|
39 |
+
'https://trio.house/potrzebujesz-kredytu-na-zakup-nieruchomosci/',
|
40 |
+
'https://trio.house/makroekonomia/',
|
41 |
+
'https://trio.house/rynek-nieruchomosci/',
|
42 |
+
'https://trio.house/2023/05/24/deweloperzy-buduja-coraz-mniej/',
|
43 |
+
'https://trio.house/2023/04/27/prognozy-na-2023-2025-co-nas-czeka/',
|
44 |
+
'https://trio.house/2023/04/18/wycinka-drzew-na-wlasnej-dzialce-w-2023/',
|
45 |
+
'https://trio.house/2023/04/03/lipiec-rozpoczynamy-juz-w-kwietniu/',
|
46 |
+
'https://trio.house/2023/04/03/zmiany-w-podatku-od-czynnosci-cywilnoprawnych/',
|
47 |
+
'https://trio.house/2023/03/23/czy-aby-napewno-najdrozsze-mieszkania-sa-w-stolicy/',
|
48 |
+
], llm_predictor=llm_predictor, prompt_helper=prompt_helper)
|
49 |
+
index.storage_context.persist('index.json')
|
50 |
+
|
51 |
+
return index
|
52 |
+
|
53 |
+
def chatbotCustom(input):
|
54 |
+
storage_context = StorageContext.from_defaults(persist_dir="index.json")
|
55 |
+
index = load_index_from_storage(storage_context)
|
56 |
+
query_engine = index.as_query_engine()
|
57 |
+
response = query_engine.query(input)
|
58 |
+
# response = index.query(input, similarity_top_k=5, response_mode="tree_summarize")
|
59 |
+
return response.response
|
60 |
+
|
61 |
+
def chatbotGPT(input):
|
62 |
+
if input:
|
63 |
+
messages.append({"role": "user", "content": input})
|
64 |
+
chat = openai.ChatCompletion.create(
|
65 |
+
model="gpt-3.5-turbo", messages=messages
|
66 |
+
)
|
67 |
+
reply = chat.choices[0].message.content
|
68 |
+
messages.append({"role": "assistant", "content": reply})
|
69 |
+
return reply
|
70 |
+
|
71 |
+
def clear():
|
72 |
+
return None, None
|
73 |
+
|
74 |
+
theme = gr.themes.Default(font=[gr.themes.GoogleFont("Roboto"), "sans-serif", "sans-serif"], primary_hue="neutral", secondary_hue="neutral", neutral_hue="neutral").set(
|
75 |
+
button_primary_background_fill="#3FCCA5",
|
76 |
+
button_primary_background_fill_dark="#3FCCA5",
|
77 |
+
button_primary_text_color="#003F62",
|
78 |
+
body_background_fill="FFFFFF",
|
79 |
+
body_background_fill_dark="FFFFFF"
|
80 |
+
)
|
81 |
+
|
82 |
+
with gr.Blocks(theme=theme) as trioGPT:
|
83 |
+
inputs = gr.Textbox(lines=4, elem_id="inputs", label="Porozmawiaj z naszym chatbotem Real-Estate AI")#, elem_classes="textbox")
|
84 |
+
outputs = gr.Textbox(label="Odpowiedź", elem_id="outputs")#, elem_classes="textbox")
|
85 |
+
with gr.Row():
|
86 |
+
submit_btn = gr.Button("Wyślij", variant="primary")
|
87 |
+
clear_btn = gr.Button("Wyczyść")
|
88 |
+
|
89 |
+
submit_btn.click(chatbotCustom, inputs=inputs, outputs=outputs)
|
90 |
+
clear_btn.click(fn=clear, inputs=None, outputs=[inputs, outputs])
|
91 |
+
|
92 |
+
index = construct_index("data")
|
93 |
+
trioGPT.launch()#(share=True)
|