-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathagi.R
88 lines (73 loc) · 2.32 KB
/
agi.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
source("check_deps.R")
library(openai)
library(R6)
library(jsonlite)
library(promises)
library(future)
library(later)
plan(multicore)
#plan(sequential)
source("config.R")
a <- function() {source("agi.R")}
# first time we run keep track of root directory
if(!exists("config$rootDir")) {
config$rootDir <- getwd()
}
# it's possible an error occurred while we were in
# the working directory so switch back to global root
setwd(config$rootDir)
source('color_output.R')
source("FakeAI.R")
source("Agent.R")
source("HumanAgent.R")
source("AgentManager.R")
source("CommandHandler.R")
load_initial_prompt <- function() {
fn <- paste0("data/prompts/",config$initialPrompt)
prompt <- paste(readLines(fn),collapse="")
prompt
}
initial_prompt <- load_initial_prompt()
# set the runtime dir in the config so that
# other functions can use it
config$runtimeDir <- paste0(config$runtimeDirPrefix,"/",config$aiName)
# check if a previous state exists
existingStateFile <- paste0(
config$runtimeDir,"/",config$aiName,".rds"
)
# if it does, ask to restore
restoreState <- F
if(file.exists(existingStateFile)) {
answer <- readline(paste0("Would you like to restore state for AI \"",config$aiName,"\"? (y/n) : "))
if(answer=="y") {
restoreState <- T
# we do not want to clean the working directory
# if we are restoring the state, so override this
config$cleanWorkingDir <- F
}
}
# create the command handler (this also sets up the
# working directory)
commandHandler <- CommandHandler$new(config)
# create agent manager
agentManager <- AgentManager$new(config)
# create the main human agent
humanAgent <- agentManager$spawnHuman()
# if we are restoring the state we need to load the agent
# (future: all the agents, starting with the human agent)
# and tell the manager
if(restoreState) {
# note that load puts the object back to the same
# name it was stored under
load(existingStateFile)
agentManager$restoreAgent(primaryAgent$id)
# we want to send an initial prompt which
# tells the AI to continue in this case
humanAgent$chatWithAgent(primaryAgent$id,"Your state has been restored since you were last ran, please continue.")
} else {
# otherwise we create a new agent
primaryAgent <- agentManager$newAgent()
# and send the initial prompt
humanAgent$chatWithAgent(primaryAgent$id,initial_prompt)
}
# save(primaryAgent,file=existingStateFile)