Skip to content

Commit

Permalink
refined code and added some examples (#34)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Jul 12, 2023
1 parent d875099 commit f03c5fd
Show file tree
Hide file tree
Showing 14 changed files with 868 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,4 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
/api.key
/examples/test.ipynb
4 changes: 2 additions & 2 deletions configs/memory.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ description: main agent leveraging OpenAI function call API.
prompt_template: !prompt ZeroShotVanillaPrompt
memory:
memory_type: chroma # chroma or pinecone
threshold_1: 3 # first-level memory
threshold_2: 3 # second-level memory
threshold_1: 1 # first-level memory
threshold_2: 1 # second-level memory
params:
index: main
top_k: 2
Expand Down
5 changes: 1 addition & 4 deletions configs/react.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,5 @@ plugins:
# - answer things about math


## Authentication
auth:
OPENAI_API_KEY: !file /home/api.key
WOLFRAM_ALPHA_APPID: !file /home/wolfram.key


9 changes: 9 additions & 0 deletions examples/agent_as_plugin/agent_as_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from gentopia.assembler.agent_assembler import AgentAssembler
from gentopia.output import enable_log
from gentopia import chat

if __name__ == '__main__':
enable_log()
assembler = AgentAssembler(file='configs/main.yaml')
agent = assembler.get_agent()
chat(agent, verbose=True)
9 changes: 9 additions & 0 deletions examples/basic_usage/basic_usage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from gentopia.assembler.agent_assembler import AgentAssembler
from gentopia.output import enable_log
from gentopia import chat

if __name__ == '__main__':
enable_log()
assembler = AgentAssembler(file='configs/mathria.yaml')
agent = assembler.get_agent()
chat(agent)
19 changes: 19 additions & 0 deletions examples/custom_agent/configs/env.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Agent Config
name: !env AGENT_NAME
type: openai
version: 0.0.1
description: main agent leveraging OpenAI function call API.
prompt_template: !prompt ZeroShotVanillaPrompt
llm:
model_name: gpt-4-0613
params:
temperature: 0.0
top_p: 0.9
repetition_penalty: 1.0
max_tokens: 1024
target_tasks:
- anything
plugins:
- name: google_search
- name: web_page
- !include sample_agent.yaml
19 changes: 19 additions & 0 deletions examples/custom_agent/configs/include.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Agent Config
name: main
type: openai
version: 0.0.1
description: main agent leveraging OpenAI function call API.
prompt_template: !prompt ZeroShotVanillaPrompt
llm:
model_name: gpt-4-0613
params:
temperature: 0.0
top_p: 0.9
repetition_penalty: 1.0
max_tokens: 1024
target_tasks:
- anything
plugins:
- name: google_search
- name: web_page
- !include sample_agent.yaml
18 changes: 18 additions & 0 deletions examples/custom_agent/configs/sample_agent.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Agent Config
name: main
type: openai
version: 0.0.1
description: main agent leveraging OpenAI function call API.
prompt_template: !prompt ZeroShotVanillaPrompt
llm:
model_name: gpt-4-0613
params:
temperature: 0.0
top_p: 0.9
repetition_penalty: 1.0
max_tokens: 1024
target_tasks:
- anything
plugins:
- name: google_search
- name: web_page
715 changes: 715 additions & 0 deletions examples/custom_agent/custom_agent.ipynb

Large diffs are not rendered by default.

31 changes: 30 additions & 1 deletion gentopia/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,30 @@
from .assembler.agent_assembler import AgentAssembler
import signal

from .assembler.agent_assembler import AgentAssembler
from .output import enable_log
from .output.base_output import *
from .output.console_output import ConsoleOutput


def chat(agent, output = ConsoleOutput(), verbose = False, log_level = None, log_path = None):
output.panel_print("[green]Welcome to Gentopia!", title="[blue]Gentopia")
if verbose:
output.panel_print(str(agent), title="[red]Agent")
if log_level is not None:
if not check_log():
enable_log( path=log_path, log_level=log_level)
def handler(signum, frame):
output.print("\n[red]Bye!")
exit(0)

signal.signal(signal.SIGINT, handler)

while True:
output.print("[green]User: ", end="")
text = input()
if text:
response = agent.stream(text, output=output)
else:
response = agent.stream(output=output)

output.done(_all=True)
3 changes: 1 addition & 2 deletions gentopia/agent/react/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ def run(self, instruction):
return AgentOutput(output=response.content, cost=total_cost, token_usage=total_token)

def stream(self, instruction: Optional[str] = None, output: Optional[BaseOutput] = None):
self.intermediate_steps.clear()
total_cost = 0.0
total_token = 0
if output is None:
Expand All @@ -166,8 +167,6 @@ def stream(self, instruction: Optional[str] = None, output: Optional[BaseOutput]
# print(i.content)
output.clear()



logging.info(f"Response: {content}")
self.intermediate_steps.append([self._parse_output(content), ])
if isinstance(self.intermediate_steps[-1][0], AgentFinish):
Expand Down
3 changes: 3 additions & 0 deletions gentopia/output/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import logging
import os


def enable_log(path: str = "./agent.log", log_level: str= "info" ):
if path is None:
path = "./agent.log"
os.environ["LOG_LEVEL"] = log_level
os.environ["LOG_PATH"] = path
logging.basicConfig(level=log_level.upper(), filename=path)
Expand Down
22 changes: 21 additions & 1 deletion gentopia/utils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,24 @@

#TODO: get default client param model
def get_default_client_param_model(model_name:str) -> BaseParamModel:
return None
return None

def print_tree(obj, indent=0):
for attr in dir(obj):
if not attr.startswith('_'):
value = getattr(obj, attr)
if not callable(value):
if not isinstance(value, dict) and not isinstance(value, list):
print('| ' * indent + '|--', f'{attr}: {value}')
else:
if not value:
print('| ' * indent + '|--', f'{attr}: {value}')
print('| ' * indent + '|--', f'{attr}:')
if hasattr(value, '__dict__'):
print_tree(value, indent + 1)
elif isinstance(value, list):
for item in value:
print_tree(item, indent + 1)
elif isinstance(value, dict):
for key, item in value.items():
print_tree(item, indent + 1)
40 changes: 20 additions & 20 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from gentopia.model.param_model import HuggingfaceParamModel
from gentopia.output import enable_log
from gentopia import chat
from gentopia.output.console_output import ConsoleOutput

import logging
Expand Down Expand Up @@ -47,38 +48,37 @@ def print_tree(obj, indent=0):
print_tree(item, indent + 1)


def ask(agent):
out = ConsoleOutput()

def handler(signum, frame):
out.print("\n[red]Bye!")
exit(0)

signal.signal(signal.SIGINT, handler)
while True:
out.print("[green]User: ", end="")
text = input()
if text:
response = agent.stream(text, output=out)
else:
response = agent.stream(output=out)

out.done(_all=True)
print("\n")
# def ask(agent, output = ConsoleOutput()):
#
# def handler(signum, frame):
# output.print("\n[red]Bye!")
# exit(0)
#
# signal.signal(signal.SIGINT, handler)
# while True:
# output.print("[green]User: ", end="")
# text = input()
# if text:
# response = agent.stream(text, output=output)
# else:
# response = agent.stream(output=output)
#
# output.done(_all=True)
# print("\n")


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

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

# # assembler.manager = LocalLLMManager()
agent = assembler.get_agent()
ask(agent)
chat(agent)
#
# print(agent)
# x = " What is Trump's current age raised to the 0.43 power?"
Expand Down

0 comments on commit f03c5fd

Please # to comment.