Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Latest commit

 

History

History
233 lines (168 loc) · 5.69 KB

README.md

File metadata and controls

233 lines (168 loc) · 5.69 KB

Module flappy

This package is the Kotlin version of the flappy implementation.

Features

  • Easily switch between different LLMs
  • Controlled, consistent, and machine-readable LLM outputs
  • Implement via pure Kotlin

Installation

In Gradle(Kotlin), add the following dependency to your build.gradle.kts file:

// plugins
id("com.google.osdetector") version "1.7.3"

// dependencies
implementation("com.pleisto:flappy:0.0.8")
implementation("com.pleisto:flappy-java-bindings:0.0.8")
implementation("com.pleisto:flappy-java-bindings:0.0.8:${osdetector.classifier}")

In other build system, please refer to here

Usage

Create a Synthesized Function

A synthesized function allows developers to format natural language using configuration fields for the LLM.

Kotlin
class LawMetaArguments(
  @FlappyField(description = "Lawsuit full text.")
  val lawsuit: String
)

class LawMetaReturn(
  @FlappyField
  val verdict: Verdict,

  @FlappyField
  val plaintiff: String,

  @FlappyField
  val defendant: String,

  @FlappyField
  val judgeOptions: List<String>
)

val lawGetMeta = FlappySynthesizedFunction(
  name = "getMeta",
  description = "Extract meta data from a lawsuit full text.",
  args = LawMetaArguments::class.java,
  returnType = LawMetaReturn::class.java,
)
Java
class LawMetaArguments {
  @FlappyField(description = "Lawsuit full text.")
  String lawsuit;
}

class LawMetaReturn {
  @FlappyField
  Verdict verdict;

  @FlappyField
  String plaintiff;

  @FlappyField
  String defendant;

  @FlappyField
  List<String> judgeOptions;
}

  FlappyFunction<?, ?> lawGetMeta = new FlappySynthesizedFunction(
    "getMeta",
    "Extract meta data from a lawsuit full text.",
    LawMetaArguments.class,
    LawMetaReturn.class
  );

Create an Invoke Function

In addition to synthesized functions, developers can also add custom methods for the agent to invoke by including invokeFunction.

Kotlin
class GetLatestLawsuitsArguments(
  @FlappyField
  val plaintiff: String,

  @FlappyField(description = "For demo purpose. set to False")
  val arg1: Boolean,

  @FlappyField(description = "ignore it", optional = true)
  val arg2: List<String>?
)

class GetLatestLawsuitsReturn(
  @FlappyField
  val output: String
)


val lawGetLatestLawsuitsByPlaintiff = FlappyInvokeFunction(
  name = "getLatestLawsuitsByPlaintiff",
  description = "Get the latest lawsuits by plaintiff.",
  args = GetLatestLawsuitsArguments::class.java,
  returnType = GetLatestLawsuitsReturn::class.java,
  invoker = { _, _ -> GetLatestLawsuitsReturn(MOCK_LAWSUIT_DATA) }
)
Java
class GetLatestLawsuitsArguments {
  @FlappyField
  String plaintiff;

  @FlappyField(description = "For demo purpose. set to False")
  Boolean arg1;

  @FlappyField(description = "ignore it", optional = true)
  List<String> arg2 = null;
}

static class GetLatestLawsuitsReturn {
  @FlappyField
  String output;

  public GetLatestLawsuitsReturn(String output) {
    this.output = output;
  }
}

  FlappyFunction<?, ?> lawGetLatestLawsuitsByPlaintiff = new FlappyInvokeFunction(
    "getLatestLawsuitsByPlaintiff",
    "Get the latest lawsuits by plaintiff.",
    GetLatestLawsuitsArguments.class,
    GetLatestLawsuitsReturn.class,
    (a, agent, $completion) -> new GetLatestLawsuitsReturn(MOCK_LAWSUIT_DATA)
  );

Create an Agent

To create an agent, you need to provide an LLM (Large Language Model) along with the methods you want the agent to use.

Kotlin
val llm = ChatGPT(
  ChatGPT.ChatGPTConfig(token = dotenv["OPENAI_TOKEN"], host = dotenv["OPENAI_API_BASE"])
)

val lawAgent = FlappyBaseAgent(
  maxRetry = 2,
  inferenceLLM = llm,
  functions = listOf(lawGetMeta, lawGetLatestLawsuitsByPlaintiff)
)
Java
ChatGPT llm = new ChatGPT(new ChatGPT.ChatGPTConfig(null, dotenv.get("OPENAI_TOKEN"), dotenv.get("OPENAI_API_BASE")));
FlappyBaseAgent lawAgent=new FlappyBaseAgent(
  llm,Arrays.asList(lawGetMeta,lawGetLatestLawsuitsByPlaintiff)
);

Examples

Kotlin

Java

What's next

  • Template Engine
  • Code Interpreter