Skip to content

Commit

Permalink
update prompt
Browse files Browse the repository at this point in the history
fix render issue
  • Loading branch information
guchengxi1994 committed May 1, 2024
1 parent 0f506d6 commit 362040f
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 6 deletions.
4 changes: 2 additions & 2 deletions assets/llm.json
Git LFS file not shown
3 changes: 3 additions & 0 deletions assets/llm/chat.png
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 assets/llm/engineer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 1 addition & 3 deletions lib/layout/components/animated_sidebar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ class _AnimatedSidebarState extends ConsumerState<AnimatedSidebar>
});
if (width == LayoutStyle.sidebarExpand) {
ref.read(sidebarProvider.notifier).changeStatus(true);
}

if (width == LayoutStyle.sidebarCollapse) {
} else {
ref.read(sidebarProvider.notifier).changeStatus(false);
}
});
Expand Down
78 changes: 78 additions & 0 deletions lib/llm/chatchat/components/latex.dart
Original file line number Diff line number Diff line change
@@ -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<String, String> 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);
}
}
20 changes: 19 additions & 1 deletion lib/llm/chatchat/models/response_message_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions lib/llm/langchain/components/tools_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ class _ToolsScreenState extends State<ToolsScreen> {
))
],
),
const SizedBox(
height: 10,
),
Expanded(
child: Align(
alignment: Alignment.topLeft,
Expand Down
16 changes: 16 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 362040f

Please sign in to comment.