BananaPrompts

A helpful Elixir library for crafting and managing prompts for large language models (LLMs), particularly designed to integrate seamlessly with the Banana AI platform. This package provides utilities for constructing, validating, and formatting prompts, enabling you to build robust and reliable LLM-powered applications.

Installation

To install BananaPrompts, add it as a dependency to your mix.exs file: elixir def deps do [

{:banana_prompts, "~> 0.1.0"} # Replace with the actual latest version

] end

Then, run mix deps.get to fetch the dependency.

Usage

Here are a few examples demonstrating how to use BananaPrompts:

1. Simple Prompt Construction: elixir alias BananaPrompts.Prompt

prompt = Prompt.new("Translate this to French: {{text}}", %{text: "Hello, world!"}) |> Prompt.render()

IO.puts(prompt) # Output: Translate this to French: Hello, world!

2. Prompt Validation: elixir alias BananaPrompts.Prompt

prompt_string = "Generate a short story about a {{animal}} in a {{location}}."

case Prompt.validate(prompt_string, [:animal, :location]) do {:ok, missing_keys} ->

IO.puts("Prompt is valid.")

->

IO.puts("Missing keys: #{inspect(missing_keys)}")

end

3. Complex Prompt with Conditional Logic (Example):

While this package doesn't inherently provide conditional logic, you can use Elixir's powerful pattern matching and functions to achieve similar results: elixir defmodule MyPrompts do import BananaPrompts.Prompt, only: [new: 2, render: 1]

def generate_prompt(type, data) do

case type do
  :short ->
    new("Write a short description of a {{thing}}.", data)
    |> render()

  :long ->
    new("Write a detailed explanation of a {{thing}}.", data)
    |> render()

  _ ->
    "Invalid prompt type."
end

end end

IO.puts(MyPrompts.generate_prompt(:short, %{thing: "banana"}))

Output: Write a short description of a banana.

4. Using Pipes for Concise Prompt Creation: elixir alias BananaPrompts.Prompt

prompt = %{ template: "Summarize the following text: {{text}}", data: %{text: "This is a very long piece of text that needs to be summarized."} } |> Map.put(:template, Map.get(%, :template)) |> Map.put(:data, Map.get(%, :data)) |> then(fn map -> BananaPrompts.Prompt.new(map[:template], map[:data]) end) |> BananaPrompts.Prompt.render()

IO.puts(prompt)

Output: Summarize the following text: This is a very long piece of text that needs to be summarized.

5. Handling Optional Parameters: elixir alias BananaPrompts.Prompt

defmodule MyPrompts do import BananaPrompts.Prompt, only: [new: 2, render: 1]

def generate_prompt(thing, options \ %{}) do

template = "Describe the {{thing}}" <> (if options[:detail], do: " in detail", else: "") <> "."
new(template, %{thing: thing}) |> render()

end end

IO.puts(MyPrompts.generate_prompt("banana"))

Output: Describe the banana.

IO.puts(MyPrompts.generate_prompt("apple", detail: true))

Output: Describe the apple in detail.

Features

License

MIT

This package is part of the banana-prompts ecosystem. For advanced features and enterprise-grade tools, visit: https://bananaproai.com/banana-prompts/