Skip to content

Commit

Permalink
Added description to GPT FunctionCall
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Jun 7, 2024
1 parent 207c5c2 commit 25207e0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
7 changes: 5 additions & 2 deletions ihp-openai/IHP/OpenAI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ data JsonSchema
deriving (Eq, Show)

data Property
= Property { propertyName :: !Text, type_ :: !JsonSchema, required :: !Bool }
= Property { propertyName :: !Text, type_ :: !JsonSchema, required :: !Bool, description :: !(Maybe Text) }
deriving (Eq, Show)

instance ToJSON CompletionRequest where
Expand Down Expand Up @@ -117,8 +117,11 @@ instance ToJSON JsonSchema where
toJSON (JsonSchemaObject properties) =
object
[ "type" .= ("object" :: Text)
, "properties" .= (object (concat (map (\property -> [ (Key.fromText property.propertyName) .= (toJSON property.type_) ]) properties)))
, "properties" .= (object (concat (map (\property -> [ (Key.fromText property.propertyName) .= ((toJSON property.type_) `mergeObj` (object [ "description" .= property.description ])) ]) properties)))
]
where
mergeObj (Object first) (Object second) = Object (first <> second)
mergeObj _ _ = error "JsonSchema.mergeObj failed with invalid type"
toJSON JsonSchemaString =
object [ "type" .= ("string" :: Text) ]

Expand Down
13 changes: 12 additions & 1 deletion ihp-openai/Test/IHP/OpenAISpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import IHP.OpenAI
import NeatInterpolation (trimming)
import qualified Data.Text.Encoding as Text
import qualified Data.Text as Text
import Data.Aeson

main :: IO ()
main = hspec do
Expand Down Expand Up @@ -584,4 +585,14 @@ tests = do

let parseLines = foldl (\state line -> (parseResponseChunk state (Text.encodeUtf8 line)).state) emptyParserState (Text.lines input)

parseLines `shouldBe` result
parseLines `shouldBe` result

describe "ToJSON Tool" do
it "encode Function call with parameter descriptions" do
let function = Function
{ name = "fetchUrl"
, description = Just "Fetches a url"
, parameters = Just (JsonSchemaObject [ Property { propertyName = "url", type_ = JsonSchemaString, required = True, description = Just "The url to fetch" }])
}

encode function `shouldBe` "{\"function\":{\"description\":\"Fetches a url\",\"name\":\"fetchUrl\",\"parameters\":{\"properties\":{\"url\":{\"description\":\"The url to fetch\",\"type\":\"string\"}},\"type\":\"object\"}},\"type\":\"function\"}"
1 change: 1 addition & 0 deletions ihp-openai/ihp-openai.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ test-suite tests
, neat-interpolation
, ihp-openai
, text
, aeson
hs-source-dirs: Test
default-language: Haskell2010
default-extensions:
Expand Down

0 comments on commit 25207e0

Please sign in to comment.