Skip to content

Commit

Permalink
Merge pull request #47 from guchengxi1994/master
Browse files Browse the repository at this point in the history
bump to 0.0.10
  • Loading branch information
guchengxi1994 authored May 29, 2024
2 parents e6d9b06 + 2826e3c commit 346921f
Show file tree
Hide file tree
Showing 92 changed files with 3,495 additions and 8,593 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# all_in_one

> this project is just for fun, a lot of features are not stable and not usable.
> only tested on `kimi` and `langchain`
### how to run

Expand Down
29 changes: 29 additions & 0 deletions langchain_lib/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/
migrate_working_dir/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
/pubspec.lock
**/doc/api/
.dart_tool/
build/
10 changes: 10 additions & 0 deletions langchain_lib/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: "5dcb86f68f239346676ceb1ed1ea385bd215fba1"
channel: "stable"

project_type: package
3 changes: 3 additions & 0 deletions langchain_lib/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.0.1

* TODO: Describe initial release.
1 change: 1 addition & 0 deletions langchain_lib/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TODO: Add your license here.
39 changes: 39 additions & 0 deletions langchain_lib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!--
This README describes the package. If you publish this package to pub.dev,
this README's contents appear on the landing page for your package.
For information about how to write a good package README, see the guide for
[writing package pages](https://dart.dev/guides/libraries/writing-package-pages).
For general information about developing packages, see the Dart guide for
[creating packages](https://dart.dev/guides/libraries/create-library-packages)
and the Flutter guide for
[developing packages and plugins](https://flutter.dev/developing-packages).
-->

TODO: Put a short description of the package here that helps potential users
know whether this package might be useful for them.

## Features

TODO: List what your package can do. Maybe include images, gifs, or videos.

## Getting started

TODO: List prerequisites and provide or point to information on how to
start using the package.

## Usage

TODO: Include short and useful examples for package users. Add longer examples
to `/example` folder.

```dart
const like = 'sample';
```

## Additional information

TODO: Tell users more about the package: where to find more information, how to
contribute to the package, how to file issues, what response they can expect
from the package authors, and more.
8 changes: 8 additions & 0 deletions langchain_lib/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
analyzer:
errors:
avoid_print: ignore
constant_identifier_names: ignore
include: package:flutter_lints/flutter.yaml

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
18 changes: 18 additions & 0 deletions langchain_lib/lib/client/client.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:langchain/langchain.dart';
import 'package:langchain_lib/models/template_item.dart';

abstract class Client {
int maxTryTimes = 60; // kimi没充值的情况下每分钟请求3个

Future<ChatResult> invoke(List<ChatMessage> history);

Stream<ChatResult> stream(List<ChatMessage> history);

BaseChain? intoChain(List<TemplateItem> items);

Future<Map<String, dynamic>> invokeChain(
BaseChain chain, int chainLength, String input);

Future<Map<String, dynamic>> invokeChainWithTemplateItems(
List<TemplateItem> items);
}
27 changes: 27 additions & 0 deletions langchain_lib/lib/client/load_env.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'dart:io';

Map<String, String> loadEnv(String path) {
File f = File(path);
if (f.existsSync()) {
final lines = f.readAsLinesSync();
// return f.readAsLinesSync();
final Map<String, String> result = {};
for (final i in lines) {
if (i.startsWith('#')) {
continue;
}
final s = i.split("=");
if (s.length == 2) {
if (s[0].trim() == "LLM_BASE" ||
s[0].trim() == "LLM_MODEL_NAME" ||
s[0].trim() == "LLM_SK") {
result[s[0].trim()] = s[1].trim();
}
}
}

return result;
} else {
return {};
}
}
140 changes: 140 additions & 0 deletions langchain_lib/lib/client/openai_client.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import 'package:langchain/langchain.dart';
import 'package:langchain_lib/client/client.dart';
import 'package:langchain_lib/models/template_item.dart';
import 'package:langchain_openai/langchain_openai.dart';

import 'load_env.dart';

class OpenaiClient extends Client {
final String baseUrl;
final String? apiKey;
final String? modelName;

OpenaiClient({required this.baseUrl, this.apiKey, this.modelName}) {
model = ChatOpenAI(
apiKey: apiKey,
baseUrl: baseUrl,
defaultOptions: ChatOpenAIOptions(model: modelName));
}

late final ChatOpenAI model;

@override
Future<ChatResult> invoke(List<ChatMessage> history) async {
final prompt = PromptValue.chat(history);
return await model.invoke(prompt);
}

@override
Stream<ChatResult> stream(List<ChatMessage> history) {
final prompt = PromptValue.chat(history);
return model.stream(prompt);
}

static Client? fromEnv(String path) {
final envs = loadEnv(path);
if (envs.length != 3) {
return OpenaiClient(baseUrl: "");
}

return OpenaiClient(
baseUrl: envs["LLM_BASE"] ?? "",
apiKey: envs["LLM_SK"] ?? "",
modelName: envs["LLM_MODEL_NAME"] ?? "");
}

@override
BaseChain? intoChain(List<TemplateItem> items) {
if (items.isEmpty) {
return null;
}
if (items.length == 1) {
final prompt = LLMChain(
llm: model,
outputKey: "output",
prompt: PromptTemplate.fromTemplate("{input}"));

return prompt;
} else {
// final prompt = SequentialChain(chains: []);
List<BaseChain> chains = [];
for (int i = 0; i < items.length; i++) {
String input = "input$i";
String output = "input${i + 1}";
String promptStr;
if (i == 0) {
promptStr = "请根据以下要求,帮我生成对应的文案。 {$input}";
} else {
promptStr =
"请根据以下内容和额外要求,帮我生成对应的文案。内容: {$input}, 额外要求: ${items[i].prompt}";
}
print(promptStr);
final prompt = PromptTemplate.fromTemplate(promptStr);
// chains.add()
final chain = LLMChain(llm: model, prompt: prompt, outputKey: output);
chains.add(chain);
}
final seqChain = SequentialChain(
chains: chains,
// memory: const SimpleMemory(),
returnIntermediateOutputs: true);
return seqChain;
}
}

@override
Future<Map<String, dynamic>> invokeChain(
BaseChain chain, int chainLength, String input) async {
final Future<Map<String, dynamic>> fResult;

if (chainLength == 1) {
fResult = chain.call(input);
} else {
fResult = chain.call({"input0": input}, returnOnlyOutputs: false);
}

int tryTimes = 0;
Map<String, dynamic> r = {};
while (tryTimes < super.maxTryTimes) {
tryTimes += 1;
try {
final result = await fResult;
if (result.length == 1) {
r["input$chainLength"] = result['output'].content.toString();
} else {
for (int i = 0; i < chainLength; i++) {
if (result["input${i + 1}"] != null) {
if (result["input${i + 1}"] is AIChatMessage) {
r["input${i + 1}"] = result["input${i + 1}"].content;
} else {
r["input${i + 1}"] = result["input${i + 1}"];
}
}
}
}
break;
} catch (e) {
await Future.delayed(const Duration(seconds: 1));
}
}

return r;
}

@override
Future<Map<String, dynamic>> invokeChainWithTemplateItems(
List<TemplateItem> items) async {
final chain = intoChain(items);
final result = <String, dynamic>{};
if (chain != null) {
final chainResult =
await invokeChain(chain, items.length, items.first.prompt);

for (int i = 0; i < chainResult.length; i++) {
result[items[i].prompt] = chainResult.values.elementAt(i);
}
}

return result;
}
}
5 changes: 5 additions & 0 deletions langchain_lib/lib/langchain_lib.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
library langchain_lib;

export 'client/openai_client.dart';
export 'package:langchain/langchain.dart';
export 'message/message_util.dart';
22 changes: 22 additions & 0 deletions langchain_lib/lib/message/message_util.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:langchain/langchain.dart';

class MessageUtil {
MessageUtil._();
static ChatMessage createSystemMessage(final String content) {
return SystemChatMessage(content: content);
}

static ChatMessage createHumanMessage(
/* support text only right now */ final String content) {
return HumanChatMessage(content: ChatMessageContentText(text: content));
}

static ChatMessage createAiMessage(final String content) {
return AIChatMessage(content: content);
}

static ChatMessage createToolMessage(
final String content, final String toolId) {
return ToolChatMessage(content: content, toolCallId: toolId);
}
}
74 changes: 74 additions & 0 deletions langchain_lib/lib/models/appflowy_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import 'package:json_annotation/json_annotation.dart';

part 'appflowy_model.g.dart';

@JsonSerializable()
class Root {
final Document document;

Root({required this.document});

factory Root.fromJson(Map<String, dynamic> json) => _$RootFromJson(json);
Map<String, dynamic> toJson() => _$RootToJson(this);
}

@JsonSerializable()
class Document {
final String type;
final List<Children> children;

Document({required this.type, required this.children});

factory Document.fromJson(Map<String, dynamic> json) =>
_$DocumentFromJson(json);
Map<String, dynamic> toJson() => _$DocumentToJson(this);
}

@JsonSerializable()
class Children {
final String type;
final Data data;

Children({required this.type, required this.data});

factory Children.fromJson(Map<String, dynamic> json) =>
_$ChildrenFromJson(json);
Map<String, dynamic> toJson() => _$ChildrenToJson(this);
}

@JsonSerializable()
class Data {
final int? level;
final List<Delum> delta;
final String? align;

Data({this.level, required this.delta, this.align});

factory Data.fromJson(Map<String, dynamic> json) => _$DataFromJson(json);
Map<String, dynamic> toJson() => _$DataToJson(this);
}

@JsonSerializable()
class Delum {
final String insert;
final Attributes? attributes;

Delum({required this.insert, this.attributes});

factory Delum.fromJson(Map<String, dynamic> json) => _$DelumFromJson(json);
Map<String, dynamic> toJson() => _$DelumToJson(this);
}

@JsonSerializable()
class Attributes {
final bool? bold;
final bool? italic;
final String? file;
final String? sql;

Attributes({this.bold, this.italic, this.file, this.sql});

factory Attributes.fromJson(Map<String, dynamic> json) =>
_$AttributesFromJson(json);
Map<String, dynamic> toJson() => _$AttributesToJson(this);
}
Loading

0 comments on commit 346921f

Please sign in to comment.