Skip to content

Commit f03c5fd

Browse files
liuxukun2000Satanbillxbf
authored
refined code and added some examples (#34)
* Allow loading of user-defined tools * Allow loading of user-defined tools, parallelize tasks using threads * refined code and added some examples * fix react * delete auth --------- Co-authored-by: Satan <liuxk2019@mail.sustech.edu.cn> Co-authored-by: Binfeng Xu <65674752+billxbf@users.noreply.github.com>
1 parent d875099 commit f03c5fd

File tree

14 files changed

+868
-30
lines changed

14 files changed

+868
-30
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,4 @@ cython_debug/
165165
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
166166
#.idea/
167167
/api.key
168+
/examples/test.ipynb

configs/memory.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ description: main agent leveraging OpenAI function call API.
66
prompt_template: !prompt ZeroShotVanillaPrompt
77
memory:
88
memory_type: chroma # chroma or pinecone
9-
threshold_1: 3 # first-level memory
10-
threshold_2: 3 # second-level memory
9+
threshold_1: 1 # first-level memory
10+
threshold_2: 1 # second-level memory
1111
params:
1212
index: main
1313
top_k: 2

configs/react.yaml

+1-4
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,5 @@ plugins:
3232
# - answer things about math
3333

3434

35-
## Authentication
36-
auth:
37-
OPENAI_API_KEY: !file /home/api.key
38-
WOLFRAM_ALPHA_APPID: !file /home/wolfram.key
35+
3936

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from gentopia.assembler.agent_assembler import AgentAssembler
2+
from gentopia.output import enable_log
3+
from gentopia import chat
4+
5+
if __name__ == '__main__':
6+
enable_log()
7+
assembler = AgentAssembler(file='configs/main.yaml')
8+
agent = assembler.get_agent()
9+
chat(agent, verbose=True)

examples/basic_usage/basic_usage.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from gentopia.assembler.agent_assembler import AgentAssembler
2+
from gentopia.output import enable_log
3+
from gentopia import chat
4+
5+
if __name__ == '__main__':
6+
enable_log()
7+
assembler = AgentAssembler(file='configs/mathria.yaml')
8+
agent = assembler.get_agent()
9+
chat(agent)
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Agent Config
2+
name: !env AGENT_NAME
3+
type: openai
4+
version: 0.0.1
5+
description: main agent leveraging OpenAI function call API.
6+
prompt_template: !prompt ZeroShotVanillaPrompt
7+
llm:
8+
model_name: gpt-4-0613
9+
params:
10+
temperature: 0.0
11+
top_p: 0.9
12+
repetition_penalty: 1.0
13+
max_tokens: 1024
14+
target_tasks:
15+
- anything
16+
plugins:
17+
- name: google_search
18+
- name: web_page
19+
- !include sample_agent.yaml
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Agent Config
2+
name: main
3+
type: openai
4+
version: 0.0.1
5+
description: main agent leveraging OpenAI function call API.
6+
prompt_template: !prompt ZeroShotVanillaPrompt
7+
llm:
8+
model_name: gpt-4-0613
9+
params:
10+
temperature: 0.0
11+
top_p: 0.9
12+
repetition_penalty: 1.0
13+
max_tokens: 1024
14+
target_tasks:
15+
- anything
16+
plugins:
17+
- name: google_search
18+
- name: web_page
19+
- !include sample_agent.yaml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Agent Config
2+
name: main
3+
type: openai
4+
version: 0.0.1
5+
description: main agent leveraging OpenAI function call API.
6+
prompt_template: !prompt ZeroShotVanillaPrompt
7+
llm:
8+
model_name: gpt-4-0613
9+
params:
10+
temperature: 0.0
11+
top_p: 0.9
12+
repetition_penalty: 1.0
13+
max_tokens: 1024
14+
target_tasks:
15+
- anything
16+
plugins:
17+
- name: google_search
18+
- name: web_page

examples/custom_agent/custom_agent.ipynb

+715
Large diffs are not rendered by default.

gentopia/__init__.py

+30-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,30 @@
1-
from .assembler.agent_assembler import AgentAssembler
1+
import signal
2+
3+
from .assembler.agent_assembler import AgentAssembler
4+
from .output import enable_log
5+
from .output.base_output import *
6+
from .output.console_output import ConsoleOutput
7+
8+
9+
def chat(agent, output = ConsoleOutput(), verbose = False, log_level = None, log_path = None):
10+
output.panel_print("[green]Welcome to Gentopia!", title="[blue]Gentopia")
11+
if verbose:
12+
output.panel_print(str(agent), title="[red]Agent")
13+
if log_level is not None:
14+
if not check_log():
15+
enable_log( path=log_path, log_level=log_level)
16+
def handler(signum, frame):
17+
output.print("\n[red]Bye!")
18+
exit(0)
19+
20+
signal.signal(signal.SIGINT, handler)
21+
22+
while True:
23+
output.print("[green]User: ", end="")
24+
text = input()
25+
if text:
26+
response = agent.stream(text, output=output)
27+
else:
28+
response = agent.stream(output=output)
29+
30+
output.done(_all=True)

gentopia/agent/react/agent.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ def run(self, instruction):
146146
return AgentOutput(output=response.content, cost=total_cost, token_usage=total_token)
147147

148148
def stream(self, instruction: Optional[str] = None, output: Optional[BaseOutput] = None):
149+
self.intermediate_steps.clear()
149150
total_cost = 0.0
150151
total_token = 0
151152
if output is None:
@@ -166,8 +167,6 @@ def stream(self, instruction: Optional[str] = None, output: Optional[BaseOutput]
166167
# print(i.content)
167168
output.clear()
168169

169-
170-
171170
logging.info(f"Response: {content}")
172171
self.intermediate_steps.append([self._parse_output(content), ])
173172
if isinstance(self.intermediate_steps[-1][0], AgentFinish):

gentopia/output/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import logging
22
import os
33

4+
45
def enable_log(path: str = "./agent.log", log_level: str= "info" ):
6+
if path is None:
7+
path = "./agent.log"
58
os.environ["LOG_LEVEL"] = log_level
69
os.environ["LOG_PATH"] = path
710
logging.basicConfig(level=log_level.upper(), filename=path)

gentopia/utils/util.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,24 @@
4646

4747
#TODO: get default client param model
4848
def get_default_client_param_model(model_name:str) -> BaseParamModel:
49-
return None
49+
return None
50+
51+
def print_tree(obj, indent=0):
52+
for attr in dir(obj):
53+
if not attr.startswith('_'):
54+
value = getattr(obj, attr)
55+
if not callable(value):
56+
if not isinstance(value, dict) and not isinstance(value, list):
57+
print('| ' * indent + '|--', f'{attr}: {value}')
58+
else:
59+
if not value:
60+
print('| ' * indent + '|--', f'{attr}: {value}')
61+
print('| ' * indent + '|--', f'{attr}:')
62+
if hasattr(value, '__dict__'):
63+
print_tree(value, indent + 1)
64+
elif isinstance(value, list):
65+
for item in value:
66+
print_tree(item, indent + 1)
67+
elif isinstance(value, dict):
68+
for key, item in value.items():
69+
print_tree(item, indent + 1)

test.py

+20-20
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from gentopia.model.param_model import HuggingfaceParamModel
1414
from gentopia.output import enable_log
15+
from gentopia import chat
1516
from gentopia.output.console_output import ConsoleOutput
1617

1718
import logging
@@ -47,38 +48,37 @@ def print_tree(obj, indent=0):
4748
print_tree(item, indent + 1)
4849

4950

50-
def ask(agent):
51-
out = ConsoleOutput()
52-
53-
def handler(signum, frame):
54-
out.print("\n[red]Bye!")
55-
exit(0)
56-
57-
signal.signal(signal.SIGINT, handler)
58-
while True:
59-
out.print("[green]User: ", end="")
60-
text = input()
61-
if text:
62-
response = agent.stream(text, output=out)
63-
else:
64-
response = agent.stream(output=out)
65-
66-
out.done(_all=True)
67-
print("\n")
51+
# def ask(agent, output = ConsoleOutput()):
52+
#
53+
# def handler(signum, frame):
54+
# output.print("\n[red]Bye!")
55+
# exit(0)
56+
#
57+
# signal.signal(signal.SIGINT, handler)
58+
# while True:
59+
# output.print("[green]User: ", end="")
60+
# text = input()
61+
# if text:
62+
# response = agent.stream(text, output=output)
63+
# else:
64+
# response = agent.stream(output=output)
65+
#
66+
# output.done(_all=True)
67+
# print("\n")
6868

6969

7070
if __name__ == '__main__':
7171
# mp.set_start_method('spawn', force=True)# calculate sqrt(10), and then calculate sqrt(100)
7272
# config = Config.load('main.yaml') # then tell me what is GIL in python
7373
# print(config)calculate sqrt(10),then tell me what is GIL in python, and then calculate sqrt(100)
7474
# exit(0)give me some sentences in markdown format
75-
enable_log(log_level='debug')
75+
enable_log(log_level='info')
7676

7777
assembler = AgentAssembler(file='configs/mathria.yaml')
7878

7979
# # assembler.manager = LocalLLMManager()
8080
agent = assembler.get_agent()
81-
ask(agent)
81+
chat(agent)
8282
#
8383
# print(agent)
8484
# x = " What is Trump's current age raised to the 0.43 power?"

0 commit comments

Comments
 (0)