stephenmccartney1234 commited on
Commit
78c7928
·
verified ·
1 Parent(s): 5f0986f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -22
app.py CHANGED
@@ -1,33 +1,47 @@
1
- from transformers import RagTokenizer, RagRetriever, RagTokenForGeneration
2
- import openai
3
  import os
 
 
 
 
 
 
 
 
 
4
 
5
- # Set your API key
6
  os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
7
 
8
 
9
- # Initialize the RAG tokenizer and retriever
10
- tokenizer = RagTokenizer.from_pretrained("facebook/rag-token-nq")
11
- retriever = RagRetriever.from_pretrained("facebook/rag-token-nq", index_name="exact", use_dummy_dataset=True)
 
 
 
 
 
 
12
 
13
- # Initialize the RAG token for generation
14
- model = RagTokenForGeneration.from_pretrained("facebook/rag-token-nq", retriever=retriever)
15
 
16
- # Define the context for the question
17
- context = "Python is a popular programming language used for various applications."
18
 
19
- # Ask a question
20
- question = "What is Python used for?"
21
 
22
- # Prepare input for the model
23
- input_dict = tokenizer(question, context, return_tensors="pt")
 
 
 
 
 
 
24
 
25
- # Generate an answer using OpenAI's GPT-3
26
- generated = openai.Completion.create(
27
- model="text-davinci-003",
28
- prompt=f"Question: {question} Context: {context}",
29
- max_tokens=150
30
- )
31
 
32
- # Print the generated answer
33
- print(generated.choices[0].text.strip())
 
1
+ from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
 
2
  import os
3
+ from langchain.chat_models import ChatOpenAI
4
+ import gradio as gr
5
+ from gpt_index import SimpleDirectoryReader, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
6
+
7
+ import sys
8
+
9
+ llm = ChatOpenAI(temperature=0.7, model_name="gpt-3.5-turbo-instruct", engine="gpt-3.5-turbo-instruct", max_tokens=num_outputs)
10
+
11
+
12
 
 
13
  os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
14
 
15
 
16
+ def construct_index(directory_path):
17
+ max_input_size = 4096
18
+ num_outputs = 512
19
+ max_chunk_overlap = 20
20
+ chunk_size_limit = 600
21
+
22
+ prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
23
+
24
+ llm_predictor = LLMPredictor(llm=llm)
25
 
26
+ documents = SimpleDirectoryReader(directory_path).load_data()
 
27
 
28
+ index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
 
29
 
30
+ index.save_to_disk('index.json')
 
31
 
32
+ return index
33
+
34
+ def chatbot(input_text):
35
+ predetermined_text = "- please only answer using information found in the directory 'docs' and nothing else"
36
+ input_text = input_text + predetermined_text
37
+ index = GPTSimpleVectorIndex.load_from_disk('index.json')
38
+ response = index.query(input_text, response_mode="compact")
39
+ return response.response
40
 
41
+ iface = gr.Interface(fn=chatbot,
42
+ inputs=gr.components.Textbox(lines=7, label="What do you want to know about the project?"),
43
+ outputs="text",
44
+ title="Project Knowledge AI")
 
 
45
 
46
+ index = construct_index("docs")
47
+ iface.launch()