Skip to content

extension function for Chatclient.CalLResponseSpec.entity(Class<?>) a… #1729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

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