Spaces:
Runtime error
Runtime error
Commit
·
a5829ff
1
Parent(s):
53ebb52
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
from langchain.chat_models import ChatOpenAI
|
2 |
import gradio as gr
|
3 |
import sys
|
@@ -5,6 +6,24 @@ import os
|
|
5 |
|
6 |
os.environ["OPENAI_API_KEY"] = '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'
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
def chatbot(input_text):
|
9 |
index = GPTSimpleVectorIndex.load_from_disk('index.json')
|
10 |
response = index.query(input_text, response_mode="compact")
|
@@ -15,4 +34,5 @@ iface = gr.Interface(fn=chatbot,
|
|
15 |
outputs="text",
|
16 |
title="Custom-trained AI Chatbot")
|
17 |
|
|
|
18 |
iface.launch()
|
|
|
1 |
+
from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
|
2 |
from langchain.chat_models import ChatOpenAI
|
3 |
import gradio as gr
|
4 |
import sys
|
|
|
6 |
|
7 |
os.environ["OPENAI_API_KEY"] = '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'
|
8 |
|
9 |
+
def construct_index(directory_path):
|
10 |
+
max_input_size = 4096
|
11 |
+
num_outputs = 512
|
12 |
+
max_chunk_overlap = 20
|
13 |
+
chunk_size_limit = 600
|
14 |
+
|
15 |
+
prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
|
16 |
+
|
17 |
+
llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.7, model_name="gpt-4", max_tokens=num_outputs))
|
18 |
+
|
19 |
+
documents = SimpleDirectoryReader(directory_path).load_data()
|
20 |
+
|
21 |
+
index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
|
22 |
+
|
23 |
+
index.save_to_disk('index.json')
|
24 |
+
|
25 |
+
return index
|
26 |
+
|
27 |
def chatbot(input_text):
|
28 |
index = GPTSimpleVectorIndex.load_from_disk('index.json')
|
29 |
response = index.query(input_text, response_mode="compact")
|
|
|
34 |
outputs="text",
|
35 |
title="Custom-trained AI Chatbot")
|
36 |
|
37 |
+
index = construct_index("docs")
|
38 |
iface.launch()
|