Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
from groq import Groq
|
4 |
+
from langchain_community.vectorstores import FAISS
|
5 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
6 |
+
|
7 |
+
def run_groq(prompt):
|
8 |
+
|
9 |
+
client = Groq(api_key = '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')
|
10 |
+
|
11 |
+
chat_completion = client.chat.completions.create(
|
12 |
+
messages = [
|
13 |
+
{
|
14 |
+
'role' : 'user' ,
|
15 |
+
'content' : prompt
|
16 |
+
}
|
17 |
+
] , model = 'llama3-70b-8192'
|
18 |
+
)
|
19 |
+
|
20 |
+
return chat_completion.choices[0].message.content
|
21 |
+
|
22 |
+
vc = FAISS.load_local(
|
23 |
+
'vc' ,
|
24 |
+
embeddings = HuggingFaceEmbeddings(model_name = 'all-MiniLM-L6-v2') ,
|
25 |
+
allow_dangerous_deserialization = True
|
26 |
+
)
|
27 |
+
|
28 |
+
def search(query) :
|
29 |
+
|
30 |
+
similar_docs = vc.similarity_search(query)
|
31 |
+
context = '\n'.join([doc.page_content for doc in similar_docs])
|
32 |
+
|
33 |
+
prompt = f'''
|
34 |
+
You are given a user query, some textual context and rules, all inside xml tags. You have to answer the query based on the context while respecting the rules.
|
35 |
+
|
36 |
+
<context>
|
37 |
+
{context}
|
38 |
+
</context>
|
39 |
+
|
40 |
+
<rules>
|
41 |
+
- If you don't know, just say so.
|
42 |
+
- If you are not sure, ask for clarification.
|
43 |
+
- Answer in the same language as the user query.
|
44 |
+
- If the context appears unreadable or of poor quality, tell the user then answer as best as you can.
|
45 |
+
- If the answer is not in the context but you think you know the answer, explain that to the user then answer with your own knowledge.
|
46 |
+
- Answer directly and without using xml tags.
|
47 |
+
|
48 |
+
- The context will contain some youtube video ids,
|
49 |
+
- Try to cite links using the ids each time you answer the query
|
50 |
+
- When citing the links , use the format https://www.youtube.com/watch?v=<id>
|
51 |
+
- DO not mention that you are using the context or the infromation is from the context or the information provided, treat the given context as your learned knowldge
|
52 |
+
|
53 |
+
- Try to strucutre your response in markdown format, use points, tables, bold formats and much more to beautify the response
|
54 |
+
</rules>
|
55 |
+
|
56 |
+
<user_query>
|
57 |
+
{query}
|
58 |
+
</user_query>
|
59 |
+
'''
|
60 |
+
|
61 |
+
response = run_groq(prompt)
|
62 |
+
|
63 |
+
return response
|
64 |
+
|
65 |
+
def check_prompt(prompt) :
|
66 |
+
|
67 |
+
try : prompt.replace('' , '') ; return True
|
68 |
+
except : return False
|
69 |
+
|
70 |
+
def check_mesaage() :
|
71 |
+
|
72 |
+
if 'messages' not in st.session_state : st.session_state.messages = []
|
73 |
+
|
74 |
+
check_mesaage()
|
75 |
+
|
76 |
+
for message in st.session_state.messages :
|
77 |
+
|
78 |
+
with st.chat_message(message['role']) : st.markdown(message['content'])
|
79 |
+
|
80 |
+
prompt = st.chat_input('Ask me anything')
|
81 |
+
|
82 |
+
if check_prompt(prompt) :
|
83 |
+
|
84 |
+
with st.chat_message('user'): st.markdown(prompt)
|
85 |
+
|
86 |
+
st.session_state.messages.append({
|
87 |
+
'role' : 'user' ,
|
88 |
+
'content' : prompt
|
89 |
+
})
|
90 |
+
|
91 |
+
if prompt != None or prompt != '' :
|
92 |
+
|
93 |
+
response = search(prompt)
|
94 |
+
|
95 |
+
with st.chat_message('assistant') : st.markdown(response)
|
96 |
+
|
97 |
+
st.session_state.messages.append({
|
98 |
+
'role' : 'assistant' ,
|
99 |
+
'content' : response
|
100 |
+
})
|