Skip to content

Commit

Permalink
Add multi_agent example
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan committed Sep 2, 2024
1 parent 437d703 commit 8e7ab02
Show file tree
Hide file tree
Showing 6 changed files with 296 additions and 164 deletions.
54 changes: 54 additions & 0 deletions example/listener.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import 'dart:convert';
import 'package:lite_agent_core_dart/lite_agent_core.dart';
import 'package:opentool_dart/opentool_dart.dart';

void listen(AgentMessageDto agentMessageDto) {
String system = "🖥SYSTEM";
String user = "👤USER";
String agent = "🤖AGENT";
String llm = "💡LLM";
String tool = "🔧TOOL";
String client = "🔗CLIENT";

String message = "";
if (agentMessageDto.type == ToolMessageType.TEXT)
message = agentMessageDto.message as String;
if (agentMessageDto.type == ToolMessageType.IMAGE_URL)
message = agentMessageDto.message as String;
if (agentMessageDto.type == ToolMessageType.FUNCTION_CALL_LIST) {
List<dynamic> originalFunctionCallList =
agentMessageDto.message as List<dynamic>;
List<FunctionCall> functionCallList =
originalFunctionCallList.map((dynamic json) {
return FunctionCall.fromJson(json);
}).toList();
message = jsonEncode(functionCallList);
}
if (agentMessageDto.type == ToolMessageType.TOOL_RETURN) {
message = jsonEncode(ToolReturn.fromJson(agentMessageDto.message));
}
;

String from = "";
if (agentMessageDto.from == ToolRoleType.SYSTEM) {
from = system;
message = "\n$message";
}
if (agentMessageDto.from == ToolRoleType.USER) from = user;
if (agentMessageDto.from == ToolRoleType.AGENT) from = agent;
if (agentMessageDto.from == ToolRoleType.LLM) from = llm;
if (agentMessageDto.from == ToolRoleType.TOOL) from = tool;
if (agentMessageDto.from == ToolRoleType.CLIENT) from = client;

String to = "";
if (agentMessageDto.to == ToolRoleType.SYSTEM) to = system;
if (agentMessageDto.to == ToolRoleType.USER) to = user;
if (agentMessageDto.to == ToolRoleType.AGENT) to = agent;
if (agentMessageDto.to == ToolRoleType.LLM) to = llm;
if (agentMessageDto.to == ToolRoleType.TOOL) to = tool;
if (agentMessageDto.to == ToolRoleType.CLIENT) to = client;

if (from.isNotEmpty && to.isNotEmpty) {
print("#${agentMessageDto.sessionId}:${agentMessageDto.taskId}# $from -> $to: [${agentMessageDto.type}] $message");
}
}
69 changes: 8 additions & 61 deletions example/mock/mock_client_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:dio/dio.dart';
import 'package:lite_agent_core_dart/lite_agent_core.dart';
import 'package:dotenv/dotenv.dart';
import 'package:lite_agent_core_dart_server/src/config.dart';
import 'package:opentool_dart/opentool_dart.dart';
import '../listener.dart';

String prompt = "List count of the storage";

Expand All @@ -29,7 +29,7 @@ Future<void> main() async {

if (sessionDto != null) {
print("[sessionDto] " + sessionDto.toJson().toString());
WebSocket webSocket = await connectChat(sessionDto.id, (agentMessageDto) => printAgentMessage(agentMessageDto));
WebSocket webSocket = await connectChat(sessionDto.id, (agentMessageDto) => listen(agentMessageDto));

print("[webSocket] " + webSocket.toString());

Expand Down Expand Up @@ -75,9 +75,8 @@ Future<SessionDto?> initChat(CapabilityDto capabilityDto) async {
}

Future<WebSocket> connectChat(
String sessionId, onReceive(AgentMessageDto)) async {
final String url =
'ws://127.0.0.1:${config.server.port}${config.server.apiPathPrefix}/chat?id=$sessionId';
String sessionId, onReceive(AgentMessageDto)) async {
final String url = 'ws://127.0.0.1:${config.server.port}${config.server.apiPathPrefix}/chat?id=$sessionId';

final WebSocket socket = await WebSocket.connect(
url,
Expand Down Expand Up @@ -109,7 +108,7 @@ Future<WebSocket> connectChat(

Future<void> sendUserMessage(WebSocket socket, String prompt) async {
UserMessageDto userMessageDto = UserMessageDto(type: UserMessageDtoType.text, message: prompt);
UserTaskDto userTaskDto = UserTaskDto(taskId: "0", contentList: [userMessageDto]);
UserTaskDto userTaskDto = UserTaskDto(contentList: [userMessageDto]);
socket.add(jsonEncode(userTaskDto.toJson()));
}

Expand Down Expand Up @@ -146,58 +145,6 @@ Future<SessionDto?> clearChat(String sessionId, WebSocket socket) async {
return null;
}

void printAgentMessage(AgentMessageDto agentMessageDto) {
String system = "🖥SYSTEM";
String user = "👤USER";
String agent = "🤖AGENT";
String llm = "💡LLM";
String tool = "🔧TOOL";
String client = "🔗CLIENT";

String message = "";
if (agentMessageDto.type == ToolMessageType.TEXT)
message = agentMessageDto.message as String;
if (agentMessageDto.type == ToolMessageType.IMAGE_URL)
message = agentMessageDto.message as String;
if (agentMessageDto.type == ToolMessageType.FUNCTION_CALL_LIST) {
List<dynamic> originalFunctionCallList =
agentMessageDto.message as List<dynamic>;
List<FunctionCall> functionCallList =
originalFunctionCallList.map((dynamic json) {
return FunctionCall.fromJson(json);
}).toList();
message = jsonEncode(functionCallList);
}
if (agentMessageDto.type == ToolMessageType.TOOL_RETURN) {
message = jsonEncode(ToolReturn.fromJson(agentMessageDto.message));
}
;

String from = "";
if (agentMessageDto.from == ToolRoleType.SYSTEM) {
from = system;
message = "\n$message";
}
if (agentMessageDto.from == ToolRoleType.USER) from = user;
if (agentMessageDto.from == ToolRoleType.AGENT) from = agent;
if (agentMessageDto.from == ToolRoleType.LLM) from = llm;
if (agentMessageDto.from == ToolRoleType.TOOL) from = tool;
if (agentMessageDto.from == ToolRoleType.CLIENT) from = client;

String to = "";
if (agentMessageDto.to == ToolRoleType.SYSTEM) to = system;
if (agentMessageDto.to == ToolRoleType.USER) to = user;
if (agentMessageDto.to == ToolRoleType.AGENT) to = agent;
if (agentMessageDto.to == ToolRoleType.LLM) to = llm;
if (agentMessageDto.to == ToolRoleType.TOOL) to = tool;
if (agentMessageDto.to == ToolRoleType.CLIENT) to = client;

if (from.isNotEmpty && to.isNotEmpty) {
print(
"#${agentMessageDto.sessionId}# $from -> $to: [${agentMessageDto.type}] $message");
}
}

LLMConfigDto _buildLLMConfigDto() {
DotEnv env = DotEnv();
env.load(['example/.env']);
Expand All @@ -217,15 +164,15 @@ Future<List<OpenSpecDto>> _buildOpenSpecList() async {
"mock_openapi.json"
];

List<OpenSpecDto> OpenSpecDtoList = [];
List<OpenSpecDto> openSpecDtoList = [];
for (String openAPIFileName in openAPIFileNameList) {
String jsonPath = "$openAPIFolder${Platform.pathSeparator}$openAPIFileName";
File file = File(jsonPath);
String jsonString = await file.readAsString();
OpenSpecDto openSpecDto =
OpenSpecDto(openSpec: jsonString, protocol: Protocol.OPENAPI);
OpenSpecDtoList.add(openSpecDto);
openSpecDtoList.add(openSpecDto);
}

return OpenSpecDtoList;
return openSpecDtoList;
}
65 changes: 29 additions & 36 deletions example/mock/server/mock_openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,15 @@
"/create": {
"post": {
"summary": "Add a new text to storage",
"parameters": [
{
"name": "text",
"in": "path",
"required": true,
"description": "Text should be added",
"schema": {
"$ref": "#/components/schemas/Text"
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Text"
}
}
}
],
},
"responses": {
"200": {
"description": "Add successfully",
Expand All @@ -68,17 +66,15 @@
"/read": {
"post": {
"summary": "Read text by Id",
"parameters": [
{
"name": "Id",
"in": "path",
"required": true,
"description": "Read text in storage by Id",
"schema": {
"$ref": "#/components/schemas/Id"
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Id"
}
}
}
],
},
"responses": {
"200": {
"description": "Text in storage",
Expand All @@ -96,17 +92,15 @@
"/update": {
"post": {
"summary": "Update text in storage by Id",
"parameters": [
{
"name": "update",
"in": "path",
"required": true,
"description": "The update info include text and Id",
"schema": {
"$ref": "#/components/schemas/Update"
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Update"
}
}
}
],
},
"responses": {
"200": {
"description": "Update result",
Expand All @@ -124,17 +118,15 @@
"/delete": {
"post": {
"summary": "Delete text in storage by Id",
"parameters": [
{
"name": "Id",
"in": "path",
"required": true,
"description": "The Id of the text be deleted",
"schema": {
"$ref": "#/components/schemas/Id"
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Id"
}
}
}
],
},
"responses": {
"200": {
"description": "Delete result",
Expand Down Expand Up @@ -166,6 +158,7 @@
},
"Id": {
"type": "object",
"description": "Text in storage by Id",
"required": [
"id"
],
Expand Down
Loading

0 comments on commit 8e7ab02

Please sign in to comment.