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

Simplify ReAct agent and add streaming #468

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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