Skip to content

Commit

Permalink
Merge pull request #22 from guchengxi1994/master
Browse files Browse the repository at this point in the history
llm
  • Loading branch information
guchengxi1994 authored May 1, 2024
2 parents ea20d32 + 362040f commit 89cea45
Show file tree
Hide file tree
Showing 21 changed files with 463 additions and 38 deletions.
3 changes: 0 additions & 3 deletions assets/fonts/.gitignore

This file was deleted.

3 changes: 3 additions & 0 deletions assets/fonts/WeiNiZhuYiLangManXingShu-2.ttf
Git LFS file not shown
3 changes: 3 additions & 0 deletions assets/llm.json
Git LFS file not shown
3 changes: 3 additions & 0 deletions assets/llm/banner.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 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.
3 changes: 3 additions & 0 deletions assets/llm/zg.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
1 change: 1 addition & 0 deletions lib/layout/layout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class _LayoutState extends ConsumerState<Layout> with TickerProviderStateMixin {
scale: _animation,
child: PageView(
controller: ref.read(pageProvider.notifier).pageController,
physics: const NeverScrollableScrollPhysics(),
children: const [
WorkboardScreen(),
EntryScreen(),
Expand Down
14 changes: 3 additions & 11 deletions lib/llm/chatchat/components/history_item_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'history_list.dart';

class HistoryList extends ConsumerWidget {
const HistoryList({super.key, required this.llmType});
const HistoryList({super.key, required this.llmType, this.bottom});
final LLMType llmType;
final Widget? bottom;

@override
Widget build(BuildContext context, WidgetRef ref) {
Expand Down Expand Up @@ -38,16 +39,7 @@ class HistoryList extends ConsumerWidget {
llmType: llmType,
);
})),
// const SizedBox(
// height: 35,
// child: Row(
// mainAxisAlignment: MainAxisAlignment.start,
// children: [
// ChangeHostToolTipWidget(),
// ChangeChatTypeWidget()
// ],
// ),
// )
bottom ?? const SizedBox()
],
),
_ => const Center(
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
59 changes: 59 additions & 0 deletions lib/llm/langchain/components/buttons.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import 'package:all_in_one/llm/langchain/notifiers/tool_notifier.dart';
import 'package:all_in_one/styles/app_style.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class Buttons extends ConsumerWidget {
const Buttons({super.key});

@override
Widget build(BuildContext context, WidgetRef ref) {
return FittedBox(
child: Container(
height: 40,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
color: AppStyle.appColor,
border: Border.all(color: AppStyle.appColor),
boxShadow: const [
BoxShadow(
color: AppStyle.appColor,
offset: Offset(0, 4),
blurRadius: 10,
spreadRadius: 1,
)
]),
child: Row(
children: [
GestureDetector(
onTap: () {
ref.read(toolProvider.notifier).changeState(null);
},
child: _wrapper(const Text(
"返回",
style: TextStyle(color: Colors.white),
)),
),
const VerticalDivider(),
GestureDetector(
onTap: () {},
child: _wrapper(const Text(
"Chains",
style: TextStyle(color: Colors.white),
)),
)
],
),
),
);
}

Widget _wrapper(Widget child) {
return SizedBox(
width: 60,
child: Center(
child: child,
),
);
}
}
30 changes: 21 additions & 9 deletions lib/llm/langchain/components/chat_ui.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:all_in_one/llm/chatchat/notifiers/history_notifier.dart';
import 'package:all_in_one/llm/chatchat/notifiers/message_notifier.dart';
import 'package:all_in_one/llm/chatchat/notifiers/message_state.dart';
import 'package:all_in_one/llm/langchain/langchain_config.dart';
import 'package:all_in_one/llm/langchain/notifiers/tool_notifier.dart';
import 'package:all_in_one/src/rust/api/llm_api.dart' as llm;
import 'package:all_in_one/src/rust/llm.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -121,6 +122,8 @@ class _ChatUIState extends ConsumerState<ChatUI> {
if (state.isLoading) {
return;
}
final sysPrompt = ref.read(toolProvider);

ref
.read(messageProvider.notifier)
.addMessageBox(RequestMessageBox(content: s));
Expand All @@ -134,19 +137,28 @@ class _ChatUIState extends ConsumerState<ChatUI> {
final messages = ref
.read(historyProvider(LLMType.openai).notifier)
.getMessages(config.historyLength, id);
for (final i in messages) {
logger.info("${i.roleType} ${i.content}");

List<LLMMessage> history;
if (sysPrompt != null && sysPrompt.content != "normal") {
history = [
sysPrompt,
...messages.map((e) =>
LLMMessage(uuid: "", content: e.content ?? "", type: e.roleType))
];
} else {
history = messages
.map((e) =>
LLMMessage(uuid: "", content: e.content ?? "", type: e.roleType))
.toList();
}

for (final i in history) {
logger.info("${i.type} ${i.content}");
}

ref
.read(historyProvider(LLMType.openai).notifier)
.updateHistory(id, s, MessageType.query);
llm.chat(
stream: true,
query: s,
history: messages
.map((e) => LLMMessage(
uuid: "", content: e.content ?? "", type: e.roleType))
.toList());
llm.chat(stream: true, query: s, history: history);
}
}
69 changes: 69 additions & 0 deletions lib/llm/langchain/components/tools_item.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import 'package:all_in_one/llm/langchain/models/tool_model.dart';
import 'package:all_in_one/llm/langchain/notifiers/tool_notifier.dart';
import 'package:all_in_one/styles/app_style.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class ToolsItem extends ConsumerStatefulWidget {
const ToolsItem({super.key, required this.toolModel});
final ToolModel toolModel;

@override
ConsumerState<ToolsItem> createState() => _ToolsItemState();
}

class _ToolsItemState extends ConsumerState<ToolsItem> {
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
ref
.read(toolProvider.notifier)
.changeState(widget.toolModel.toMessage());
},
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),
)),
],
),
),
);
}
}
Loading

0 comments on commit 89cea45

Please sign in to comment.