pratikroy311 commited on
Commit
2df718a
·
verified ·
1 Parent(s): f9618f4

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +17 -11
utils.py CHANGED
@@ -6,6 +6,7 @@ from langchain.vectorstores import Chroma
6
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
7
  from langchain_community.llms import HuggingFacePipeline
8
  from langchain.chains.question_answering import load_qa_chain
 
9
 
10
  # Load and process documents
11
  dir = "data"
@@ -31,7 +32,7 @@ vectordb = Chroma.from_documents(docs, embeddings, persist_directory=persist_dir
31
  vectordb.persist()
32
  new_db = Chroma(persist_directory=persist_directory, embedding_function=embeddings)
33
 
34
- def get_similar_docs(query, k=1, score=False):
35
  if score:
36
  similar_docs = new_db.similarity_search_with_score(query, k=k)
37
  else:
@@ -39,20 +40,25 @@ def get_similar_docs(query, k=1, score=False):
39
  return similar_docs
40
 
41
  # Load LLM model from Hugging Face
42
- model_name = "HuggingFaceH4/zephyr-7b-beta"
43
 
44
- llm = HuggingFacePipeline.from_model_id(
45
- model_id=model_name,
 
 
 
 
 
46
  task="text-generation",
47
- pipeline_kwargs={
48
- "max_new_tokens": 400,
49
- "top_k": 50,
50
- "temperature": 0.2,
51
- "do_sample": True,
52
- "repetition_penalty": 1.1,
53
- }
54
  )
55
 
 
 
56
  chain = load_qa_chain(llm, chain_type="stuff")
57
 
58
  def get_helpful_answer(text):
 
6
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
7
  from langchain_community.llms import HuggingFacePipeline
8
  from langchain.chains.question_answering import load_qa_chain
9
+ from sentence_transformers import SentenceTransformer
10
 
11
  # Load and process documents
12
  dir = "data"
 
32
  vectordb.persist()
33
  new_db = Chroma(persist_directory=persist_directory, embedding_function=embeddings)
34
 
35
+ def get_similar_docs(query, k=2, score=False):
36
  if score:
37
  similar_docs = new_db.similarity_search_with_score(query, k=k)
38
  else:
 
40
  return similar_docs
41
 
42
  # Load LLM model from Hugging Face
 
43
 
44
+ model_name = "HuggingFaceH4/zephyr-7b-beta"
45
+ model = AutoModelForCausalLM.from_pretrained(model_name)
46
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
47
+
48
+ text_generation_pipeline = pipeline(
49
+ model=model,
50
+ tokenizer=tokenizer,
51
  task="text-generation",
52
+ temperature=0.2,
53
+ do_sample=True,
54
+ repetition_penalty=1.1,
55
+ return_full_text=True,
56
+ max_new_tokens=400,
57
+ inference= True,
 
58
  )
59
 
60
+ llm = HuggingFacePipeline(pipeline=text_generation_pipeline)
61
+
62
  chain = load_qa_chain(llm, chain_type="stuff")
63
 
64
  def get_helpful_answer(text):