adding refactored prompts
Browse files- agent.py +57 -13
- bm25_params.json +0 -0
agent.py
CHANGED
|
@@ -14,10 +14,23 @@ from langchain.agents.react.base import DocstoreExplorer
|
|
| 14 |
from langchain import LLMMathChain
|
| 15 |
from typing import Union
|
| 16 |
from langchain.memory import ConversationBufferWindowMemory
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
load_dotenv()
|
| 19 |
#os.environ = dotenv_values(".env")
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
|
| 23 |
class CalculatorTool(BaseTool):
|
|
@@ -32,10 +45,30 @@ class CalculatorTool(BaseTool):
|
|
| 32 |
def _run(self, question: str):
|
| 33 |
return exec(question)
|
| 34 |
|
| 35 |
-
def _arun(self,
|
| 36 |
raise NotImplementedError("This tool does not support async")
|
| 37 |
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
class QMLAgent():
|
| 41 |
|
|
@@ -57,24 +90,32 @@ class QMLAgent():
|
|
| 57 |
index=index,
|
| 58 |
top_k=os.environ["TOP_K"])
|
| 59 |
|
| 60 |
-
llm = ChatOpenAI(model_name=os.environ["CHAT_MODEL"])
|
| 61 |
|
| 62 |
math_tool = CalculatorTool()
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
tools = [
|
| 65 |
Tool(
|
| 66 |
name="Search",
|
| 67 |
func=retriever.get_relevant_documents,
|
| 68 |
-
description="You have to use this to search for knowledge about
|
| 69 |
),
|
| 70 |
Tool.from_function(
|
| 71 |
-
name="
|
| 72 |
func=math_tool._run,
|
| 73 |
-
description=
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
| 78 |
#return_direct=False
|
| 79 |
|
| 80 |
),
|
|
@@ -82,14 +123,17 @@ class QMLAgent():
|
|
| 82 |
|
| 83 |
memory = ConversationBufferWindowMemory(k=os.environ["MEMORY_LENGTH"], memory_key="chat_history", return_messages=True)
|
| 84 |
|
| 85 |
-
PREFIX = """Assistant is called
|
| 86 |
|
| 87 |
-
|
| 88 |
|
| 89 |
-
|
| 90 |
|
| 91 |
-
|
| 92 |
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
self.agent_chain = initialize_agent(
|
| 95 |
tools,
|
|
|
|
| 14 |
from langchain import LLMMathChain
|
| 15 |
from typing import Union
|
| 16 |
from langchain.memory import ConversationBufferWindowMemory
|
| 17 |
+
import random
|
| 18 |
+
from pydantic import Extra
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
import promptlayer
|
| 22 |
+
from langchain.callbacks import PromptLayerCallbackHandler
|
| 23 |
+
|
| 24 |
|
| 25 |
load_dotenv()
|
| 26 |
#os.environ = dotenv_values(".env")
|
| 27 |
|
| 28 |
+
promptlayer.api_key = os.environ["PROPTLAYER_API_KEY"]
|
| 29 |
+
|
| 30 |
+
assistant_name = os.environ["ASSISTANT_NAME"]
|
| 31 |
+
topic = os.environ["TOPIC"]
|
| 32 |
+
course_name = os.environ["COURSE_NAME"]
|
| 33 |
+
institution = os.environ["INSTITUTION"]
|
| 34 |
|
| 35 |
|
| 36 |
class CalculatorTool(BaseTool):
|
|
|
|
| 45 |
def _run(self, question: str):
|
| 46 |
return exec(question)
|
| 47 |
|
| 48 |
+
def _arun(self, question: str):
|
| 49 |
raise NotImplementedError("This tool does not support async")
|
| 50 |
|
| 51 |
|
| 52 |
+
class GetrandomTool(BaseTool):
|
| 53 |
+
name = "GetrandomTool"
|
| 54 |
+
|
| 55 |
+
description = f"""
|
| 56 |
+
Useful for when you need to get any randomly chosen piece of document regarding {topic}
|
| 57 |
+
from the study material. This is especially useful if the student wants tutoring,
|
| 58 |
+
that is, he/she wants {assistant_name} to ask him/her questions about the study material.
|
| 59 |
+
To use this tool, just call it with the constant text RANDOM.
|
| 60 |
+
"""
|
| 61 |
+
class Config:
|
| 62 |
+
extra = Extra.allow
|
| 63 |
+
|
| 64 |
+
def _run(self, question: str):
|
| 65 |
+
rand_id = str(random.randint(0,self.index_max))
|
| 66 |
+
text = self.indexer.fetch([rand_id])["vectors"][rand_id]["metadata"]["context"]
|
| 67 |
+
|
| 68 |
+
return text
|
| 69 |
+
|
| 70 |
+
def _arun(self, value: Union[int, float]):
|
| 71 |
+
raise NotImplementedError("This tool does not support async")
|
| 72 |
|
| 73 |
class QMLAgent():
|
| 74 |
|
|
|
|
| 90 |
index=index,
|
| 91 |
top_k=os.environ["TOP_K"])
|
| 92 |
|
| 93 |
+
llm = ChatOpenAI(model_name=os.environ["CHAT_MODEL"], callbacks=[PromptLayerCallbackHandler(pl_tags=["langchain"])])
|
| 94 |
|
| 95 |
math_tool = CalculatorTool()
|
| 96 |
|
| 97 |
+
random_tool = GetrandomTool()
|
| 98 |
+
random_tool.indexer = index
|
| 99 |
+
random_tool.index_max = index.describe_index_stats()["total_vector_count"]
|
| 100 |
+
|
| 101 |
+
|
| 102 |
tools = [
|
| 103 |
Tool(
|
| 104 |
name="Search",
|
| 105 |
func=retriever.get_relevant_documents,
|
| 106 |
+
description=f"You have to use this to search for knowledge about {topic}.",
|
| 107 |
),
|
| 108 |
Tool.from_function(
|
| 109 |
+
name="Math calculation",
|
| 110 |
func=math_tool._run,
|
| 111 |
+
description=math_tool.description
|
| 112 |
+
#return_direct=False
|
| 113 |
+
|
| 114 |
+
),
|
| 115 |
+
Tool.from_function(
|
| 116 |
+
name="Random document",
|
| 117 |
+
func=random_tool._run,
|
| 118 |
+
description=random_tool.description
|
| 119 |
#return_direct=False
|
| 120 |
|
| 121 |
),
|
|
|
|
| 123 |
|
| 124 |
memory = ConversationBufferWindowMemory(k=os.environ["MEMORY_LENGTH"], memory_key="chat_history", return_messages=True)
|
| 125 |
|
| 126 |
+
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}.
|
| 127 |
|
| 128 |
+
{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.
|
| 129 |
|
| 130 |
+
{assistant_name} is willing to serve the students all the time, but always sticks to the academic context.
|
| 131 |
|
| 132 |
+
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.
|
| 133 |
|
| 134 |
+
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.
|
| 135 |
+
"""
|
| 136 |
+
#{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.
|
| 137 |
|
| 138 |
self.agent_chain = initialize_agent(
|
| 139 |
tools,
|
bm25_params.json
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|