Skip to content

Commit

Permalink
Integrate first function tool
Browse files Browse the repository at this point in the history
  • Loading branch information
banghuazhao committed Sep 29, 2024
1 parent 157490e commit 7e918cb
Show file tree
Hide file tree
Showing 29 changed files with 430 additions and 33 deletions.
29 changes: 29 additions & 0 deletions composite_calculator/.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 composite_calculator/.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: "bae5e49bc2a867403c43b2aae2de8f8c33b037e4"
channel: "[user-branch]"

project_type: package
3 changes: 3 additions & 0 deletions composite_calculator/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 composite_calculator/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 composite_calculator/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.
4 changes: 4 additions & 0 deletions composite_calculator/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include: package:flutter_lints/flutter.yaml

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
7 changes: 7 additions & 0 deletions composite_calculator/lib/composite_calculator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
library composite_calculator;

/// A Calculator.
class Calculator {
/// Returns [value] plus 1.
int addOne(int value) => value + 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class LaminaEngineeringConstantsInput {
final double E1;
final double E2;
final double G12;
final double nu12;
final double layupAngle;

LaminaEngineeringConstantsInput({
required this.E1,
required this.E2,
required this.G12,
required this.nu12,
required this.layupAngle,
});

// Factory method to create an instance with default values
factory LaminaEngineeringConstantsInput.withDefaults() {
return LaminaEngineeringConstantsInput(
E1: 150000,
E2: 10000,
G12: 5000,
nu12: 0.3,
layupAngle: 45,
);
}

Map<String, dynamic> toJson() {
return {
'E1': E1,
'E2': E2,
'G12': G12,
'nu12': nu12,
'layup_angle': layupAngle,
};
}
}
54 changes: 54 additions & 0 deletions composite_calculator/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: composite_calculator
description: "A new Flutter package project."
version: 0.0.1
homepage:

environment:
sdk: '>=3.3.0 <4.0.0'
flutter: ">=1.17.0"

dependencies:
flutter:
sdk: flutter

dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^3.0.0

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter packages.
flutter:

# To add assets to your package, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
#
# For details regarding assets in packages, see
# https://flutter.dev/assets-and-images/#from-packages
#
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware

# To add custom fonts to your package, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts in packages, see
# https://flutter.dev/custom-fonts/#from-packages
12 changes: 12 additions & 0 deletions composite_calculator/test/composite_calculator_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:flutter_test/flutter_test.dart';

import 'package:composite_calculator/composite_calculator.dart';

void main() {
test('adds one to input values', () {
final calculator = Calculator();
expect(calculator.addOne(2), 3);
expect(calculator.addOne(-7), -6);
expect(calculator.addOne(0), 1);
});
}
3 changes: 2 additions & 1 deletion data/lib/data.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export 'utils/network_exceptions.dart';
export 'data_sources/chat_remote_data_source.dart';
export 'data_sources/chat_completion_data_source.dart';
export 'repositories/chat_repository_impl.dart';
export 'repositories/chat_session_repository_imp.dart';
export "repositories/function_tools_repository_imp.dart";
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
import 'dart:convert';
import 'package:domain/entities/function_tool.dart';
import 'package:domain/entities/message.dart';
import 'package:http/http.dart' as http;
import '../utils/api_constants.dart';
import '../utils/network_exceptions.dart';

abstract class ChatRemoteDataSource {
Stream<String> sendMessages(List<Message> messages);
abstract class ChatCompletionsDataSource {
Stream<String> sendMessages(List<Message> messages, List<FunctionTool> functionTools);
}

class ChatRemoteDataSourceImpl implements ChatRemoteDataSource {
class ChatRemoteDataSourceImpl implements ChatCompletionsDataSource {
final http.Client client;

ChatRemoteDataSourceImpl({required this.client});

@override
Stream<String> sendMessages(List<Message> messages) async* {
final request = http.Request('POST', Uri.parse(ApiConstants.chatEndpoint))
Stream<String> sendMessages(List<Message> messages,
List<FunctionTool> functionTools) async* {
final request = http.Request('POST', Uri.parse(ApiConstants.chatCompletionsEndpoint))
..headers.addAll({
'Content-Type': 'application/json',
'Authorization': 'Bearer ${ApiConstants.apiKey}',
})
..body = jsonEncode({
..body = functionTools.isEmpty ? jsonEncode({
"model": "gpt-4o",
"stream": true,
'messages': messages
}) :
jsonEncode({
"model": "gpt-4o",
"stream": true,
'messages': messages,
"tools": functionTools
});

// var prettyBody = const JsonEncoder.withIndent(' ').convert(jsonDecode(request.body));
// print(prettyBody);

final response = await client.send(request);

if (response.statusCode == 200) {
Expand Down Expand Up @@ -59,6 +70,7 @@ class ChatRemoteDataSourceImpl implements ChatRemoteDataSource {
}
}
} else {
print('Failed to send message: ${response.statusCode}');
throw NetworkException('Failed to send message: ${response.statusCode}');
}
}
Expand Down
86 changes: 86 additions & 0 deletions data/lib/models/chat_completion.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
class ChatCompletion {
String? id;
String? object;
int? created;
String? model;
String? systemFingerprint;
List<Choices>? choices;

ChatCompletion(
{this.id,
this.object,
this.created,
this.model,
this.systemFingerprint,
this.choices});

ChatCompletion.fromJson(Map<String, dynamic> json) {
id = json['id'];
object = json['object'];
created = json['created'];
model = json['model'];
systemFingerprint = json['system_fingerprint'];
if (json['choices'] != null) {
choices = <Choices>[];
json['choices'].forEach((v) {
choices!.add(new Choices.fromJson(v));
});
}
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['object'] = this.object;
data['created'] = this.created;
data['model'] = this.model;
data['system_fingerprint'] = this.systemFingerprint;
if (this.choices != null) {
data['choices'] = this.choices!.map((v) => v.toJson()).toList();
}
return data;
}
}

class Choices {
int? index;
Delta? delta;
Null? logprobs;
Null? finishReason;

Choices({this.index, this.delta, this.logprobs, this.finishReason});

Choices.fromJson(Map<String, dynamic> json) {
index = json['index'];
delta = json['delta'] != null ? new Delta.fromJson(json['delta']) : null;
logprobs = json['logprobs'];
finishReason = json['finish_reason'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['index'] = this.index;
if (this.delta != null) {
data['delta'] = this.delta!.toJson();
}
data['logprobs'] = this.logprobs;
data['finish_reason'] = this.finishReason;
return data;
}
}

class Delta {
String? content;

Delta({this.content});

Delta.fromJson(Map<String, dynamic> json) {
content = json['content'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['content'] = this.content;
return data;
}
}
11 changes: 6 additions & 5 deletions data/lib/repositories/chat_repository_impl.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import 'package:domain/entities/function_tool.dart';
import 'package:domain/entities/message.dart';
import 'package:domain/repositories_abstract/chat_repository.dart';
import '../data_sources/chat_remote_data_source.dart';
import '../data_sources/chat_completion_data_source.dart';

class ChatRepositoryImp implements ChatRepository {
final ChatRemoteDataSource remoteDataSource;
final ChatCompletionsDataSource chatCompletionsDataSource;

ChatRepositoryImp({required this.remoteDataSource});
ChatRepositoryImp({required this.chatCompletionsDataSource});

@override
Stream<String> sendMessages(List<Message> messages) {
return remoteDataSource.sendMessages(messages);
Stream<String> sendMessages(List<Message> messages, List<FunctionTool> functionTools) {
return chatCompletionsDataSource.sendMessages(messages, functionTools);
}
}
Loading

0 comments on commit 7e918cb

Please sign in to comment.