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

Gathnex
6 min readNov 19

--

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 langchain py_expression_eval google-api-python-client
from openai import OpenAI
from langchain.utilities import GoogleSearchAPIWrapper
from langchain.tools import Tool
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. Here, we used the LangChain Google Search Engine tool for demo purposes. If you don’t want to use any other tool or prefer to create your own, refer to this document. In this code, you can create your own Google search tool by yourself.

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.

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

#Google search engine
search = GoogleSearchAPIWrapper()
goog = Tool(
name="Google Search",
description="Search Google for recent results.",
func=search.run
)

def search(str):
return goog(str)

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,
max_tokens=1000,
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 current CEO of OpenAI")
Output
Thought: I need to find out who the current CEO of OpenAI is.
Action: Search
Action Input: "current CEO of OpenAI"
Observation: 2 days ago ... Mira Murati: Who is the new OpenAI interim CEO? Murati now heads the company ... Now, Musk can say he also used to employ OpenAI's current CEO. ^ Mok, Aaron (September 25, 2023). "OpenAI CEO Sam Altman says he worked so hard on building his first startup with his ex-boyfriend that he got scurvy". 2 days ago ... Chief technology officer Mira Murati appointed interim CEO to lead OpenAI; Sam Altman departs the company. CEO and co-founder: Sam Altman, former president of the startup accelerator Y Combinator (2015–2023); President and co-founder: Greg Brockman, former CTO, 3rd ... OpenAI is an AI research and deployment company. Our mission is to ensure that artificial general intelligence benefits all of humanity. Jun 21, 2023 ... You ever watch Star Trek?” Sam Altman, the CEO who has become the most visible face of the current artificial-intelligence boom, has just called ... the quality of airbnb product launches sets the current high water mark in the tech industry: ... i am pro-regulation on frontier systems, which is what openai ... Mar 16, 2023 ... OpenAI CEO Sam Altman says AI will reshape society, acknowledges risks ... This feature is currently only accessible to a small set of users ... 1 day ago ... For years, Mira Murati has worked behind the scenes at OpenAI, overseeing the development and delivery of revolutionary products such as ... Mar 19, 2023 ... Sam Altman, CEO of OpenAI, the co that created ChatGPT, believes that artificial intelligence technology will reshape society as we know it, ...
Thought: The search results indicate that Mira Murati is the current interim CEO of OpenAI, taking over from Sam Altman.
Action: Response To Human
Action Input: "The current interim CEO of OpenAI is Mira Murati, who took over from Sam Altman."
Response: The current interim CEO of OpenAI is Mira Murati, who took over from Sam Altman.

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/ 🌟

Recommended from Medium

Lists

See more recommendations