From 362040f41f717bdfb927071397ed7e0856782987 Mon Sep 17 00:00:00 2001 From: xiaoshuyui <528490652@qq.com> Date: Wed, 1 May 2024 19:18:24 +0800 Subject: [PATCH] update prompt fix render issue --- assets/llm.json | 4 +- assets/llm/chat.png | 3 + assets/llm/engineer.png | 3 + lib/layout/components/animated_sidebar.dart | 4 +- lib/llm/chatchat/components/latex.dart | 78 +++++++++++++++++++ .../chatchat/models/response_message_box.dart | 20 ++++- .../langchain/components/tools_screen.dart | 3 + pubspec.lock | 16 ++++ pubspec.yaml | 1 + 9 files changed, 126 insertions(+), 6 deletions(-) create mode 100644 assets/llm/chat.png create mode 100644 assets/llm/engineer.png create mode 100644 lib/llm/chatchat/components/latex.dart diff --git a/assets/llm.json b/assets/llm.json index 9db490c..562098a 100644 --- a/assets/llm.json +++ b/assets/llm.json @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:42243e6f9a83b70f113a62d9aaa7526a54154a0ef79fc952eba20a70ce39fd71 -size 205 +oid sha256:0a4d67a3a300aa2cfda52e61619405ab600e7cf09df9efdbde776da5ae0b33ad +size 636 diff --git a/assets/llm/chat.png b/assets/llm/chat.png new file mode 100644 index 0000000..3d8e9c8 --- /dev/null +++ b/assets/llm/chat.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:816a8685ff247ca55cd8bb0087f225cce477a41c27cf52c20e042b2e9147cdaf +size 1123 diff --git a/assets/llm/engineer.png b/assets/llm/engineer.png new file mode 100644 index 0000000..40c6cc8 --- /dev/null +++ b/assets/llm/engineer.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:658098dd2669caaff4e3a385371a885187a39c16a52b72cd626ab1c30c58312d +size 159749 diff --git a/lib/layout/components/animated_sidebar.dart b/lib/layout/components/animated_sidebar.dart index 0e0c312..709fbfa 100644 --- a/lib/layout/components/animated_sidebar.dart +++ b/lib/layout/components/animated_sidebar.dart @@ -34,9 +34,7 @@ class _AnimatedSidebarState extends ConsumerState }); if (width == LayoutStyle.sidebarExpand) { ref.read(sidebarProvider.notifier).changeStatus(true); - } - - if (width == LayoutStyle.sidebarCollapse) { + } else { ref.read(sidebarProvider.notifier).changeStatus(false); } }); diff --git a/lib/llm/chatchat/components/latex.dart b/lib/llm/chatchat/components/latex.dart new file mode 100644 index 0000000..7a5ef38 --- /dev/null +++ b/lib/llm/chatchat/components/latex.dart @@ -0,0 +1,78 @@ +import 'package:flutter/material.dart'; +import 'package:markdown_widget/markdown_widget.dart'; +// ignore: depend_on_referenced_packages +import 'package:markdown/markdown.dart' as m; +import 'package:flutter_math_fork/flutter_math.dart'; + +SpanNodeGeneratorWithTag latexGenerator = SpanNodeGeneratorWithTag( + tag: _latexTag, + generator: (e, config, visitor) => + LatexNode(e.attributes, e.textContent, config)); + +const _latexTag = 'latex'; + +class LatexSyntax extends m.InlineSyntax { + LatexSyntax() : super(r'(\$\$[\s\S]+\$\$)|(\$.+?\$)'); + + @override + bool onMatch(m.InlineParser parser, Match match) { + final input = match.input; + final matchValue = input.substring(match.start, match.end); + String content = ''; + bool isInline = true; + const blockSyntax = '\$\$'; + const inlineSyntax = '\$'; + if (matchValue.startsWith(blockSyntax) && + matchValue.endsWith(blockSyntax) && + (matchValue != blockSyntax)) { + content = matchValue.substring(2, matchValue.length - 2); + isInline = false; + } else if (matchValue.startsWith(inlineSyntax) && + matchValue.endsWith(inlineSyntax) && + matchValue != inlineSyntax) { + content = matchValue.substring(1, matchValue.length - 1); + } + m.Element el = m.Element.text(_latexTag, matchValue); + el.attributes['content'] = content; + el.attributes['isInline'] = '$isInline'; + parser.addNode(el); + return true; + } +} + +class LatexNode extends SpanNode { + final Map attributes; + final String textContent; + final MarkdownConfig config; + + LatexNode(this.attributes, this.textContent, this.config); + + @override + InlineSpan build() { + final content = attributes['content'] ?? ''; + final isInline = attributes['isInline'] == 'true'; + final style = parentStyle ?? config.p.textStyle; + if (content.isEmpty) return TextSpan(style: style, text: textContent); + final latex = Math.tex( + content, + mathStyle: MathStyle.text, + textStyle: style.copyWith(color: Colors.black), + textScaleFactor: 1, + onErrorFallback: (error) { + return Text( + textContent, + style: style.copyWith(color: Colors.red), + ); + }, + ); + return WidgetSpan( + alignment: PlaceholderAlignment.middle, + child: !isInline + ? Container( + width: double.infinity, + margin: const EdgeInsets.symmetric(vertical: 16), + child: Center(child: latex), + ) + : latex); + } +} diff --git a/lib/llm/chatchat/models/response_message_box.dart b/lib/llm/chatchat/models/response_message_box.dart index cc5cb70..acd2e7f 100644 --- a/lib/llm/chatchat/models/response_message_box.dart +++ b/lib/llm/chatchat/models/response_message_box.dart @@ -2,6 +2,7 @@ import 'package:all_in_one/common/color_utils.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; // import 'package:flutter_markdown/flutter_markdown.dart'; +import '../components/latex.dart'; import 'message_box.dart'; import 'package:markdown_widget/markdown_widget.dart'; @@ -34,7 +35,24 @@ class ResponseMessageBox extends MessageBox { children: [ Align( alignment: Alignment.topLeft, - child: MarkdownBlock(data: content), + child: MarkdownBlock( + data: content, + generator: MarkdownGenerator( + generators: [latexGenerator], + inlineSyntaxList: [LatexSyntax()], + richTextBuilder: (span) => + Text.rich(span, textScaler: const TextScaler.linear(1)), + ), + ), + // child: MarkdownWidget( + // data: content, + // markdownGenerator: MarkdownGenerator( + // generators: [latexGenerator], + // inlineSyntaxList: [LatexSyntax()], + // richTextBuilder: (span) => + // Text.rich(span, textScaler: const TextScaler.linear(1)), + // ), + // ), ), Material( color: Colors.transparent, diff --git a/lib/llm/langchain/components/tools_screen.dart b/lib/llm/langchain/components/tools_screen.dart index 3a73c8f..1f64274 100644 --- a/lib/llm/langchain/components/tools_screen.dart +++ b/lib/llm/langchain/components/tools_screen.dart @@ -65,6 +65,9 @@ class _ToolsScreenState extends State { )) ], ), + const SizedBox( + height: 10, + ), Expanded( child: Align( alignment: Alignment.topLeft, diff --git a/pubspec.lock b/pubspec.lock index 65fa4b4..eb945be 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -460,6 +460,14 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "3.0.2" + flutter_math_fork: + dependency: "direct main" + description: + name: flutter_math_fork + sha256: "94bee4642892a94939af0748c6a7de0ff8318feee588379dcdfea7dc5cba06c8" + url: "https://pub.flutter-io.cn" + source: hosted + version: "0.7.2" flutter_riverpod: dependency: "direct main" description: @@ -1149,6 +1157,14 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "1.2.1" + tuple: + dependency: transitive + description: + name: tuple + sha256: a97ce2013f240b2f3807bcbaf218765b6f301c3eff91092bcfa23a039e7dd151 + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.0.2" typed_data: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 310af25..57dff67 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -27,6 +27,7 @@ dependencies: git: url: https://github.com/guchengxi1994/flutter_context_menu flutter_layout_grid: ^2.0.6 + flutter_math_fork: ^0.7.2 flutter_riverpod: ^2.5.1 flutter_rust_bridge: 2.0.0-dev.31 gauge_indicator: ^0.4.3