Skip to main content

LLM Agents

Small library to build agents which are controlled by large language models (LLMs) which is heavily inspired by langchain.

The goal was to get a better grasp of how such an agent works and understand it all in very few lines of code.

Langchain is great, but it already has a few more files and abstraction layers, so I thought it would be nice to build the most important parts of a simple agent from scratch.

Some more infos are in this Hacker News discussion from April 5th 2023 and the related blog post.

How it works

The agent works like this:

  • It gets instructed by a prompt which tells it the basic way to solve a task using tools
  • Tools are custom build components which the agent can use
    • So far, I've implemented the ability to execute Python code in a REPL, to use the Google search and to search on Hacker News
  • The agent runs in a loop of Thought, Action, Observation, Thought, ...
    • The Thought and Action (with the Action Input to the action) are the parts which are generated by an LLM
    • The Observation is generated by using a tool (for example the print outputs of Python or the text result of a Google search)
  • The LLM gets the new information appended to the prompt in each loop cycle and thus can act on that information
  • Once the agent has enough information it provides the final answer

For more details on how it works, check out this blog post

How to use it

You can install this libaray locally by running: pip install -e . inside it's directory after cloning it.

You also need to provide the following env variables:

You can simply export them in bash like: export OPENAI_API_KEY='sh-lsdf....'

Then you can run the script python run_agent.py and ask your question.

To construct your own agent do it like this:

from llm_agents import Agent, ChatLLM, PythonREPLTool, HackerNewsSearchTool, SerpAPITool

agent = Agent(llm=ChatLLM(), tools=[PythonREPLTool(), SerpAPITool(), HackerNewsSearchTool()])
result = agent.run("Your question to the agent")

print(f"Final answer is {result}")

Of course, you can also build your custom tools or omit tools, for exmaple if you don't want to create a SERPAPI key.

Metadata

Release files for llm-agents 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for llm-agents 0.0.2
File Interpreter ABI Platform
llm_agents-0.0.2-py3-none-any.whl Python 3 none any Details

Release files / llm_agents-0.0.2-py3-none-any.whl

Download URL llm_agents-0.0.2-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5c5370c6b9ed3c75a9a10dfc4f1faff58aa4c9839a9e5388ff93f22c9ce23778
BLAKE2b-256 checksum
How to use checksums
485007816faaf2b17de88b1752c19b69a73f346018abdcca0e2cd03ec67b8a8b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Release history Release notifications | RSS feed

This release

0.0.2 This release

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page