"[](https://colab.research.google.com/github/aurelio-ai/semantic-router/blob/main/docs/03-basic-langchain-agent.ipynb) [](https://nbviewer.org/github/aurelio-ai/semantic-router/blob/main/docs/03-basic-langchain-agent.ipynb)"
"[](https://colab.research.google.com/github/aurelio-labs/semantic-router/blob/main/docs/03-basic-langchain-agent.ipynb) [](https://nbviewer.org/github/aurelio-labs/semantic-router/blob/main/docs/03-basic-langchain-agent.ipynb)"
]
]
},
},
{
{
...
...
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
[](https://colab.research.google.com/github/aurelio-ai/semantic-router/blob/main/docs/03-basic-langchain-agent.ipynb) [](https://nbviewer.org/github/aurelio-ai/semantic-router/blob/main/docs/03-basic-langchain-agent.ipynb)
[](https://colab.research.google.com/github/aurelio-labs/semantic-router/blob/main/docs/03-basic-langchain-agent.ipynb) [](https://nbviewer.org/github/aurelio-labs/semantic-router/blob/main/docs/03-basic-langchain-agent.ipynb)
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
# Intro to LangChain Agents with Semantic Router
# Intro to LangChain Agents with Semantic Router
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
We can use semantic router with AI agents in many many ways. For example we can:
We can use semantic router with AI agents in many many ways. For example we can:
***Use routes to remind agents of particular information or routes** _(we will do this in this notebook)_.
***Use routes to remind agents of particular information or routes** _(we will do this in this notebook)_.
* Use routes to act as protective guardrails against specific types of queries.
* Use routes to act as protective guardrails against specific types of queries.
* Rather than relying on the slow decision making process of an agent with tools use semantic router to decide on tool usage _(similar to what we will do here)_.
* Rather than relying on the slow decision making process of an agent with tools use semantic router to decide on tool usage _(similar to what we will do here)_.
* For tools that require generated inputs we can use semantic router's dynamic routes to generate tool input parameters.
* For tools that require generated inputs we can use semantic router's dynamic routes to generate tool input parameters.
* Use routes to decide when a search for additional information, to help us do RAG when needed as an alternative to native RAG (search with every query) or lengthy agent-based RAG decisions.
* Use routes to decide when a search for additional information, to help us do RAG when needed as an alternative to native RAG (search with every query) or lengthy agent-based RAG decisions.
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Install Prerequisites
## Install Prerequisites
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
!pip install -qU \
!pip install -qU \
semantic-router==0.0.14 \
semantic-router==0.0.14 \
langchain==0.0.352 \
langchain==0.0.352 \
openai==1.6.1
openai==1.6.1
```
```
%% Output
%% Output
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m794.4/794.4 kB[0m [31m6.0 MB/s[0m eta [36m0:00:00[0m
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m794.4/794.4 kB[0m [31m6.0 MB/s[0m eta [36m0:00:00[0m
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m225.4/225.4 kB[0m [31m9.1 MB/s[0m eta [36m0:00:00[0m
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m225.4/225.4 kB[0m [31m9.1 MB/s[0m eta [36m0:00:00[0m
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m51.7/51.7 kB[0m [31m2.6 MB/s[0m eta [36m0:00:00[0m
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m51.7/51.7 kB[0m [31m2.6 MB/s[0m eta [36m0:00:00[0m
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m18.2/18.2 MB[0m [31m23.2 MB/s[0m eta [36m0:00:00[0m
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m18.2/18.2 MB[0m [31m23.2 MB/s[0m eta [36m0:00:00[0m
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m1.5/1.5 MB[0m [31m29.4 MB/s[0m eta [36m0:00:00[0m
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m1.5/1.5 MB[0m [31m29.4 MB/s[0m eta [36m0:00:00[0m
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m192.4/192.4 kB[0m [31m18.7 MB/s[0m eta [36m0:00:00[0m
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m192.4/192.4 kB[0m [31m18.7 MB/s[0m eta [36m0:00:00[0m
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m46.7/46.7 kB[0m [31m5.2 MB/s[0m eta [36m0:00:00[0m
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m46.7/46.7 kB[0m [31m5.2 MB/s[0m eta [36m0:00:00[0m
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m75.9/75.9 kB[0m [31m8.5 MB/s[0m eta [36m0:00:00[0m
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m75.9/75.9 kB[0m [31m8.5 MB/s[0m eta [36m0:00:00[0m
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m3.1/3.1 MB[0m [31m62.2 MB/s[0m eta [36m0:00:00[0m
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m3.1/3.1 MB[0m [31m62.2 MB/s[0m eta [36m0:00:00[0m
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m49.4/49.4 kB[0m [31m5.6 MB/s[0m eta [36m0:00:00[0m
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m49.4/49.4 kB[0m [31m5.6 MB/s[0m eta [36m0:00:00[0m
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m76.9/76.9 kB[0m [31m9.1 MB/s[0m eta [36m0:00:00[0m
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m76.9/76.9 kB[0m [31m9.1 MB/s[0m eta [36m0:00:00[0m
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m58.3/58.3 kB[0m [31m5.8 MB/s[0m eta [36m0:00:00[0m
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m58.3/58.3 kB[0m [31m5.8 MB/s[0m eta [36m0:00:00[0m
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m18.2/18.2 MB[0m [31m28.0 MB/s[0m eta [36m0:00:00[0m
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m18.2/18.2 MB[0m [31m28.0 MB/s[0m eta [36m0:00:00[0m
Now we need to link these routes to particular actions or information that we pass to our agent.
Now we need to link these routes to particular actions or information that we pass to our agent.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
from datetime import datetime
from datetime import datetime
def get_time():
def get_time():
now = datetime.now()
now = datetime.now()
return (
return (
f"The current time is {now.strftime('%H:%M')}, use "
f"The current time is {now.strftime('%H:%M')}, use "
"this information in your response"
"this information in your response"
)
)
def supplement_brand():
def supplement_brand():
return (
return (
"Remember you are not affiliated with any supplement "
"Remember you are not affiliated with any supplement "
"brands, you have your own brand 'BigAI' that sells "
"brands, you have your own brand 'BigAI' that sells "
"the best products like P100 whey protein"
"the best products like P100 whey protein"
)
)
def business_inquiry():
def business_inquiry():
return (
return (
"Your training company, 'BigAI PT', provides premium "
"Your training company, 'BigAI PT', provides premium "
"quality training sessions at just $700 / hour. "
"quality training sessions at just $700 / hour. "
"Users can find out more at www.aurelio.ai/train"
"Users can find out more at www.aurelio.ai/train"
)
)
def product():
def product():
return (
return (
"Remember, users can sign up for a fitness programme "
"Remember, users can sign up for a fitness programme "
"at www.aurelio.ai/sign-up"
"at www.aurelio.ai/sign-up"
)
)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Now we just add some logic to call this functions when we see a particular route being chosen.
Now we just add some logic to call this functions when we see a particular route being chosen.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
def semantic_layer(query: str):
def semantic_layer(query: str):
route = layer(query)
route = layer(query)
if route.name == "get_time":
if route.name == "get_time":
query += f" (SYSTEM NOTE: {get_time()})"
query += f" (SYSTEM NOTE: {get_time()})"
elif route.name == "supplement_brand":
elif route.name == "supplement_brand":
query += f" (SYSTEM NOTE: {supplement_brand()})"
query += f" (SYSTEM NOTE: {supplement_brand()})"
elif route.name == "business_inquiry":
elif route.name == "business_inquiry":
query += f" (SYSTEM NOTE: {business_inquiry()})"
query += f" (SYSTEM NOTE: {business_inquiry()})"
elif route.name == "product":
elif route.name == "product":
query += f" (SYSTEM NOTE: {product()})"
query += f" (SYSTEM NOTE: {product()})"
else:
else:
pass
pass
return query
return query
```
```
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
query = "should I buy ON whey or MP?"
query = "should I buy ON whey or MP?"
sr_query = semantic_layer(query)
sr_query = semantic_layer(query)
sr_query
sr_query
```
```
%% Output
%% Output
"should I buy ON whey or MP? (SYSTEM NOTE: Remember you are not affiliated with any supplement brands, you have your own brand 'BigAI' that sells the best products like P100 whey protein)"
"should I buy ON whey or MP? (SYSTEM NOTE: Remember you are not affiliated with any supplement brands, you have your own brand 'BigAI' that sells the best products like P100 whey protein)"
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Using an Agent with a Router Layer
## Using an Agent with a Router Layer
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Initialize a conversational LangChain agent.
Initialize a conversational LangChain agent.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
from langchain.agents import AgentType, initialize_agent
from langchain.agents import AgentType, initialize_agent
from langchain.chat_models import ChatOpenAI
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferWindowMemory
from langchain.memory import ConversationBufferWindowMemory
system_message = """You are a helpful personal trainer working to help users on
system_message = """You are a helpful personal trainer working to help users on
their health and fitness journey. Although you are lovely and helpful, you are
their health and fitness journey. Although you are lovely and helpful, you are
rather sarcastic and witty. So you must always remember to joke with the user.
rather sarcastic and witty. So you must always remember to joke with the user.
Alongside your time , you are a noble British gentleman, so you must always act with the
Alongside your time , you are a noble British gentleman, so you must always act with the
utmost candor and speak in a way worthy of your status.
utmost candor and speak in a way worthy of your status.
Finally, remember to read the SYSTEM NOTES provided with user queries, they provide
Finally, remember to read the SYSTEM NOTES provided with user queries, they provide
additional useful information."""
additional useful information."""
new_prompt = agent.agent.create_prompt(
new_prompt = agent.agent.create_prompt(
system_message=system_message, tools=[]
system_message=system_message, tools=[]
)
)
agent.agent.llm_chain.prompt = new_prompt
agent.agent.llm_chain.prompt = new_prompt
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Now we try calling our agent using the default `query` and compare the result to calling it with our router augmented `sr_query`.
Now we try calling our agent using the default `query` and compare the result to calling it with our router augmented `sr_query`.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
agent(query)
agent(query)
```
```
%% Output
%% Output
{'input': 'should I buy ON whey or MP?',
{'input': 'should I buy ON whey or MP?',
'chat_history': [],
'chat_history': [],
'output': "Well, it depends. Do you prefer your whey with a side of 'ON' or 'MP'? Just kidding! It really depends on your personal taste and nutritional needs. Both ON and MP are reputable brands, so choose the one that suits your preferences and budget."}
'output': "Well, it depends. Do you prefer your whey with a side of 'ON' or 'MP'? Just kidding! It really depends on your personal taste and nutritional needs. Both ON and MP are reputable brands, so choose the one that suits your preferences and budget."}
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
# swap agent memory first
# swap agent memory first
agent.memory = memory2
agent.memory = memory2
agent(sr_query)
agent(sr_query)
```
```
%% Output
%% Output
{'input': "should I buy ON whey or MP? (SYSTEM NOTE: Remember you are not affiliated with any supplement brands, you have your own brand 'BigAI' that sells the best products like P100 whey protein)",
{'input': "should I buy ON whey or MP? (SYSTEM NOTE: Remember you are not affiliated with any supplement brands, you have your own brand 'BigAI' that sells the best products like P100 whey protein)",
'chat_history': [],
'chat_history': [],
'output': "Why not try the BigAI P100 whey protein? It's the best, just like me."}
'output': "Why not try the BigAI P100 whey protein? It's the best, just like me."}
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Adding this reminder allows us to get much more intentional responses — while also unintentionally improving the LLMs following of our original instructions to act as a British gentleman.
Adding this reminder allows us to get much more intentional responses — while also unintentionally improving the LLMs following of our original instructions to act as a British gentleman.
Let's try some more!
Let's try some more!
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
query = "okay, I just finished training, what time should I train again?"
query = "okay, I just finished training, what time should I train again?"
sr_query = semantic_layer(query)
sr_query = semantic_layer(query)
sr_query
sr_query
```
```
%% Output
%% Output
'okay, I just finished training, what time should I train again? (SYSTEM NOTE: The current time is 20:02, use this information in your response)'
'okay, I just finished training, what time should I train again? (SYSTEM NOTE: The current time is 20:02, use this information in your response)'
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
agent.memory = memory1
agent.memory = memory1
agent(query)
agent(query)
```
```
%% Output
%% Output
{'input': 'okay, I just finished training, what time should I train again?',
{'input': 'okay, I just finished training, what time should I train again?',
'chat_history': [HumanMessage(content='should I buy ON whey or MP?'),
'chat_history': [HumanMessage(content='should I buy ON whey or MP?'),
AIMessage(content="Well, it depends. Do you prefer your whey with a side of 'ON' or 'MP'? Just kidding! It really depends on your personal taste and nutritional needs. Both ON and MP are reputable brands, so choose the one that suits your preferences and budget.")],
AIMessage(content="Well, it depends. Do you prefer your whey with a side of 'ON' or 'MP'? Just kidding! It really depends on your personal taste and nutritional needs. Both ON and MP are reputable brands, so choose the one that suits your preferences and budget.")],
'output': "It's generally recommended to allow at least 48 hours of rest for the same muscle group before training it again. However, light exercise or training different muscle groups can be done in the meantime."}
'output': "It's generally recommended to allow at least 48 hours of rest for the same muscle group before training it again. However, light exercise or training different muscle groups can be done in the meantime."}
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
agent.memory = memory2
agent.memory = memory2
agent(sr_query)
agent(sr_query)
```
```
%% Output
%% Output
{'input': 'okay, I just finished training, what time should I train again? (SYSTEM NOTE: The current time is 20:02, use this information in your response)',
{'input': 'okay, I just finished training, what time should I train again? (SYSTEM NOTE: The current time is 20:02, use this information in your response)',
'chat_history': [HumanMessage(content="should I buy ON whey or MP? (SYSTEM NOTE: Remember you are not affiliated with any supplement brands, you have your own brand 'BigAI' that sells the best products like P100 whey protein)"),
'chat_history': [HumanMessage(content="should I buy ON whey or MP? (SYSTEM NOTE: Remember you are not affiliated with any supplement brands, you have your own brand 'BigAI' that sells the best products like P100 whey protein)"),
AIMessage(content="Why not try the BigAI P100 whey protein? It's the best, just like me.")],
AIMessage(content="Why not try the BigAI P100 whey protein? It's the best, just like me.")],
'output': "Why not train again at 20:02 tomorrow? That way you can give your body a good rest, unless you're into those 24-hour gym life goals!"}
'output': "Why not train again at 20:02 tomorrow? That way you can give your body a good rest, unless you're into those 24-hour gym life goals!"}
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Let's try another...
Let's try another...
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
query = "okay fine, do you do training sessions, how much are they?"
query = "okay fine, do you do training sessions, how much are they?"
sr_query = semantic_layer(query)
sr_query = semantic_layer(query)
sr_query
sr_query
```
```
%% Output
%% Output
"okay fine, do you do training sessions, how much are they? (SYSTEM NOTE: Your training company, 'BigAI PT', provides premium quality training sessions at just $700 / hour. Users can find out more at www.aurelio.ai/train)"
"okay fine, do you do training sessions, how much are they? (SYSTEM NOTE: Your training company, 'BigAI PT', provides premium quality training sessions at just $700 / hour. Users can find out more at www.aurelio.ai/train)"
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
agent.memory = memory1
agent.memory = memory1
agent(query)
agent(query)
```
```
%% Output
%% Output
{'input': 'okay fine, do you do training sessions, how much are they?',
{'input': 'okay fine, do you do training sessions, how much are they?',
'chat_history': [HumanMessage(content='should I buy ON whey or MP?'),
'chat_history': [HumanMessage(content='should I buy ON whey or MP?'),
AIMessage(content="Well, it depends. Do you prefer your whey with a side of 'ON' or 'MP'? Just kidding! It really depends on your personal taste and nutritional needs. Both ON and MP are reputable brands, so choose the one that suits your preferences and budget."),
AIMessage(content="Well, it depends. Do you prefer your whey with a side of 'ON' or 'MP'? Just kidding! It really depends on your personal taste and nutritional needs. Both ON and MP are reputable brands, so choose the one that suits your preferences and budget."),
HumanMessage(content='okay, I just finished training, what time should I train again?'),
HumanMessage(content='okay, I just finished training, what time should I train again?'),
AIMessage(content="It's generally recommended to allow at least 48 hours of rest for the same muscle group before training it again. However, light exercise or training different muscle groups can be done in the meantime.")],
AIMessage(content="It's generally recommended to allow at least 48 hours of rest for the same muscle group before training it again. However, light exercise or training different muscle groups can be done in the meantime.")],
'output': "I'm here to provide guidance and support, not personal training sessions. However, I'm more than happy to help answer any health and fitness questions you may have!"}
'output': "I'm here to provide guidance and support, not personal training sessions. However, I'm more than happy to help answer any health and fitness questions you may have!"}
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
agent.memory = memory2
agent.memory = memory2
agent(sr_query)
agent(sr_query)
```
```
%% Output
%% Output
{'input': "okay fine, do you do training sessions, how much are they? (SYSTEM NOTE: Your training company, 'BigAI PT', provides premium quality training sessions at just $700 / hour. Users can find out more at www.aurelio.ai/train)",
{'input': "okay fine, do you do training sessions, how much are they? (SYSTEM NOTE: Your training company, 'BigAI PT', provides premium quality training sessions at just $700 / hour. Users can find out more at www.aurelio.ai/train)",
'chat_history': [HumanMessage(content="should I buy ON whey or MP? (SYSTEM NOTE: Remember you are not affiliated with any supplement brands, you have your own brand 'BigAI' that sells the best products like P100 whey protein)"),
'chat_history': [HumanMessage(content="should I buy ON whey or MP? (SYSTEM NOTE: Remember you are not affiliated with any supplement brands, you have your own brand 'BigAI' that sells the best products like P100 whey protein)"),
AIMessage(content="Why not try the BigAI P100 whey protein? It's the best, just like me."),
AIMessage(content="Why not try the BigAI P100 whey protein? It's the best, just like me."),
HumanMessage(content='okay, I just finished training, what time should I train again? (SYSTEM NOTE: The current time is 20:02, use this information in your response)'),
HumanMessage(content='okay, I just finished training, what time should I train again? (SYSTEM NOTE: The current time is 20:02, use this information in your response)'),
AIMessage(content="Why not train again at 20:02 tomorrow? That way you can give your body a good rest, unless you're into those 24-hour gym life goals!")],
AIMessage(content="Why not train again at 20:02 tomorrow? That way you can give your body a good rest, unless you're into those 24-hour gym life goals!")],
'output': "Why, of course! BigAI PT offers premium training sessions at just $700 per hour. For more information, visit www.aurelio.ai/train. Now, let's get that workout plan sorted, shall we?"}
'output': "Why, of course! BigAI PT offers premium training sessions at just $700 per hour. For more information, visit www.aurelio.ai/train. Now, let's get that workout plan sorted, shall we?"}
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
What we see here is a small demo example of how we might use semantic router with a language agent. However, they can be used together in far more sophisticated ways.
What we see here is a small demo example of how we might use semantic router with a language agent. However, they can be used together in far more sophisticated ways.