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

Parallel tool calls + abstract away serialization framework #758

Merged
merged 8 commits into from
Jun 18, 2024

Conversation

raulraja
Copy link
Contributor

@raulraja raulraja commented Jun 11, 2024

  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

@raulraja raulraja marked this pull request as ready for review June 12, 2024 12:46
Copy link
Contributor

@javipacheco javipacheco left a comment

Choose a reason for hiding this comment

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

LGTM! Great job!

I have tested the code and apps locally and everything works as expected

@raulraja raulraja merged commit 7f52a44 into main Jun 18, 2024
6 checks passed
@raulraja raulraja deleted the parallel-tool-calls branch June 18, 2024 07:21
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.

2 participants