Skip to content

Commit

Permalink
Simpligy reAct agent and add Streaming (#468)
Browse files Browse the repository at this point in the history
  • Loading branch information
raulraja authored Sep 29, 2023
1 parent adb90be commit ff0dd01
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 207 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.xebia.functional.xef.conversation

import com.xebia.functional.xef.AIError
import com.xebia.functional.xef.llm.Chat
import com.xebia.functional.xef.llm.ChatWithFunctions
import com.xebia.functional.xef.llm.Images
import com.xebia.functional.xef.llm.*
import com.xebia.functional.xef.llm.models.functions.CFunction
import com.xebia.functional.xef.llm.models.images.ImagesGenerationResponse
import com.xebia.functional.xef.prompt.Prompt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ import kotlinx.serialization.SerialInfo
@SerialInfo
@Repeatable
@Retention(AnnotationRetention.BINARY)
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.FIELD)
@Target(AnnotationTarget.CLASS, AnnotationTarget.PROPERTY, AnnotationTarget.FIELD)
actual annotation class Description actual constructor(actual val value: String)

@SerialInfo
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.CLASS, AnnotationTarget.PROPERTY, AnnotationTarget.FIELD)
@Target(
AnnotationTarget.CLASS,
AnnotationTarget.CLASS,
AnnotationTarget.PROPERTY,
AnnotationTarget.FIELD
)
actual annotation class Descriptive actual constructor(actual val value: String)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import kotlinx.serialization.SerialInfo
*/
@SerialInfo
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.FIELD)
@Target(AnnotationTarget.CLASS, AnnotationTarget.PROPERTY, AnnotationTarget.FIELD)
actual annotation class Description actual constructor(actual val value: String)

@SerialInfo
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.xebia.functional.xef.conversation.reasoning

import com.xebia.functional.xef.conversation.llm.openai.OpenAI
import com.xebia.functional.xef.prompt.Prompt
import com.xebia.functional.xef.prompt.templates.user
import com.xebia.functional.xef.reasoning.code.Code
import com.xebia.functional.xef.reasoning.tools.ReActAgent
import kotlinx.coroutines.flow.collect

suspend fun main() {
OpenAI.conversation {
Expand All @@ -23,12 +22,15 @@ suspend fun main() {
)
val prDescription =
agent.run(
Prompt {
+user(
"Create a PR description for https://patch-diff.githubusercontent.com/raw/xebia-functional/xef/pull/283.diff"
)
}
"Create a PR description for https://patch-diff.githubusercontent.com/raw/xebia-functional/xef/pull/283.diff"
)
println(prDescription)
prDescription.collect {
when (it) {
is ReActAgent.Result.Log -> println(it.message)
is ReActAgent.Result.ToolResult -> println("${it.tool}(${it.input}) = ${it.result}")
is ReActAgent.Result.Finish -> println(it.result)
is ReActAgent.Result.MaxIterationsReached -> println(it.message)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,33 @@
package com.xebia.functional.xef.conversation.reasoning

import com.xebia.functional.xef.conversation.llm.openai.OpenAI
import com.xebia.functional.xef.prompt.Prompt
import com.xebia.functional.xef.prompt.templates.user
import com.xebia.functional.xef.reasoning.serpapi.Search
import com.xebia.functional.xef.reasoning.tools.LLMTool
import com.xebia.functional.xef.reasoning.tools.ReActAgent

suspend fun main() {
OpenAI.conversation {
val model = OpenAI().DEFAULT_CHAT
val serialization = OpenAI().DEFAULT_SERIALIZATION
val math =
LLMTool.create(
name = "Calculator",
description =
"Perform math operations and calculations processing them with an LLM model. The tool input is a simple string containing the operation to solve expressed in numbers and math symbols.",
model = model,
scope = this
)
val search = Search(model = model, scope = this)

val reActAgent =
ReActAgent(
model = serialization,
scope = this,
tools =
listOf(
search,
math,
),
tools = listOf(search),
)
val result =
reActAgent.run(
Prompt {
+user(
"Find and multiply the number of Leonardo di Caprio's girlfriends by the number of Metallica albums"
)
}
"Find and multiply the number of days in a week by the number of months in a year. Then subtract the number of letters in the English alphabet from the result. Display the result of all operations as a single number"
)
println(result)

result.collect {
when (it) {
is ReActAgent.Result.Log -> println(it.message)
is ReActAgent.Result.ToolResult -> println("${it.tool}(${it.input}) = ${it.result}")
is ReActAgent.Result.Finish -> println(it.result)
is ReActAgent.Result.MaxIterationsReached -> println(it.message)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.xebia.functional.xef.conversation.reasoning

import com.xebia.functional.xef.conversation.llm.openai.OpenAI
import com.xebia.functional.xef.prompt.Prompt
import com.xebia.functional.xef.prompt.templates.user
import com.xebia.functional.xef.reasoning.tools.LLMTool
import com.xebia.functional.xef.reasoning.tools.ReActAgent
import com.xebia.functional.xef.reasoning.wikipedia.*
import com.xebia.functional.xef.reasoning.wikipedia.SearchWikipedia
import com.xebia.functional.xef.reasoning.wikipedia.SearchWikipediaByPageId
import com.xebia.functional.xef.reasoning.wikipedia.SearchWikipediaByTitle

suspend fun main() {
OpenAI.conversation {
Expand All @@ -14,8 +14,7 @@ suspend fun main() {
val math =
LLMTool.create(
name = "Calculator",
description =
"Perform math operations and calculations processing them with an LLM model. The tool input is a simple string containing the operation to solve expressed in numbers and math symbols.",
description = "Math operations expressed in numbers and math symbols",
model = model,
scope = this
)
Expand All @@ -31,10 +30,15 @@ suspend fun main() {
)
val result =
reActAgent.run(
Prompt {
+user("Find and multiply the number of human bones by the number of Metallica albums")
}
"Find and multiply the number of human bones by the number of Metallica albums"
)
println(result)
result.collect {
when (it) {
is ReActAgent.Result.Log -> println(it.message)
is ReActAgent.Result.ToolResult -> println("${it.tool}(${it.input}) = ${it.result}")
is ReActAgent.Result.Finish -> println(it.result)
is ReActAgent.Result.MaxIterationsReached -> println(it.message)
}
}
}
}
Loading

0 comments on commit ff0dd01

Please sign in to comment.