Skip to main content

README - LangchainAgentFactory

LangchainAgentFactory is a wrapper around the powerful LangChain library that provides a user-friendly interface to generate various types of agents based on Open AI's GPT models. This library leverages LangChain's robust features, providing an extra layer of abstraction that simplifies the process of creating conversational AI agents with powerful tools like google search and sending emails.

Features

  • Simplifies the use of the powerful LangChain framework.
  • Generate various types of agents with custom toolsets.
  • Supports conversation history tracking with the LangChain memory buffer.
  • Allows for customizing the 'creativity' (temperature) of the chatbot and the maximum token limit.

Prerequisites

Ensure Python 3.8+ is installed on your system.

Installation

Install LangChainAgentFactory via pip:

pip install LangChainAgentFactory

Pip will automatically handle the installation of dependencies.

Set Up Required Environment Variables

To use LangChainAgentFactory, you need to set up your OpenAI API Key in your environment variables:

Windows

  • Powershell:

    $env:OPENAI_API_KEY = 'YOUR_OPENAI_API_KEY'
    
  • Command Prompt:

    set OPENAI_API_KEY=YOUR_OPENAI_API_KEY
    

MacOS

  • Terminal:

    export OPENAI_API_KEY=YOUR_OPENAI_API_KEY
    

Usage

For all agents, import dependencies:

from LangchainAgentFactory import AgentFactory

Chat Agent With Memory

Create an instance of LangchainAgentFactory, then call the agent method to create an agent. You can specify the type of agent you want and whether you want verbose output:

Note: This will only create a chat bot with no access to tools

factory = AgentFactory()
agent = factory.agent()

# converse with the agent
user_input = input()
agent.run(user_input)

Chat Agent With Memory and Email Sending Function

The LangchainAgentFactory allows the creation of an AI agent that can use various tools, where a tool is essentially a function. These functions can either have a single text parameter or multiple parameters. If you have multiple tools, the agent can chain them together. Here's an example demonstrating how to create a tool and use it in your agent.

First, we define a function with a doc string that describes the parameters and what the function does. The function must return a string.

As an example, we'll implement send_email that sends an email using SMTP and ttls:

import os
import smtplib
import ssl
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from LangChainAgentFactory import AgentFactory

def send_email(subject, message, to_email):
    """Send an email to the specified email address with the specified subject and message."""
    from_email = os.getenv("EMAIL_USER")
    password = os.getenv("EMAIL_PASSWORD")
    smtp_server = os.getenv("EMAIL_SMTP_SERVER")

    msg = MIMEMultipart()
    msg['From'] = from_email
    msg['To'] = to_email
    msg['Subject'] = subject
    msg.attach(MIMEText(message, 'plain'))    

    smtp_server = "smtp.hostinger.com"
    smtp_port = 587

    server = None
    context = ssl.create_default_context()
    try:
        server = smtplib.SMTP(smtp_server, smtp_port, timeout=60)
        server.starttls(context=context)
        server.login(from_email, password)
        text = msg.as_string()
        server.sendmail(from_email, to_email, text)
    except smtplib.SMTPException as e:
        return f"Failed to send email due to SMTP Exception: {str(e)}"
    except Exception as e:
        return f"An unexpected error occurred while sending the email: {str(e)}"
    finally:
        if server is not None:
            server.quit()

    return f"Success! The message was sent to {to_email}" # Note the return is in natural language. This message is what the LLM receives as an indicator of what happened, not necessarily what the LLM will respond with.

Now, we use StructuredTool.from_function to create a tool from the send_email function and create a tools array with it:

from langchain.agents import StructuredTool # used for multi-parameter tools. You can use `Tool` for single-parameter tools

tools = [
    StructuredTool.from_function(send_email)
]

Finally, we pass this tools array to the LangchainAgentFactory and create an agent:

agentFactory = AgentFactory(tools=tools)
agent = agentFactory.agent()

Now, your agent has the ability to send emails!

user_input = "Send an email to mark.smith@example.com informing him of what you can do."
agent.run(user_input)

Send Email using a natural language query

If you add multiple tools and want to verify what the agent has access to, simply ask!

user_input = "What tools do you have access to?"
agent.run(user_input)

What Functions Can You Access?

License

This project is licensed under the terms of the MIT license. See the LICENSE file for details.

Support

For support or any questions, raise a GitHub Issue.

Release files for LangChainAgentFactory 0.3

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

Source distribution (sdist)

Source distribution for LangChainAgentFactory 0.3
File Size Uploaded
LangChainAgentFactory-0.3.tar.gz 7.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for LangChainAgentFactory 0.3
File Interpreter ABI Platform
LangChainAgentFactory-0.3-py3-none-any.whl Python 3 none any Details

Total release size: 13.9 kB

Release files / LangChainAgentFactory-0.3.tar.gz

Download URL LangChainAgentFactory-0.3.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ff958a21b85eebdee0f3db6099b0b36eec70b1bbaf4c2abaaa5ce5a1bf9b0afa
BLAKE2b-256 checksum
How to use checksums
00567961a111255df42e97bd66d7c6d28bc0dbeae2da2fa971e5174463dd225e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Release files / LangChainAgentFactory-0.3-py3-none-any.whl

Download URL LangChainAgentFactory-0.3-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
56944ca9dfab59452f41b03e2987c22784e476f01e10c2c1fad3f13dbfdab429
BLAKE2b-256 checksum
How to use checksums
464e48de442496567edf8bd88a3347c4fe75363135686c84b7e37e09d8cc2f12
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.3 This release

2 release files

0.2

2 release files

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