Skip to content

Commit

Permalink
Merge pull request #31 from guchengxi1994/master
Browse files Browse the repository at this point in the history
llm template
  • Loading branch information
guchengxi1994 authored May 12, 2024
2 parents b2261be + 36af52b commit 0604030
Show file tree
Hide file tree
Showing 29 changed files with 3,681 additions and 205 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### how to run

* create a `.env` file in root folder with configs like
* create an `env` file in root folder with configs like
```
LLM_BASE = LLM API
LLM_MODEL_NAME = model name
Expand All @@ -26,4 +26,14 @@

![image-20240504112102907](./images/image-20240504112102907.png)

* ...
* generate document from `template`(WIP)

| define template | define relation |
| ------------------------------------------------------ | ------------------------------------------------------ |
| ![image-20240511-215317](./images/20240511-215317.jpg) | ![image-20240511-215326](./images/20240511-215326.jpg) |
| generate | optimize |
| ![image-20240511-215330](./images/20240511-215330.jpg) | ![image-20240511-215333](./images/20240511-215333.jpg) |



* ...
3 changes: 3 additions & 0 deletions images/20240511-215317.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions images/20240511-215326.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions images/20240511-215330.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions images/20240511-215333.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 45 additions & 41 deletions lib/llm/langchain/components/tools_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,56 @@ class ToolsItem extends ConsumerStatefulWidget {
class _ToolsItemState extends ConsumerState<ToolsItem> {
@override
Widget build(BuildContext context) {
return InkWell(
return GestureDetector(
onTap: () {
ref.read(toolProvider.notifier).changeState(widget.toolModel);
ref.read(toolProvider.notifier).jumpTo(1);
},
child: FittedBox(
child: Stack(
children: [
const SizedBox(
height: 70,
width: 200,
),
Positioned(
bottom: 0,
child: Container(
width: 200,
height: 50,
padding: const EdgeInsets.only(left: 30),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(4),
color: Colors.white,
border: Border.all(color: AppStyle.appColor),
boxShadow: const [
BoxShadow(
color: AppStyle.appColor,
offset: Offset(0, 4),
blurRadius: 10,
spreadRadius: 3,
)
]),
child: Center(
child: Text(
widget.toolModel.name,
style: const TextStyle(fontFamily: "xing", fontSize: 20),
child: MouseRegion(
cursor: SystemMouseCursors.click,
child: FittedBox(
child: Stack(
children: [
const SizedBox(
height: 70,
width: 200,
),
Positioned(
bottom: 0,
child: Container(
width: 200,
height: 50,
padding: const EdgeInsets.only(left: 30),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(4),
color: Colors.white,
border: Border.all(color: AppStyle.appColor),
boxShadow: const [
BoxShadow(
color: AppStyle.appColor,
offset: Offset(0, 4),
blurRadius: 10,
spreadRadius: 3,
)
]),
child: Center(
child: Text(
widget.toolModel.name,
style:
const TextStyle(fontFamily: "xing", fontSize: 20),
),
),
),
)),
Positioned(
top: 0,
left: 10,
child: SizedBox(
width: 50,
height: 50,
child: Image.asset(widget.toolModel.imgPath),
)),
],
)),
Positioned(
top: 0,
left: 10,
child: SizedBox(
width: 50,
height: 50,
child: Image.asset(widget.toolModel.imgPath),
)),
],
),
),
),
);
Expand Down
163 changes: 163 additions & 0 deletions lib/llm/template_editor/components/chain_flow.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// ignore_for_file: avoid_init_to_null

import 'dart:ui';

import 'package:all_in_one/llm/template_editor/components/connector_painter.dart';
import 'package:all_in_one/llm/template_editor/notifiers/chain_flow_notifier.dart';
import 'package:all_in_one/llm/template_editor/notifiers/chain_flow_state.dart';
import 'package:all_in_one/src/rust/api/llm_api.dart';
import 'package:all_in_one/styles/app_style.dart';
import 'package:auto_size_text/auto_size_text.dart';
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class ChainFlow extends ConsumerStatefulWidget {
const ChainFlow({super.key});

@override
ConsumerState<ChainFlow> createState() => _ChainFlowState();
}

class _ChainFlowState extends ConsumerState<ChainFlow> {
late final ScrollController controller1 = ScrollController()
..addListener(_scrollListener);
final ScrollController controller2 = ScrollController();

late double childrenHeight = 0;
List<String> items = [];

init() async {
items =
await templateToPrompts(template: ref.read(chainFlowProvider).content);
childrenHeight = items.length * 50;
}

int? first = null;
String? firstStr = null;

void _scrollListener() {
if (!controller2.hasClients) {
return;
}
controller2.jumpTo(controller1.offset);
}

@override
Widget build(BuildContext context) {
return FutureBuilder(
future: init(),
builder: (c, s) {
if (s.connectionState == ConnectionState.done) {
return Container(
width: 400,
height: double.infinity,
color: Colors.white,
padding: const EdgeInsets.all(10),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
width: 100,
child: _wrapper2(
SingleChildScrollView(
controller: controller2,
child: CustomPaint(
painter: ConnectorPainter(
bounds: ref
.watch(chainFlowProvider)
.items
.map((e) => e.ids
.map((ei) => Offset(100, ei * 50 + 25))
.toList())
.toList(),
),
size: Size(100, childrenHeight),
),
),
context)),
const SizedBox(
width: 10,
),
Expanded(
child: _wrapper(
SingleChildScrollView(
controller: controller1,
child: SizedBox(
height: childrenHeight,
child: Column(
children: items
.mapIndexed((i, e) => InkWell(
onTap: () {
if (first == null) {
first = i;
firstStr = e;
} else {
ref
.read(chainFlowProvider
.notifier)
.addItem(ChainFlowItem(
ids: [first!, i],
contents: [firstStr!, e],
));

first = null;
firstStr = null;
}
},
child: _itemBuilder(e),
))
.toList(),
),
),
),
context)),
],
),
);
}
return const Center(
child: CircularProgressIndicator(),
);
});
}

Widget _itemBuilder(String s) {
return Container(
height: 40,
margin: const EdgeInsets.all(5),
padding: const EdgeInsets.all(5),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(4),
color: AppStyle.appColorLight),
child: Align(
alignment: Alignment.centerLeft,
child: AutoSizeText(
s,
maxLines: 1,
style: const TextStyle(color: AppStyle.white),
),
),
);
}

Widget _wrapper(Widget c, BuildContext context) {
return ScrollConfiguration(
behavior: ScrollConfiguration.of(context)
.copyWith(scrollbars: false, dragDevices: {
//必须设置此事件,不然无法滚动
PointerDeviceKind.touch,
PointerDeviceKind.mouse,
}),
child: c, //嵌套你的SingleChildScrollView组件
);
}

Widget _wrapper2(Widget c, BuildContext context) {
return ScrollConfiguration(
behavior: ScrollConfiguration.of(context)
.copyWith(scrollbars: false, dragDevices: {}),
child: c, //嵌套你的SingleChildScrollView组件
);
}
}
49 changes: 49 additions & 0 deletions lib/llm/template_editor/components/connector_painter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'package:flutter/material.dart';

class ConnectorPainter extends CustomPainter {
final List<List<Offset>> bounds;

ConnectorPainter({
required this.bounds,
});

@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = Colors.blue // 线条颜色
..strokeWidth = 2; // 线条宽度

for (var i = 0; i < bounds.length; i++) {
for (var j = 0; j < bounds[i].length - 1; j++) {
final startPoint = bounds[i][j];
final endPoint = bounds[i][j + 1];

Path path = Path()
..moveTo(startPoint.dx, startPoint.dy)
..lineTo(startPoint.dx - 10, startPoint.dy - 10)
..lineTo(startPoint.dx - 10, startPoint.dy + 10)
..close();

Path path2 = Path()
..moveTo(endPoint.dx, endPoint.dy)
..lineTo(endPoint.dx - 10, endPoint.dy - 10)
..lineTo(endPoint.dx - 10, endPoint.dy + 10)
..close();

final left1 = Offset(startPoint.dx / 2, startPoint.dy);
final left2 = Offset(endPoint.dx / 2, endPoint.dy);

canvas.drawPath(path, paint);
canvas.drawLine(startPoint, left1, paint);
canvas.drawLine(left1, left2, paint);
canvas.drawLine(left2, endPoint, paint);
canvas.drawPath(path2, paint);
}
}
}

@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return true;
}
}
Loading

0 comments on commit 0604030

Please sign in to comment.