Sure, here's a condensed version that includes the code examples: To use Claude and…
- User
Sure, here's a condensed version that includes the code examples: To use Claude and OpenAI API with LangChain, you can follow these steps:
Set up the environment:
pythonCopyimport os
Set the required environment variables
os.environ["LANGCHAIN_TRACING_V2"] = "true" os.environ["LANGCHAIN_API_KEY"] = "" os.environ["OPENAI_API_KEY"] = ""
Define the tools:
pythonCopyfrom langchain_community.tools.tavily_search import TavilySearchResults from langchain.tools.retriever import create_retriever_tool from langchain_community.document_loaders import WebBaseLoader from langchain_community.vectorstores import FAISS from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import RecursiveCharacterTextSplitter
Tavily search tool
search = TavilySearchResults()
Retriever tool
loader = WebBaseLoader("https://docs.smith.langchain.com/overview") docs = loader.load() documents = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200).split_documents(docs) vector = FAISS.from_documents(documents, OpenAIEmbeddings()) retriever = vector.as_retriever() retriever_tool = create_retriever_tool(retriever, "langsmith_search", "Search for information about LangSmith.")
tools = [search, retriever_tool]
Create the agent:
pythonCopyfrom langchain_openai import ChatOpenAI from langchain import hub
Load the prompt
prompt = hub.pull("hwchase17/openai-functions-agent")
Create the agent
llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0) agent = create_tool_calling_agent(llm, tools, prompt) agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
Run the agent:
pythonCopyagent_executor.invoke({"input": "hi!"}) agent_executor.invoke({"input": "how can langsmith help with testing?"}) agent_executor.invoke({"input": "whats the weather in sf?"}) This code creates an agent that can use the Tavily search tool and a retriever tool to search for information and answer queries. The agent can also remember previous interactions by using the chat_history parameter. To access files and modify them, you can use the window.fs.readFile API provided by LangChain. Here's an example: pythonCopy# Read a CSV file response = await window.fs.readFile('monthly_profits.csv') text = new TextDecoder().decode(response)
Process the CSV data
import Papa from 'papaparse' parsedData = Papa.parse(text, { header: true, dynamicTyping: true, skipEmptyLines: true }) This allows you to read the contents of CSV, XLSX, or other file formats, and then process the data as needed within your agent or other LangChain components. Remember to handle any errors that may occur when reading or processing files, and to use the appropriate libraries and techniques for working with different file formats.
Creating Tools in LangChain When constructing an agent in LangChain, you need to provide it with a list of tools that it can use. The tool consists of several components:
name: A unique name within the set of tools. description: Describes what the tool does, used as context. args_schema: Optional Pydantic schema for tool parameters. return_direct: Whether the agent should stop and return the result directly.
LangChain supports creating tools from:
Functions LangChain Runnables Subclassing BaseTool
Creating Tools from Functions The @tool decorator is the simplest way to define a custom tool. It uses the function name as the tool name by default, and the function's docstring as the description. pythonCopyfrom langchain_core.tools import tool
@tool def multiply(a: int, b: int) -> int: """Multiply two numbers.""" return a * b The StructuredTool.from_function class method provides more configurability, allowing you to specify the name, description, args schema, and sync/async implementations. Creating Tools from Runnables LangChain Runnables that accept string or dict input can be converted to tools using the as_tool method, which allows you to specify names, descriptions, and additional schema information. Creating Tools by Subclassing BaseTool Subclassing BaseTool provides the most control, but requires more code. You can specify the tool's name, description, args schema, and implement the _run and _arun methods. Handling Async Tools LangChain provides an async implementation that assumes the function is expensive to compute, delegating execution to another thread. If you're working in an async codebase, you should create async tools to avoid the overhead. Handling Tool Errors To handle errors in tools, you can raise a ToolException and set the handle_tool_error parameter on the tool. This allows the agent to recover from the error and continue execution. Returning Artifacts from Tools Sometimes, tools may generate artifacts (e.g., custom objects) that you want to make accessible to downstream components, without exposing the raw output to the model. You can do this by returning a tuple of (content, artifact) and setting response_format="content_and_artifact" on the tool. CopyRetryBLall code examplesEditHere are the code examples from the condensed version: Creating Tools from Functions: pythonCopyfrom langchain_core.tools import tool
@tool def multiply(a: int, b: int) -> int: """Multiply two numbers.""" return a * b Using StructuredTool.from_function: pythonCopyfrom langchain_core.tools import StructuredTool
def multiply(a: int, b: int) -> int: """Multiply two numbers.""" return a * b
async def amultiply(a: int, b: int) -> int: """Multiply two numbers.""" return a * b
calculator = StructuredTool.from_function(func=multiply, coroutine=amultiply) Creating Tools from Runnables: pythonCopyfrom langchain_core.language_models import GenericFakeChatModel from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages( [("human", "Hello. Please respond in the style of {answer_style}.")] )
llm = GenericFakeChatModel(messages=iter(["hello matey"])) chain = prompt | llm | StrOutputParser()
as_tool = chain.as_tool( name="Style responder", description="Description of when to use tool." ) Subclassing BaseTool: pythonCopyfrom typing import Optional, Type from langchain_core.callbacks import ( AsyncCallbackManagerForToolRun, CallbackManagerForToolRun ) from langchain_core.tools import BaseTool from pydantic import BaseModel, Field
class CalculatorInput(BaseModel): a: int = Field(description="first number") b: int = Field(description="second number")
class CustomCalculatorTool(BaseTool): name: str = "Calculator" description: str = "useful for when you need to answer questions about math" args_schema: Type[BaseModel] = CalculatorInput return_direct: bool = True
def _run(self, a: int, b: int, run_manager: Optional[CallbackManagerForToolRun] = None) -> str: return a * b async def _arun(self, a: int, b: int, run_manager: Optional[AsyncCallbackManagerForToolRun] = None) -> str: return self._run(a, b, run_manager=run_manager.get_sync())multiply = CustomCalculatorTool() Handling Tool Errors: pythonCopyfrom langchain_core.tools import ToolException
def get_weather(city: str) -> int: raise ToolException(f"Error: There is no city by the name of {city}.")
get_weather_tool = StructuredTool.from_function( func=get_weather, handle_tool_error=True, ) Returning Artifacts from Tools: pythonCopyimport random from typing import List, Tuple from langchain_core.tools import tool
@tool(response_format="content_and_artifact") def generate_random_ints(min: int, max: int, size: int) -> Tuple[str, List[int]]: array = [random.randint(min, max) for _ in range(size)] content = f"Successfully generated array of {size} random ints in [{min}, {max}]." return content, array
now ok with all this information, i need you to create full code so that i can use claude and open ai api with langchain so that it have the ability to run the program and see its output, error and access the file directory that the program is in (so it can modify other files if the program is complex) and access to files (and understand its content) such as xslx, mat, csv, etc..
- Assistant
Sure! Below is a comprehensive Python script that integrates both Claude (from Anthropic) and OpenAI's GPT models using LangChain. This script allows the agent to:
- Interact with both Claude and OpenAI APIs.
- Execute system commands and view their outputs and errors.
- Access and modify files in the directory, including reading and processing various file formats like CSV, XLSX, and MAT.
Prerequisites
-
Python 3.8 or higher installed on your system.
-
API Keys for:
-
Install Required Packages:
You can install all necessary packages using
pip. Create arequirements.txtwith the following content or install them directly.pip install langchain langchain-openai langchain-claude langchain-community faiss-cpu openai anthropic pandas openpyxl scipyAlternatively, using the provided
requirements.txt:# requirements.txt langchain langchain-openai langchain-claude langchain-community faiss-cpu openai anthropic pandas openpyxl scipyThen run:
pip install -r requirements.txt
Full Python Script
import os import subprocess import pandas as pd import scipy.io from typing import List, Tuple, Any, Optional from pydantic import BaseModel, Field from langchain import AgentExecutor, hub from langchain.chat_models import ChatOpenAI, ClaudeChat from langchain.tools import BaseTool, ToolException, StructuredTool, tool from langchain.tools.retriever import create_retriever_tool from langchain_community.tools.tavily_search import TavilySearchResults from langchain_community.document_loaders import WebBaseLoader from langchain_community.vectorstores import FAISS from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import RecursiveCharacterTextSplitter from langchain_llms import Claude # Replace with actual import if different import asyncio # --------------------------- # Environment Setup # --------------------------- # Set the required environment variables os.environ["LANGCHAIN_TRACING_V2"] = "true" os.environ["LANGCHAIN_API_KEY"] = "<your-langchain-api-key>" os.environ["OPENAI_API_KEY"] = "<your-openai-api-key>" os.environ["ANTHROPIC_API_KEY"] = "<your-anthropic-api-key>" # --------------------------- # Define Tools # --------------------------- # 1. Tavily Search Tool search_tool = TavilySearchResults() # 2. Retriever Tool loader = WebBaseLoader("https://docs.smith.langchain.com/overview") docs = loader.load() text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200) documents = text_splitter.split_documents(docs) embeddings = OpenAIEmbeddings() vector_store = FAISS.from_documents(documents, embeddings) retriever = vector_store.as_retriever() retriever_tool = create_retriever_tool( retriever, name="langsmith_search", description="Search for information about LangSmith." ) # 3. Command Execution Tool @tool def run_command(command: str) -> Tuple[str, str]: """ Executes a system command. Args: command (str): The command to execute. Returns: Tuple[str, str]: A tuple containing standard output and standard error. """ try: result = subprocess.run(command, shell=True, capture_output=True, text=True, check=True) return (result.stdout, result.stderr) except subprocess.CalledProcessError as e: raise ToolException(f"Command execution failed: {e.stderr}") # 4. File Access Tool @tool def read_file(file_path: str) -> str: """ Reads the content of a file. Args: file_path (str): Path to the file. Returns: str: Content of the file. """ try: with open(file_path, 'r', encoding='utf-8') as f: return f.read() except Exception as e: raise ToolException(f"Failed to read file {file_path}: {str(e)}") @tool def write_file(file_path: str, content: str) -> str: """ Writes content to a file. Args: file_path (str): Path to the file. content (str): Content to write. Returns: str: Success message. """ try: with open(file_path, 'w', encoding='utf-8') as f: f.write(content) return f"Successfully wrote to {file_path}." except Exception as e: raise ToolException(f"Failed to write to file {file_path}: {str(e)}") # 5. File Processing Tools @tool def read_csv(file_path: str) -> Any: """ Reads a CSV file and returns a DataFrame summary. Args: file_path (str): Path to the CSV file. Returns: str: Summary of the DataFrame. """ try: df = pd.read_csv(file_path) return f"CSV File '{file_path}' Loaded Successfully.\nColumns: {df.columns.tolist()}\nFirst 5 Rows:\n{df.head()}" except Exception as e: raise ToolException(f"Failed to read CSV file {file_path}: {str(e)}") @tool def read_xlsx(file_path: str) -> Any: """ Reads an XLSX file and returns a DataFrame summary. Args: file_path (str): Path to the XLSX file. Returns: str: Summary of the DataFrame. """ try: df = pd.read_excel(file_path) return f"XLSX File '{file_path}' Loaded Successfully.\nColumns: {df.columns.tolist()}\nFirst 5 Rows:\n{df.head()}" except Exception as e: raise ToolException(f"Failed to read XLSX file {file_path}: {str(e)}") @tool def read_mat(file_path: str) -> Any: """ Reads a MAT file and returns its contents. Args: file_path (str): Path to the MAT file. Returns: str: Contents of the MAT file. """ try: mat = scipy.io.loadmat(file_path) return f"MAT File '{file_path}' Loaded Successfully.\nContents: {mat}" except Exception as e: raise ToolException(f"Failed to read MAT file {file_path}: {str(e)}") # 6. Custom Calculator Tool (Example) @tool def multiply(a: int, b: int) -> int: """Multiply two numbers.""" return a * b # 7. Error Handling Example Tool @tool(handle_tool_error=True) def get_weather(city: str) -> int: """ Example tool that raises an error if city is not found. Args: city (str): Name of the city. Returns: int: Example return value. """ raise ToolException(f"Error: There is no city by the name of {city}.") # 8. Returning Artifacts Example Tool import random @tool(response_format="content_and_artifact") def generate_random_ints(min: int, max: int, size: int) -> Tuple[str, List[int]]: """ Generates a list of random integers. Args: min (int): Minimum integer value. max (int): Maximum integer value. size (int): Number of integers to generate. Returns: Tuple[str, List[int]]: A message and the list of integers. """ array = [random.randint(min, max) for _ in range(size)] content = f"Successfully generated array of {size} random ints in [{min}, {max}]." return content, array # Aggregate all tools tools = [ search_tool, retriever_tool, run_command, read_file, write_file, read_csv, read_xlsx, read_mat, multiply, get_weather, generate_random_ints ] # --------------------------- # Create the Agent # --------------------------- # Initialize OpenAI and Claude LLMs openai_llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0) claude_llm = ClaudeChat(model="claude-v1", temperature=0) # Replace 'claude-v1' with actual model name # Load a custom prompt or use a predefined one # For demonstration, using a simple prompt from LangChain Hub prompt = hub.pull("hwchase17/openai-functions-agent") # Function to select which LLM to use (simple round-robin or based on context) class MultiLLMAgentExecutor(AgentExecutor): def __init__(self, openai_llm, claude_llm, **kwargs): super().__init__(**kwargs) self.openai_llm = openai_llm self.claude_llm = claude_llm self.llm_toggle = True # Simple toggle between LLMs def get_llm(self): if self.llm_toggle: self.llm_toggle = False return self.openai_llm else: self.llm_toggle = True return self.claude_llm def _call(self, inputs: dict) -> Any: self.agent.llm = self.get_llm() return super()._call(inputs) # Create the agent with tool calling capability from langchain.agents import Tool, AgentType, initialize_agent agent = initialize_agent( tools=tools, llm=openai_llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True ) # Wrap the agent in our MultiLLMAgentExecutor agent_executor = MultiLLMAgentExecutor( openai_llm=openai_llm, claude_llm=claude_llm, agent=agent, tools=tools, verbose=True ) # --------------------------- # Run the Agent # --------------------------- def main(): print("Agent is ready to receive commands. Type 'exit' to quit.") while True: user_input = input(">> ") if user_input.lower() in ["exit", "quit"]: print("Exiting agent.") break try: response = agent_executor.invoke({"input": user_input}) print(response) except Exception as e: print(f"An error occurred: {e}") if __name__ == "__main__": main()Explanation of the Script
-
Environment Setup:
- Sets the necessary environment variables for LangChain, OpenAI, and Anthropic (Claude) APIs. Replace the placeholder strings (
<your-...-api-key>) with your actual API keys.
- Sets the necessary environment variables for LangChain, OpenAI, and Anthropic (Claude) APIs. Replace the placeholder strings (
-
Define Tools:
- Search Tools: Integrates
TavilySearchResultsand a retriever tool usingFAISSvector store to enable searching capabilities within the LangChain agent. - Command Execution Tool: Allows the agent to run system commands and capture their outputs and errors.
- File Access Tools: Provides functionalities to read from and write to files. This is crucial for agents that need to interact with the filesystem.
- File Processing Tools: Specialized tools to read and process different file formats like CSV, XLSX, and MAT using
pandasandscipy. - Custom Calculator & Error Handling Tools: Examples demonstrating how to create tools that perform calculations and handle errors gracefully.
- Artifact Returning Tool: An example of a tool that returns both content and artifacts, allowing downstream components to access structured data without exposing raw outputs to the model.
- Search Tools: Integrates
-
Create the Agent:
- Initializes both OpenAI's GPT-3.5 and Anthropic's Claude LLMs.
- Defines a
MultiLLMAgentExecutorclass to alternate between using OpenAI and Claude models. This can be enhanced to choose models based on context or other criteria. - Aggregates all defined tools and initializes the agent using
initialize_agentfrom LangChain. - Wraps the agent in the
MultiLLMAgentExecutorto manage multiple LLMs.
-
Run the Agent:
- Defines a
mainfunction that runs an interactive loop, allowing you to input commands to the agent. - The agent processes the input, utilizes the appropriate tools, and returns responses, including outputs from executed system commands or processed file data.
- Defines a
Usage Instructions
-
Set Up API Keys:
- Replace the placeholders in the environment variables section with your actual API keys.
os.environ["LANGCHAIN_API_KEY"] = "your-langchain-api-key" os.environ["OPENAI_API_KEY"] = "your-openai-api-key" os.environ["ANTHROPIC_API_KEY"] = "your-anthropic-api-key" -
Run the Script:
Navigate to the directory containing the script and run:
python your_script_name.py -
Interact with the Agent:
- Execute Commands: Type any system command, such as
ls(Unix) ordir(Windows), to see directory contents. - Read Files: Use the
read_filetool by asking the agent to read specific files. - Process Files: Inquire about processing CSV, XLSX, or MAT files.
- Perform Calculations: Ask the agent to perform mathematical operations.
- Generate Random Integers: Use the appropriate tool to generate random numbers.
Example Interactions:
>> ls >> Read the contents of data.csv >> Multiply 5 and 7 >> Generate 10 random integers between 1 and 100 >> exit - Execute Commands: Type any system command, such as
Security Considerations
-
Caution with Command Execution: The
run_commandtool can execute arbitrary system commands. Ensure that this capability is restricted and not exposed in production environments, as it poses significant security risks. -
File Access: Similarly, file read/write capabilities should be carefully managed to prevent unauthorized access or modifications.
-
API Key Management: Store your API keys securely, preferably using environment variables or secure key management systems, and avoid hardcoding them in scripts.
Extending Functionality
-
Additional Tools: You can create more custom tools by following the examples provided, using the
@tooldecorator or subclassingBaseTool. -
LLM Selection Logic: Enhance the
MultiLLMAgentExecutorto select LLMs based on more sophisticated criteria, such as the complexity of the query or specific user requirements. -
Error Handling: Implement more robust error handling and logging mechanisms to monitor the agent's activities and potential issues.
-
User Interface: Integrate with a frontend interface or APIs to allow remote interactions with the agent instead of using a command-line interface.
Conclusion
This script provides a versatile foundation for building a multi-LLM agent using LangChain, capable of executing system commands, interacting with the filesystem, and processing various file types. Customize and extend it according to your specific use cases and ensure that you adhere to security best practices when deploying such powerful agents.