Roy310 commited on
Commit
5f658e6
1 Parent(s): f05cf9c

Upload at1.py

Browse files
Files changed (1) hide show
  1. at1.py +87 -0
at1.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from langchain_groq import ChatGroq
3
+ from langchain_community.llms import Ollama
4
+ from langchain_community.document_loaders import WebBaseLoader
5
+ from langchain_community.document_loaders import TextLoader
6
+ from langchain_community.document_loaders.csv_loader import CSVLoader
7
+ from langchain_community.embeddings import OllamaEmbeddings
8
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
9
+ from langchain.chains.combine_documents import create_stuff_documents_chain
10
+ from langchain_core.prompts import ChatPromptTemplate
11
+ from langchain.chains import create_retrieval_chain
12
+ from langchain_community.vectorstores import FAISS
13
+ from langchain_huggingface.embeddings import HuggingFaceEmbeddings
14
+ from dotenv import load_dotenv
15
+ import os
16
+ import time
17
+ from datetime import datetime
18
+
19
+ load_dotenv()
20
+
21
+ print("Function started at",datetime.now())
22
+ start=datetime.now()
23
+
24
+
25
+ os.environ["TOKENIZERS_PARALLELISM"] = "false"
26
+
27
+ groq_api_key="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"
28
+ loader1=TextLoader("Chat/ffreedomsample.txt")
29
+ loader2=TextLoader("Chat/lotr.txt")
30
+ loader3=CSVLoader("at_!/courseclean100.csv")
31
+ docs=loader3.load()
32
+ print("Document loaded")
33
+
34
+ # print("Documents loaded in ", (datetime.now()-start).seconds)
35
+
36
+ text_splitter=RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=100)
37
+ final_documents=text_splitter.split_documents(docs)
38
+ print("Text has been split at", datetime.now())
39
+
40
+
41
+ embeddings = HuggingFaceEmbeddings(model_name="mixedbread-ai/mxbai-embed-large-v1",)
42
+ # embeddings = HuggingFaceEmbeddings(model_name="Alibaba-NLP/gte-Qwen2-1.5B-instruct",)
43
+
44
+ vectors=FAISS.from_documents(final_documents, embeddings)
45
+
46
+
47
+ print("Vectorization done in", (datetihme.now()-start).seconds, "seconds")
48
+
49
+ # st.title("ChatGroq Ffreedom Llama3")
50
+
51
+ # llm=ChatGroq(groq_api_key=groq_api_key,model_name="llama3-8b-8192")
52
+ llm=Ollama(model="llama3.1")
53
+
54
+ prompt=ChatPromptTemplate.from_template(
55
+ """
56
+ You are Kevai, you are a virtual assistant for a company Ffreedom, which offers various courses in the domain of personal finance, small business and farming, help users find the right course or solve their queries based on the provided context.
57
+ intoduce youself once at the beginning thereafter only introduce when asked
58
+ Answer the user query based on data you are taking in.
59
+ Answer within 100 words.
60
+ {context}
61
+ </context>
62
+ Question: {input}
63
+ """
64
+ )
65
+ document_chain = create_stuff_documents_chain(llm, prompt)
66
+ retriever = vectors.as_retriever()
67
+ retrieval_chain = create_retrieval_chain(retriever, document_chain)
68
+
69
+ # prompt=st.text_input("Enter Query")
70
+
71
+ while True:
72
+ t=input("Enter Query\n\n")
73
+ if(t is not None):
74
+ ret=datetime.now()
75
+ response=retrieval_chain.invoke({"input":t,"prompt":prompt})
76
+ print("\n")
77
+ print(response['answer'], "\n")
78
+ print("Response in", (datetime.now()-ret).seconds, "seconds\n\n")
79
+ else:
80
+ print("incorrect input")
81
+
82
+ gr.ChatInterface(
83
+ chat_function,
84
+ textbox=gr.Textbox(placeholder="Welcome aboard!", container=False, scale = 9),
85
+ textbox=gr.Textbox(placeholder="how can i help you further", container=False, scale = 3)
86
+ # additional_inputs=[gr.Textbox("You are helpful AI", label="System Prompt"),gr.Slider(500,4000, label="Max New Tokens"),gr.Slider(0,1, label="Temperature")]
87
+ ).launch()