How to Create your own LLM Agent from Scratch: A Step-by-Step Guide

Gathnex
6 min readNov 19, 2023

In this Article , we are going to build a LLM Agent from scratch using Python✅. No Langchain❌, No Llama Index❌, etc.

What are Agents?

LLM agents are are programs that use large language models to decide how and when to use tools to complete tasks.

Why are LLM agents essential for tasks? LLMs are powerful; however, they may not be able to perform certain tasks. Let’s explore the reasons why.

Large Language Models are powerful neural networks trained on massive amounts of text data. They can generate text, translate languages, write different kinds of creative content, and answer your questions in an informative way but not for doing a tasks.

The core idea of agents is to use a language model to choose a sequence of actions to take. In chains, a sequence of actions is hardcoded (in code). In agents, a language model is used as a reasoning engine to determine which actions to take and in which order.

In simpler terms, LLMs are like the engine that powers a car, while LLM agents are the car itself, with additional features that make it useful for real-world tasks.

Agents and Tools

To use agents, we require three things:

  • A base LLM,
  • A tool that we will be interacting with,
  • An agent to control the interaction.

Agent Implementation

!pip install -q openai py_expression_eval google-api-python-client
from openai import OpenAI
from googleapiclient.discovery import build
from py_expression_eval import Parser
import re, time, os
  • Set up a Custom Search Engine and Google API Key, following these instructions.
  • Get an API Key and Custom Search Engine ID from the previous step, and set them as environment variables GOOGLE_API_KEY and GOOGLE_CSE_ID respectively.
client = OpenAI(api_key='Openai_api_key')
os.environ["GOOGLE_CSE_ID"] = "Google CSE ID"
os.environ["GOOGLE_API_KEY"] = "Google API Key"

Tools

Search: Conduct a search engine query using Google’s official custom search engine. You can send 100 requests per day in the free tier.

Calculator: I use py_expression_eval as a calculator (good balance between being able to run complex math expressions, without many of the risks of trying to pull in a full Python REPL/eval).

We have used two tools for the demo. You can use whatever tool you want as per your requirements.

#Google search engine
def search(search_term):
search_result = ""
service = build("customsearch", "v1", developerKey=os.environ.get("GOOGLE_API_KEY"))
res = service.cse().list(q=search_term, cx=os.environ.get("GOOGLE_CSE_ID"), num = 10).execute()
for result in res['items']:
search_result = search_result + result['snippet']
return search_result

#Calculator
parser = Parser()
def calculator(str):
return parser.parse(str).evaluate({})

Prompt

First, let’s set up the system prompt.

System_prompt = """
Answer the following questions and obey the following commands as best you can.

You have access to the following tools:

Search: Search: useful for when you need to answer questions about current events. You should ask targeted questions.
Calculator: Useful for when you need to answer questions about math. Use python code, eg: 2 + 2
Response To Human: When you need to respond to the human you are talking to.

You will receive a message from the human, then you should start a loop and do one of two things

Option 1: You use a tool to answer the question.
For this, you should use the following format:
Thought: you should always think about what to do
Action: the action to take, should be one of [Search, Calculator]
Action Input: "the input to the action, to be sent to the tool"

After this, the human will respond with an observation, and you will continue.

Option 2: You respond to the human.
For this, you should use the following format:
Action: Response To Human
Action Input: "your response to the human, summarizing what you did and what you learned"

Begin!
"""

So what the above loop can do ?

Telling the model that it will be run in the loop. In that loop, the LLM has two options: it can either ‘use a tool,’ giving that tool an input, or it can respond to the human. We give the model a list of the tools and a description of when/how to use each one. The thought-action pattern creates a ‘chain of thought,’ telling the model to think about what it’s doing.

We used the GPT-4 model for this demo, but you can also use open source models like Llama, Mistral, Zephyr, etc.

def Stream_agent(prompt):
messages = [
{ "role": "system", "content": System_prompt },
{ "role": "user", "content": prompt },
]
def extract_action_and_input(text):
action_pattern = r"Action: (.+?)\n"
input_pattern = r"Action Input: \"(.+?)\""
action = re.findall(action_pattern, text)
action_input = re.findall(input_pattern, text)
return action, action_input
while True:
response = client.chat.completions.create(
model="gpt-4",
messages=messages,
temperature=0,
top_p=1,)
response_text = response.choices[0].message.content
print(response_text)
#To prevent the Rate Limit error for free-tier users, we need to decrease the number of requests/minute.
time.sleep(20)
action, action_input = extract_action_and_input(response_text)
if action[-1] == "Search":
tool = search
elif action[-1] == "Calculator":
tool = calculator
elif action[-1] == "Response To Human":
print(f"Response: {action_input[-1]}")
break
observation = tool(action_input[-1])
print("Observation: ", observation)
messages.extend([
{ "role": "system", "content": response_text },
{ "role": "user", "content": f"Observation: {observation}" },
])

Let’s test our agent.

Stream_agent("who is new openai board members")
Output
Thought: I need to find the current information about the new board members of OpenAI.
Action: Search
Action Input: "New OpenAI board members"
Observation: 7 days ago ... I am returning to OpenAI as CEO. Mira will return to her role as CTO. The new initial board will consist of Bret Taylor (Chair), Larry Summers, ...Nov 21, 2023 ... Altman were jettisoned from the board, whose members now include Bret Taylor, an early Facebook officer and former co-chief executive of ...OpenAI is governed by the board of the OpenAI Nonprofit, currently comprised of Independent Directors Bret Taylor (Chair), Larry Summers, and Adam D'Angelo.Nov 22, 2023 ... Here are the newest members of OpenAI's board · Bret Taylor, board chair · Larry Summers · Adam D'Angelo.Nov 22, 2023 ... The company will have an initial board consisting of Bret Taylor, the former chair of Twitter's board before its takeover by Elon Musk; Larry ...Nov 23, 2023 ... Who are OpenAI's new board members as Sam Altman returns? ... Nov 22 (Reuters) - ChatGPT-maker OpenAI on Tuesday said it reached an agreement for ...Nov 22, 2023 ... https://www.bloomberg.com/news/articles/2023-11-22/sam-altman-to-return-as-openai-ceo-with-a-new-board What are your thoughts?6 days ago ... Who is Bret Taylor? New OpenAI Board Member is Former Salesforce CEO · Bret Taylor is a 43-year-old American computer programmer, entrepreneur ...The new initial board included former Salesforce co-CEO Bret Taylor as chairman. OpenAI also announced that Microsoft would have a non-voting board seat at ...Nov 22, 2023 ... The company that developed ChatGPT has added new members to its board as it welcomes Sam Altman back as CEO · Sam Altman is the value in OpenAI: ...
Action: Response To Human
Action Input: "The new board members of OpenAI include Bret Taylor, who is serving as the Chair, Larry Summers, and Adam D'Angelo."
Response: The new board members of OpenAI include Bret Taylor, who is serving as the Chair, Larry Summers, and Adam D'Angelo.

Resourse : https://github.com/gathnexadmin/LLM_Agent/tree/main

Conclusion

This is the basic idea of an LLM agent, which is built based on this paper. The output was really good when compared to Langchain and Llamaindex agents.

Reference

  1. https://arxiv.org/abs/2210.03629

--

--

Gathnex

🤖 Exploring Generative AI & LLM. Join the Gathnex community for cutting-edge discussions and updates! LinkedIn : https://www.linkedin.com/company/gathnex/ 🌟