lcpp_ngin 1.0.0 copy "lcpp_ngin: ^1.0.0" to clipboard
lcpp_ngin: ^1.0.0 copied to clipboard

LlamaC++ Engin is a dart implementation of llama.cpp. It is a revision of lcpp from mobile artificial intelligence distribution (maid).

example/lib/main.dart

import 'dart:io';

import 'package:flutter/material.dart';

import 'package:file_picker/file_picker.dart';
import 'package:lcpp_ngin/lcpp_ngin.dart';

void main() {
  runApp(const LlamaApp());
}

class LlamaApp extends StatefulWidget {
  const LlamaApp({super.key});

  @override
  State<LlamaApp> createState() => LlamaAppState();
}

class LlamaAppState extends State<LlamaApp> {
  final TextEditingController controller = TextEditingController();
  // final List<ChatMessage> messages = [];
  LlamaCpp? model;
  String? modelPath;
  bool busy = false;

  void loadModel() async {
    final result = await FilePicker.platform.pickFiles(
        dialogTitle: "Load Model File",
        type: FileType.any,
        allowMultiple: false,
        allowCompression: false);

    if (result == null ||
        result.files.isEmpty ||
        result.files.single.path == null) {
      throw Exception('No file selected');
    }

    File resultFile = File(result.files.single.path!);

    final exists = await resultFile.exists();
    if (!exists) {
      throw Exception('File does not exist');
    }

    final llamaCpp = LlamaCpp(
        contextParams: ContextParams(nCtx: 2048, nBatch: 2048),
        lcppParams: LlamaCppParams(modelPath: result.files.single.path!));

    setState(() {
      model = llamaCpp;
      modelPath = result.files.single.path;
    });
  }

  void onSubmitted(String value) async {
    if (model == null) {
      return;
    }

    setState(() {
      busy = true;
      // messages.add(UserChatMessage(value));
      controller.clear();
    });

    // final stream = model!.prompt(messages.copy());

    // final StreamController<ChatMessage> sc = StreamController();

    // final stream = sc.stream();

    // messages.add(AssistantChatMessage(''));

    // await for (var response in stream) {
    //   setState(() {
    //     messages.last.content += response.finishReason != FinishReason.stop ? response.output : '';
    //   });
    // }

    setState(() => busy = false);
  }

  void onStop() {
    model?.stop();
    setState(() => busy = false);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: buildHome());
  }

  Widget buildHome() {
    return Scaffold(
      appBar: buildAppBar(),
      body: buildBody(),
    );
  }

  PreferredSizeWidget buildAppBar() {
    return AppBar(
        title: Text(modelPath ?? 'No model loaded'),
        leading: IconButton(
          icon: const Icon(Icons.folder_open),
          onPressed: (){}
        ));
  }

  Widget buildBody() {
    return Column(
      children: [
        Expanded(
          child: ListView.builder(
            itemCount: 0, // messages.length,
            itemBuilder: (context, index) {
              // final message = messages[index];
              return ListTile(
                title: Text("message.role"),
                subtitle: Text("message.content"),
              );
            },
          ),
        ),
        buildInputField(),
      ],
    );
  }

  Widget buildInputField() {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Row(
        children: [
          Expanded(
            child: TextField(
              controller: controller,
              onSubmitted: onSubmitted,
              decoration: const InputDecoration(
                labelText: 'Enter your message',
                border: OutlineInputBorder(),
              ),
            ),
          ),
          busy
              ? IconButton(
                  icon: const Icon(Icons.stop),
                  onPressed: () => onStop(),
                )
              : IconButton(
                  icon: const Icon(Icons.send),
                  onPressed: () => onSubmitted(controller.text),
                ),
        ],
      ),
    );
  }
}
0
likes
140
points
24
downloads

Publisher

unverified uploader

Weekly Downloads

LlamaC++ Engin is a dart implementation of llama.cpp. It is a revision of lcpp from mobile artificial intelligence distribution (maid).

Repository (GitHub)
View/report issues

Topics

#ai #nlp #llm

Documentation

API reference

License

MIT (license)

Dependencies

collection, ffi, flutter, langchain_core, path

More

Packages that depend on lcpp_ngin

Packages that implement lcpp_ngin