Spaces:
Sleeping
Sleeping
Delete utils.py
Browse files
utils.py
DELETED
@@ -1,93 +0,0 @@
|
|
1 |
-
from langchain.document_loaders import ApifyDatasetLoader
|
2 |
-
from langchain.utilities import ApifyWrapper
|
3 |
-
from langchain.docstore.document import Document
|
4 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
5 |
-
from langchain.embeddings.cohere import CohereEmbeddings
|
6 |
-
from langchain.vectorstores.deeplake import DeepLake
|
7 |
-
from langchain_cohere import CohereRerank
|
8 |
-
from langchain.retrievers import ContextualCompressionRetriever
|
9 |
-
from langchain.memory import ConversationBufferWindowMemory
|
10 |
-
import os
|
11 |
-
from langchain.chains.conversational_retrieval.base import ConversationalRetrievalChain
|
12 |
-
from langchain_groq import ChatGroq
|
13 |
-
from dotenv import load_dotenv
|
14 |
-
load_dotenv()
|
15 |
-
|
16 |
-
def get_and_load_data():
|
17 |
-
apify_key = os.getenv("apify")
|
18 |
-
|
19 |
-
apify = ApifyWrapper()
|
20 |
-
|
21 |
-
loader = apify.call_actor(
|
22 |
-
actor_id="apify/website-content-crawler",
|
23 |
-
run_input={"startUrls": [{"url": "https://en.wikipedia.org/wiki/Artificial_intelligence"}]},
|
24 |
-
dataset_mapping_function=lambda dataset_item: Document(
|
25 |
-
page_content=dataset_item["text"] if dataset_item["text"] else "No content available",
|
26 |
-
metadata={
|
27 |
-
"source": dataset_item["url"],
|
28 |
-
"title": dataset_item["metadata"]["title"]
|
29 |
-
}
|
30 |
-
),
|
31 |
-
)
|
32 |
-
docs = loader.load()
|
33 |
-
text_splitter = RecursiveCharacterTextSplitter(
|
34 |
-
chunk_size=1000, chunk_overlap=20, length_function=len
|
35 |
-
)
|
36 |
-
docs_split = text_splitter.split_documents(docs)
|
37 |
-
embeddings = CohereEmbeddings(model="embed-english-v2.0")
|
38 |
-
username = "gneyapandya1234"
|
39 |
-
db_id= "educational_chatbot"
|
40 |
-
|
41 |
-
dbs = DeepLake(dataset_path=f"hub://{username}/{db_id}", embedding_function=embeddings)
|
42 |
-
dbs.add_documents(docs_split)
|
43 |
-
|
44 |
-
def deeplake():
|
45 |
-
embeddings= CohereEmbeddings(model = "embed-english-v2.0")
|
46 |
-
dbs = DeepLake(
|
47 |
-
dataset_path="hub://gneyapandya1234/educational_chatbot",
|
48 |
-
read_only=True,
|
49 |
-
embedding_function= embeddings
|
50 |
-
)
|
51 |
-
retriever = dbs.as_retriever()
|
52 |
-
retriever.search_kwargs["distance_metric"] = "cos"
|
53 |
-
retriever.search_kwargs["fetch_k"] = 20
|
54 |
-
# retriever.search_kwargs["maximal_marginal_relevance"] = True
|
55 |
-
retriever.search_kwargs["k"] = 20
|
56 |
-
|
57 |
-
compressor = CohereRerank(
|
58 |
-
model = "rerank-english-v2.0",
|
59 |
-
top_n=5
|
60 |
-
)
|
61 |
-
compressor_retriever = ContextualCompressionRetriever(
|
62 |
-
base_compressor = compressor , base_retriever=retriever
|
63 |
-
)
|
64 |
-
print("DOne")
|
65 |
-
return dbs, compressor_retriever, retriever
|
66 |
-
|
67 |
-
def memory():
|
68 |
-
mem = ConversationBufferWindowMemory(
|
69 |
-
k=3,
|
70 |
-
memory_key="chat_history",
|
71 |
-
return_messages=True,
|
72 |
-
output_key="answer"
|
73 |
-
)
|
74 |
-
return mem
|
75 |
-
def create_llm():
|
76 |
-
llm = ChatGroq(api_key= os.getenv("GROQ_API_KEY"),model="llama3-70b-8192")
|
77 |
-
return llm
|
78 |
-
|
79 |
-
def chain(llm,compression_retriever,memory):
|
80 |
-
qa = ConversationalRetrievalChain.from_llm(
|
81 |
-
llm = llm,
|
82 |
-
memory= memory,
|
83 |
-
retriever= compression_retriever,
|
84 |
-
verbose= True,
|
85 |
-
return_source_documents=True
|
86 |
-
)
|
87 |
-
return qa
|
88 |
-
def final_function():
|
89 |
-
llm = create_llm()
|
90 |
-
mem =memory()
|
91 |
-
dbs, compressor_retriever, retriever = deeplake()
|
92 |
-
qa= chain(llm,compressor_retriever,mem)
|
93 |
-
return qa, mem
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|