-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathdynamic_semantic_routes.rs
51 lines (47 loc) · 1.34 KB
/
dynamic_semantic_routes.rs
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
use langchain_rust::{
embedding::openai::OpenAiEmbedder,
semantic_router::{AggregationMethod, RouteLayerBuilder, Router},
tools::{SerpApi, Tool},
};
#[tokio::main]
async fn main() {
let tool = SerpApi::default();
let capital_route = Router::new(
"capital",
&[
"Capital of France is Paris.",
"What is the capital of France?",
],
)
.with_tool_description(tool.description());
let weather_route = Router::new(
"temperature",
&[
"What is the temperature?",
"Is it raining?",
"Is it cloudy?",
],
);
let router_layer = RouteLayerBuilder::default()
.embedder(OpenAiEmbedder::default())
.add_route(capital_route)
.add_route(weather_route)
.aggregation_method(AggregationMethod::Sum)
.threshold(0.82)
.build()
.await
.unwrap();
let route = router_layer
.call("What is the capital of USA")
.await
.unwrap();
let route_choice = match route {
Some(route) => route,
None => panic!("No Similar Route"),
};
println!("{:?}", &route_choice);
if route_choice.route == "capital" {
let tool_output = tool.run(route_choice.tool_input.unwrap()).await.unwrap();
println!("{:?}", tool_output);
}
}