Flutter轻量级代理核心插件lite_agent_core_dart的使用
Flutter轻量级代理核心插件lite_agent_core_dart的使用
LiteAgent核心插件dart版
特性
- 支持纯文本代理,无需JSON Spec。
- 支持OpenAPI/OpenRPC/OpenModbus/OpenTool JSON Spec。
- 支持LLM功能调用至HTTP API/json-rpc 2.0 over HTTP/Modbus以及其他自定义工具。
使用方法
准备工作
- 准备一些可调用的OpenSpec json文件,根据
/example/json/open*/*.json
路径。 - 运行你的工具服务器,该服务器在json文件中描述。
- 在
example
文件夹下添加.env
文件,并在.env
文件中添加以下内容:
baseUrl = https://xxx.xxx.com # LLM API BaseURL
apiKey = sk-xxxxxxxxxxxxxxxxxxxx # LLM API ApiKey
- 使用以下方法运行代理服务。
方法1(推荐):AgentService
- 根据
/example/service_example
目录。 - 支持通过会话ID进行多代理会话。
- 支持在同一代理中处理多个任务,通过
taskId
识别不同的任务。完成任务后,任务消息可以作为新的任务上下文添加到会话中。
Future<void> main() async {
CapabilityDto capabilityDto = CapabilityDto(
llmConfig: _buildLLMConfig(), // LLM配置
systemPrompt: _buildSystemPrompt(), // 系统提示
openSpecList: await _buildOpenSpecList() // 可用的OpenSpec描述字符串列表
);
SessionDto sessionDto = await agentService.initChat(
capabilityDto,
listen // 订阅代理消息,代理与用户/客户端/LLM/工具角色对话
); // 获取会话ID
String prompt = "<USER PROMPT, e.g. call any one tool>";
UserTaskDto userTaskDto = UserTaskDto(taskId: "<Identify different tasks, NOT more than 36 chars>", contentList: [UserMessageDto(type: UserMessageDtoType.text, message: prompt)]); // 用户内容列表,支持文本/图片URL类型
await agentService.startChat(sessionDto.id, userTaskDto);
}
- 多代理支持
Future<void> main() async {
SessionDto sessionDto1 = await _buildTextAgent();
SessionDto sessionDto2 = await _buildToolAgent();
CapabilityDto capabilityDto = CapabilityDto(llmConfig: llmConfig, systemPrompt: systemPrompt,
sessionList: [sessionDto1, sessionDto2]
);
SessionDto sessionDto = await agentService.initChat(capabilityDto, listen);
UserTaskDto userTaskDto = UserTaskDto(contentList: [UserMessageDto(type: UserMessageDtoType.text, message: prompt)]);
await agentService.startChat(sessionDto.id, userTaskDto);
}
- 反射支持
Future<void> main() async {
CapabilityDto capabilityDto = CapabilityDto(
llmConfig: _buildLLMConfig(),
systemPrompt: _buildSystemPrompt(),
openSpecList: await _buildOpenSpecList(),
/// 添加反射提示列表
toolReflectionList: await _buildToolReflectionList()
);
SessionDto sessionDto = await agentService.initChat(capabilityDto, listen);
String prompt = "<USER PROMPT, e.g. call any one tool>";
UserTaskDto userTaskDto = UserTaskDto(taskId: "<Identify different tasks, NOT more than 36 chars>", contentList: [UserMessageDto(type: UserMessageDtoType.text, message: prompt)]);
await agentService.startChat(sessionDto.id, userTaskDto);
}
方法2:ToolAgent
- 根据
/example/agent_example
目录。 - 纯原生调用,支持单一会话。
- 方法1
AgentService
是对此的友好封装。
Future<void> main() async {
ToolAgent toolAgent = ToolAgent(
llmRunner: _buildLLMRunner(),
session: _buildSession(),
toolRunnerList: await _buildToolRunnerList(),
systemPrompt: _buildSystemPrompt()
);
String prompt = "<USER PROMPT, e.g. call any one tool>";
toolAgent.userToAgent(taskId: "<Identify different tasks, NOT more than 36 chars>", [Content(type: ContentType.text, message: prompt)]);
}
更多关于Flutter轻量级代理核心插件lite_agent_core_dart的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter轻量级代理核心插件lite_agent_core_dart的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是一个关于如何使用Flutter轻量级代理核心插件lite_agent_core_dart
的代码示例。这个示例将展示如何初始化插件、设置代理,并通过代理进行HTTP请求。
首先,确保你已经在pubspec.yaml
文件中添加了lite_agent_core_dart
依赖:
dependencies:
flutter:
sdk: flutter
lite_agent_core_dart: ^最新版本号 # 请替换为实际的最新版本号
然后,运行flutter pub get
来安装依赖。
接下来,编写Flutter代码来使用这个插件:
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:lite_agent_core_dart/lite_agent_core_dart.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Lite Agent Core Dart Example'),
),
body: Center(
child: ProxyExample(),
),
),
);
}
}
class ProxyExample extends StatefulWidget {
@override
_ProxyExampleState createState() => _ProxyExampleState();
}
class _ProxyExampleState extends State<ProxyExample> {
String responseText = "";
@override
void initState() {
super.initState();
_setupProxy();
}
Future<void> _setupProxy() async {
// 初始化LiteAgent
LiteAgent agent = LiteAgent();
// 设置代理配置(例如:HTTP代理)
ProxyConfig proxyConfig = ProxyConfig(
type: ProxyType.http,
host: 'your-proxy-host',
port: 8080,
username: 'your-username', // 如果需要认证,请提供用户名
password: 'your-password', // 如果需要认证,请提供密码
);
// 应用代理配置
await agent.setup(proxyConfig);
// 使用代理进行HTTP请求
_makeRequest();
}
Future<void> _makeRequest() async {
try {
// 使用dio库进行HTTP请求
Response response = await Dio().get('https://www.example.com');
setState(() {
responseText = response.data.toString();
});
} catch (error) {
setState(() {
responseText = 'Error: ${error.toString()}';
});
}
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Proxy Response:'),
Text(responseText),
],
);
}
}
注意事项:
-
代理服务器信息:替换
your-proxy-host
、your-username
和your-password
为实际的代理服务器信息。 -
依赖库:上面的示例使用了
dio
库来进行HTTP请求,你需要在pubspec.yaml
中添加dio
依赖:dependencies: dio: ^最新版本号 # 请替换为实际的最新版本号
-
错误处理:在实际应用中,你可能需要更详细的错误处理逻辑。
-
权限:在某些平台上(如Android和iOS),你可能需要配置网络权限或使用特定的API来允许应用通过代理进行网络通信。
这个示例展示了如何使用lite_agent_core_dart
插件来设置代理,并通过代理发送HTTP请求。希望这对你有所帮助!