intelli-monad: Type level prompt with openai.

[ development, library, mit, program ] [ Propose Tags ] [ Report a vulnerability ]
Versions [RSS] 0.1.0.0, 0.1.0.1, 0.1.0.2
Change log CHANGELOG.md
Dependencies aeson (>=2.1 && <2.3), aeson-pretty (>=0.8.10 && <0.9), base (>=4 && <5), base64-bytestring (>=1.2.1 && <1.3), bytestring (>=0.11.5 && <0.12), containers (>=0.6.7 && <0.7), haskeline (>=0.8.2 && <0.9), http-client (>=0.7.16 && <0.8), http-client-tls (>=0.3.6 && <0.4), http-conduit (>=2.2 && <2.4), intelli-monad, JuicyPixels (>=3.3.8 && <3.4), megaparsec (>=9.5 && <9.7), openai-servant-gen (>=0.1.0 && <0.2), optparse-applicative (>=0.18 && <0.19), persistent (>=2.14.6 && <2.15), persistent-sqlite (>=2.13.3 && <2.14), process (>=1.6.17 && <1.7), servant (>=0.20.1 && <0.21), servant-client (>=0.20 && <0.21), sixel (>=0.1.2 && <0.2), temporary (>=1.3 && <1.4), text (>=2.0.2 && <2.1), time (>=1.12.2 && <1.13), transformers (>=0.6.1 && <0.7), vector (>=0.13.1 && <0.14), xml-conduit (>=1.9 && <2.0), yaml (>=0.11 && <0.12) [details]
License MIT
Author Junji Hashimoto
Maintainer junji.hashimoto@gmail.com
Category Development
Home page https://github.com/junjihashimoto/intelli-monad
Uploaded by junjihashimoto at 2024-03-30T08:53:29Z
Distributions NixOS:0.1.0.2
Executables auto-talk, calc, intelli-monad
Downloads 121 total (4 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2024-03-30 [all 1 reports]

Readme for intelli-monad-0.1.0.2

[back to package description]

intelli-monad

intelli-monad provides high level APIs for prompt engineering using openai.

Featuring:

  • Type level function calling with JSON-Schema
  • Validation of return value
  • Repl interface
  • Persistent of prompt logs with SQLite
  • Session management with repl

intelli-monad is based on openai-servant-gen. openai-servant-gen is automatically generated from OpenAPI interface.

Install

git clone git@github.com:junjihashimoto/intelli-monad.git
cd intelli-monad
export PATH=~/.local/bin:$PATH
cabal install intelli-monad

Usage of repl

After install intelli-monad, set OPENAI_API_KEY, then run intelli-monad command. The system commands begin with prefix ":". Anything else will be the user's prompt.

$ export OPENAI_API_KEY=xxx
$ export OPENAI_MODEL=xxx
$ intelli-monad
% :help
:quit
:clear
:show contents
:show usage
:show request
:show context
:show session
:list sessions
:copy session <from> <to>
:delete session <session name>
:switch session <session name>
:help
% hello
assistant: Hello! How can I assist you today?

Usage of function calling with validation

Here is an example of function calling and validation. In this example, validation is performed using the input of function calling.

Define the function calling as ValidateNumber, and define the context as Math.

JSONSchema type-class can output it as JSON Schema. Defining HasFunctionObject class adds descriptin to each field. This allows Openai's interface to understand the meaning of each field. Tool type-class defines the input and output types of function calling, and defines the contents of the function.

CustomInstruction type-class defines the context with headers and footers.

runPromptWithValidation function calls LLM. The results will be validated and a number will be returned.

{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}

module Main where

import Data.Aeson
import Data.Proxy
import GHC.Generics
import IntelliMonad.Persist
import IntelliMonad.Prompt
import IntelliMonad.Types
import OpenAI.Types

data ValidateNumber = ValidateNumber
  { number :: Double
  }
  deriving (Eq, Show, Generic, JSONSchema, FromJSON, ToJSON)

instance HasFunctionObject ValidateNumber where
  getFunctionName = "output_number"
  getFunctionDescription = "validate input number"
  getFieldDescription "number" = "A number that system outputs."

instance Tool ValidateNumber where
  data Output ValidateNumber = ValidateNumberOutput
    { code :: Int,
      stdout :: String,
      stderr :: String
    }
    deriving (Eq, Show, Generic, FromJSON, ToJSON)
  toolExec _ = return $ ValidateNumberOutput 0 "" ""

data Math = Math

instance CustomInstruction Math where
  customHeader = [(Content System (Message "Calcurate user input, then output just the number. Then call 'output_number' function.") "" defaultUTCTime)]
  customFooter = []

main :: IO ()
main = do
  v <- runPromptWithValidation @ValidateNumber @StatelessConf [] [CustomInstructionProxy (Proxy @Math)] "default" (fromModel "gpt-4") "2+3+3+sin(3)"
  print (v :: Maybe ValidateNumber)