Skip to content

Commit

Permalink
Remove unnecessary state passing and added GPT-4o (#11)
Browse files Browse the repository at this point in the history
* Remove unnecessary state passing and added gpt-4o

* Action order

* Fix dependency + log duration

---------

Co-authored-by: Niels Simonides <[email protected]>
  • Loading branch information
nsmnds and Niels Simonides authored May 15, 2024
1 parent 26aff6f commit 4fbd7fe
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 deletions.
3 changes: 1 addition & 2 deletions src/core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ kotlin {
val commonMain by getting {
dependencies {
api(libs.coroutines.core)
implementation(libs.serialization.json)
implementation(libs.kotlinx.datetime)
api(libs.kotlinx.datetime)
api(libs.serialization.json)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ suspend fun Agent.start(): Run =

private suspend fun executeAction(action: Action): Pair<State, FinishedOrStuck> =
when (action) {
is Initialize -> executeAction(action.process(action.state))
is SendModelRequest -> executeAction(action.process(action.state))
is ProcessModelResponse -> executeAction(action.process(action.state))
is ExecuteTools -> executeAction(action.process(action.state))
is Finished -> action.process(action.state)
is Initialize -> executeAction(action.process())
is SendModelRequest -> executeAction(action.process())
is ProcessModelResponse -> executeAction(action.process())
is ExecuteTools -> executeAction(action.process())
is Finished -> action.process()
}

private suspend fun Initialize.process(state: State): Action {
private suspend fun Initialize.process(): Action {
state.addMessages(initializeStartMessages(agent))
return SendModelRequest(state, agent)
}

private suspend fun ProcessModelResponse.process(state: State): Action =
private suspend fun ProcessModelResponse.process(): Action =
when (responseMessage) {
is Message.ToolCalls -> ExecuteTools(state, agent, responseMessage.toolCalls)
else -> {
Expand All @@ -56,15 +56,15 @@ private suspend fun ProcessModelResponse.process(state: State): Action =
}
}

private suspend fun SendModelRequest.process(state: State): ProcessModelResponse {
private suspend fun SendModelRequest.process(): ProcessModelResponse {
val message = agent.sendModelRequest(state).message
state.addMessage(message)
return ProcessModelResponse(state, agent, message)
}

private fun Finished.process(state: State) = state to finishedOrStuck
private fun Finished.process() = state to finishedOrStuck

private suspend fun ExecuteTools.process(state: State): Action {
private suspend fun ExecuteTools.process(): Action {
val toolResults = executeToolCalls(agent, toolCalls)
val finished = toolResults.filterIsInstance<ToolExecutionResult.FinishedToolResult>().firstOrNull()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import kotlinx.serialization.json.JsonObject
suspend fun runAdministrativeAgentExample(openAIAPIKey: String) {
val run =
agent {
openAIModel(openAIAPIKey, OpenAIModelIdentifier.GPT4Turbo)
openAIModel(openAIAPIKey, OpenAIModelIdentifier.GPT4O)
task("Retrieve all employees to inspect their hour status") {
addInstruction(
"For all employees: only when the employee has not yet received 5 reminders to completed his hours send him a reminder through Signal. Base the tone of the message on the number of reminders sent",
Expand All @@ -42,7 +42,9 @@ suspend fun runAdministrativeAgentExample(openAIAPIKey: String) {
when (run.result.reason) {
FinishedAllTasks -> "Hours inspected successfully"
ImStuck -> "Agent is stuck and could not complete task"
}.also(::println)
}.also {
println("$it took ${run.finishedAt - run.startedAt}")
}
}

val getAllEmployeesOverviewTool =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ suspend fun runOpenAPIAgent(
hackerNewsOpenAPISpec: String,
) {
agent {
openAIModel(openAIAPIKey, OpenAIModelIdentifier.GPT4Turbo)
openAIModel(openAIAPIKey, OpenAIModelIdentifier.GPT4O)
task("Send Hacker News stories about AI") {
addInstruction("Retrieve the top 10 Hacker News stories")
addInstruction("Send stories, if any, about AI to [email protected]")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import community.flock.aigentic.mapper.toModelResponse
import community.flock.aigentic.request.createChatCompletionsRequest
import kotlin.time.Duration.Companion.seconds

@Suppress("ktlint")
@Suppress("ktlint:standard:class-naming")
sealed class OpenAIModelIdentifier(
val stringValue: String
val stringValue: String,
) : ModelIdentifier {
data object GPT4O : OpenAIModelIdentifier("gpt-4o")
data object GPT4Turbo : OpenAIModelIdentifier("gpt-4-turbo")
data object GPT3_5Turbo : OpenAIModelIdentifier("gpt-3.5-turbo")
}
Expand Down

0 comments on commit 4fbd7fe

Please sign in to comment.