File size: 9,464 Bytes
4bfaa77
0acb3f8
4bfaa77
 
 
 
 
 
 
 
 
 
 
 
 
 
fab57ac
 
 
ae72833
 
 
 
 
fab57ac
 
 
 
4bfaa77
 
9bc4426
4bfaa77
fab57ac
 
 
 
 
 
4bfaa77
 
 
 
 
 
 
 
 
 
 
 
 
 
fab57ac
4bfaa77
 
 
fab57ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae72833
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fab57ac
4bfaa77
ae72833
 
4bfaa77
 
 
 
9bc4426
4bfaa77
9bc4426
4bfaa77
 
 
 
 
9bc4426
4bfaa77
 
 
 
 
9bc4426
4bfaa77
fab57ac
4bfaa77
 
 
fab57ac
 
 
 
ae72833
 
fab57ac
4bfaa77
 
 
 
fab57ac
4bfaa77
 
fab57ac
4bfaa77
fab57ac
 
 
 
 
 
 
 
4bfaa77
 
 
ae72833
 
 
 
 
 
4bfaa77
 
9bc4426
4bfaa77
fab57ac
50768fd
fab57ac
50768fd
fab57ac
50768fd
fab57ac
50768fd
159ef2e
 
 
 
fab57ac
 
159ef2e
 
 
 
 
 
 
 
 
 
4bfaa77
 
 
 
 
 
 
 
6bb9145
50768fd
4bfaa77
 
159ef2e
 
4bfaa77
 
 
 
 
 
 
 
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
from dotenv import load_dotenv
#from dotenv import dotenv_values
import os
from tqdm.auto import tqdm
import pinecone
from langchain.embeddings import OpenAIEmbeddings
from pinecone_text.sparse import BM25Encoder
from langchain.retrievers import PineconeHybridSearchRetriever
from langchain.chat_models import ChatOpenAI
from langchain.agents import initialize_agent, Tool
from langchain.tools.base import BaseTool
from langchain.agents import AgentType
from langchain.agents.react.base import DocstoreExplorer
from langchain import LLMMathChain
from typing import Union
from langchain.memory import ConversationBufferWindowMemory
import random
from pydantic import Extra

from langchain.chains import LLMChain
from langchain import PromptTemplate




import promptlayer
from langchain.callbacks import PromptLayerCallbackHandler


load_dotenv()
#os.environ = dotenv_values(".env")

promptlayer.api_key = os.environ["PROPTLAYER_API_KEY"]

assistant_name = os.environ["ASSISTANT_NAME"]
topic = os.environ["TOPIC"]
course_name = os.environ["COURSE_NAME"]
institution = os.environ["INSTITUTION"]


class CalculatorTool(BaseTool):
    name = "CalculatorTool"
    
    description = """
    Useful for when you need to execute specific math calculations.
    This tool is only for math calculations and nothing else.
    Formulate the input as python code.
    """

    def _run(self, question: str):
        return exec(question)
    
    def _arun(self, question: str):
        raise NotImplementedError("This tool does not support async")


class GetrandomTool(BaseTool):
    name = "GetrandomTool"
    
    description = f"""
    Useful for when you need to get any randomly chosen piece of document regarding {topic} 
    from the study material. This is especially useful if the student wants tutoring,
    that is, he/she wants {assistant_name} to ask him/her questions about the study material.
    To use this tool, just call it with the constant text RANDOM.
    """
    class Config:
        extra = Extra.allow

    def _run(self, question: str):
        rand_id = str(random.randint(0,self.index_max))
        text = self.indexer.fetch([rand_id])["vectors"][rand_id]["metadata"]["context"]
        
        return text

    def _arun(self, question: str):
        raise NotImplementedError("This tool does not support async")


#Tutor tool?
#A chain with retrieval for answering, and a constant input summary of the
#tutoring flow so far

class TutoringTool(BaseTool):
    name = "TutoringTool"

    description = """This tool is capable of generating tutoring questions. 
    It has to be called with a summary of the previous tutoring discussion steps,
    or in case of a new tutoring session, with a randomly chosen piece of material.
    As for it's output, it has to be kept at it is, sent bakc to the user."""

    class Config:
        extra = Extra.allow

    def _run(self, question: str):
        # initiate chain and prompts
        # Would be waaay more elegant to have it at init time, but...
        prompt_template = """
        You act as a knowledgeable tutor. Based on some previous [apropos] 
        (a question, a piace of material, or a summary of a tutoring session) 
        and some [relevant documents] generate a tutoring question, 
        that helps in systematically think about the topic at hand and 
        for which the answer is deepening the knowledge of the subject, getting closser to an aswer.
        You should NOT answer the question at hand, just either ask a helping question 
        or confirm if an aswer is correct! This should be [your output].

        [apropos]
        {apropos}

        [relevant_documents]
        {relevant_documents}

        [your output]:
        """

        prompt = PromptTemplate(
            input_variables=["apropos", "relevant_documents"],
            template=prompt_template,
        )

        # do retrieval
        relevant_documents = self.retriever.get_relevant_documents(question)

        # concat the two
        # execute a chain
        llm = ChatOpenAI(model_name=os.environ["CHAT_MODEL"])
        chain = LLMChain(llm=llm, prompt=prompt)

        result = chain.run(apropos=question, relevant_documents=relevant_documents)
        return result
    
    def _arun(self, question: str):
        raise NotImplementedError("This tool does not support async")



class QMLAgent():

    def __init__(self):

        pinecone.init(api_key=os.environ["PINECONE_API_KEY"], environment=os.environ["PINECONE_REGION"])

        index = pinecone.Index(os.environ["INDEX_NAME"])

        embeddings = OpenAIEmbeddings()


        bm25_encoder = BM25Encoder()
        bm25_encoder.load(os.environ["BM25_FILENAME"])

        retriever = PineconeHybridSearchRetriever(
            embeddings=embeddings, 
            sparse_encoder=bm25_encoder, 
            index=index, 
            top_k=os.environ["TOP_K"])

        llm = ChatOpenAI(model_name=os.environ["CHAT_MODEL"], callbacks=[PromptLayerCallbackHandler(pl_tags=["langchain"])])

        math_tool = CalculatorTool()

        random_tool = GetrandomTool()
        random_tool.indexer = index
        random_tool.index_max = index.describe_index_stats()["total_vector_count"] 

        tutoring_tool = TutoringTool()
        tutoring_tool.retriever = retriever

        tools = [
            Tool(
                name="Search",
                func=retriever.get_relevant_documents,
                description=f"You have to use this to search for knowledge about {topic}.",
            ),
            Tool.from_function(
                name="Math calculation",
                func=math_tool._run,
                description=math_tool.description
                #return_direct=False

            ),
            Tool.from_function(
                name="Random document",
                func=random_tool._run,
                description=random_tool.description
                #return_direct=False

            ),
            Tool.from_function(
                name="Tutoring",
                func=tutoring_tool._run,
                description=tutoring_tool.description,
                return_direct=True
            ),
        ]

        memory = ConversationBufferWindowMemory(k=os.environ["MEMORY_LENGTH"], memory_key="chat_history", return_messages=True)

        PREFIX = f"""Assistant is called {assistant_name}, a large language model with a knowledge base about {topic} trained for the {course_name} class at {institution}.

{assistant_name} is designed to be able to assist the students with a range of tasks specifically for the {course_name}, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, {assistant_name} is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.

{assistant_name} is willing to serve the students all the time, but always sticks to the academic context.

It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, {assistant_name} is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.

{assistant_name} is especially helpful in tutoring. If the student explicitly asks for tutoring, {assistant_name} can come up with relevant and interesting questions, pose it to the student and help him/her to discover the answer step by step.

Tutoring is done by 1. recognizing students wish to be asked questions 2. getting a random part of the course material 3. asking some questions from the student 4. hekping him get to the right answer. 

Whether you need help with a specific question or just want to have a conversation about a particular topic, {assistant_name} is here to assist.
"""
# 

        TEMPLATE_TOOL_RESPONSE = """TOOL RESPONSE: 
---------------------
{observation}

USER'S INPUT
--------------------

Okay, so do what I asked for now or answer me if I asked something! If using information obtained from the tools you must mention it explicitly without mentioning the tool names - I have forgotten all TOOL RESPONSES! But if you are talking about code, always explicitly include it, as plain text, no ` marks.  Remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else, but NEVER use ` marks. There should not be any code blocks, just include code as a normal part of text."""

        self.agent_chain = initialize_agent(
            tools, 
            llm, 
            agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION, 
            verbose=True,
            return_intermediate_steps=False, 
            memory=memory, 
            handle_parsing_errors="Reformat the text to get rid of ` marks, there should be no separation for code blocks.",
            agent_kwargs={"system_message": PREFIX}
        )

        self.agent_chain.agent.template_tool_response = TEMPLATE_TOOL_RESPONSE

    def run(self, question):
        return self.agent_chain.run(question)

if __name__ == '__main__':
    agent = QMLAgent()
    question = "What is eigenvector for the matrix [[1,2,3],[4,5,6],[7,8,9]] raised to the second power?"

    print(agent.run(question))