File size: 5,769 Bytes
e7c2848
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import os
import gradio as gr

from dotenv import load_dotenv
from openai import AzureOpenAI

from langchain_openai import AzureOpenAIEmbeddings

from langchain_community.vectorstores import Chroma
from langchain.retrievers import ContextualCompressionRetriever
from langchain.retrievers.document_compressors import CrossEncoderReranker
from langchain_community.cross_encoders import HuggingFaceCrossEncoder

load_dotenv()

client = AzureOpenAI(
    api_key=os.environ['AZURE_OPENAI_KEY'],
    azure_endpoint=os.environ['AZURE_OPENAI_ENDPOINT'],
    api_version='2024-02-01'
)

model_name = 'gpt-4o-mini'

embedding_model = AzureOpenAIEmbeddings(
    api_key=os.environ['AZURE_OPENAI_KEY'],
    azure_endpoint=os.environ['AZURE_OPENAI_ENDPOINT'],
    api_version='2024-02-01',
    azure_deployment="text-embedding-ada-002"
)

tesla_10k_collection = 'tesla-10k-2021-2023'

vectorstore_persisted = Chroma(
    collection_name=tesla_10k_collection,
    persist_directory='./tesla_db',
    embedding_function=embedding_model
)

retriever = vectorstore_persisted.as_retriever(
    search_type='similarity',
    search_kwargs={'k': 20}
)

cross_encoder_model = HuggingFaceCrossEncoder(model_name="cross-encoder/ms-marco-MiniLM-L-6-v2")
compressor = CrossEncoderReranker(model=cross_encoder_model, top_n=5)
compression_retriever = ContextualCompressionRetriever(
    base_compressor=compressor, base_retriever=retriever
)

# RAG Q&A

qna_system_message = """
You are an expert analyst at a financial services firm who answers user queries on annual reports.
User input will have the context required by you to answer user questions.
This context will begin with the word: ###Context.
The context contains documents relevant to the user query.
It also contains references to the metadata associated with the relevant documents.
In sum, the context provided to you will be a combination of information and the metadata for the source of information.  

User questions will begin with the word: ###Question.

Please answer user questions only using the context provided in the input and provide citations.
Remember, you must return both an answer and citations. A citation consists of a VERBATIM quote that
justifies the answer and the metadata of the quote article. 
Return a citation for every quote across all articles that justify the answer. 
Use the following format for your final output:

<cited_answer>
    <answer></answer>
    <citations>
        <citation><source_doc_year></source_doc_year><source_page></source_page><quote></quote></citation>
        <citation><source_doc_year></source_doc_year><source_page></source_page><quote></quote></citation>
        ...
    </citations>
</cited_answer>

If the answer is not found in the context, respond "I don't know".
"""

qna_user_message_template = """
###Context
Here are some documents that are relevant to the question mentioned below.
{context}

###Question
{question}
"""


def predict(user_input: str):

    relevant_document_chunks = retriever.invoke(user_input)

    relevant_document_chunks = compression_retriever.invoke(user_input)

    context_citation_list = [
        f'Information: {d.page_content}\nMetadata: {d.metadata}'
        for d in relevant_document_chunks
    ]

    context_for_query = "\n---\n".join(context_citation_list)

    prompt = [
        {'role':'system', 'content': qna_system_message},
        {'role': 'user', 'content': qna_user_message_template.format(
            context=context_for_query,
            question=user_input
            )
        }
    ]

    try:
        response = client.chat.completions.create(
            model=model_name,
            messages=prompt,
            temperature=0
        )

        prediction = response.choices[0].message.content.strip()
    except Exception as e:
        prediction = f'Sorry, I encountered the following error: \n {e}'

    return prediction


def parse_prediction(user_input: str):

    answer = predict(user_input)

    final_answer = answer[answer.find('<answer>')+len('<answer>'): answer.find('</answer>')]
    citations = answer[answer.find('<citations>')+len('<citations>'): answer.find('</citations>')].strip().split('</citations>')
    references = ''

    for i, citation in enumerate(citations):
        quote = citation[citation.find('<quote>')+len("<quote>"): citation.find('</quote>')]
        year = citation[citation.find('<source_doc_year>')+len("<source_doc_year>"): citation.find('</source_doc_year>')]
        page = citation[citation.find('<source_page>')+len("<source_page>"): citation.find('</source_page>')]
        references += f'{i+1}. Quote: {quote}, Annual Report: {year}, Page: {page}'

    return f'Answer: {final_answer}\n' + f'\nReferences:\n {references}'

# UI

textbox = gr.Textbox(placeholder="Enter your query here", lines=6)

demo = gr.Interface(
    inputs=textbox, fn=parse_prediction, outputs="text",
    title="AMA on Tesla 10-K statements",
    description="This web API presents an interface to ask questions on contents of the Tesla 10-K reports for the period 2021 - 2023.",
    article="Note that questions that are not relevant to the Tesla 10-K report will not be answered.",
    examples=[["What was the total revenue of the company in 2022?", ""],
              ["Summarize the Management Discussion and Analysis section of the 2021 report in 50 words.", ""],
              ["What was the company's debt level in 2023?", ""],
              ["Summarize 5 key risks identified in the 2023 10k report? Respond with bullet point summaries.", ""],
              ["What is the view of the management on the future of electric vehicle batteries?",""]
             ],
    cache_examples=False,
    theme=gr.themes.Base(),
    concurrency_limit=16
)

demo.queue()
demo.launch()