[](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:
# Intro to LangChain Agents with Semantic Router
%% Cell type:markdown id: tags:
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 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)_.
* 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.
%% Cell type:markdown id: tags:
## Install Prerequisites
%% Cell type:code id: tags:
```
!pip install -qU \
semantic-router=20 \
semantic-router==0.0.20 \
langchain==0.0.352 \
openai==1.6.1
openai>=1.6.1
```
%% Output
[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 [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 [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 [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 [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 [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 [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.
%% Cell type:code id: tags:
```
from datetime import datetime
def get_time():
now = datetime.now()
return (
f"The current time is {now.strftime('%H:%M')}, use "
"this information in your response"
)
def supplement_brand():
return (
"Remember you are not affiliated with any supplement "
"brands, you have your own brand 'BigAI' that sells "
"the best products like P100 whey protein"
)
def business_inquiry():
return (
"Your training company, 'BigAI PT', provides premium "
"quality training sessions at just $700 / hour. "
"Users can find out more at www.aurelio.ai/train"
)
def product():
return (
"Remember, users can sign up for a fitness programme "
"at www.aurelio.ai/sign-up"
)
```
%% Cell type:markdown id: tags:
Now we just add some logic to call this functions when we see a particular route being chosen.
%% Cell type:code id: tags:
```
def semantic_layer(query: str):
route = rl(query)
if route.name == "get_time":
query += f" (SYSTEM NOTE: {get_time()})"
elif route.name == "supplement_brand":
query += f" (SYSTEM NOTE: {supplement_brand()})"
elif route.name == "business_inquiry":
query += f" (SYSTEM NOTE: {business_inquiry()})"
elif route.name == "product":
query += f" (SYSTEM NOTE: {product()})"
else:
pass
return query
```
%% Cell type:code id: tags:
```
query = "should I buy ON whey or MP?"
sr_query = semantic_layer(query)
sr_query
```
%% 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)"
%% Cell type:markdown id: tags:
## Using an Agent with a Router Layer
%% Cell type:markdown id: tags:
Initialize a conversational LangChain agent.
%% Cell type:code id: tags:
```
from langchain.agents import AgentType, initialize_agent
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferWindowMemory
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:
```
agent(query)
```
%% Output
{'input': 'should I buy ON whey or MP?',
'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."}
%% Cell type:code id: tags:
```
# swap agent memory first
agent.memory = memory2
agent(sr_query)
```
%% 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)",
'chat_history': [],
'output': "Why not try the BigAI P100 whey protein? It's the best, just like me."}
%% 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.
Let's try some more!
%% Cell type:code id: tags:
```
query = "okay, I just finished training, what time should I train again?"
sr_query = semantic_layer(query)
sr_query
```
%% 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)'
%% Cell type:code id: tags:
```
agent.memory = memory1
agent(query)
```
%% Output
{'input': 'okay, I just finished training, what time should I train again?',
'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.")],
'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:
```
agent.memory = memory2
agent(sr_query)
```
%% 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)',
'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.")],
'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:
Let's try another...
%% Cell type:code id: tags:
```
query = "okay fine, do you do training sessions, how much are they?"
sr_query = semantic_layer(query)
sr_query
```
%% 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)"
%% Cell type:code id: tags:
```
agent.memory = memory1
agent(query)
```
%% Output
{'input': 'okay fine, do you do training sessions, how much are they?',
'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."),
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.")],
'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:
```
agent.memory = memory2
agent(sr_query)
```
%% 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)",
'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."),
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!")],
'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:
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.