Use Gradio Apps as tools for LLM Agents
Project description
Gradio Tools: Gradio 🤝 LLM Agents
Any Gradio application at the tips of your LLM's fingers 🦾
gradio_tool can turn any Gradio application into a tool that an LLM agent can use to complete its task.
Currently supported are:
- LangChain agents can use to complete its task.
- MiniChain
Example Usage
Simply import the desired tools from gradio_tool
(or create your own!) and pass to initialize_agent
from LangChain.
In this example, we use some pre-built tools to generate images, caption them, and create a music clip to match its artistic style!
Read the How It Works section to learn how to create your own tools! We welcome any new tools to the library!
os.environ["OPENAI_API_KEY"] = "<Secret Key>"
from langchain.agents import initialize_agent
from langchain.llms import OpenAI
import os
from gradio_tool.tool import StableDiffusionTool, ImageCaptioningTool, ImageToMusicTool
from langchain.memory import ConversationBufferMemory
llm = OpenAI(temperature=0)
memory = ConversationBufferMemory(memory_key="chat_history")
tools = [StableDiffusionTool().langchain, ImageCaptioningTool().langchain, ImageToMusicTool().langchain]
agent = initialize_agent(tools, llm, memory=memory, agent="conversational-react-description", verbose=True)
output = agent.run(input=("I would please like a photo of a dog riding a skateboard. "
"Please caption this image and create a song for it."))
How it works
The core abstraction is the GradioTool
, which lets you define a new tool for your LLM as long as you implement a standard interface:
class GradioTool(BaseTool):
def __init__(self, name: str, description: str, src: str) -> None:
@abstractmethod
def create_job(self, query: str) -> Job:
pass
@abstractmethod
def postprocess(self, output: Tuple[Any] | Any) -> str:
pass
The requirements are:
- The name for your tool
- The description for your tool. This is crucial! Agents decide which tool to use based on their description. Be precise and be sure to inclue example of what the input and the output of the tool should look like.
- The url or space id, e.g.
freddyaboulton/calculator
, of the Gradio application. Based on this value,gradio_tool
will create a gradio client instance to query the upstream application via API. Be sure to click the link and learn more about the gradio client library if you are not familiar with it. - create_job - Given a string, this method should parse that string and return a job from the client. Most times, this is as simple as passing the string to the
submit
function of the client. More info on creating jobs here - postprocess - Given the result of the job, convert it to a string the LLM can display to the user.
- Optional - Some libraries, e.g. MiniChain, may need some info about the underlying gradio input and output types used by the tool. By default, this will return gr.Textbox() but
if you'd like to provide more accurate info, implement the
_block_input(self)
and_block_output(self)
methods of the tool.
And that's it!
Pre-built tools
gradio_tool comes with a set of pre-built tools you can leverage immediately! These are
- StableDiffusionTool - Generate an image from a given prompt using the open source stable diffusion demo hosted on HuggingFace spaces
- ImageCaptionTool - Caption an image by providing a filepath based on Niels Rogge's HuggingFace Space
- ImageToMusicTool - Create an audio clip that matches the style of a given image file based on Sylvain Filoni's HuggingFace Space
Appendix
What are agents?
A LangChain agent is a Large Language Model (LLM) that takes user input and reports an output based on using one of many tools at its disposal.
What is Gradio?
Gradio is the defacto standard tool for building Machine Learning Web Applications and sharing them with the world - all with just python! 🐍
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file gradio_tools-0.0.3.tar.gz
.
File metadata
- Download URL: gradio_tools-0.0.3.tar.gz
- Upload date:
- Size: 3.5 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a19f43f3a1b0d3dedec8e04f6be27cf00b2df297e3dda165572c099947f28a6 |
|
MD5 | 1e67a44f150ee9c20f62ace5aee2cc86 |
|
BLAKE2b-256 | 5c72e6152aa7acc43c9e2942892acd6b57c9b5fb1b932680c8225bb3e397e66c |
File details
Details for the file gradio_tools-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: gradio_tools-0.0.3-py3-none-any.whl
- Upload date:
- Size: 193.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c99c6da30e399d7f7348279486c37cd2794a51ba35bca4cd783cab69a8f0c4be |
|
MD5 | e802d6e6e54d3b37319e466e80686a1e |
|
BLAKE2b-256 | f4070793b626ff6e85f2bea329ccd3c35242cef2cf45ebd3cfe73e9524207c16 |