Skip to content
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

Use @Description on properties to enhance serialized prompt results. #222

Merged
merged 4 commits into from
Jul 3, 2023

Conversation

raulraja
Copy link
Contributor

@raulraja raulraja commented Jul 3, 2023

This PR introduces a new @description annotation in the com.xebia.functional.xef.auto package, which is used to enhance the context of a property and improve the deserialization results. The @description annotation can be applied to properties or fields and accepts an array of strings as the description.

In the JsonSchema.kt file, the @jsonschema annotation has been updated to remove the nested @description annotation. Instead, it now imports the @description annotation from the com.xebia.functional.xef.auto package.

package com.xebia.functional.xef.auto.fields

import com.xebia.functional.xef.auto.Description
import com.xebia.functional.xef.auto.ai
import com.xebia.functional.xef.auto.llm.openai.getOrElse
import com.xebia.functional.xef.auto.llm.openai.prompt
import kotlinx.serialization.Serializable

@Serializable
data class Book(
    @Description(["The title of the book."])
    val title: String,
    @Description(["The author of the book."])
    val author: String,
    @Description(["A 50 word summary of the book."])
    val summary: String
)

suspend fun main() {
    ai {
        val toKillAMockingbird: Book = prompt("To Kill a Mockingbird by Harper Lee")
        println(toKillAMockingbird)
    }.getOrElse { println(it) }
}

Produces a more accurate summary given the field description.

Book(
  title=To Kill a Mockingbird, 
  author=Harper Lee, 
  summary=To Kill a Mockingbird is a classic novel written by Harper Lee. Set in the 1930s in the fictional town of Maycomb, Alabama, the story follows Scout Finch, a young girl growing up in a racially divided community. Through Scout's innocent eyes, the novel explores themes of racism, injustice, and the loss of innocence. With its memorable characters and powerful portrayal of social issues, To Kill a Mockingbird continues to be a widely studied and beloved book.
  )

This functionality was already available but hidden in the JsonSchema.Description annotation.

This PR additionally provides a prompt function that you can use without text or messages, which is convenient when you want the LLM to just reply in terms of the model descriptions.

package com.xebia.functional.xef.auto.fields

import com.xebia.functional.xef.agents.search
import com.xebia.functional.xef.auto.Description
import com.xebia.functional.xef.auto.ai
import com.xebia.functional.xef.auto.llm.openai.getOrElse
import com.xebia.functional.xef.auto.llm.openai.prompt
import kotlinx.serialization.Serializable
import java.time.LocalDate

@Serializable
data class NewsSummary(
    @Description(["A title for the relevant news article."])
    val title: String,
    @Description(["A description of the relevant news article"])
    val author: String,
    @Description(["A 50 word summary of the article."])
    val summary: String
)

@Serializable
data class NewsItems(
    @Description(["A list of news items about the context"])
    val items: List<NewsSummary>
)


suspend fun main() {
    ai {
        contextScope(search("Covid news on ${LocalDate.now()}")) {
            val news: NewsItems = prompt()
            println(news)
        }

    }.getOrElse { println(it) }
}

@raulraja
Copy link
Contributor Author

raulraja commented Jul 3, 2023

@xebia-functional/team-ai

@raulraja raulraja merged commit 1a616f8 into main Jul 3, 2023
1 check passed
@raulraja raulraja deleted the serialization-annotations branch July 3, 2023 22:29
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.

None yet

2 participants