File size: 3,262 Bytes
376e29a
 
 
 
 
 
 
 
77f8e7b
376e29a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc5b375
 
2766d76
376e29a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import streamlit as st 

from groq import Groq
from langchain_community.vectorstores import FAISS
from langchain_huggingface import HuggingFaceEmbeddings

def run_groq(prompt):

    client = Groq(api_key = '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')

    chat_completion = client.chat.completions.create(
        messages = [
            {
                'role' : 'user' , 
                'content' : prompt
            }
        ] , model = 'llama3-70b-8192'
    )

    return chat_completion.choices[0].message.content

vc = FAISS.load_local(
    'vc' , 
    embeddings = HuggingFaceEmbeddings(model_name = 'all-MiniLM-L6-v2') , 
    allow_dangerous_deserialization = True
)

def search(query) : 

    similar_docs = vc.similarity_search(query) 
    context = '\n'.join([doc.page_content for doc in similar_docs])

    prompt = f'''
    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.

    <context>
    {context}
    </context>

    <rules>
    - If you don't know, just say so.
    - If you are not sure, ask for clarification.
    - Answer in the same language as the user query.
    - If the context appears unreadable or of poor quality, tell the user then answer as best as you can.
    - 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.
    - Answer directly and without using xml tags.

    - The context will contain some youtube video ids,
    - Try to cite links using the ids each time you answer the query
    - When citing the links , use the format https://www.youtube.com/watch?v=<id>
    - 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
    - The context will also have yotube video_title at the end after the video_id, only mention the video in the citation if the title matches with the query. ALso mention the video title while giving citations
    - Always provide the citations at the end with their title and link
    
    - Try to strucutre your response in markdown format, use points, tables, bold formats and much more to beautify the response
    </rules>

    <user_query>
    {query}
    </user_query>
    '''

    response = run_groq(prompt)

    return response

def check_prompt(prompt) : 

    try : prompt.replace('' , '') ; return True 
    except : return False

def check_mesaage() : 

    if 'messages' not in st.session_state : st.session_state.messages = []

check_mesaage()

for message in st.session_state.messages : 

    with st.chat_message(message['role']) : st.markdown(message['content'])

prompt = st.chat_input('Ask me anything')

if check_prompt(prompt) :

    with st.chat_message('user'): st.markdown(prompt)

    st.session_state.messages.append({
        'role' : 'user' , 
        'content' : prompt
    })

    if prompt != None or prompt != '' : 

        response = search(prompt)

        with st.chat_message('assistant') : st.markdown(response)

        st.session_state.messages.append({
            'role' : 'assistant' , 
            'content' : response
        })