Skip to content

Conversation

joshlong
Copy link
Member

@joshlong joshlong commented Nov 12, 2024

this adds kotlin support for using the ChatClient to get typesafe responses back

val joke:Joke = cc
      .prompt("tell me a joke")
      .call()
      .entity<Joke>()
println(joke)

this is as opposed to the more verbose and less idiomatic Kotlin code without the extension function

val joke : Joke = cc
    .prompt("tell me a joke")
    .call()
    .entity(Joke::class.java)
```

* @author Josh Long
*/

inline fun <reified T> ChatClient.CallResponseSpec.entity(): T = entity(T::class.java) as T
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joshlong Could you please use ParameterizedTypeReference instead of Class in order to avoid type erasure?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sdeleuze
Copy link
Contributor

Also please add an extension for responseEntity(...) as well.

@joshlong
Copy link
Member Author

ive updated the PR accordingly. thanks for the great feedback

@markpollack markpollack assigned markpollack and unassigned sdeleuze Nov 18, 2024
@markpollack
Copy link
Member

merge in 944fb99

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants