How to create agents?


The quick start on how to create your own agent.

Published on June 01, 2023 by TheDevsLot

create agent

6 min READ

Quick Start

The best way to get started with Gentopia is to go through our short demo tutorials.

Grab one of the following agent template and store it in agent.yaml, then jump to Assembling Agents section. See Agent Components for custom configurations.

Vanilla Agent

vanilla agents are plain LLMs instances without any plugin. It is useful if you simply want to create context shift, such as granting identities or instructing an LLM to behave in a particular way.

# Vanilla agent template
name: vanilla_template
version: 0.0.1
type: vanilla
description: A plain gpt-4 LLM.
target_tasks:
  - anything to do with an LLM 
llm:
  model_name: gpt-4
  params:
    temperature: 0.0
    top_p: 0.9
    repetition_penalty: 1.0
    max_tokens: 1024
prompt_template: !prompt VanillaPrompt

ReAct Agent

react agents are by far the most common type of agents. It prompts LLMs to respond in multiple “Thought -> Action -> Observation” cycles. A stop word “Observation: “ is used to detect plugin calls, and “Final Answer: “ is used to detect cycle end. It prompts LLM at each tool-call with all previous history stored in a “scratchpad”.

# ReAct Agent Template
name: react_template
version: 0.0.1
type: react
description: A react agent capable of online web search and browsing.
target_tasks:
  - web search
  - web browsing
llm:
  model_name: gpt-4
  params:
    temperature: 0.0
    top_p: 0.9
    repetition_penalty: 1.0
    max_tokens: 1024
prompt_template: !prompt ZeroShotReactPrompt
plugins:
  - name: google_search
  - name: web_page

OpenAI Function Agent

openai agents are powered by OpenAI function-call API. It uses a similar sequential style as react agents, interleaving LLM response and tool-calls. According to OpenAI, it is syntax fine-tuned to generate function call message.

# OpenAI agent template
name: openai_template
version: 0.0.1
type: openai
description: An openai agent capable of online web search and browsing.
target_tasks:
  - web search
  - web browsing
llm:
  model_name: gpt-4-0613
  params:
    temperature: 0.0
    top_p: 0.9
    repetition_penalty: 1.0
    max_tokens: 1024
prompt_template: !prompt VanillaPrompt
plugins:
  - name: google_search
  - name: web_page

ReWOO Agent

rewoo agents are designed to improve efficiency of sequential agent types by prompting LLM to plan ahead and use hypothetical tool responses to complete the reasoning process. Then the algorithm parses that “blueprint” into a Directed Acyclic Graph (DAG) and executes it in parallel with topological order.

# ReWOO agent template
name: rewoo_template
version: 0.0.1
type: rewoo
description: A rewoo agent capable of online web search and browsing.
target_tasks:
  - web search
  - web browsing
llm:
  Planner:
    model_name: gpt-4
    params:
      temperature: 0.0
      top_p: 0.9
      repetition_penalty: 1.0
      max_tokens: 1024
  Solver:
    model_name: gpt-3.5-turbo
    params:
      temperature: 0.0
      top_p: 0.9
      repetition_penalty: 1.0
      max_tokens: 1024
prompt_template:
  Planner: !prompt ZeroShotPlannerPrompt
  Solver: !prompt ZeroShotSolverPrompt
plugins:
  - name: google_search
  - name: web_page

OpenAI Function Agent (with Memory)

openai_memory agents are specialized openai agents enabling memory retrieval from vector database. Abusing index retrieval is highly expensive and lagging, so we generally don’t encourage it unless you expect unavoidably long context. After tons of tuning, we present this novel 2-level memory architecture for fast and robust memmory retrieval.

# OpenAI agent template
name: openai_memory_template
version: 0.0.1
type: openai_memory
description: An openai memory agent capable of online web search and browsing in unlimited context length.
target_tasks:
  - web search
  - web browsing
llm:
  model_name: gpt-4-0613
  params:
    temperature: 0.0
    top_p: 0.9
    repetition_penalty: 1.0
    max_tokens: 1024
prompt_template: !prompt VanillaPrompt
plugins:
  - name: google_search
  - name: web_page
memory:
  memory_type: chroma
  conversation_threshold: 2      # first-level memory
  reasoning_threshold: 2         # second-level memory
  params:
    index: main
    top_k: 2

Howto: Assemble your Agent

While it’s recommended to use Gentopia in GentPool, you may follow instructions below to run in your own python environment:

Suppose you have saved your agent config to my_agent.yaml, make sure you have set up .env environmental keys similar to here. Then to chat with your agent in our built-in streaming CLI, run:

import dotenv
from gentopia import chat
from gentopia.assembler.agent_assembler import AgentAssembler

dotenv.load_dotenv(".env")  # load environmental keys
agent = AgentAssembler(file='my_agent.yaml').get_agent()

chat(agent) 

To run that agent in your own app

# AgentOutput contains response content, running cost and token usgae.
agent.run("Your instruction or message.")

To stream output in your app, inherit BaseOutput with your own output handler

oh = Your_OutputHandler()
assert isinstance(oh, BaseOutput)
# Response content streams into your output handler.
agent.stream("Your instruction or message.", output_handler=oh)