-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add default questions in chat screen.
- Loading branch information
1 parent
7e918cb
commit c89a73d
Showing
13 changed files
with
398 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import 'package:domain/entities/message.dart'; | ||
|
||
class ChatChunk { | ||
String? id; | ||
String? object; | ||
int? created; | ||
String? model; | ||
String? systemFingerprint; | ||
List<Choices>? choices; | ||
|
||
ChatChunk( | ||
{this.id, | ||
this.object, | ||
this.created, | ||
this.model, | ||
this.systemFingerprint, | ||
this.choices}); | ||
|
||
ChatChunk.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)); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
class Choices { | ||
int? index; | ||
Delta? delta; | ||
String? logprobs; | ||
String? 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']; | ||
} | ||
} | ||
|
||
class Delta { | ||
String? content; | ||
List<ToolCalls>? toolCalls; | ||
|
||
Delta({this.content, this.toolCalls}); | ||
|
||
Delta.fromJson(Map<String, dynamic> json) { | ||
content = json['content']; | ||
if (json['tool_calls'] != null) { | ||
toolCalls = <ToolCalls>[]; | ||
json['tool_calls'].forEach((v) { | ||
toolCalls!.add(ToolCalls.fromJson(v)); | ||
}); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,58 @@ | ||
import 'dart:async'; | ||
|
||
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_completion_data_source.dart'; | ||
import '../models/chat_chunk.dart'; | ||
|
||
class ChatRepositoryImp implements ChatRepository { | ||
final ChatCompletionsDataSource chatCompletionsDataSource; | ||
|
||
ChatRepositoryImp({required this.chatCompletionsDataSource}); | ||
|
||
@override | ||
Stream<String> sendMessages(List<Message> messages, List<FunctionTool> functionTools) { | ||
return chatCompletionsDataSource.sendMessages(messages, functionTools); | ||
Stream<Message> sendMessages( | ||
List<Message> messages, List<FunctionTool> functionTools) { | ||
// Create a StreamController to accumulate and emit the content as strings | ||
final StreamController<Message> controller = StreamController<Message>(); | ||
|
||
// Call the original sendMessages method that returns Stream<ChatChunk> | ||
final chatChunks = | ||
chatCompletionsDataSource.sendMessages(messages, functionTools); | ||
|
||
Message buffer = Message(role: "assistant"); | ||
// Listen to the incoming stream of ChatChunks | ||
chatChunks.listen((ChatChunk chunk) { | ||
// Accumulate or extract the content from the ChatChunk and add it to the stream | ||
final token = chunk.choices?.first.delta?.content; | ||
if (token != null) { | ||
buffer.content = (buffer.content ?? "") + token; | ||
} | ||
|
||
final toolCalls = chunk.choices?.first.delta?.toolCalls; | ||
if (toolCalls != null) { | ||
if (buffer.toolCalls == null) { | ||
buffer.toolCalls = toolCalls; | ||
} else { | ||
final newArguments = toolCalls.first.function?.arguments; | ||
if (newArguments != null) { | ||
final currentArg = | ||
buffer.toolCalls?.first.function?.arguments ?? ""; | ||
buffer.toolCalls?.first.function?.arguments = | ||
currentArg + newArguments; | ||
} | ||
} | ||
} | ||
|
||
controller.add(buffer); // Emit the content to the stream | ||
}, onError: (error) { | ||
controller.addError(error); // Handle errors | ||
}, onDone: () { | ||
controller.close(); // Close the stream when done | ||
}); | ||
|
||
// Return the stream of accumulated content as strings | ||
return controller.stream; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,78 @@ | ||
class Message { | ||
final String role; | ||
String content; | ||
String? role; | ||
String? content; | ||
List<ToolCalls>? toolCalls; | ||
String? tool_call_id; | ||
|
||
Message({required this.role, required this.content}); | ||
Message({this.role, this.content, this.toolCalls, this.tool_call_id}); | ||
|
||
// Factory constructor for creating a new Message instance from JSON | ||
factory Message.fromJson(Map<String, dynamic> json) { | ||
return Message( | ||
role: json['role'], | ||
content: json['content'], | ||
); | ||
Message.fromJson(Map<String, dynamic> json) { | ||
role = json['role'] ?? 'user'; | ||
content = json['content']; | ||
if (json['tool_calls'] != null) { | ||
toolCalls = <ToolCalls>[]; | ||
json['tool_calls'].forEach((v) { | ||
toolCalls!.add(ToolCalls.fromJson(v)); | ||
}); | ||
} | ||
tool_call_id = json['tool_call_id']; | ||
} | ||
|
||
// Method for converting a Message instance to JSON format | ||
Map<String, dynamic> toJson() { | ||
return { | ||
'role': role, | ||
'content': content, | ||
}; | ||
final Map<String, dynamic> data = <String, dynamic>{}; | ||
data['role'] = role; | ||
data['content'] = content; | ||
if (toolCalls != null) { | ||
data['tool_calls'] = toolCalls!.map((v) => v.toJson()).toList(); | ||
} | ||
data['tool_call_id'] = tool_call_id; | ||
return data; | ||
} | ||
} | ||
|
||
class ToolCalls { | ||
String? id; | ||
String? type; | ||
FunctionCall? function; | ||
|
||
ToolCalls({this.id, this.type, this.function}); | ||
|
||
ToolCalls.fromJson(Map<String, dynamic> json) { | ||
id = json['id']; | ||
type = json['type']; | ||
function = json['function'] != null | ||
? FunctionCall.fromJson(json['function']) | ||
: null; | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
final Map<String, dynamic> data = <String, dynamic>{}; | ||
data['id'] = id; | ||
data['type'] = type; | ||
if (function != null) { | ||
data['function'] = function!.toJson(); | ||
} | ||
return data; | ||
} | ||
} | ||
|
||
class FunctionCall { | ||
String? name; | ||
String? arguments; | ||
|
||
FunctionCall({this.name, this.arguments}); | ||
|
||
FunctionCall.fromJson(Map<String, dynamic> json) { | ||
name = json['name']; | ||
arguments = json['arguments']; | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
final Map<String, dynamic> data = <String, dynamic>{}; | ||
data['name'] = name; | ||
data['arguments'] = arguments; | ||
return data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.