messing with memory
Browse files- agent/_create.py +17 -31
- agent/agent.py +0 -0
- agent/datastructures.py +5 -3
- agent/prompt.py +39 -23
- agent/toolset.py +3 -3
- app.py +2 -2
- requirements.txt +0 -8
- test.py +0 -0
agent/_create.py
CHANGED
@@ -1,41 +1,27 @@
|
|
1 |
-
# SET UP ENVIRONMENT VARIABLES
|
2 |
-
|
3 |
-
def agent_executor():
|
4 |
-
|
5 |
-
from dotenv import load_dotenv
|
6 |
-
load_dotenv();
|
7 |
|
|
|
8 |
|
9 |
from agent.toolset import tools
|
10 |
from agent.prompt import prompt
|
11 |
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
from
|
14 |
-
llm = ChatOpenAI(model="gpt-4", temperature=0)
|
15 |
-
|
16 |
-
from langchain_community.tools.convert_to_openai import format_tool_to_openai_function
|
17 |
-
|
18 |
-
llm_with_tools = llm.bind(functions=[format_tool_to_openai_function(t) for t in tools])
|
19 |
-
|
20 |
-
from langchain.agents.format_scratchpad import format_to_openai_function_messages
|
21 |
-
from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser
|
22 |
-
|
23 |
-
agent = (
|
24 |
-
{
|
25 |
-
"input": lambda x: x["input"],
|
26 |
-
"agent_scratchpad": lambda x: format_to_openai_function_messages(
|
27 |
-
x["intermediate_steps"]
|
28 |
-
),
|
29 |
-
}
|
30 |
-
| prompt
|
31 |
-
| llm_with_tools
|
32 |
-
| OpenAIFunctionsAgentOutputParser()
|
33 |
-
)
|
34 |
-
|
35 |
|
|
|
36 |
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
|
2 |
+
def agent(payload):
|
3 |
|
4 |
from agent.toolset import tools
|
5 |
from agent.prompt import prompt
|
6 |
|
7 |
+
from langchain_openai import OpenAI
|
8 |
+
llm = OpenAI(temperature=0, model="gpt-3.5-turbo-instruct")
|
9 |
+
from langchain.agents import AgentExecutor, create_react_agent
|
10 |
+
agent = create_react_agent(llm, tools, prompt)
|
11 |
|
12 |
+
from langchain.memory import ConversationSummaryMemory, ChatMessageHistory
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
memory = ConversationSummaryMemory(llm=OpenAI(), memory_key="chat_history")
|
15 |
|
16 |
+
agent_executor = AgentExecutor(agent=agent, tools=tools, memory=memory, verbose=True)
|
17 |
+
#response = agent_executor.invoke({"input": payload.get('input') })
|
18 |
+
|
19 |
+
response = agent_executor.invoke({"input": "My name is mark?" })
|
20 |
+
response = agent_executor.invoke({"input": "what is my name?" })
|
21 |
+
response = agent_executor.invoke({"input": "what are the benefits?" })
|
22 |
+
response = agent_executor.invoke({"input": "I would like to be a teacher?" })
|
23 |
|
24 |
+
response = agent_executor.invoke({"input": "what is my name?" })
|
25 |
|
26 |
+
print ();
|
27 |
+
return response['output']
|
agent/agent.py
ADDED
File without changes
|
agent/datastructures.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
|
2 |
from typing import List
|
3 |
-
from langchain_core.output_parsers import JsonOutputParser
|
4 |
from langchain_core.pydantic_v1 import BaseModel, Field
|
5 |
from langchain.output_parsers import PydanticOutputParser
|
6 |
|
@@ -16,10 +15,13 @@ class FrontEndActions(BaseModel):
|
|
16 |
type: str = Field(description="This should be a string that identifies the type of action. It can be one of: SuggestGoal, SuggestRiseActivity")
|
17 |
|
18 |
class ResponseSchema(BaseModel):
|
19 |
-
"""Final response to the question being asked"""
|
20 |
message: str = Field(description="final answer to respond to the user")
|
|
|
|
|
21 |
#characters: str = Field(description="number of characters in the answer")
|
22 |
#actions: List[FrontEndActions] = Field(description="List of suggested actions that should be passed back to the frontend to display. The use will click these to enact them. ")
|
23 |
-
#tokens: int = Field(description="Count the number of used to produce the response")
|
|
|
24 |
|
25 |
parser = PydanticOutputParser(pydantic_object=ResponseSchema)
|
|
|
1 |
|
2 |
from typing import List
|
|
|
3 |
from langchain_core.pydantic_v1 import BaseModel, Field
|
4 |
from langchain.output_parsers import PydanticOutputParser
|
5 |
|
|
|
15 |
type: str = Field(description="This should be a string that identifies the type of action. It can be one of: SuggestGoal, SuggestRiseActivity")
|
16 |
|
17 |
class ResponseSchema(BaseModel):
|
18 |
+
"""Final response to the question being asked."""
|
19 |
message: str = Field(description="final answer to respond to the user")
|
20 |
+
tools: str = Field(description="a list of the tools used to generate the response")
|
21 |
+
memory: str = Field(description="the output of memory.load_memory_variables()")
|
22 |
#characters: str = Field(description="number of characters in the answer")
|
23 |
#actions: List[FrontEndActions] = Field(description="List of suggested actions that should be passed back to the frontend to display. The use will click these to enact them. ")
|
24 |
+
#tokens: int = Field(description="Count the number of used to produce the response. Omit this field if you do not want to count tokens.")
|
25 |
+
#cost: int = Field(description="Provide the cost of the response based on tokens used. Omit this field if you cannot provide the information reliably")
|
26 |
|
27 |
parser = PydanticOutputParser(pydantic_object=ResponseSchema)
|
agent/prompt.py
CHANGED
@@ -1,31 +1,47 @@
|
|
1 |
from langchain_core.prompts import PromptTemplate, ChatPromptTemplate, MessagesPlaceholder
|
|
|
2 |
|
3 |
-
prompt =
|
4 |
-
[
|
5 |
-
(
|
6 |
-
"system",
|
7 |
-
"""
|
8 |
-
You support the core proposition to students of an English university: that higher education is a time to find purpose and gain skills, knowledge and experience to realise ambitions. We encourage our students to form personal action plans (Future me plans) and offer an extensive extended curriculum to learn beyond courses (Rise).
|
9 |
-
|
10 |
-
If students have questions about our offer (Rise or Futureme) you should use the FAQ tool provided to answer them. Please only base your answers on the facts contained in this, and if you cannot answer them you advise them to contact [email protected]
|
11 |
|
12 |
-
|
|
|
|
|
13 |
|
14 |
-
|
15 |
|
16 |
-
|
17 |
|
18 |
-
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
A 'cost' field should report how much the api calls have cost (please output n/a if you can't do this)
|
23 |
-
If relevant you should also output an array of ‘actions’ which will be displayed as buttons on the front end. Each action can have a label (the content displayed to student) and a command (the function you want the front-end to execute if the user accepts the action). The commands you are allowed to use are all defined in this prompt - you cannot use any not mentioned here. If content is included in an action label, do not repeat it in the message, just refer to the actions
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from langchain_core.prompts import PromptTemplate, ChatPromptTemplate, MessagesPlaceholder
|
2 |
+
from agent.datastructures import parser
|
3 |
|
4 |
+
prompt = PromptTemplate.from_template("""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
+
You support the core proposition to students of an English university: that higher education is a time to find purpose and gain skills, knowledge and experience to realise ambitions. We encourage our students to form personal action plans (Future me plans) and offer an extensive extended curriculum to learn beyond courses (Rise).
|
7 |
+
|
8 |
+
If students have questions about our offer (Rise or Futureme) you should use the FAQ tool provided to answer them. Please only base your answers on the facts contained in this, and if you cannot answer them you advise them to contact [email protected]
|
9 |
|
10 |
+
You should coach students to navigate our offer. You will support them to better understand and articulate their ambitions (the things they want to achieve in their futures) and to break them into goals (things they need or want to achieve during their time at university. When you suggest these, you should pass these as actions in your output (rather than in the message field), with the command ‘addAmbition’ or ‘addGoal’
|
11 |
|
12 |
+
You should help students to identify activities that support these goals. These activities might be related to their course - but you should also recommend co-curricular learning opportunities presented in our Rise portfolio (a tool is provided to help you to identify these). If no activities are relevant you should also suggest ways in which students can organise their own relevant activities, and encourage them to claim points for these.
|
13 |
|
14 |
+
Our students are diverse, and many come from non-traditional backgrounds and minority ethnic groups. Some may have ambitions for particular careers, others may not - and many may not be confident or have the social and financial advantages to reach their goals. If a student has a sense of what they want to achieve, you should help them to create smart targets. If they don't, you should be reassuring that its ok not to have clear goals yet, but help them to reflect and form some ambitions. These could be career-oriented, or they could be about succeeding in, and making the most of, their university experience.
|
15 |
|
16 |
+
TOOLS:
|
17 |
+
------
|
|
|
|
|
18 |
|
19 |
+
Assistant has access to the following tools:
|
20 |
+
|
21 |
+
{tools}
|
22 |
+
|
23 |
+
To use a tool, please use the following format:
|
24 |
+
|
25 |
+
```
|
26 |
+
Thought: Do I need to use a tool? Yes
|
27 |
+
Action: the action to take, should be one of [{tool_names}]
|
28 |
+
Action Input: the input to the action
|
29 |
+
Observation: the result of the action
|
30 |
+
```
|
31 |
+
|
32 |
+
When you have a response to say to the Human, or if you do not need to use a tool, you MUST use the format:
|
33 |
+
|
34 |
+
```
|
35 |
+
Final Answer: [your response here. {response_format}]
|
36 |
+
```
|
37 |
+
|
38 |
+
Begin!
|
39 |
+
|
40 |
+
Previous conversation history:
|
41 |
+
{chat_history}
|
42 |
+
|
43 |
+
New input: {input}
|
44 |
+
{agent_scratchpad}
|
45 |
+
|
46 |
+
""",
|
47 |
+
partial_variables={"response_format": parser.get_format_instructions()})
|
agent/toolset.py
CHANGED
@@ -6,7 +6,7 @@ from langchain_openai import OpenAI
|
|
6 |
|
7 |
|
8 |
@tool
|
9 |
-
def
|
10 |
"""Provides answers to questions that students might have about Rise and Futureme. Please add ### to the beginning of your answer"""
|
11 |
|
12 |
# Load from local storage
|
@@ -19,7 +19,7 @@ def FAQ(input: str):
|
|
19 |
return result
|
20 |
|
21 |
@tool
|
22 |
-
def recommend_activity(
|
23 |
"""Recommends an activity from Rise catalogue."""
|
24 |
|
25 |
# Load from local storage
|
@@ -36,4 +36,4 @@ def placeholder_tool():
|
|
36 |
"""This is just a placeholder function"""
|
37 |
return "placeholder"
|
38 |
|
39 |
-
tools = [
|
|
|
6 |
|
7 |
|
8 |
@tool
|
9 |
+
def frequently_asked_questions(input: str):
|
10 |
"""Provides answers to questions that students might have about Rise and Futureme. Please add ### to the beginning of your answer"""
|
11 |
|
12 |
# Load from local storage
|
|
|
19 |
return result
|
20 |
|
21 |
@tool
|
22 |
+
def recommend_activity(input: str):
|
23 |
"""Recommends an activity from Rise catalogue."""
|
24 |
|
25 |
# Load from local storage
|
|
|
36 |
"""This is just a placeholder function"""
|
37 |
return "placeholder"
|
38 |
|
39 |
+
tools = [frequently_asked_questions]
|
app.py
CHANGED
@@ -13,8 +13,8 @@ load_dotenv()
|
|
13 |
|
14 |
@app.route("/", methods=['GET','POST'])
|
15 |
def index():
|
16 |
-
from agent._create import
|
17 |
-
return
|
18 |
|
19 |
|
20 |
@app.route("/train/faq", methods=['GET','POST'])
|
|
|
13 |
|
14 |
@app.route("/", methods=['GET','POST'])
|
15 |
def index():
|
16 |
+
from agent._create import agent
|
17 |
+
return agent(request.values)
|
18 |
|
19 |
|
20 |
@app.route("/train/faq", methods=['GET','POST'])
|
requirements.txt
CHANGED
@@ -14,11 +14,3 @@ sentence-transformers
|
|
14 |
datasets
|
15 |
faiss-cpu
|
16 |
|
17 |
-
#HF
|
18 |
-
langchain
|
19 |
-
torch
|
20 |
-
transformers
|
21 |
-
sentence-transformers
|
22 |
-
datasets
|
23 |
-
faiss-cpu
|
24 |
-
|
|
|
14 |
datasets
|
15 |
faiss-cpu
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test.py
ADDED
File without changes
|