-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Function Calling using the FCC's Broadband measuring report…
… data Introduces the chatOptionsBuilder parameter to the AiAgent.createPrompt(..) method.
- Loading branch information
1 parent
7a96233
commit 78bdddd
Showing
11 changed files
with
234 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/main/java/com/datastax/ai/agent/broadbandStats/AiAgentFccBroadbandDataTool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* See the NOTICE file distributed with this work for additional information | ||
* regarding copyright ownership. | ||
*/ | ||
package com.datastax.ai.agent.broadbandStats; | ||
|
||
import java.util.Map; | ||
|
||
import com.datastax.ai.agent.base.AiAgent; | ||
import com.datastax.ai.agent.base.AiAgentDelegator; | ||
|
||
|
||
import org.springframework.ai.chat.messages.UserMessage; | ||
import org.springframework.ai.chat.prompt.Prompt; | ||
import org.springframework.ai.openai.OpenAiChatOptions; | ||
|
||
|
||
/** | ||
* You will need to | ||
* `export TAVILY_API_KEY=…` | ||
* | ||
* Get a free API key at https://app.tavily.com/ | ||
*/ | ||
public class AiAgentFccBroadbandDataTool extends AiAgentDelegator<OpenAiChatOptions.Builder> { | ||
|
||
public static AiAgentFccBroadbandDataTool create(AiAgent agent) { | ||
return new AiAgentFccBroadbandDataTool(agent); | ||
} | ||
|
||
AiAgentFccBroadbandDataTool(AiAgent agent) { | ||
super(agent); | ||
} | ||
|
||
@Override | ||
public Prompt createPrompt( | ||
UserMessage message, | ||
Map<String,Object> promptProperties, | ||
OpenAiChatOptions.Builder chatOptionsBuilder) { | ||
|
||
return super.createPrompt( | ||
message, | ||
promptProperties(promptProperties), | ||
chatOptionsBuilder(chatOptionsBuilder)); | ||
} | ||
|
||
@Override | ||
public OpenAiChatOptions.Builder chatOptionsBuilder(OpenAiChatOptions.Builder chatOptionsBuilder) { | ||
return chatOptionsBuilder.withFunction("fccBroadbandDataService"); | ||
} | ||
|
||
|
||
} |
73 changes: 73 additions & 0 deletions
73
src/main/java/com/datastax/ai/agent/broadbandStats/FccBroadbandDataService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* See the NOTICE file distributed with this work for additional information | ||
* regarding copyright ownership. | ||
*/ | ||
package com.datastax.ai.agent.broadbandStats; | ||
|
||
import java.time.Instant; | ||
import java.util.function.Function; | ||
|
||
import com.datastax.ai.agent.broadbandStats.FccBroadbandDataService.Request; | ||
import com.datastax.ai.agent.broadbandStats.FccBroadbandDataService.Response; | ||
import com.datastax.oss.driver.api.core.CqlSession; | ||
import com.datastax.oss.driver.api.core.cql.Row; | ||
import com.datastax.oss.driver.api.core.cql.SimpleStatement; | ||
import com.datastax.oss.driver.api.querybuilder.QueryBuilder; | ||
|
||
final class FccBroadbandDataService implements Function<Request, Response> { | ||
|
||
public record TimeRange(Instant from, Instant to) {} | ||
|
||
public record Request(int device_id, TimeRange range) {} | ||
|
||
public record Response( | ||
long sk_tx_bytes, | ||
long sk_rx_bytes, | ||
long cust_wired_tx_bytes, | ||
long cust_wired_rx_bytes, | ||
long cust_wifi_tx_bytes, | ||
long cust_wifi_rx_bytes) {} | ||
|
||
private final CqlSession cqlSession; | ||
|
||
FccBroadbandDataService(CqlSession cqlSession) { | ||
this.cqlSession = cqlSession; | ||
} | ||
|
||
@Override | ||
public Response apply(Request t) { | ||
|
||
SimpleStatement stmt = QueryBuilder.selectFrom("datastax_ai_agent", "network_traffic").all() | ||
.whereColumn("unit_id").isEqualTo(QueryBuilder.literal(t.device_id)) | ||
.whereColumn("dtime").isGreaterThanOrEqualTo(QueryBuilder.literal(t.range.from)) | ||
.whereColumn("dtime").isLessThanOrEqualTo(QueryBuilder.literal(t.range.to)) | ||
.build(); | ||
|
||
|
||
Response sum = new Response(0, 0, 0, 0, 0, 0); | ||
for (Row r : cqlSession.execute(stmt)) { | ||
sum = new Response( | ||
sum.sk_tx_bytes + r.getLong("sk_tx_bytes"), | ||
sum.sk_rx_bytes + r.getLong("sk_rx_bytes"), | ||
sum.cust_wired_tx_bytes + r.getLong("cust_wired_tx_bytes"), | ||
sum.cust_wired_rx_bytes + r.getLong("cust_wired_rx_bytes"), | ||
sum.cust_wifi_tx_bytes + r.getLong("cust_wifi_tx_bytes"), | ||
sum.cust_wifi_rx_bytes + r.getLong("cust_wifi_rx_bytes") | ||
); | ||
} | ||
return sum; | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/datastax/ai/agent/broadbandStats/FccBroadbandDataTool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* See the NOTICE file distributed with this work for additional information | ||
* regarding copyright ownership. | ||
*/ | ||
package com.datastax.ai.agent.broadbandStats; | ||
|
||
import java.util.function.Function; | ||
|
||
import com.datastax.ai.agent.broadbandStats.FccBroadbandDataService.Request; | ||
import com.datastax.ai.agent.broadbandStats.FccBroadbandDataService.Response; | ||
import com.datastax.oss.driver.api.core.CqlSession; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Description; | ||
|
||
@Configuration | ||
public class FccBroadbandDataTool { | ||
|
||
@Bean | ||
@Description("Get device data usage over a period of time") | ||
public Function<Request, Response> fccBroadbandDataService(CqlSession cqlSession) { | ||
return new FccBroadbandDataService(cqlSession); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.