rishisim commited on
Commit
527bfa3
1 Parent(s): b2b549a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -1
app.py CHANGED
@@ -1,11 +1,88 @@
1
  import gradio as gr
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  def chatresponse(message, history):
4
- return history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  # Launch the Gradio chat interface
7
  gr.ChatInterface(chatresponse).launch()
8
 
 
 
 
 
 
 
 
 
9
  # import gradio as gr
10
  # from huggingface_hub import InferenceClient
11
 
 
1
  import gradio as gr
2
 
3
+
4
+ import os
5
+ hftoken = os.environ["hftoken"]
6
+
7
+ from langchain_huggingface import HuggingFaceEndpoint
8
+
9
+ repo_id = "mistralai/Mistral-7B-Instruct-v0.3"
10
+ llm = HuggingFaceEndpoint(repo_id = repo_id, max_new_tokens = 128, temperature = 0.7, huggingfacehub_api_token = hftoken)
11
+
12
+ from langchain_core.output_parsers import StrOutputParser
13
+ from langchain_core.prompts import ChatPromptTemplate
14
+
15
+ prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}")
16
+ chain = prompt | llm | StrOutputParser()
17
+
18
+ # from langchain.document_loaders.csv_loader import CSVLoader
19
+ from langchain_community.document_loaders.csv_loader import CSVLoader
20
+
21
+
22
+ loader = CSVLoader(file_path='aiotsmartlabs_faq.csv', source_column = 'prompt')
23
+ data = loader.load()
24
+
25
+ from langchain_huggingface import HuggingFaceEmbeddings
26
+ from langchain_chroma import Chroma
27
+ from langchain_huggingface.embeddings import HuggingFaceEndpointEmbeddings
28
+
29
+ # CHECK MTEB LEADERBOARD & FIND BEST EMBEDDING MODEL
30
+ model = "BAAI/bge-m3"
31
+ embeddings = HuggingFaceEndpointEmbeddings(model = model)
32
+
33
+ vectorstore = Chroma.from_documents(documents = data, embedding = embeddings)
34
+ retriever = vectorstore.as_retriever()
35
+
36
+ # from langchain.prompts import PromptTemplate
37
+
38
+ from langchain_core.prompts import ChatPromptTemplate
39
+
40
+ prompt = ChatPromptTemplate.from_template("""Given the following history, context and a question, generate an answer based on the context only.
41
+
42
+ In the answer try to provide as much text as possible from "response" section in the source document context without making much changes.
43
+ If somebody asks "Who are you?" or a similar phrase, state "I am Rishi's assistant built using a Large Language Model!"
44
+ If the answer is not found in the context, kindly state "I don't know. Please ask Rishi on Discord. Discord Invite Link: https://discord.gg/6ezpZGeCcM. Or email at rishi@aiotsmartlabs.com" Don't try to make up an answer.
45
+
46
+ CONTEXT: {context}
47
+
48
+ HISTORY: {history}
49
+
50
+ QUESTION: {question}""")
51
+
52
+ from langchain_core.runnables import RunnablePassthrough
53
+
54
+ # Define the chat response function
55
  def chatresponse(message, history):
56
+ history_langchain_format = []
57
+ for human, ai in history:
58
+ history_langchain_format.append(HumanMessage(content=human))
59
+ history_langchain_format.append(AIMessage(content=ai))
60
+ history_langchain_format.append(HumanMessage(content=message))
61
+ gpt_response = llm(history_langchain_format)
62
+
63
+ rag_chain = (
64
+ {"context": retriever, "history": history_langchain_format, "question": RunnablePassthrough()}
65
+ | prompt
66
+ | llm
67
+ | StrOutputParser()
68
+ )
69
+
70
+
71
+ output = rag_chain.invoke(message)
72
+ response = output.split('ANSWER: ')[-1].strip()
73
+ return response
74
 
75
  # Launch the Gradio chat interface
76
  gr.ChatInterface(chatresponse).launch()
77
 
78
+ # import gradio as gr
79
+
80
+ # def chatresponse(message, history):
81
+ # return history
82
+
83
+ # # Launch the Gradio chat interface
84
+ # gr.ChatInterface(chatresponse).launch()
85
+
86
  # import gradio as gr
87
  # from huggingface_hub import InferenceClient
88