Spaces:
Sleeping
Sleeping
Upload RagBotAssignment.py
Browse files- RagBotAssignment.py +87 -0
RagBotAssignment.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""RagBot (2).ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1pDSwQZ5XyUQf_efd7Y1dJsLE_L8JmEda
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
!pip install pypdf
|
| 11 |
+
|
| 12 |
+
!pip install -q transformers einops accelerate langchain bitsandbytes
|
| 13 |
+
|
| 14 |
+
!pip install sentence_transformers
|
| 15 |
+
|
| 16 |
+
!pip install llama_index
|
| 17 |
+
!pip install llama-index-llms-huggingface
|
| 18 |
+
!pip install llama-index-readers-web
|
| 19 |
+
!pip install llama-index-embeddings-langchain
|
| 20 |
+
|
| 21 |
+
from llama_index.core import VectorStoreIndex,SimpleDirectoryReader,ServiceContext
|
| 22 |
+
from llama_index.llms.huggingface import HuggingFaceLLM
|
| 23 |
+
from llama_index.core.prompts.prompts import SimpleInputPrompt
|
| 24 |
+
|
| 25 |
+
documents = SimpleDirectoryReader('/content/data').load_data()
|
| 26 |
+
documents
|
| 27 |
+
|
| 28 |
+
system_prompts = """
|
| 29 |
+
You are a Q&A assistant. Your goal is to answer questions as
|
| 30 |
+
accurately as possible based on the instructions and context provided.
|
| 31 |
+
"""
|
| 32 |
+
## Default format supportable by LLama2
|
| 33 |
+
query_wrapper_prompt = SimpleInputPrompt("<|USER|>{query_str}<|ASSISTANT|>")
|
| 34 |
+
|
| 35 |
+
!!huggingface-cli login
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
import torch
|
| 40 |
+
|
| 41 |
+
llm = HuggingFaceLLM(
|
| 42 |
+
context_window=4096,
|
| 43 |
+
max_new_tokens=256,
|
| 44 |
+
generate_kwargs={"temperature": 0.0, "do_sample": False},
|
| 45 |
+
system_prompt=system_prompts,
|
| 46 |
+
query_wrapper_prompt=query_wrapper_prompt,
|
| 47 |
+
tokenizer_name="meta-llama/Llama-2-7b-chat-hf",
|
| 48 |
+
model_name="meta-llama/Llama-2-7b-chat-hf",
|
| 49 |
+
device_map="auto",
|
| 50 |
+
# uncomment this if using CUDA to reduce memory usage
|
| 51 |
+
model_kwargs={"torch_dtype": torch.float16 , "load_in_8bit":True}
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
|
| 55 |
+
from llama_index.core import ServiceContext
|
| 56 |
+
from llama_index.embeddings.langchain import LangchainEmbedding
|
| 57 |
+
|
| 58 |
+
embed_model=LangchainEmbedding(
|
| 59 |
+
HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2"))
|
| 60 |
+
|
| 61 |
+
service_context=ServiceContext.from_defaults(
|
| 62 |
+
chunk_size=1024,
|
| 63 |
+
llm=llm,
|
| 64 |
+
embed_model=embed_model
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
index=VectorStoreIndex.from_documents(documents,service_context=service_context)
|
| 68 |
+
|
| 69 |
+
query_engine=index.as_query_engine()
|
| 70 |
+
|
| 71 |
+
response=query_engine.query("who is ceo of i2e")
|
| 72 |
+
print(response)
|
| 73 |
+
|
| 74 |
+
response=query_engine.query("who is ceo of google")
|
| 75 |
+
print(response)
|
| 76 |
+
|
| 77 |
+
response=query_engine.query("who is vishal, give a short discription of him")
|
| 78 |
+
print(response)
|
| 79 |
+
|
| 80 |
+
response=query_engine.query("who is vishal, give a long discription of him")
|
| 81 |
+
print(response)
|
| 82 |
+
|
| 83 |
+
response=query_engine.query("what is i2e")
|
| 84 |
+
print(response)
|
| 85 |
+
|
| 86 |
+
response=query_engine.query("about home page")
|
| 87 |
+
print(response)
|