xebia-functional / xef

Building applications with LLMs through composability, in Kotlin, Scala, ...
https://xef.ai
Apache License 2.0
174 stars 15 forks source link

Parallel tool calls + abstract away serialization framework #758

Closed raulraja closed 3 months ago

raulraja commented 3 months ago
  1. Support for parallel tool calls, execution, and submission to the model in the chat completions API.
  2. Abstract away serialization framework and kotlin serializer code outside AI type.
package com.xebia.functional.xef.dsl.chat

import com.xebia.functional.xef.AI
import com.xebia.functional.xef.AIConfig
import com.xebia.functional.xef.AIEvent
import com.xebia.functional.xef.Tool
import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.Serializable

fun ballLocationInfo(input: String): String =
  "The ball is in the 47 cup."

fun lookUnderCupNumber(cupNumber: Int): String =
  if (cupNumber == 47) "You found the ball and it's red and shiny."
  else "Nothing found under cup number $cupNumber. Use the ballLocationInfo tool to find which cup the ball is under."

@Serializable
data class RevealedSecret(val secret: String)

suspend fun main() {
  val revealedSecret: Flow<AIEvent<RevealedSecret>> = AI(
    prompt = "Where is the ball? use the available tools to find out.",
    config = AIConfig(
      tools = listOf(
        Tool.toolOf(::ballLocationInfo),
        Tool.toolOf(::lookUnderCupNumber)
      )
    )
  )
  revealedSecret.collect {
    when (it) {
      //emoji for start is: 🚀
      AIEvent.Start -> println("🚀 Starting...")
      is AIEvent.Result -> println("🎉 ${it.value.secret}")
      is AIEvent.ToolExecutionRequest -> println("🔧 Executing tool: ${it.tool.function.name} with input: ${it.input}")
      is AIEvent.ToolExecutionResponse -> println("🔨 Tool response: ${it.tool.function.name} resulted in: ${it.output}")
      is AIEvent.Stop -> {
        println("🛑 Stopping...")
        println("📊 Usage: ${it.usage}")
      }
    }
  }
}

🚀 Starting... 🔧 Executing tool: lookUnderCupNumber with input: {"argument": 2} 🔧 Executing tool: ballLocationInfo with input: {"argument": "where is the ball?"} 🔧 Executing tool: lookUnderCupNumber with input: {"argument": 1} 🔧 Executing tool: lookUnderCupNumber with input: {"argument": 3} 🔨 Tool response: ballLocationInfo resulted in: The ball is in the 47 cup. 🔨 Tool response: lookUnderCupNumber resulted in: Nothing found under cup number 3. Use the ballLocationInfo tool to find which cup the ball is under. 🔨 Tool response: lookUnderCupNumber resulted in: Nothing found under cup number 1. Use the ballLocationInfo tool to find which cup the ball is under. 🔨 Tool response: lookUnderCupNumber resulted in: Nothing found under cup number 2. Use the ballLocationInfo tool to find which cup the ball is under. 🔧 Executing tool: lookUnderCupNumber with input: {"argument":47} 🔨 Tool response: lookUnderCupNumber resulted in: You found the ball and it's red and shiny. 🎉 The ball is in cup number 47, and it's red and shiny. 🛑 Stopping... 📊 Usage: Usage(llmCalls=3, toolCalls=6, inputTokens=688, outputTokens=120, totalTokens=808)

Usage is computed for the multiple rounds