abhivsh commited on
Commit
e4eb7c7
·
verified ·
1 Parent(s): 6515db3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -22
app.py CHANGED
@@ -14,12 +14,20 @@ from langchain_openai import ChatOpenAI
14
  from langchain.memory import ConversationBufferMemory
15
  from langchain.chains import ConversationalRetrievalChain
16
  from langchain.chains import VectorDBQA
17
- from langchain.llms import OpenAI
 
 
 
 
 
 
 
18
 
19
 
20
  import gradio as gr
21
- import os
22
  import requests
 
 
23
 
24
  import sys
25
  sys.path.append('../..')
@@ -53,10 +61,6 @@ vectordb = initialize.initialize()
53
 
54
 
55
 
56
- from langchain import HuggingFacePipeline, PromptTemplate, LLMChain, RetrievalQA
57
- from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
58
- import torch
59
-
60
  quantization_config = {
61
  "load_in_4bit": True,
62
  "bnb_4bit_compute_dtype": torch.float16,
@@ -64,20 +68,12 @@ quantization_config = {
64
  "bnb_4bit_use_double_quant": True,
65
  }
66
 
67
- llm = HuggingFacePipeline(pipeline=pipeline)
68
- model_id = "mistralai/Mistral-7B-Instruct-v0.1"
69
- model_4bit = AutoModelForCausalLM.from_pretrained(
70
- model_id, device="cuda", quantization_config=quantization_config
71
- )
72
-
73
- tokenizer = AutoTokenizer.from_pretrained(model_id)
74
-
75
  pipeline = pipeline(
76
  "text-generation",
77
  model=model_4bit,
78
  tokenizer=tokenizer,
79
  use_cache=True,
80
- device=0, # '0' is for GPU, 'cpu' for CPU
81
  max_length=500,
82
  do_sample=True,
83
  top_k=5,
@@ -86,19 +82,23 @@ pipeline = pipeline(
86
  pad_token_id=tokenizer.eos_token_id,
87
  )
88
 
89
- template = """[INST] You are a helpful, respectful and honest assistant. Answer exactly in few words from the context
90
- Answer the question below from the context below:
91
- {context}
92
- {question} [/INST]
93
- """
94
 
 
 
 
 
95
 
 
 
 
 
 
96
 
97
 
98
- def chat_query(retrieverQA, text_query):
99
 
100
  retrieverQA = RetrievalQA.from_chain_type(llm=llm, chain_type="retrieval", retriever=vectordb.as_retriever(), verbose=True)
101
- result = retrieverQA.run(text_query)
102
  return result
103
 
104
 
 
14
  from langchain.memory import ConversationBufferMemory
15
  from langchain.chains import ConversationalRetrievalChain
16
  from langchain.chains import VectorDBQA
17
+ from langchain_community.llms import OpenAI
18
+ from langchain_core.prompts import PromptTemplate
19
+ from langchain_community.llms.huggingface_pipeline import HuggingFacePipeline
20
+ from langchain.chains import LLMChain
21
+ from langchain_community.llms.huggingface_pipeline import HuggingFacePipeline
22
+
23
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
24
+ import torch
25
 
26
 
27
  import gradio as gr
 
28
  import requests
29
+ import os
30
+
31
 
32
  import sys
33
  sys.path.append('../..')
 
61
 
62
 
63
 
 
 
 
 
64
  quantization_config = {
65
  "load_in_4bit": True,
66
  "bnb_4bit_compute_dtype": torch.float16,
 
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='cpu', # '0' is for GPU, 'cpu' for CPU
77
  max_length=500,
78
  do_sample=True,
79
  top_k=5,
 
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