Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -52,7 +52,7 @@ hf_token = os.environ.get('hf_token')
|
|
52 |
GEMINI_API_KEY = os.environ.get('GEMINI_API_KEY')
|
53 |
fs_token = os.environ.get('fs_token')
|
54 |
|
55 |
-
llm_name = "gpt-3.5-turbo"
|
56 |
|
57 |
vectordb = initialize.initialize()
|
58 |
|
@@ -61,45 +61,45 @@ vectordb = initialize.initialize()
|
|
61 |
|
62 |
|
63 |
|
64 |
-
quantization_config = {
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
}
|
70 |
-
|
71 |
-
pipeline = pipeline(
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
)
|
84 |
|
85 |
|
86 |
-
llm = HuggingFacePipeline(pipeline=pipeline)
|
87 |
-
model_id = "mistralai/Mistral-7B-Instruct-v0.1"
|
88 |
-
model_4bit = AutoModelForCausalLM.from_pretrained(model_id, quantization_config=quantization_config)
|
89 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
90 |
|
91 |
-
# template = """[INST] You are a helpful, respectful and honest assistant. Answer exactly in few words from the context
|
92 |
-
# Answer the question below from the context below:
|
93 |
-
# {context}
|
94 |
-
# {question} [/INST]
|
95 |
-
# """
|
96 |
|
97 |
|
98 |
-
def chat_query(message, history):
|
99 |
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
|
104 |
|
105 |
|
@@ -107,23 +107,23 @@ def chat_query(message, history):
|
|
107 |
#-------------------------------------------
|
108 |
|
109 |
|
110 |
-
|
111 |
|
112 |
-
|
113 |
|
114 |
-
#
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
|
119 |
-
#
|
120 |
-
|
121 |
-
|
122 |
|
123 |
-
#
|
124 |
-
#
|
125 |
-
#
|
126 |
-
#
|
127 |
|
128 |
|
129 |
|
|
|
52 |
GEMINI_API_KEY = os.environ.get('GEMINI_API_KEY')
|
53 |
fs_token = os.environ.get('fs_token')
|
54 |
|
55 |
+
llm_name = "gpt-3.5-turbo-0301"
|
56 |
|
57 |
vectordb = initialize.initialize()
|
58 |
|
|
|
61 |
|
62 |
|
63 |
|
64 |
+
# quantization_config = {
|
65 |
+
# "load_in_4bit": True,
|
66 |
+
# "bnb_4bit_compute_dtype": torch.float16,
|
67 |
+
# "bnb_4bit_quant_type": "nf4",
|
68 |
+
# "bnb_4bit_use_double_quant": True,
|
69 |
+
# }
|
70 |
+
|
71 |
+
# pipeline = pipeline(
|
72 |
+
# "text-generation",
|
73 |
+
# model=model_4bit,
|
74 |
+
# tokenizer=tokenizer,
|
75 |
+
# use_cache=True,
|
76 |
+
# device=0, # '0' is for GPU, 'cpu' for CPU
|
77 |
+
# max_length=500,
|
78 |
+
# do_sample=True,
|
79 |
+
# top_k=5,
|
80 |
+
# num_return_sequences=1,
|
81 |
+
# eos_token_id=tokenizer.eos_token_id,
|
82 |
+
# pad_token_id=tokenizer.eos_token_id,
|
83 |
+
# )
|
84 |
|
85 |
|
86 |
+
# llm = HuggingFacePipeline(pipeline=pipeline)
|
87 |
+
# model_id = "mistralai/Mistral-7B-Instruct-v0.1"
|
88 |
+
# model_4bit = AutoModelForCausalLM.from_pretrained(model_id, quantization_config=quantization_config)
|
89 |
+
# tokenizer = AutoTokenizer.from_pretrained(model_id)
|
90 |
|
91 |
+
# # template = """[INST] You are a helpful, respectful and honest assistant. Answer exactly in few words from the context
|
92 |
+
# # Answer the question below from the context below:
|
93 |
+
# # {context}
|
94 |
+
# # {question} [/INST]
|
95 |
+
# # """
|
96 |
|
97 |
|
98 |
+
# def chat_query(message, history):
|
99 |
|
100 |
+
# retrieverQA = RetrievalQA.from_chain_type(llm=llm, chain_type="retrieval", retriever=vectordb.as_retriever(), verbose=True)
|
101 |
+
# result = retrieverQA.run()
|
102 |
+
# return result
|
103 |
|
104 |
|
105 |
|
|
|
107 |
#-------------------------------------------
|
108 |
|
109 |
|
110 |
+
def chat_query(question, history):
|
111 |
|
112 |
+
llm = ChatOpenAI(model=llm_name, temperature=0.1, api_key = OPENAI_API_KEY)
|
113 |
|
114 |
+
# Conversation Retrival Chain with Memory
|
115 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
116 |
+
retriever=vectordb.as_retriever()
|
117 |
+
qa = ConversationalRetrievalChain.from_llm(llm, retriever=retriever, memory=memory)
|
118 |
|
119 |
+
# Replace input() with question variable for Gradio
|
120 |
+
result = qa({"question": question})
|
121 |
+
return result['answer']
|
122 |
|
123 |
+
# Chatbot only answers based on Documents
|
124 |
+
# qa = VectorDBQA.from_chain_type(llm=OpenAI(openai_api_key = OPENAI_API_KEY, ), chain_type="stuff", vectorstore=vectordb)
|
125 |
+
# result = qa.run(question)
|
126 |
+
# return result
|
127 |
|
128 |
|
129 |
|