-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathagent.nu
More file actions
118 lines (102 loc) · 3.24 KB
/
agent.nu
File metadata and controls
118 lines (102 loc) · 3.24 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# Agent
use agent/agents.nu [ run_agent, agent ]
use common/utils.nu *
def think_code [$query] {
init_logger
"## Calling think_code" | log
$query | log
let all_agents = ( ls **/agents.json | get name | reduce --fold {} { |e, acc| ( open $e | merge $acc) } )
mut agent = agent $all_agents.cbes_oai
let answer = ( $agent | run_agent $query )
$answer
}
def run [$query] {
let s = $in
init_logger
"## Calling run" | log
let all_agents = ( ls **/agents.json | get name | reduce --fold {} { |e, acc| ( open $e | merge $acc) } )
mut agent = agent $all_agents.cbes_oai
mut agentPrompt = "";
if ( $s != null ) {
$agentPrompt = ( [$query, " ", $s] | str join )
} else {
$agentPrompt = $query
}
let answer = $agent | run_agent $agentPrompt
$answer
}
def agent_run [agent_name, query] {
let s = $in
init_logger
"## Calling run" | log
echo ( ls agents.json )
let all_agents = ( ls **/agents.json | get name | reduce --fold {} { |e, acc| ( open $e | merge $acc) } )
mut agent = agent ( $all_agents | get $agent_name )
mut agentPrompt = "";
if ( $s != null ) {
$agentPrompt = ( [$query, " ", $s] | str join )
} else {
$agentPrompt = $query
}
let answer = $agent | run_agent $agentPrompt
$answer
}
def agent_as_tools [query] {
let s = $in
init_logger
"## Calling run" | log
mut agent = agent {
"runtime" : "openai",
"model":"gpt-4o-mini",
"prompt":"agent_as_tools.txt",
"model_tools":"agent_as_tools.json",
"tool_functions":"functions.nu",
"description":"An agent calling other agents as tools",
"options" : {
"stream": false,
"temperature": 0
}
}
mut agentPrompt = "";
if ( $s != null ) {
$agentPrompt = ( [$query, " ", $s] | str join )
} else {
$agentPrompt = $query
}
let answer = $agent | run_agent $agentPrompt
$answer
}
def function_from_json [jsonPath] {
let agents = open $jsonPath
let tuples = $agents | transpose k v | each { |a|
let tool = {
"type": "function",
"function": {
"name": $a.k,
"description": $a.v.description,
"parameters": {
"type": "object",
"properties": {
"query": {
"description": "the question you want to ask the agent",
"type": "string"
}
},
"required": [
"query"
]
}
}
}
let function = $" def ($a.k) [query] {\n let result_tool = \( cbsh -c \( $\" source agent.nu; agent_run \"($a.k)\" \"\($query\)\" \" \) \) | complete \n if \( $result_tool.exit_code == 0 \) { \n return $result_tool.stdout \n } else {\n print $result_tool.stderr \n return $result_tool \n } \n}"
[$tool, $function]
}
mut tools = []
mut functions = []
for t in $tuples {
$tools = $tools | append $t.0
$functions = $functions | append $t.1
}
$functions | to text | save -f functions.nu
$tools | to json | save -f agent_as_tools.json
}