id
stringlengths
14
16
text
stringlengths
36
2.73k
source
stringlengths
49
117
6cc3d0c5fb2d-1
toolkit = PowerBIToolkit( powerbi=PowerBIDataset(dataset_id="<dataset_id>", table_names=['table1', 'table2'], credential=DefaultAzureCredential()), llm=smart_llm ) agent_executor = create_pbi_agent( llm=fast_llm, toolkit=toolkit, verbose=True, ) Example: describing a table# agent_executor.run("Describe table1") Example: simple query on a table# In this example, the agent actually figures out the correct query to get a row count of the table. agent_executor.run("How many records are in table1?") Example: running queries# agent_executor.run("How many records are there by dimension1 in table2?") agent_executor.run("What unique values are there for dimensions2 in table2") Example: add your own few-shot prompts# #fictional example few_shots = """ Question: How many rows are in the table revenue? DAX: EVALUATE ROW("Number of rows", COUNTROWS(revenue_details)) ---- Question: How many rows are in the table revenue where year is not empty? DAX: EVALUATE ROW("Number of rows", COUNTROWS(FILTER(revenue_details, revenue_details[year] <> ""))) ---- Question: What was the average of value in revenue in dollars? DAX: EVALUATE ROW("Average", AVERAGE(revenue_details[dollar_value])) ---- """ toolkit = PowerBIToolkit( powerbi=PowerBIDataset(dataset_id="<dataset_id>", table_names=['table1', 'table2'], credential=DefaultAzureCredential()), llm=smart_llm, examples=few_shots, ) agent_executor = create_pbi_agent(
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/powerbi.html
6cc3d0c5fb2d-2
examples=few_shots, ) agent_executor = create_pbi_agent( llm=fast_llm, toolkit=toolkit, verbose=True, ) agent_executor.run("What was the maximum of value in revenue in dollars in 2022?") previous PlayWright Browser Toolkit next Python Agent Contents Some notes Initialization Example: describing a table Example: simple query on a table Example: running queries Example: add your own few-shot prompts By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 07, 2023.
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/powerbi.html
472e443c378b-0
.ipynb .pdf Pandas Dataframe Agent Contents Multi DataFrame Example Pandas Dataframe Agent# This notebook shows how to use agents to interact with a pandas dataframe. It is mostly optimized for question answering. NOTE: this agent calls the Python agent under the hood, which executes LLM generated Python code - this can be bad if the LLM generated Python code is harmful. Use cautiously. from langchain.agents import create_pandas_dataframe_agent from langchain.llms import OpenAI import pandas as pd df = pd.read_csv('titanic.csv') agent = create_pandas_dataframe_agent(OpenAI(temperature=0), df, verbose=True) agent.run("how many rows are there?") > Entering new AgentExecutor chain... Thought: I need to count the number of rows Action: python_repl_ast Action Input: df.shape[0] Observation: 891 Thought: I now know the final answer Final Answer: There are 891 rows. > Finished chain. 'There are 891 rows.' agent.run("how many people have more than 3 siblings") > Entering new AgentExecutor chain... Thought: I need to count the number of people with more than 3 siblings Action: python_repl_ast Action Input: df[df['SibSp'] > 3].shape[0] Observation: 30 Thought: I now know the final answer Final Answer: 30 people have more than 3 siblings. > Finished chain. '30 people have more than 3 siblings.' agent.run("whats the square root of the average age?") > Entering new AgentExecutor chain... Thought: I need to calculate the average age first Action: python_repl_ast Action Input: df['Age'].mean()
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/pandas.html
472e443c378b-1
Action: python_repl_ast Action Input: df['Age'].mean() Observation: 29.69911764705882 Thought: I now need to calculate the square root of the average age Action: python_repl_ast Action Input: math.sqrt(df['Age'].mean()) Observation: NameError("name 'math' is not defined") Thought: I need to import the math library Action: python_repl_ast Action Input: import math Observation: Thought: I now need to calculate the square root of the average age Action: python_repl_ast Action Input: math.sqrt(df['Age'].mean()) Observation: 5.449689683556195 Thought: I now know the final answer Final Answer: The square root of the average age is 5.449689683556195. > Finished chain. 'The square root of the average age is 5.449689683556195.' Multi DataFrame Example# This next part shows how the agent can interact with multiple dataframes passed in as a list. df1 = df.copy() df1["Age"] = df1["Age"].fillna(df1["Age"].mean()) agent = create_pandas_dataframe_agent(OpenAI(temperature=0), [df, df1], verbose=True) agent.run("how many rows in the age column are different?") > Entering new AgentExecutor chain... Thought: I need to compare the age columns in both dataframes Action: python_repl_ast Action Input: len(df1[df1['Age'] != df2['Age']]) Observation: 177 Thought: I now know the final answer Final Answer: 177 rows in the age column are different. > Finished chain. '177 rows in the age column are different.' previous Natural Language APIs next
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/pandas.html
472e443c378b-2
'177 rows in the age column are different.' previous Natural Language APIs next PlayWright Browser Toolkit Contents Multi DataFrame Example By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 07, 2023.
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/pandas.html
743e75257d92-0
.ipynb .pdf Spark SQL Agent Contents Initialization Example: describing a table Example: running queries Spark SQL Agent# This notebook shows how to use agents to interact with a Spark SQL. Similar to SQL Database Agent, it is designed to address general inquiries about Spark SQL and facilitate error recovery. NOTE: Note that, as this agent is in active development, all answers might not be correct. Additionally, it is not guaranteed that the agent won’t perform DML statements on your Spark cluster given certain questions. Be careful running it on sensitive data! Initialization# from langchain.agents import create_spark_sql_agent from langchain.agents.agent_toolkits import SparkSQLToolkit from langchain.chat_models import ChatOpenAI from langchain.utilities.spark_sql import SparkSQL from pyspark.sql import SparkSession spark = SparkSession.builder.getOrCreate() schema = "langchain_example" spark.sql(f"CREATE DATABASE IF NOT EXISTS {schema}") spark.sql(f"USE {schema}") csv_file_path = "titanic.csv" table = "titanic" spark.read.csv(csv_file_path, header=True, inferSchema=True).write.saveAsTable(table) spark.table(table).show() Setting default log level to "WARN". To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel). 23/05/18 16:03:10 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable +-----------+--------+------+--------------------+------+----+-----+-----+----------------+-------+-----+--------+ |PassengerId|Survived|Pclass| Name| Sex| Age|SibSp|Parch| Ticket| Fare|Cabin|Embarked|
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/spark_sql.html
743e75257d92-1
+-----------+--------+------+--------------------+------+----+-----+-----+----------------+-------+-----+--------+ | 1| 0| 3|Braund, Mr. Owen ...| male|22.0| 1| 0| A/5 21171| 7.25| null| S| | 2| 1| 1|Cumings, Mrs. Joh...|female|38.0| 1| 0| PC 17599|71.2833| C85| C| | 3| 1| 3|Heikkinen, Miss. ...|female|26.0| 0| 0|STON/O2. 3101282| 7.925| null| S| | 4| 1| 1|Futrelle, Mrs. Ja...|female|35.0| 1| 0| 113803| 53.1| C123| S| | 5| 0| 3|Allen, Mr. Willia...| male|35.0| 0| 0| 373450| 8.05| null| S| | 6| 0| 3| Moran, Mr. James| male|null| 0| 0| 330877| 8.4583| null| Q|
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/spark_sql.html
743e75257d92-2
| 7| 0| 1|McCarthy, Mr. Tim...| male|54.0| 0| 0| 17463|51.8625| E46| S| | 8| 0| 3|Palsson, Master. ...| male| 2.0| 3| 1| 349909| 21.075| null| S| | 9| 1| 3|Johnson, Mrs. Osc...|female|27.0| 0| 2| 347742|11.1333| null| S| | 10| 1| 2|Nasser, Mrs. Nich...|female|14.0| 1| 0| 237736|30.0708| null| C| | 11| 1| 3|Sandstrom, Miss. ...|female| 4.0| 1| 1| PP 9549| 16.7| G6| S| | 12| 1| 1|Bonnell, Miss. El...|female|58.0| 0| 0| 113783| 26.55| C103| S| | 13| 0| 3|Saundercock, Mr. ...| male|20.0| 0| 0| A/5. 2151| 8.05| null| S|
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/spark_sql.html
743e75257d92-3
| 14| 0| 3|Andersson, Mr. An...| male|39.0| 1| 5| 347082| 31.275| null| S| | 15| 0| 3|Vestrom, Miss. Hu...|female|14.0| 0| 0| 350406| 7.8542| null| S| | 16| 1| 2|Hewlett, Mrs. (Ma...|female|55.0| 0| 0| 248706| 16.0| null| S| | 17| 0| 3|Rice, Master. Eugene| male| 2.0| 4| 1| 382652| 29.125| null| Q| | 18| 1| 2|Williams, Mr. Cha...| male|null| 0| 0| 244373| 13.0| null| S| | 19| 0| 3|Vander Planke, Mr...|female|31.0| 1| 0| 345763| 18.0| null| S| | 20| 1| 3|Masselmani, Mrs. ...|female|null| 0| 0| 2649| 7.225| null| C| +-----------+--------+------+--------------------+------+----+-----+-----+----------------+-------+-----+--------+ only showing top 20 rows
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/spark_sql.html
743e75257d92-4
only showing top 20 rows # Note, you can also connect to Spark via Spark connect. For example: # db = SparkSQL.from_uri("sc://localhost:15002", schema=schema) spark_sql = SparkSQL(schema=schema) llm = ChatOpenAI(temperature=0) toolkit = SparkSQLToolkit(db=spark_sql, llm=llm) agent_executor = create_spark_sql_agent( llm=llm, toolkit=toolkit, verbose=True ) Example: describing a table# agent_executor.run("Describe the titanic table") > Entering new AgentExecutor chain... Action: list_tables_sql_db Action Input: Observation: titanic Thought:I found the titanic table. Now I need to get the schema and sample rows for the titanic table. Action: schema_sql_db Action Input: titanic Observation: CREATE TABLE langchain_example.titanic ( PassengerId INT, Survived INT, Pclass INT, Name STRING, Sex STRING, Age DOUBLE, SibSp INT, Parch INT, Ticket STRING, Fare DOUBLE, Cabin STRING, Embarked STRING) ; /* 3 rows from titanic table: PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked 1 0 3 Braund, Mr. Owen Harris male 22.0 1 0 A/5 21171 7.25 None S 2 1 1 Cumings, Mrs. John Bradley (Florence Briggs Thayer) female 38.0 1 0 PC 17599 71.2833 C85 C
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/spark_sql.html
743e75257d92-5
3 1 3 Heikkinen, Miss. Laina female 26.0 0 0 STON/O2. 3101282 7.925 None S */ Thought:I now know the schema and sample rows for the titanic table. Final Answer: The titanic table has the following columns: PassengerId (INT), Survived (INT), Pclass (INT), Name (STRING), Sex (STRING), Age (DOUBLE), SibSp (INT), Parch (INT), Ticket (STRING), Fare (DOUBLE), Cabin (STRING), and Embarked (STRING). Here are some sample rows from the table: 1. PassengerId: 1, Survived: 0, Pclass: 3, Name: Braund, Mr. Owen Harris, Sex: male, Age: 22.0, SibSp: 1, Parch: 0, Ticket: A/5 21171, Fare: 7.25, Cabin: None, Embarked: S 2. PassengerId: 2, Survived: 1, Pclass: 1, Name: Cumings, Mrs. John Bradley (Florence Briggs Thayer), Sex: female, Age: 38.0, SibSp: 1, Parch: 0, Ticket: PC 17599, Fare: 71.2833, Cabin: C85, Embarked: C 3. PassengerId: 3, Survived: 1, Pclass: 3, Name: Heikkinen, Miss. Laina, Sex: female, Age: 26.0, SibSp: 0, Parch: 0, Ticket: STON/O2. 3101282, Fare: 7.925, Cabin: None, Embarked: S > Finished chain.
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/spark_sql.html
743e75257d92-6
> Finished chain. 'The titanic table has the following columns: PassengerId (INT), Survived (INT), Pclass (INT), Name (STRING), Sex (STRING), Age (DOUBLE), SibSp (INT), Parch (INT), Ticket (STRING), Fare (DOUBLE), Cabin (STRING), and Embarked (STRING). Here are some sample rows from the table: \n\n1. PassengerId: 1, Survived: 0, Pclass: 3, Name: Braund, Mr. Owen Harris, Sex: male, Age: 22.0, SibSp: 1, Parch: 0, Ticket: A/5 21171, Fare: 7.25, Cabin: None, Embarked: S\n2. PassengerId: 2, Survived: 1, Pclass: 1, Name: Cumings, Mrs. John Bradley (Florence Briggs Thayer), Sex: female, Age: 38.0, SibSp: 1, Parch: 0, Ticket: PC 17599, Fare: 71.2833, Cabin: C85, Embarked: C\n3. PassengerId: 3, Survived: 1, Pclass: 3, Name: Heikkinen, Miss. Laina, Sex: female, Age: 26.0, SibSp: 0, Parch: 0, Ticket: STON/O2. 3101282, Fare: 7.925, Cabin: None, Embarked: S' Example: running queries# agent_executor.run("whats the square root of the average age?") > Entering new AgentExecutor chain... Action: list_tables_sql_db Action Input: Observation: titanic
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/spark_sql.html
743e75257d92-7
Action: list_tables_sql_db Action Input: Observation: titanic Thought:I should check the schema of the titanic table to see if there is an age column. Action: schema_sql_db Action Input: titanic Observation: CREATE TABLE langchain_example.titanic ( PassengerId INT, Survived INT, Pclass INT, Name STRING, Sex STRING, Age DOUBLE, SibSp INT, Parch INT, Ticket STRING, Fare DOUBLE, Cabin STRING, Embarked STRING) ; /* 3 rows from titanic table: PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked 1 0 3 Braund, Mr. Owen Harris male 22.0 1 0 A/5 21171 7.25 None S 2 1 1 Cumings, Mrs. John Bradley (Florence Briggs Thayer) female 38.0 1 0 PC 17599 71.2833 C85 C 3 1 3 Heikkinen, Miss. Laina female 26.0 0 0 STON/O2. 3101282 7.925 None S */ Thought:There is an Age column in the titanic table. I should write a query to calculate the average age and then find the square root of the result. Action: query_checker_sql_db Action Input: SELECT SQRT(AVG(Age)) as square_root_of_avg_age FROM titanic Observation: The original query seems to be correct. Here it is again: SELECT SQRT(AVG(Age)) as square_root_of_avg_age FROM titanic
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/spark_sql.html
743e75257d92-8
SELECT SQRT(AVG(Age)) as square_root_of_avg_age FROM titanic Thought:The query is correct, so I can execute it to find the square root of the average age. Action: query_sql_db Action Input: SELECT SQRT(AVG(Age)) as square_root_of_avg_age FROM titanic Observation: [('5.449689683556195',)] Thought:I now know the final answer Final Answer: The square root of the average age is approximately 5.45. > Finished chain. 'The square root of the average age is approximately 5.45.' agent_executor.run("What's the name of the oldest survived passenger?") > Entering new AgentExecutor chain... Action: list_tables_sql_db Action Input: Observation: titanic Thought:I should check the schema of the titanic table to see what columns are available. Action: schema_sql_db Action Input: titanic Observation: CREATE TABLE langchain_example.titanic ( PassengerId INT, Survived INT, Pclass INT, Name STRING, Sex STRING, Age DOUBLE, SibSp INT, Parch INT, Ticket STRING, Fare DOUBLE, Cabin STRING, Embarked STRING) ; /* 3 rows from titanic table: PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked 1 0 3 Braund, Mr. Owen Harris male 22.0 1 0 A/5 21171 7.25 None S
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/spark_sql.html
743e75257d92-9
2 1 1 Cumings, Mrs. John Bradley (Florence Briggs Thayer) female 38.0 1 0 PC 17599 71.2833 C85 C 3 1 3 Heikkinen, Miss. Laina female 26.0 0 0 STON/O2. 3101282 7.925 None S */ Thought:I can use the titanic table to find the oldest survived passenger. I will query the Name and Age columns, filtering by Survived and ordering by Age in descending order. Action: query_checker_sql_db Action Input: SELECT Name, Age FROM titanic WHERE Survived = 1 ORDER BY Age DESC LIMIT 1 Observation: SELECT Name, Age FROM titanic WHERE Survived = 1 ORDER BY Age DESC LIMIT 1 Thought:The query is correct. Now I will execute it to find the oldest survived passenger. Action: query_sql_db Action Input: SELECT Name, Age FROM titanic WHERE Survived = 1 ORDER BY Age DESC LIMIT 1 Observation: [('Barkworth, Mr. Algernon Henry Wilson', '80.0')] Thought:I now know the final answer. Final Answer: The oldest survived passenger is Barkworth, Mr. Algernon Henry Wilson, who was 80 years old. > Finished chain. 'The oldest survived passenger is Barkworth, Mr. Algernon Henry Wilson, who was 80 years old.' previous Spark Dataframe Agent next SQL Database Agent Contents Initialization Example: describing a table Example: running queries By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 07, 2023.
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/spark_sql.html
f2ba056e40d7-0
.ipynb .pdf Azure Cognitive Services Toolkit Contents Create the Toolkit Use within an Agent Azure Cognitive Services Toolkit# This toolkit is used to interact with the Azure Cognitive Services API to achieve some multimodal capabilities. Currently There are four tools bundled in this toolkit: AzureCogsImageAnalysisTool: used to extract caption, objects, tags, and text from images. (Note: this tool is not available on Mac OS yet, due to the dependency on azure-ai-vision package, which is only supported on Windows and Linux currently.) AzureCogsFormRecognizerTool: used to extract text, tables, and key-value pairs from documents. AzureCogsSpeech2TextTool: used to transcribe speech to text. AzureCogsText2SpeechTool: used to synthesize text to speech. First, you need to set up an Azure account and create a Cognitive Services resource. You can follow the instructions here to create a resource. Then, you need to get the endpoint, key and region of your resource, and set them as environment variables. You can find them in the “Keys and Endpoint” page of your resource. # !pip install --upgrade azure-ai-formrecognizer > /dev/null # !pip install --upgrade azure-cognitiveservices-speech > /dev/null # For Windows/Linux # !pip install --upgrade azure-ai-vision > /dev/null import os os.environ["OPENAI_API_KEY"] = "sk-" os.environ["AZURE_COGS_KEY"] = "" os.environ["AZURE_COGS_ENDPOINT"] = "" os.environ["AZURE_COGS_REGION"] = "" Create the Toolkit# from langchain.agents.agent_toolkits import AzureCognitiveServicesToolkit toolkit = AzureCognitiveServicesToolkit() [tool.name for tool in toolkit.get_tools()]
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/azure_cognitive_services.html
f2ba056e40d7-1
[tool.name for tool in toolkit.get_tools()] ['Azure Cognitive Services Image Analysis', 'Azure Cognitive Services Form Recognizer', 'Azure Cognitive Services Speech2Text', 'Azure Cognitive Services Text2Speech'] Use within an Agent# from langchain import OpenAI from langchain.agents import initialize_agent, AgentType llm = OpenAI(temperature=0) agent = initialize_agent( tools=toolkit.get_tools(), llm=llm, agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=True, ) agent.run("What can I make with these ingredients?" "https://images.openai.com/blob/9ad5a2ab-041f-475f-ad6a-b51899c50182/ingredients.png") > Entering new AgentExecutor chain... Action: ``` { "action": "Azure Cognitive Services Image Analysis", "action_input": "https://images.openai.com/blob/9ad5a2ab-041f-475f-ad6a-b51899c50182/ingredients.png" } ``` Observation: Caption: a group of eggs and flour in bowls Objects: Egg, Egg, Food Tags: dairy, ingredient, indoor, thickening agent, food, mixing bowl, powder, flour, egg, bowl Thought: I can use the objects and tags to suggest recipes Action: ``` { "action": "Final Answer", "action_input": "You can make pancakes, omelettes, or quiches with these ingredients!" } ``` > Finished chain. 'You can make pancakes, omelettes, or quiches with these ingredients!' audio_file = agent.run("Tell me a joke and read it out for me.")
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/azure_cognitive_services.html
f2ba056e40d7-2
audio_file = agent.run("Tell me a joke and read it out for me.") > Entering new AgentExecutor chain... Action: ``` { "action": "Azure Cognitive Services Text2Speech", "action_input": "Why did the chicken cross the playground? To get to the other slide!" } ``` Observation: /tmp/tmpa3uu_j6b.wav Thought: I have the audio file of the joke Action: ``` { "action": "Final Answer", "action_input": "/tmp/tmpa3uu_j6b.wav" } ``` > Finished chain. '/tmp/tmpa3uu_j6b.wav' from IPython import display audio = display.Audio(audio_file) display.display(audio) previous Toolkits next CSV Agent Contents Create the Toolkit Use within an Agent By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 07, 2023.
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/azure_cognitive_services.html
27abb9b509f5-0
.ipynb .pdf CSV Agent Contents Multi CSV Example CSV Agent# This notebook shows how to use agents to interact with a csv. It is mostly optimized for question answering. NOTE: this agent calls the Pandas DataFrame agent under the hood, which in turn calls the Python agent, which executes LLM generated Python code - this can be bad if the LLM generated Python code is harmful. Use cautiously. from langchain.agents import create_csv_agent from langchain.llms import OpenAI agent = create_csv_agent(OpenAI(temperature=0), 'titanic.csv', verbose=True) agent.run("how many rows are there?") > Entering new AgentExecutor chain... Thought: I need to count the number of rows Action: python_repl_ast Action Input: df.shape[0] Observation: 891 Thought: I now know the final answer Final Answer: There are 891 rows. > Finished chain. 'There are 891 rows.' agent.run("how many people have more than 3 siblings") > Entering new AgentExecutor chain... Thought: I need to count the number of people with more than 3 siblings Action: python_repl_ast Action Input: df[df['SibSp'] > 3].shape[0] Observation: 30 Thought: I now know the final answer Final Answer: 30 people have more than 3 siblings. > Finished chain. '30 people have more than 3 siblings.' agent.run("whats the square root of the average age?") > Entering new AgentExecutor chain... Thought: I need to calculate the average age first Action: python_repl_ast Action Input: df['Age'].mean() Observation: 29.69911764705882
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/csv.html
27abb9b509f5-1
Observation: 29.69911764705882 Thought: I now need to calculate the square root of the average age Action: python_repl_ast Action Input: math.sqrt(df['Age'].mean()) Observation: NameError("name 'math' is not defined") Thought: I need to import the math library Action: python_repl_ast Action Input: import math Observation: Thought: I now need to calculate the square root of the average age Action: python_repl_ast Action Input: math.sqrt(df['Age'].mean()) Observation: 5.449689683556195 Thought: I now know the final answer Final Answer: 5.449689683556195 > Finished chain. '5.449689683556195' Multi CSV Example# This next part shows how the agent can interact with multiple csv files passed in as a list. agent = create_csv_agent(OpenAI(temperature=0), ['titanic.csv', 'titanic_age_fillna.csv'], verbose=True) agent.run("how many rows in the age column are different?") > Entering new AgentExecutor chain... Thought: I need to compare the age columns in both dataframes Action: python_repl_ast Action Input: len(df1[df1['Age'] != df2['Age']]) Observation: 177 Thought: I now know the final answer Final Answer: 177 rows in the age column are different. > Finished chain. '177 rows in the age column are different.' previous Azure Cognitive Services Toolkit next Gmail Toolkit Contents Multi CSV Example By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 07, 2023.
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/csv.html
0644112876f9-0
.ipynb .pdf SQL Database Agent Contents Initialization Example: describing a table Example: describing a table, recovering from an error Example: running queries Recovering from an error SQL Database Agent# This notebook showcases an agent designed to interact with a sql databases. The agent builds off of SQLDatabaseChain and is designed to answer more general questions about a database, as well as recover from errors. Note that, as this agent is in active development, all answers might not be correct. Additionally, it is not guaranteed that the agent won’t perform DML statements on your database given certain questions. Be careful running it on sensitive data! This uses the example Chinook database. To set it up follow the instructions on https://database.guide/2-sample-databases-sqlite/, placing the .db file in a notebooks folder at the root of this repository. Initialization# from langchain.agents import create_sql_agent from langchain.agents.agent_toolkits import SQLDatabaseToolkit from langchain.sql_database import SQLDatabase from langchain.llms.openai import OpenAI from langchain.agents import AgentExecutor db = SQLDatabase.from_uri("sqlite:///../../../../notebooks/Chinook.db") toolkit = SQLDatabaseToolkit(db=db) agent_executor = create_sql_agent( llm=OpenAI(temperature=0), toolkit=toolkit, verbose=True ) Example: describing a table# agent_executor.run("Describe the playlisttrack table") > Entering new AgentExecutor chain... Action: list_tables_sql_db Action Input: "" Observation: Artist, Invoice, Playlist, Genre, Album, PlaylistTrack, Track, InvoiceLine, MediaType, Employee, Customer Thought: I should look at the schema of the playlisttrack table Action: schema_sql_db Action Input: "PlaylistTrack" Observation:
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/sql_database.html
0644112876f9-1
Action: schema_sql_db Action Input: "PlaylistTrack" Observation: CREATE TABLE "PlaylistTrack" ( "PlaylistId" INTEGER NOT NULL, "TrackId" INTEGER NOT NULL, PRIMARY KEY ("PlaylistId", "TrackId"), FOREIGN KEY("TrackId") REFERENCES "Track" ("TrackId"), FOREIGN KEY("PlaylistId") REFERENCES "Playlist" ("PlaylistId") ) SELECT * FROM 'PlaylistTrack' LIMIT 3; PlaylistId TrackId 1 3402 1 3389 1 3390 Thought: I now know the final answer Final Answer: The PlaylistTrack table has two columns, PlaylistId and TrackId, and is linked to the Playlist and Track tables. > Finished chain. 'The PlaylistTrack table has two columns, PlaylistId and TrackId, and is linked to the Playlist and Track tables.' Example: describing a table, recovering from an error# In this example, the agent tries to search for a table that doesn’t exist, but finds the next best result agent_executor.run("Describe the playlistsong table") > Entering new AgentExecutor chain... Action: list_tables_sql_db Action Input: "" Observation: Genre, PlaylistTrack, MediaType, Invoice, InvoiceLine, Track, Playlist, Customer, Album, Employee, Artist Thought: I should look at the schema of the PlaylistSong table Action: schema_sql_db Action Input: "PlaylistSong" Observation: Error: table_names {'PlaylistSong'} not found in database Thought: I should check the spelling of the table Action: list_tables_sql_db Action Input: "" Observation: Genre, PlaylistTrack, MediaType, Invoice, InvoiceLine, Track, Playlist, Customer, Album, Employee, Artist Thought: The table is called PlaylistTrack
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/sql_database.html
0644112876f9-2
Thought: The table is called PlaylistTrack Action: schema_sql_db Action Input: "PlaylistTrack" Observation: CREATE TABLE "PlaylistTrack" ( "PlaylistId" INTEGER NOT NULL, "TrackId" INTEGER NOT NULL, PRIMARY KEY ("PlaylistId", "TrackId"), FOREIGN KEY("TrackId") REFERENCES "Track" ("TrackId"), FOREIGN KEY("PlaylistId") REFERENCES "Playlist" ("PlaylistId") ) SELECT * FROM 'PlaylistTrack' LIMIT 3; PlaylistId TrackId 1 3402 1 3389 1 3390 Thought: I now know the final answer Final Answer: The PlaylistTrack table contains two columns, PlaylistId and TrackId, which are both integers and are used to link Playlist and Track tables. > Finished chain. 'The PlaylistTrack table contains two columns, PlaylistId and TrackId, which are both integers and are used to link Playlist and Track tables.' Example: running queries# agent_executor.run("List the total sales per country. Which country's customers spent the most?") > Entering new AgentExecutor chain... Action: list_tables_sql_db Action Input: "" Observation: Invoice, MediaType, Artist, InvoiceLine, Genre, Playlist, Employee, Album, PlaylistTrack, Track, Customer Thought: I should look at the schema of the relevant tables to see what columns I can use. Action: schema_sql_db Action Input: "Invoice, Customer" Observation: CREATE TABLE "Customer" ( "CustomerId" INTEGER NOT NULL, "FirstName" NVARCHAR(40) NOT NULL, "LastName" NVARCHAR(20) NOT NULL, "Company" NVARCHAR(80), "Address" NVARCHAR(70),
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/sql_database.html
0644112876f9-3
"Address" NVARCHAR(70), "City" NVARCHAR(40), "State" NVARCHAR(40), "Country" NVARCHAR(40), "PostalCode" NVARCHAR(10), "Phone" NVARCHAR(24), "Fax" NVARCHAR(24), "Email" NVARCHAR(60) NOT NULL, "SupportRepId" INTEGER, PRIMARY KEY ("CustomerId"), FOREIGN KEY("SupportRepId") REFERENCES "Employee" ("EmployeeId") ) SELECT * FROM 'Customer' LIMIT 3; CustomerId FirstName LastName Company Address City State Country PostalCode Phone Fax Email SupportRepId 1 Luís Gonçalves Embraer - Empresa Brasileira de Aeronáutica S.A. Av. Brigadeiro Faria Lima, 2170 São José dos Campos SP Brazil 12227-000 +55 (12) 3923-5555 +55 (12) 3923-5566 [email protected] 3 2 Leonie Köhler None Theodor-Heuss-Straße 34 Stuttgart None Germany 70174 +49 0711 2842222 None [email protected] 5 3 François Tremblay None 1498 rue Bélanger Montréal QC Canada H2G 1A7 +1 (514) 721-4711 None [email protected] 3 CREATE TABLE "Invoice" ( "InvoiceId" INTEGER NOT NULL, "CustomerId" INTEGER NOT NULL, "InvoiceDate" DATETIME NOT NULL, "BillingAddress" NVARCHAR(70), "BillingCity" NVARCHAR(40),
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/sql_database.html
0644112876f9-4
"BillingCity" NVARCHAR(40), "BillingState" NVARCHAR(40), "BillingCountry" NVARCHAR(40), "BillingPostalCode" NVARCHAR(10), "Total" NUMERIC(10, 2) NOT NULL, PRIMARY KEY ("InvoiceId"), FOREIGN KEY("CustomerId") REFERENCES "Customer" ("CustomerId") ) SELECT * FROM 'Invoice' LIMIT 3; InvoiceId CustomerId InvoiceDate BillingAddress BillingCity BillingState BillingCountry BillingPostalCode Total 1 2 2009-01-01 00:00:00 Theodor-Heuss-Straße 34 Stuttgart None Germany 70174 1.98 2 4 2009-01-02 00:00:00 Ullevålsveien 14 Oslo None Norway 0171 3.96 3 8 2009-01-03 00:00:00 Grétrystraat 63 Brussels None Belgium 1000 5.94 Thought: I should query the Invoice and Customer tables to get the total sales per country. Action: query_sql_db Action Input: SELECT c.Country, SUM(i.Total) AS TotalSales FROM Invoice i INNER JOIN Customer c ON i.CustomerId = c.CustomerId GROUP BY c.Country ORDER BY TotalSales DESC LIMIT 10
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/sql_database.html
0644112876f9-5
Observation: [('USA', 523.0600000000003), ('Canada', 303.9599999999999), ('France', 195.09999999999994), ('Brazil', 190.09999999999997), ('Germany', 156.48), ('United Kingdom', 112.85999999999999), ('Czech Republic', 90.24000000000001), ('Portugal', 77.23999999999998), ('India', 75.25999999999999), ('Chile', 46.62)] Thought: I now know the final answer Final Answer: The customers from the USA spent the most, with a total of $523.06. > Finished chain. 'The customers from the USA spent the most, with a total of $523.06.' agent_executor.run("Show the total number of tracks in each playlist. The Playlist name should be included in the result.") > Entering new AgentExecutor chain... Action: list_tables_sql_db Action Input: "" Observation: Invoice, MediaType, Artist, InvoiceLine, Genre, Playlist, Employee, Album, PlaylistTrack, Track, Customer Thought: I should look at the schema of the Playlist and PlaylistTrack tables to see what columns I can use. Action: schema_sql_db Action Input: "Playlist, PlaylistTrack" Observation: CREATE TABLE "Playlist" ( "PlaylistId" INTEGER NOT NULL, "Name" NVARCHAR(120), PRIMARY KEY ("PlaylistId") ) SELECT * FROM 'Playlist' LIMIT 3; PlaylistId Name 1 Music 2 Movies 3 TV Shows CREATE TABLE "PlaylistTrack" ( "PlaylistId" INTEGER NOT NULL, "TrackId" INTEGER NOT NULL,
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/sql_database.html
0644112876f9-6
"TrackId" INTEGER NOT NULL, PRIMARY KEY ("PlaylistId", "TrackId"), FOREIGN KEY("TrackId") REFERENCES "Track" ("TrackId"), FOREIGN KEY("PlaylistId") REFERENCES "Playlist" ("PlaylistId") ) SELECT * FROM 'PlaylistTrack' LIMIT 3; PlaylistId TrackId 1 3402 1 3389 1 3390 Thought: I can use a SELECT statement to get the total number of tracks in each playlist. Action: query_checker_sql_db Action Input: SELECT Playlist.Name, COUNT(PlaylistTrack.TrackId) AS TotalTracks FROM Playlist INNER JOIN PlaylistTrack ON Playlist.PlaylistId = PlaylistTrack.PlaylistId GROUP BY Playlist.Name Observation: SELECT Playlist.Name, COUNT(PlaylistTrack.TrackId) AS TotalTracks FROM Playlist INNER JOIN PlaylistTrack ON Playlist.PlaylistId = PlaylistTrack.PlaylistId GROUP BY Playlist.Name Thought: The query looks correct, I can now execute it. Action: query_sql_db Action Input: SELECT Playlist.Name, COUNT(PlaylistTrack.TrackId) AS TotalTracks FROM Playlist INNER JOIN PlaylistTrack ON Playlist.PlaylistId = PlaylistTrack.PlaylistId GROUP BY Playlist.Name LIMIT 10 Observation: [('90’s Music', 1477), ('Brazilian Music', 39), ('Classical', 75), ('Classical 101 - Deep Cuts', 25), ('Classical 101 - Next Steps', 25), ('Classical 101 - The Basics', 25), ('Grunge', 15), ('Heavy Metal Classic', 26), ('Music', 6580), ('Music Videos', 1)] Thought: I now know the final answer.
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/sql_database.html
0644112876f9-7
Thought: I now know the final answer. Final Answer: The total number of tracks in each playlist are: '90’s Music' (1477), 'Brazilian Music' (39), 'Classical' (75), 'Classical 101 - Deep Cuts' (25), 'Classical 101 - Next Steps' (25), 'Classical 101 - The Basics' (25), 'Grunge' (15), 'Heavy Metal Classic' (26), 'Music' (6580), 'Music Videos' (1). > Finished chain. "The total number of tracks in each playlist are: '90’s Music' (1477), 'Brazilian Music' (39), 'Classical' (75), 'Classical 101 - Deep Cuts' (25), 'Classical 101 - Next Steps' (25), 'Classical 101 - The Basics' (25), 'Grunge' (15), 'Heavy Metal Classic' (26), 'Music' (6580), 'Music Videos' (1)." Recovering from an error# In this example, the agent is able to recover from an error after initially trying to access an attribute (Track.ArtistId) which doesn’t exist. agent_executor.run("Who are the top 3 best selling artists?") > Entering new AgentExecutor chain... Action: list_tables_sql_db Action Input: "" Observation: MediaType, Track, Invoice, Album, Playlist, Customer, Employee, InvoiceLine, PlaylistTrack, Genre, Artist Thought: I should look at the schema of the Artist, InvoiceLine, and Track tables to see what columns I can use. Action: schema_sql_db Action Input: "Artist, InvoiceLine, Track" Observation: CREATE TABLE "Artist" ( "ArtistId" INTEGER NOT NULL,
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/sql_database.html
0644112876f9-8
CREATE TABLE "Artist" ( "ArtistId" INTEGER NOT NULL, "Name" NVARCHAR(120), PRIMARY KEY ("ArtistId") ) SELECT * FROM 'Artist' LIMIT 3; ArtistId Name 1 AC/DC 2 Accept 3 Aerosmith CREATE TABLE "Track" ( "TrackId" INTEGER NOT NULL, "Name" NVARCHAR(200) NOT NULL, "AlbumId" INTEGER, "MediaTypeId" INTEGER NOT NULL, "GenreId" INTEGER, "Composer" NVARCHAR(220), "Milliseconds" INTEGER NOT NULL, "Bytes" INTEGER, "UnitPrice" NUMERIC(10, 2) NOT NULL, PRIMARY KEY ("TrackId"), FOREIGN KEY("MediaTypeId") REFERENCES "MediaType" ("MediaTypeId"), FOREIGN KEY("GenreId") REFERENCES "Genre" ("GenreId"), FOREIGN KEY("AlbumId") REFERENCES "Album" ("AlbumId") ) SELECT * FROM 'Track' LIMIT 3; TrackId Name AlbumId MediaTypeId GenreId Composer Milliseconds Bytes UnitPrice 1 For Those About To Rock (We Salute You) 1 1 1 Angus Young, Malcolm Young, Brian Johnson 343719 11170334 0.99 2 Balls to the Wall 2 2 1 None 342562 5510424 0.99 3 Fast As a Shark 3 2 1 F. Baltes, S. Kaufman, U. Dirkscneider & W. Hoffman 230619 3990994 0.99 CREATE TABLE "InvoiceLine" ( "InvoiceLineId" INTEGER NOT NULL, "InvoiceId" INTEGER NOT NULL,
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/sql_database.html
0644112876f9-9
"InvoiceId" INTEGER NOT NULL, "TrackId" INTEGER NOT NULL, "UnitPrice" NUMERIC(10, 2) NOT NULL, "Quantity" INTEGER NOT NULL, PRIMARY KEY ("InvoiceLineId"), FOREIGN KEY("TrackId") REFERENCES "Track" ("TrackId"), FOREIGN KEY("InvoiceId") REFERENCES "Invoice" ("InvoiceId") ) SELECT * FROM 'InvoiceLine' LIMIT 3; InvoiceLineId InvoiceId TrackId UnitPrice Quantity 1 1 2 0.99 1 2 1 4 0.99 1 3 2 6 0.99 1 Thought: I should query the database to get the top 3 best selling artists. Action: query_sql_db Action Input: SELECT Artist.Name, SUM(InvoiceLine.Quantity) AS TotalQuantity FROM Artist INNER JOIN Track ON Artist.ArtistId = Track.ArtistId INNER JOIN InvoiceLine ON Track.TrackId = InvoiceLine.TrackId GROUP BY Artist.Name ORDER BY TotalQuantity DESC LIMIT 3 Observation: Error: (sqlite3.OperationalError) no such column: Track.ArtistId [SQL: SELECT Artist.Name, SUM(InvoiceLine.Quantity) AS TotalQuantity FROM Artist INNER JOIN Track ON Artist.ArtistId = Track.ArtistId INNER JOIN InvoiceLine ON Track.TrackId = InvoiceLine.TrackId GROUP BY Artist.Name ORDER BY TotalQuantity DESC LIMIT 3] (Background on this error at: https://sqlalche.me/e/14/e3q8) Thought: I should double check my query before executing it. Action: query_checker_sql_db
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/sql_database.html
0644112876f9-10
Thought: I should double check my query before executing it. Action: query_checker_sql_db Action Input: SELECT Artist.Name, SUM(InvoiceLine.Quantity) AS TotalQuantity FROM Artist INNER JOIN Track ON Artist.ArtistId = Track.ArtistId INNER JOIN InvoiceLine ON Track.TrackId = InvoiceLine.TrackId GROUP BY Artist.Name ORDER BY TotalQuantity DESC LIMIT 3 Observation: SELECT Artist.Name, SUM(InvoiceLine.Quantity) AS TotalQuantity FROM Artist INNER JOIN Track ON Artist.ArtistId = Track.ArtistId INNER JOIN InvoiceLine ON Track.TrackId = InvoiceLine.TrackId GROUP BY Artist.Name ORDER BY TotalQuantity DESC LIMIT 3; Thought: I now know the final answer. Action: query_sql_db Action Input: SELECT Artist.Name, SUM(InvoiceLine.Quantity) AS TotalQuantity FROM Artist INNER JOIN Album ON Artist.ArtistId = Album.ArtistId INNER JOIN Track ON Album.AlbumId = Track.AlbumId INNER JOIN InvoiceLine ON Track.TrackId = InvoiceLine.TrackId GROUP BY Artist.Name ORDER BY TotalQuantity DESC LIMIT 3 Observation: [('Iron Maiden', 140), ('U2', 107), ('Metallica', 91)] Thought: I now know the final answer. Final Answer: The top 3 best selling artists are Iron Maiden, U2, and Metallica. > Finished chain. 'The top 3 best selling artists are Iron Maiden, U2, and Metallica.' previous Spark SQL Agent next Vectorstore Agent Contents Initialization Example: describing a table Example: describing a table, recovering from an error Example: running queries Recovering from an error By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 07, 2023.
https://python.langchain.com/en/latest/modules/agents/toolkits/examples/sql_database.html
43353293066b-0
.ipynb .pdf Custom LLM Agent Contents Set up environment Set up tool Prompt Template Output Parser Set up LLM Define the stop sequence Set up the Agent Use the Agent Adding Memory Custom LLM Agent# This notebook goes through how to create your own custom LLM agent. An LLM agent consists of three parts: PromptTemplate: This is the prompt template that can be used to instruct the language model on what to do LLM: This is the language model that powers the agent stop sequence: Instructs the LLM to stop generating as soon as this string is found OutputParser: This determines how to parse the LLMOutput into an AgentAction or AgentFinish object The LLMAgent is used in an AgentExecutor. This AgentExecutor can largely be thought of as a loop that: Passes user input and any previous steps to the Agent (in this case, the LLMAgent) If the Agent returns an AgentFinish, then return that directly to the user If the Agent returns an AgentAction, then use that to call a tool and get an Observation Repeat, passing the AgentAction and Observation back to the Agent until an AgentFinish is emitted. AgentAction is a response that consists of action and action_input. action refers to which tool to use, and action_input refers to the input to that tool. log can also be provided as more context (that can be used for logging, tracing, etc). AgentFinish is a response that contains the final message to be sent back to the user. This should be used to end an agent run. In this notebook we walk through how to create a custom LLM agent. Set up environment# Do necessary imports, etc. from langchain.agents import Tool, AgentExecutor, LLMSingleActionAgent, AgentOutputParser from langchain.prompts import StringPromptTemplate
https://python.langchain.com/en/latest/modules/agents/agents/custom_llm_agent.html
43353293066b-1
from langchain.prompts import StringPromptTemplate from langchain import OpenAI, SerpAPIWrapper, LLMChain from typing import List, Union from langchain.schema import AgentAction, AgentFinish import re Set up tool# Set up any tools the agent may want to use. This may be necessary to put in the prompt (so that the agent knows to use these tools). # Define which tools the agent can use to answer user queries search = SerpAPIWrapper() tools = [ Tool( name = "Search", func=search.run, description="useful for when you need to answer questions about current events" ) ] Prompt Template# This instructs the agent on what to do. Generally, the template should incorporate: tools: which tools the agent has access and how and when to call them. intermediate_steps: These are tuples of previous (AgentAction, Observation) pairs. These are generally not passed directly to the model, but the prompt template formats them in a specific way. input: generic user input # Set up the base template template = """Answer the following questions as best you can, but speaking as a pirate might speak. You have access to the following tools: {tools} Use the following format: Question: the input question you must answer Thought: you should always think about what to do Action: the action to take, should be one of [{tool_names}] Action Input: the input to the action Observation: the result of the action ... (this Thought/Action/Action Input/Observation can repeat N times) Thought: I now know the final answer Final Answer: the final answer to the original input question Begin! Remember to speak as a pirate when giving your final answer. Use lots of "Arg"s Question: {input}
https://python.langchain.com/en/latest/modules/agents/agents/custom_llm_agent.html
43353293066b-2
Question: {input} {agent_scratchpad}""" # Set up a prompt template class CustomPromptTemplate(StringPromptTemplate): # The template to use template: str # The list of tools available tools: List[Tool] def format(self, **kwargs) -> str: # Get the intermediate steps (AgentAction, Observation tuples) # Format them in a particular way intermediate_steps = kwargs.pop("intermediate_steps") thoughts = "" for action, observation in intermediate_steps: thoughts += action.log thoughts += f"\nObservation: {observation}\nThought: " # Set the agent_scratchpad variable to that value kwargs["agent_scratchpad"] = thoughts # Create a tools variable from the list of tools provided kwargs["tools"] = "\n".join([f"{tool.name}: {tool.description}" for tool in self.tools]) # Create a list of tool names for the tools provided kwargs["tool_names"] = ", ".join([tool.name for tool in self.tools]) return self.template.format(**kwargs) prompt = CustomPromptTemplate( template=template, tools=tools, # This omits the `agent_scratchpad`, `tools`, and `tool_names` variables because those are generated dynamically # This includes the `intermediate_steps` variable because that is needed input_variables=["input", "intermediate_steps"] ) Output Parser# The output parser is responsible for parsing the LLM output into AgentAction and AgentFinish. This usually depends heavily on the prompt used. This is where you can change the parsing to do retries, handle whitespace, etc class CustomOutputParser(AgentOutputParser):
https://python.langchain.com/en/latest/modules/agents/agents/custom_llm_agent.html
43353293066b-3
class CustomOutputParser(AgentOutputParser): def parse(self, llm_output: str) -> Union[AgentAction, AgentFinish]: # Check if agent should finish if "Final Answer:" in llm_output: return AgentFinish( # Return values is generally always a dictionary with a single `output` key # It is not recommended to try anything else at the moment :) return_values={"output": llm_output.split("Final Answer:")[-1].strip()}, log=llm_output, ) # Parse out the action and action input regex = r"Action\s*\d*\s*:(.*?)\nAction\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)" match = re.search(regex, llm_output, re.DOTALL) if not match: raise ValueError(f"Could not parse LLM output: `{llm_output}`") action = match.group(1).strip() action_input = match.group(2) # Return the action and action input return AgentAction(tool=action, tool_input=action_input.strip(" ").strip('"'), log=llm_output) output_parser = CustomOutputParser() Set up LLM# Choose the LLM you want to use! llm = OpenAI(temperature=0) Define the stop sequence# This is important because it tells the LLM when to stop generation. This depends heavily on the prompt and model you are using. Generally, you want this to be whatever token you use in the prompt to denote the start of an Observation (otherwise, the LLM may hallucinate an observation for you). Set up the Agent# We can now combine everything to set up our agent
https://python.langchain.com/en/latest/modules/agents/agents/custom_llm_agent.html
43353293066b-4
Set up the Agent# We can now combine everything to set up our agent # LLM chain consisting of the LLM and a prompt llm_chain = LLMChain(llm=llm, prompt=prompt) tool_names = [tool.name for tool in tools] agent = LLMSingleActionAgent( llm_chain=llm_chain, output_parser=output_parser, stop=["\nObservation:"], allowed_tools=tool_names ) Use the Agent# Now we can use it! agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True) agent_executor.run("How many people live in canada as of 2023?") > Entering new AgentExecutor chain... Thought: I need to find out the population of Canada in 2023 Action: Search Action Input: Population of Canada in 2023 Observation:The current population of Canada is 38,658,314 as of Wednesday, April 12, 2023, based on Worldometer elaboration of the latest United Nations data. I now know the final answer Final Answer: Arrr, there be 38,658,314 people livin' in Canada as of 2023! > Finished chain. "Arrr, there be 38,658,314 people livin' in Canada as of 2023!" Adding Memory# If you want to add memory to the agent, you’ll need to: Add a place in the custom prompt for the chat_history Add a memory object to the agent executor. # Set up the base template template_with_history = """Answer the following questions as best you can, but speaking as a pirate might speak. You have access to the following tools: {tools} Use the following format: Question: the input question you must answer
https://python.langchain.com/en/latest/modules/agents/agents/custom_llm_agent.html
43353293066b-5
{tools} Use the following format: Question: the input question you must answer Thought: you should always think about what to do Action: the action to take, should be one of [{tool_names}] Action Input: the input to the action Observation: the result of the action ... (this Thought/Action/Action Input/Observation can repeat N times) Thought: I now know the final answer Final Answer: the final answer to the original input question Begin! Remember to speak as a pirate when giving your final answer. Use lots of "Arg"s Previous conversation history: {history} New question: {input} {agent_scratchpad}""" prompt_with_history = CustomPromptTemplate( template=template_with_history, tools=tools, # This omits the `agent_scratchpad`, `tools`, and `tool_names` variables because those are generated dynamically # This includes the `intermediate_steps` variable because that is needed input_variables=["input", "intermediate_steps", "history"] ) llm_chain = LLMChain(llm=llm, prompt=prompt_with_history) tool_names = [tool.name for tool in tools] agent = LLMSingleActionAgent( llm_chain=llm_chain, output_parser=output_parser, stop=["\nObservation:"], allowed_tools=tool_names ) from langchain.memory import ConversationBufferWindowMemory memory=ConversationBufferWindowMemory(k=2) agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True, memory=memory) agent_executor.run("How many people live in canada as of 2023?") > Entering new AgentExecutor chain... Thought: I need to find out the population of Canada in 2023
https://python.langchain.com/en/latest/modules/agents/agents/custom_llm_agent.html
43353293066b-6
Thought: I need to find out the population of Canada in 2023 Action: Search Action Input: Population of Canada in 2023 Observation:The current population of Canada is 38,658,314 as of Wednesday, April 12, 2023, based on Worldometer elaboration of the latest United Nations data. I now know the final answer Final Answer: Arrr, there be 38,658,314 people livin' in Canada as of 2023! > Finished chain. "Arrr, there be 38,658,314 people livin' in Canada as of 2023!" agent_executor.run("how about in mexico?") > Entering new AgentExecutor chain... Thought: I need to find out how many people live in Mexico. Action: Search Action Input: How many people live in Mexico as of 2023? Observation:The current population of Mexico is 132,679,922 as of Tuesday, April 11, 2023, based on Worldometer elaboration of the latest United Nations data. Mexico 2020 ... I now know the final answer. Final Answer: Arrr, there be 132,679,922 people livin' in Mexico as of 2023! > Finished chain. "Arrr, there be 132,679,922 people livin' in Mexico as of 2023!" previous Custom Agent next Custom LLM Agent (with a ChatModel) Contents Set up environment Set up tool Prompt Template Output Parser Set up LLM Define the stop sequence Set up the Agent Use the Agent Adding Memory By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 07, 2023.
https://python.langchain.com/en/latest/modules/agents/agents/custom_llm_agent.html
271ce0714828-0
.ipynb .pdf Custom Agent Custom Agent# This notebook goes through how to create your own custom agent. An agent consists of two parts: - Tools: The tools the agent has available to use. - The agent class itself: this decides which action to take. In this notebook we walk through how to create a custom agent. from langchain.agents import Tool, AgentExecutor, BaseSingleActionAgent from langchain import OpenAI, SerpAPIWrapper search = SerpAPIWrapper() tools = [ Tool( name = "Search", func=search.run, description="useful for when you need to answer questions about current events", return_direct=True ) ] from typing import List, Tuple, Any, Union from langchain.schema import AgentAction, AgentFinish class FakeAgent(BaseSingleActionAgent): """Fake Custom Agent.""" @property def input_keys(self): return ["input"] def plan( self, intermediate_steps: List[Tuple[AgentAction, str]], **kwargs: Any ) -> Union[AgentAction, AgentFinish]: """Given input, decided what to do. Args: intermediate_steps: Steps the LLM has taken to date, along with observations **kwargs: User inputs. Returns: Action specifying what tool to use. """ return AgentAction(tool="Search", tool_input=kwargs["input"], log="") async def aplan( self, intermediate_steps: List[Tuple[AgentAction, str]], **kwargs: Any ) -> Union[AgentAction, AgentFinish]: """Given input, decided what to do. Args: intermediate_steps: Steps the LLM has taken to date,
https://python.langchain.com/en/latest/modules/agents/agents/custom_agent.html
271ce0714828-1
Args: intermediate_steps: Steps the LLM has taken to date, along with observations **kwargs: User inputs. Returns: Action specifying what tool to use. """ return AgentAction(tool="Search", tool_input=kwargs["input"], log="") agent = FakeAgent() agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True) agent_executor.run("How many people live in canada as of 2023?") > Entering new AgentExecutor chain... The current population of Canada is 38,669,152 as of Monday, April 24, 2023, based on Worldometer elaboration of the latest United Nations data. > Finished chain. 'The current population of Canada is 38,669,152 as of Monday, April 24, 2023, based on Worldometer elaboration of the latest United Nations data.' previous Agent Types next Custom LLM Agent By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 07, 2023.
https://python.langchain.com/en/latest/modules/agents/agents/custom_agent.html
0d188929e298-0
.md .pdf Agent Types Contents zero-shot-react-description react-docstore self-ask-with-search conversational-react-description Agent Types# Agents use an LLM to determine which actions to take and in what order. An action can either be using a tool and observing its output, or returning a response to the user. Here are the agents available in LangChain. zero-shot-react-description# This agent uses the ReAct framework to determine which tool to use based solely on the tool’s description. Any number of tools can be provided. This agent requires that a description is provided for each tool. react-docstore# This agent uses the ReAct framework to interact with a docstore. Two tools must be provided: a Search tool and a Lookup tool (they must be named exactly as so). The Search tool should search for a document, while the Lookup tool should lookup a term in the most recently found document. This agent is equivalent to the original ReAct paper, specifically the Wikipedia example. self-ask-with-search# This agent utilizes a single tool that should be named Intermediate Answer. This tool should be able to lookup factual answers to questions. This agent is equivalent to the original self ask with search paper, where a Google search API was provided as the tool. conversational-react-description# This agent is designed to be used in conversational settings. The prompt is designed to make the agent helpful and conversational. It uses the ReAct framework to decide which tool to use, and uses memory to remember the previous conversation interactions. previous Agents next Custom Agent Contents zero-shot-react-description react-docstore self-ask-with-search conversational-react-description By Harrison Chase © Copyright 2023, Harrison Chase.
https://python.langchain.com/en/latest/modules/agents/agents/agent_types.html
0d188929e298-1
By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 07, 2023.
https://python.langchain.com/en/latest/modules/agents/agents/agent_types.html
8c7c3db9b337-0
.ipynb .pdf Custom LLM Agent (with a ChatModel) Contents Set up environment Set up tool Prompt Template Output Parser Set up LLM Define the stop sequence Set up the Agent Use the Agent Custom LLM Agent (with a ChatModel)# This notebook goes through how to create your own custom agent based on a chat model. An LLM chat agent consists of three parts: PromptTemplate: This is the prompt template that can be used to instruct the language model on what to do ChatModel: This is the language model that powers the agent stop sequence: Instructs the LLM to stop generating as soon as this string is found OutputParser: This determines how to parse the LLMOutput into an AgentAction or AgentFinish object The LLMAgent is used in an AgentExecutor. This AgentExecutor can largely be thought of as a loop that: Passes user input and any previous steps to the Agent (in this case, the LLMAgent) If the Agent returns an AgentFinish, then return that directly to the user If the Agent returns an AgentAction, then use that to call a tool and get an Observation Repeat, passing the AgentAction and Observation back to the Agent until an AgentFinish is emitted. AgentAction is a response that consists of action and action_input. action refers to which tool to use, and action_input refers to the input to that tool. log can also be provided as more context (that can be used for logging, tracing, etc). AgentFinish is a response that contains the final message to be sent back to the user. This should be used to end an agent run. In this notebook we walk through how to create a custom LLM agent. Set up environment# Do necessary imports, etc. !pip install langchain !pip install google-search-results !pip install openai
https://python.langchain.com/en/latest/modules/agents/agents/custom_llm_chat_agent.html
8c7c3db9b337-1
!pip install langchain !pip install google-search-results !pip install openai from langchain.agents import Tool, AgentExecutor, LLMSingleActionAgent, AgentOutputParser from langchain.prompts import BaseChatPromptTemplate from langchain import SerpAPIWrapper, LLMChain from langchain.chat_models import ChatOpenAI from typing import List, Union from langchain.schema import AgentAction, AgentFinish, HumanMessage import re from getpass import getpass Set up tool# Set up any tools the agent may want to use. This may be necessary to put in the prompt (so that the agent knows to use these tools). SERPAPI_API_KEY = getpass() # Define which tools the agent can use to answer user queries search = SerpAPIWrapper(serpapi_api_key=SERPAPI_API_KEY) tools = [ Tool( name = "Search", func=search.run, description="useful for when you need to answer questions about current events" ) ] Prompt Template# This instructs the agent on what to do. Generally, the template should incorporate: tools: which tools the agent has access and how and when to call them. intermediate_steps: These are tuples of previous (AgentAction, Observation) pairs. These are generally not passed directly to the model, but the prompt template formats them in a specific way. input: generic user input # Set up the base template template = """Complete the objective as best you can. You have access to the following tools: {tools} Use the following format: Question: the input question you must answer Thought: you should always think about what to do Action: the action to take, should be one of [{tool_names}] Action Input: the input to the action
https://python.langchain.com/en/latest/modules/agents/agents/custom_llm_chat_agent.html
8c7c3db9b337-2
Action Input: the input to the action Observation: the result of the action ... (this Thought/Action/Action Input/Observation can repeat N times) Thought: I now know the final answer Final Answer: the final answer to the original input question These were previous tasks you completed: Begin! Question: {input} {agent_scratchpad}""" # Set up a prompt template class CustomPromptTemplate(BaseChatPromptTemplate): # The template to use template: str # The list of tools available tools: List[Tool] def format_messages(self, **kwargs) -> str: # Get the intermediate steps (AgentAction, Observation tuples) # Format them in a particular way intermediate_steps = kwargs.pop("intermediate_steps") thoughts = "" for action, observation in intermediate_steps: thoughts += action.log thoughts += f"\nObservation: {observation}\nThought: " # Set the agent_scratchpad variable to that value kwargs["agent_scratchpad"] = thoughts # Create a tools variable from the list of tools provided kwargs["tools"] = "\n".join([f"{tool.name}: {tool.description}" for tool in self.tools]) # Create a list of tool names for the tools provided kwargs["tool_names"] = ", ".join([tool.name for tool in self.tools]) formatted = self.template.format(**kwargs) return [HumanMessage(content=formatted)] prompt = CustomPromptTemplate( template=template, tools=tools, # This omits the `agent_scratchpad`, `tools`, and `tool_names` variables because those are generated dynamically # This includes the `intermediate_steps` variable because that is needed
https://python.langchain.com/en/latest/modules/agents/agents/custom_llm_chat_agent.html
8c7c3db9b337-3
# This includes the `intermediate_steps` variable because that is needed input_variables=["input", "intermediate_steps"] ) Output Parser# The output parser is responsible for parsing the LLM output into AgentAction and AgentFinish. This usually depends heavily on the prompt used. This is where you can change the parsing to do retries, handle whitespace, etc class CustomOutputParser(AgentOutputParser): def parse(self, llm_output: str) -> Union[AgentAction, AgentFinish]: # Check if agent should finish if "Final Answer:" in llm_output: return AgentFinish( # Return values is generally always a dictionary with a single `output` key # It is not recommended to try anything else at the moment :) return_values={"output": llm_output.split("Final Answer:")[-1].strip()}, log=llm_output, ) # Parse out the action and action input regex = r"Action\s*\d*\s*:(.*?)\nAction\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)" match = re.search(regex, llm_output, re.DOTALL) if not match: raise ValueError(f"Could not parse LLM output: `{llm_output}`") action = match.group(1).strip() action_input = match.group(2) # Return the action and action input return AgentAction(tool=action, tool_input=action_input.strip(" ").strip('"'), log=llm_output) output_parser = CustomOutputParser() Set up LLM# Choose the LLM you want to use! OPENAI_API_KEY = getpass() llm = ChatOpenAI(openai_api_key=OPENAI_API_KEY, temperature=0)
https://python.langchain.com/en/latest/modules/agents/agents/custom_llm_chat_agent.html
8c7c3db9b337-4
Define the stop sequence# This is important because it tells the LLM when to stop generation. This depends heavily on the prompt and model you are using. Generally, you want this to be whatever token you use in the prompt to denote the start of an Observation (otherwise, the LLM may hallucinate an observation for you). Set up the Agent# We can now combine everything to set up our agent # LLM chain consisting of the LLM and a prompt llm_chain = LLMChain(llm=llm, prompt=prompt) tool_names = [tool.name for tool in tools] agent = LLMSingleActionAgent( llm_chain=llm_chain, output_parser=output_parser, stop=["\nObservation:"], allowed_tools=tool_names ) Use the Agent# Now we can use it! agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True) agent_executor.run("Search for Leo DiCaprio's girlfriend on the internet.") > Entering new AgentExecutor chain... Thought: I should use a reliable search engine to get accurate information. Action: Search Action Input: "Leo DiCaprio girlfriend" Observation:He went on to date Gisele Bündchen, Bar Refaeli, Blake Lively, Toni Garrn and Nina Agdal, among others, before finally settling down with current girlfriend Camila Morrone, who is 23 years his junior. I have found the answer to the question. Final Answer: Leo DiCaprio's current girlfriend is Camila Morrone. > Finished chain. "Leo DiCaprio's current girlfriend is Camila Morrone." previous Custom LLM Agent next Custom MRKL Agent Contents Set up environment Set up tool Prompt Template Output Parser
https://python.langchain.com/en/latest/modules/agents/agents/custom_llm_chat_agent.html
8c7c3db9b337-5
Contents Set up environment Set up tool Prompt Template Output Parser Set up LLM Define the stop sequence Set up the Agent Use the Agent By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 07, 2023.
https://python.langchain.com/en/latest/modules/agents/agents/custom_llm_chat_agent.html
b4a354291af4-0
.ipynb .pdf Custom MRKL Agent Contents Custom LLMChain Multiple inputs Custom MRKL Agent# This notebook goes through how to create your own custom MRKL agent. A MRKL agent consists of three parts: - Tools: The tools the agent has available to use. - LLMChain: The LLMChain that produces the text that is parsed in a certain way to determine which action to take. - The agent class itself: this parses the output of the LLMChain to determine which action to take. In this notebook we walk through how to create a custom MRKL agent by creating a custom LLMChain. Custom LLMChain# The first way to create a custom agent is to use an existing Agent class, but use a custom LLMChain. This is the simplest way to create a custom Agent. It is highly recommended that you work with the ZeroShotAgent, as at the moment that is by far the most generalizable one. Most of the work in creating the custom LLMChain comes down to the prompt. Because we are using an existing agent class to parse the output, it is very important that the prompt say to produce text in that format. Additionally, we currently require an agent_scratchpad input variable to put notes on previous actions and observations. This should almost always be the final part of the prompt. However, besides those instructions, you can customize the prompt as you wish. To ensure that the prompt contains the appropriate instructions, we will utilize a helper method on that class. The helper method for the ZeroShotAgent takes the following arguments: tools: List of tools the agent will have access to, used to format the prompt. prefix: String to put before the list of tools. suffix: String to put after the list of tools. input_variables: List of input variables the final prompt will expect.
https://python.langchain.com/en/latest/modules/agents/agents/custom_mrkl_agent.html
b4a354291af4-1
input_variables: List of input variables the final prompt will expect. For this exercise, we will give our agent access to Google Search, and we will customize it in that we will have it answer as a pirate. from langchain.agents import ZeroShotAgent, Tool, AgentExecutor from langchain import OpenAI, SerpAPIWrapper, LLMChain search = SerpAPIWrapper() tools = [ Tool( name = "Search", func=search.run, description="useful for when you need to answer questions about current events" ) ] prefix = """Answer the following questions as best you can, but speaking as a pirate might speak. You have access to the following tools:""" suffix = """Begin! Remember to speak as a pirate when giving your final answer. Use lots of "Args" Question: {input} {agent_scratchpad}""" prompt = ZeroShotAgent.create_prompt( tools, prefix=prefix, suffix=suffix, input_variables=["input", "agent_scratchpad"] ) In case we are curious, we can now take a look at the final prompt template to see what it looks like when its all put together. print(prompt.template) Answer the following questions as best you can, but speaking as a pirate might speak. You have access to the following tools: Search: useful for when you need to answer questions about current events Use the following format: Question: the input question you must answer Thought: you should always think about what to do Action: the action to take, should be one of [Search] Action Input: the input to the action Observation: the result of the action ... (this Thought/Action/Action Input/Observation can repeat N times) Thought: I now know the final answer
https://python.langchain.com/en/latest/modules/agents/agents/custom_mrkl_agent.html
b4a354291af4-2
Thought: I now know the final answer Final Answer: the final answer to the original input question Begin! Remember to speak as a pirate when giving your final answer. Use lots of "Args" Question: {input} {agent_scratchpad} Note that we are able to feed agents a self-defined prompt template, i.e. not restricted to the prompt generated by the create_prompt function, assuming it meets the agent’s requirements. For example, for ZeroShotAgent, we will need to ensure that it meets the following requirements. There should a string starting with “Action:” and a following string starting with “Action Input:”, and both should be separated by a newline. llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt) tool_names = [tool.name for tool in tools] agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names) agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True) agent_executor.run("How many people live in canada as of 2023?") > Entering new AgentExecutor chain... Thought: I need to find out the population of Canada Action: Search Action Input: Population of Canada 2023 Observation: The current population of Canada is 38,661,927 as of Sunday, April 16, 2023, based on Worldometer elaboration of the latest United Nations data. Thought: I now know the final answer Final Answer: Arrr, Canada be havin' 38,661,927 people livin' there as of 2023! > Finished chain. "Arrr, Canada be havin' 38,661,927 people livin' there as of 2023!" Multiple inputs# Agents can also work with prompts that require multiple inputs.
https://python.langchain.com/en/latest/modules/agents/agents/custom_mrkl_agent.html
b4a354291af4-3
Multiple inputs# Agents can also work with prompts that require multiple inputs. prefix = """Answer the following questions as best you can. You have access to the following tools:""" suffix = """When answering, you MUST speak in the following language: {language}. Question: {input} {agent_scratchpad}""" prompt = ZeroShotAgent.create_prompt( tools, prefix=prefix, suffix=suffix, input_variables=["input", "language", "agent_scratchpad"] ) llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt) agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools) agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True) agent_executor.run(input="How many people live in canada as of 2023?", language="italian") > Entering new AgentExecutor chain... Thought: I should look for recent population estimates. Action: Search Action Input: Canada population 2023 Observation: 39,566,248 Thought: I should double check this number. Action: Search Action Input: Canada population estimates 2023 Observation: Canada's population was estimated at 39,566,248 on January 1, 2023, after a record population growth of 1,050,110 people from January 1, 2022, to January 1, 2023. Thought: I now know the final answer.
https://python.langchain.com/en/latest/modules/agents/agents/custom_mrkl_agent.html
b4a354291af4-4
Thought: I now know the final answer. Final Answer: La popolazione del Canada è stata stimata a 39.566.248 il 1° gennaio 2023, dopo un record di crescita demografica di 1.050.110 persone dal 1° gennaio 2022 al 1° gennaio 2023. > Finished chain. 'La popolazione del Canada è stata stimata a 39.566.248 il 1° gennaio 2023, dopo un record di crescita demografica di 1.050.110 persone dal 1° gennaio 2022 al 1° gennaio 2023.' previous Custom LLM Agent (with a ChatModel) next Custom MultiAction Agent Contents Custom LLMChain Multiple inputs By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 07, 2023.
https://python.langchain.com/en/latest/modules/agents/agents/custom_mrkl_agent.html
e44ee82370a4-0
.ipynb .pdf Custom Agent with Tool Retrieval Contents Set up environment Set up tools Tool Retriever Prompt Template Output Parser Set up LLM, stop sequence, and the agent Use the Agent Custom Agent with Tool Retrieval# This notebook builds off of this notebook and assumes familiarity with how agents work. The novel idea introduced in this notebook is the idea of using retrieval to select the set of tools to use to answer an agent query. This is useful when you have many many tools to select from. You cannot put the description of all the tools in the prompt (because of context length issues) so instead you dynamically select the N tools you do want to consider using at run time. In this notebook we will create a somewhat contrieved example. We will have one legitimate tool (search) and then 99 fake tools which are just nonsense. We will then add a step in the prompt template that takes the user input and retrieves tool relevant to the query. Set up environment# Do necessary imports, etc. from langchain.agents import Tool, AgentExecutor, LLMSingleActionAgent, AgentOutputParser from langchain.prompts import StringPromptTemplate from langchain import OpenAI, SerpAPIWrapper, LLMChain from typing import List, Union from langchain.schema import AgentAction, AgentFinish import re Set up tools# We will create one legitimate tool (search) and then 99 fake tools # Define which tools the agent can use to answer user queries search = SerpAPIWrapper() search_tool = Tool( name = "Search", func=search.run, description="useful for when you need to answer questions about current events" ) def fake_func(inp: str) -> str: return "foo" fake_tools = [ Tool(
https://python.langchain.com/en/latest/modules/agents/agents/custom_agent_with_tool_retrieval.html
e44ee82370a4-1
return "foo" fake_tools = [ Tool( name=f"foo-{i}", func=fake_func, description=f"a silly function that you can use to get more information about the number {i}" ) for i in range(99) ] ALL_TOOLS = [search_tool] + fake_tools Tool Retriever# We will use a vectorstore to create embeddings for each tool description. Then, for an incoming query we can create embeddings for that query and do a similarity search for relevant tools. from langchain.vectorstores import FAISS from langchain.embeddings import OpenAIEmbeddings from langchain.schema import Document docs = [Document(page_content=t.description, metadata={"index": i}) for i, t in enumerate(ALL_TOOLS)] vector_store = FAISS.from_documents(docs, OpenAIEmbeddings()) retriever = vector_store.as_retriever() def get_tools(query): docs = retriever.get_relevant_documents(query) return [ALL_TOOLS[d.metadata["index"]] for d in docs] We can now test this retriever to see if it seems to work. get_tools("whats the weather?") [Tool(name='Search', description='useful for when you need to answer questions about current events', return_direct=False, verbose=False, callback_manager=<langchain.callbacks.shared.SharedCallbackManager object at 0x114b28a90>, func=<bound method SerpAPIWrapper.run of SerpAPIWrapper(search_engine=<class 'serpapi.google_search.GoogleSearch'>, params={'engine': 'google', 'google_domain': 'google.com', 'gl': 'us', 'hl': 'en'}, serpapi_api_key='', aiosession=None)>, coroutine=None),
https://python.langchain.com/en/latest/modules/agents/agents/custom_agent_with_tool_retrieval.html
e44ee82370a4-2
Tool(name='foo-95', description='a silly function that you can use to get more information about the number 95', return_direct=False, verbose=False, callback_manager=<langchain.callbacks.shared.SharedCallbackManager object at 0x114b28a90>, func=<function fake_func at 0x15e5bd1f0>, coroutine=None), Tool(name='foo-12', description='a silly function that you can use to get more information about the number 12', return_direct=False, verbose=False, callback_manager=<langchain.callbacks.shared.SharedCallbackManager object at 0x114b28a90>, func=<function fake_func at 0x15e5bd1f0>, coroutine=None), Tool(name='foo-15', description='a silly function that you can use to get more information about the number 15', return_direct=False, verbose=False, callback_manager=<langchain.callbacks.shared.SharedCallbackManager object at 0x114b28a90>, func=<function fake_func at 0x15e5bd1f0>, coroutine=None)] get_tools("whats the number 13?") [Tool(name='foo-13', description='a silly function that you can use to get more information about the number 13', return_direct=False, verbose=False, callback_manager=<langchain.callbacks.shared.SharedCallbackManager object at 0x114b28a90>, func=<function fake_func at 0x15e5bd1f0>, coroutine=None), Tool(name='foo-12', description='a silly function that you can use to get more information about the number 12', return_direct=False, verbose=False, callback_manager=<langchain.callbacks.shared.SharedCallbackManager object at 0x114b28a90>, func=<function fake_func at 0x15e5bd1f0>, coroutine=None),
https://python.langchain.com/en/latest/modules/agents/agents/custom_agent_with_tool_retrieval.html
e44ee82370a4-3
Tool(name='foo-14', description='a silly function that you can use to get more information about the number 14', return_direct=False, verbose=False, callback_manager=<langchain.callbacks.shared.SharedCallbackManager object at 0x114b28a90>, func=<function fake_func at 0x15e5bd1f0>, coroutine=None), Tool(name='foo-11', description='a silly function that you can use to get more information about the number 11', return_direct=False, verbose=False, callback_manager=<langchain.callbacks.shared.SharedCallbackManager object at 0x114b28a90>, func=<function fake_func at 0x15e5bd1f0>, coroutine=None)] Prompt Template# The prompt template is pretty standard, because we’re not actually changing that much logic in the actual prompt template, but rather we are just changing how retrieval is done. # Set up the base template template = """Answer the following questions as best you can, but speaking as a pirate might speak. You have access to the following tools: {tools} Use the following format: Question: the input question you must answer Thought: you should always think about what to do Action: the action to take, should be one of [{tool_names}] Action Input: the input to the action Observation: the result of the action ... (this Thought/Action/Action Input/Observation can repeat N times) Thought: I now know the final answer Final Answer: the final answer to the original input question Begin! Remember to speak as a pirate when giving your final answer. Use lots of "Arg"s Question: {input} {agent_scratchpad}""" The custom prompt template now has the concept of a tools_getter, which we call on the input to select the tools to use from typing import Callable
https://python.langchain.com/en/latest/modules/agents/agents/custom_agent_with_tool_retrieval.html
e44ee82370a4-4
from typing import Callable # Set up a prompt template class CustomPromptTemplate(StringPromptTemplate): # The template to use template: str ############## NEW ###################### # The list of tools available tools_getter: Callable def format(self, **kwargs) -> str: # Get the intermediate steps (AgentAction, Observation tuples) # Format them in a particular way intermediate_steps = kwargs.pop("intermediate_steps") thoughts = "" for action, observation in intermediate_steps: thoughts += action.log thoughts += f"\nObservation: {observation}\nThought: " # Set the agent_scratchpad variable to that value kwargs["agent_scratchpad"] = thoughts ############## NEW ###################### tools = self.tools_getter(kwargs["input"]) # Create a tools variable from the list of tools provided kwargs["tools"] = "\n".join([f"{tool.name}: {tool.description}" for tool in tools]) # Create a list of tool names for the tools provided kwargs["tool_names"] = ", ".join([tool.name for tool in tools]) return self.template.format(**kwargs) prompt = CustomPromptTemplate( template=template, tools_getter=get_tools, # This omits the `agent_scratchpad`, `tools`, and `tool_names` variables because those are generated dynamically # This includes the `intermediate_steps` variable because that is needed input_variables=["input", "intermediate_steps"] ) Output Parser# The output parser is unchanged from the previous notebook, since we are not changing anything about the output format. class CustomOutputParser(AgentOutputParser):
https://python.langchain.com/en/latest/modules/agents/agents/custom_agent_with_tool_retrieval.html
e44ee82370a4-5
class CustomOutputParser(AgentOutputParser): def parse(self, llm_output: str) -> Union[AgentAction, AgentFinish]: # Check if agent should finish if "Final Answer:" in llm_output: return AgentFinish( # Return values is generally always a dictionary with a single `output` key # It is not recommended to try anything else at the moment :) return_values={"output": llm_output.split("Final Answer:")[-1].strip()}, log=llm_output, ) # Parse out the action and action input regex = r"Action\s*\d*\s*:(.*?)\nAction\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)" match = re.search(regex, llm_output, re.DOTALL) if not match: raise ValueError(f"Could not parse LLM output: `{llm_output}`") action = match.group(1).strip() action_input = match.group(2) # Return the action and action input return AgentAction(tool=action, tool_input=action_input.strip(" ").strip('"'), log=llm_output) output_parser = CustomOutputParser() Set up LLM, stop sequence, and the agent# Also the same as the previous notebook llm = OpenAI(temperature=0) # LLM chain consisting of the LLM and a prompt llm_chain = LLMChain(llm=llm, prompt=prompt) tools = get_tools("whats the weather?") tool_names = [tool.name for tool in tools] agent = LLMSingleActionAgent( llm_chain=llm_chain, output_parser=output_parser, stop=["\nObservation:"],
https://python.langchain.com/en/latest/modules/agents/agents/custom_agent_with_tool_retrieval.html
e44ee82370a4-6
output_parser=output_parser, stop=["\nObservation:"], allowed_tools=tool_names ) Use the Agent# Now we can use it! agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True) agent_executor.run("What's the weather in SF?") > Entering new AgentExecutor chain... Thought: I need to find out what the weather is in SF Action: Search Action Input: Weather in SF Observation:Mostly cloudy skies early, then partly cloudy in the afternoon. High near 60F. ENE winds shifting to W at 10 to 15 mph. Humidity71%. UV Index6 of 10. I now know the final answer Final Answer: 'Arg, 'tis mostly cloudy skies early, then partly cloudy in the afternoon. High near 60F. ENE winds shiftin' to W at 10 to 15 mph. Humidity71%. UV Index6 of 10. > Finished chain. "'Arg, 'tis mostly cloudy skies early, then partly cloudy in the afternoon. High near 60F. ENE winds shiftin' to W at 10 to 15 mph. Humidity71%. UV Index6 of 10." previous Custom MultiAction Agent next Conversation Agent (for Chat Models) Contents Set up environment Set up tools Tool Retriever Prompt Template Output Parser Set up LLM, stop sequence, and the agent Use the Agent By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 07, 2023.
https://python.langchain.com/en/latest/modules/agents/agents/custom_agent_with_tool_retrieval.html
066cd85bded5-0
.ipynb .pdf Custom MultiAction Agent Custom MultiAction Agent# This notebook goes through how to create your own custom agent. An agent consists of two parts: - Tools: The tools the agent has available to use. - The agent class itself: this decides which action to take. In this notebook we walk through how to create a custom agent that predicts/takes multiple steps at a time. from langchain.agents import Tool, AgentExecutor, BaseMultiActionAgent from langchain import OpenAI, SerpAPIWrapper def random_word(query: str) -> str: print("\nNow I'm doing this!") return "foo" search = SerpAPIWrapper() tools = [ Tool( name = "Search", func=search.run, description="useful for when you need to answer questions about current events" ), Tool( name = "RandomWord", func=random_word, description="call this to get a random word." ) ] from typing import List, Tuple, Any, Union from langchain.schema import AgentAction, AgentFinish class FakeAgent(BaseMultiActionAgent): """Fake Custom Agent.""" @property def input_keys(self): return ["input"] def plan( self, intermediate_steps: List[Tuple[AgentAction, str]], **kwargs: Any ) -> Union[List[AgentAction], AgentFinish]: """Given input, decided what to do. Args: intermediate_steps: Steps the LLM has taken to date, along with observations **kwargs: User inputs. Returns: Action specifying what tool to use. """ if len(intermediate_steps) == 0: return [
https://python.langchain.com/en/latest/modules/agents/agents/custom_multi_action_agent.html
066cd85bded5-1
""" if len(intermediate_steps) == 0: return [ AgentAction(tool="Search", tool_input=kwargs["input"], log=""), AgentAction(tool="RandomWord", tool_input=kwargs["input"], log=""), ] else: return AgentFinish(return_values={"output": "bar"}, log="") async def aplan( self, intermediate_steps: List[Tuple[AgentAction, str]], **kwargs: Any ) -> Union[List[AgentAction], AgentFinish]: """Given input, decided what to do. Args: intermediate_steps: Steps the LLM has taken to date, along with observations **kwargs: User inputs. Returns: Action specifying what tool to use. """ if len(intermediate_steps) == 0: return [ AgentAction(tool="Search", tool_input=kwargs["input"], log=""), AgentAction(tool="RandomWord", tool_input=kwargs["input"], log=""), ] else: return AgentFinish(return_values={"output": "bar"}, log="") agent = FakeAgent() agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True) agent_executor.run("How many people live in canada as of 2023?") > Entering new AgentExecutor chain... The current population of Canada is 38,669,152 as of Monday, April 24, 2023, based on Worldometer elaboration of the latest United Nations data. Now I'm doing this! foo > Finished chain. 'bar' previous Custom MRKL Agent next Custom Agent with Tool Retrieval By Harrison Chase © Copyright 2023, Harrison Chase.
https://python.langchain.com/en/latest/modules/agents/agents/custom_multi_action_agent.html
066cd85bded5-2
By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 07, 2023.
https://python.langchain.com/en/latest/modules/agents/agents/custom_multi_action_agent.html
8fe452728d05-0
.ipynb .pdf MRKL MRKL# This notebook showcases using an agent to replicate the MRKL chain. This uses the example Chinook database. To set it up follow the instructions on https://database.guide/2-sample-databases-sqlite/, placing the .db file in a notebooks folder at the root of this repository. from langchain import LLMMathChain, OpenAI, SerpAPIWrapper, SQLDatabase, SQLDatabaseChain from langchain.agents import initialize_agent, Tool from langchain.agents import AgentType llm = OpenAI(temperature=0) search = SerpAPIWrapper() llm_math_chain = LLMMathChain(llm=llm, verbose=True) db = SQLDatabase.from_uri("sqlite:///../../../../../notebooks/Chinook.db") db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True) tools = [ Tool( name = "Search", func=search.run, description="useful for when you need to answer questions about current events. You should ask targeted questions" ), Tool( name="Calculator", func=llm_math_chain.run, description="useful for when you need to answer questions about math" ), Tool( name="FooBar DB", func=db_chain.run, description="useful for when you need to answer questions about FooBar. Input should be in the form of a question containing full context" ) ] mrkl = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True) mrkl.run("Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?") > Entering new AgentExecutor chain...
https://python.langchain.com/en/latest/modules/agents/agents/examples/mrkl.html
8fe452728d05-1
> Entering new AgentExecutor chain... I need to find out who Leo DiCaprio's girlfriend is and then calculate her age raised to the 0.43 power. Action: Search Action Input: "Who is Leo DiCaprio's girlfriend?" Observation: DiCaprio met actor Camila Morrone in December 2017, when she was 20 and he was 43. They were spotted at Coachella and went on multiple vacations together. Some reports suggested that DiCaprio was ready to ask Morrone to marry him. The couple made their red carpet debut at the 2020 Academy Awards. Thought: I need to calculate Camila Morrone's age raised to the 0.43 power. Action: Calculator Action Input: 21^0.43 > Entering new LLMMathChain chain... 21^0.43 ```text 21**0.43 ``` ...numexpr.evaluate("21**0.43")... Answer: 3.7030049853137306 > Finished chain. Observation: Answer: 3.7030049853137306 Thought: I now know the final answer. Final Answer: Camila Morrone is Leo DiCaprio's girlfriend and her current age raised to the 0.43 power is 3.7030049853137306. > Finished chain. "Camila Morrone is Leo DiCaprio's girlfriend and her current age raised to the 0.43 power is 3.7030049853137306." mrkl.run("What is the full name of the artist who recently released an album called 'The Storm Before the Calm' and are they in the FooBar database? If so, what albums of theirs are in the FooBar database?") > Entering new AgentExecutor chain...
https://python.langchain.com/en/latest/modules/agents/agents/examples/mrkl.html
8fe452728d05-2
> Entering new AgentExecutor chain... I need to find out the artist's full name and then search the FooBar database for their albums. Action: Search Action Input: "The Storm Before the Calm" artist Observation: The Storm Before the Calm (stylized in all lowercase) is the tenth (and eighth international) studio album by Canadian-American singer-songwriter Alanis Morissette, released June 17, 2022, via Epiphany Music and Thirty Tigers, as well as by RCA Records in Europe. Thought: I now need to search the FooBar database for Alanis Morissette's albums. Action: FooBar DB Action Input: What albums by Alanis Morissette are in the FooBar database? > Entering new SQLDatabaseChain chain... What albums by Alanis Morissette are in the FooBar database? SQLQuery: /Users/harrisonchase/workplace/langchain/langchain/sql_database.py:191: SAWarning: Dialect sqlite+pysqlite does *not* support Decimal objects natively, and SQLAlchemy must convert from floating point - rounding errors and other issues may occur. Please consider storing Decimal numbers as strings or integers on this platform for lossless storage. sample_rows = connection.execute(command) SELECT "Title" FROM "Album" INNER JOIN "Artist" ON "Album"."ArtistId" = "Artist"."ArtistId" WHERE "Name" = 'Alanis Morissette' LIMIT 5; SQLResult: [('Jagged Little Pill',)] Answer: The albums by Alanis Morissette in the FooBar database are Jagged Little Pill. > Finished chain. Observation: The albums by Alanis Morissette in the FooBar database are Jagged Little Pill. Thought: I now know the final answer.
https://python.langchain.com/en/latest/modules/agents/agents/examples/mrkl.html
8fe452728d05-3
Thought: I now know the final answer. Final Answer: The artist who released the album 'The Storm Before the Calm' is Alanis Morissette and the albums of hers in the FooBar database are Jagged Little Pill. > Finished chain. "The artist who released the album 'The Storm Before the Calm' is Alanis Morissette and the albums of hers in the FooBar database are Jagged Little Pill." previous Conversation Agent next MRKL Chat By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 07, 2023.
https://python.langchain.com/en/latest/modules/agents/agents/examples/mrkl.html
76449ec41f86-0
.ipynb .pdf Conversation Agent Conversation Agent# This notebook walks through using an agent optimized for conversation. Other agents are often optimized for using tools to figure out the best response, which is not ideal in a conversational setting where you may want the agent to be able to chat with the user as well. This is accomplished with a specific type of agent (conversational-react-description) which expects to be used with a memory component. from langchain.agents import Tool from langchain.agents import AgentType from langchain.memory import ConversationBufferMemory from langchain import OpenAI from langchain.utilities import SerpAPIWrapper from langchain.agents import initialize_agent search = SerpAPIWrapper() tools = [ Tool( name = "Current Search", func=search.run, description="useful for when you need to answer questions about current events or the current state of the world" ), ] memory = ConversationBufferMemory(memory_key="chat_history") llm=OpenAI(temperature=0) agent_chain = initialize_agent(tools, llm, agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION, verbose=True, memory=memory) agent_chain.run(input="hi, i am bob") > Entering new AgentExecutor chain... Thought: Do I need to use a tool? No AI: Hi Bob, nice to meet you! How can I help you today? > Finished chain. 'Hi Bob, nice to meet you! How can I help you today?' agent_chain.run(input="what's my name?") > Entering new AgentExecutor chain... Thought: Do I need to use a tool? No AI: Your name is Bob! > Finished chain. 'Your name is Bob!'
https://python.langchain.com/en/latest/modules/agents/agents/examples/conversational_agent.html
76449ec41f86-1
AI: Your name is Bob! > Finished chain. 'Your name is Bob!' agent_chain.run("what are some good dinners to make this week, if i like thai food?") > Entering new AgentExecutor chain... Thought: Do I need to use a tool? Yes Action: Current Search Action Input: Thai food dinner recipes Observation: 59 easy Thai recipes for any night of the week · Marion Grasby's Thai spicy chilli and basil fried rice · Thai curry noodle soup · Marion Grasby's Thai Spicy ... Thought: Do I need to use a tool? No AI: Here are some great Thai dinner recipes you can try this week: Marion Grasby's Thai Spicy Chilli and Basil Fried Rice, Thai Curry Noodle Soup, Thai Green Curry with Coconut Rice, Thai Red Curry with Vegetables, and Thai Coconut Soup. I hope you enjoy them! > Finished chain. "Here are some great Thai dinner recipes you can try this week: Marion Grasby's Thai Spicy Chilli and Basil Fried Rice, Thai Curry Noodle Soup, Thai Green Curry with Coconut Rice, Thai Red Curry with Vegetables, and Thai Coconut Soup. I hope you enjoy them!" agent_chain.run(input="tell me the last letter in my name, and also tell me who won the world cup in 1978?") > Entering new AgentExecutor chain... Thought: Do I need to use a tool? Yes Action: Current Search Action Input: Who won the World Cup in 1978 Observation: Argentina national football team Thought: Do I need to use a tool? No AI: The last letter in your name is "b" and the winner of the 1978 World Cup was the Argentina national football team. > Finished chain.
https://python.langchain.com/en/latest/modules/agents/agents/examples/conversational_agent.html
76449ec41f86-2
> Finished chain. 'The last letter in your name is "b" and the winner of the 1978 World Cup was the Argentina national football team.' agent_chain.run(input="whats the current temperature in pomfret?") > Entering new AgentExecutor chain... Thought: Do I need to use a tool? Yes Action: Current Search Action Input: Current temperature in Pomfret Observation: Partly cloudy skies. High around 70F. Winds W at 5 to 10 mph. Humidity41%. Thought: Do I need to use a tool? No AI: The current temperature in Pomfret is around 70F with partly cloudy skies and winds W at 5 to 10 mph. The humidity is 41%. > Finished chain. 'The current temperature in Pomfret is around 70F with partly cloudy skies and winds W at 5 to 10 mph. The humidity is 41%.' previous Conversation Agent (for Chat Models) next MRKL By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 07, 2023.
https://python.langchain.com/en/latest/modules/agents/agents/examples/conversational_agent.html
a5ab9b46fc43-0
.ipynb .pdf Self Ask With Search Self Ask With Search# This notebook showcases the Self Ask With Search chain. from langchain import OpenAI, SerpAPIWrapper from langchain.agents import initialize_agent, Tool from langchain.agents import AgentType llm = OpenAI(temperature=0) search = SerpAPIWrapper() tools = [ Tool( name="Intermediate Answer", func=search.run, description="useful for when you need to ask with search" ) ] self_ask_with_search = initialize_agent(tools, llm, agent=AgentType.SELF_ASK_WITH_SEARCH, verbose=True) self_ask_with_search.run("What is the hometown of the reigning men's U.S. Open champion?") > Entering new AgentExecutor chain... Yes. Follow up: Who is the reigning men's U.S. Open champion? Intermediate answer: Carlos Alcaraz Garfia Follow up: Where is Carlos Alcaraz Garfia from? Intermediate answer: El Palmar, Spain So the final answer is: El Palmar, Spain > Finished chain. 'El Palmar, Spain' previous ReAct next Structured Tool Chat Agent By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 07, 2023.
https://python.langchain.com/en/latest/modules/agents/agents/examples/self_ask_with_search.html
f5d03f0fc3f7-0
.ipynb .pdf Conversation Agent (for Chat Models) Conversation Agent (for Chat Models)# This notebook walks through using an agent optimized for conversation, using ChatModels. Other agents are often optimized for using tools to figure out the best response, which is not ideal in a conversational setting where you may want the agent to be able to chat with the user as well. This is accomplished with a specific type of agent (chat-conversational-react-description) which expects to be used with a memory component. !pip install langchain !pip install google-search-results !pip install openai from langchain.agents import Tool from langchain.memory import ConversationBufferMemory from langchain.chat_models import ChatOpenAI from langchain.utilities import SerpAPIWrapper from langchain.agents import initialize_agent from langchain.agents import AgentType from getpass import getpass SERPAPI_API_KEY = getpass() search = SerpAPIWrapper(serpapi_api_key=SERPAPI_API_KEY) tools = [ Tool( name = "Current Search", func=search.run, description="useful for when you need to answer questions about current events or the current state of the world. the input to this should be a single search term." ), ] memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True) OPENAI_API_KEY = getpass() llm=ChatOpenAI(openai_api_key=OPENAI_API_KEY, temperature=0) agent_chain = initialize_agent(tools, llm, agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION, verbose=True, memory=memory) agent_chain.run(input="hi, i am bob") > Entering new AgentExecutor chain... { "action": "Final Answer",
https://python.langchain.com/en/latest/modules/agents/agents/examples/chat_conversation_agent.html
f5d03f0fc3f7-1
> Entering new AgentExecutor chain... { "action": "Final Answer", "action_input": "Hello Bob! How can I assist you today?" } > Finished chain. 'Hello Bob! How can I assist you today?' agent_chain.run(input="what's my name?") > Entering new AgentExecutor chain... { "action": "Final Answer", "action_input": "Your name is Bob." } > Finished chain. 'Your name is Bob.' agent_chain.run("what are some good dinners to make this week, if i like thai food?") > Entering new AgentExecutor chain... { "action": "Current Search", "action_input": "Thai food dinner recipes" } Observation: 64 easy Thai recipes for any night of the week · Thai curry noodle soup · Thai yellow cauliflower, snake bean and tofu curry · Thai-spiced chicken hand pies · Thai ... Thought:{ "action": "Final Answer", "action_input": "Here are some Thai food dinner recipes you can try this week: Thai curry noodle soup, Thai yellow cauliflower, snake bean and tofu curry, Thai-spiced chicken hand pies, and many more. You can find the full list of recipes at the source I found earlier." } > Finished chain. 'Here are some Thai food dinner recipes you can try this week: Thai curry noodle soup, Thai yellow cauliflower, snake bean and tofu curry, Thai-spiced chicken hand pies, and many more. You can find the full list of recipes at the source I found earlier.' agent_chain.run(input="tell me the last letter in my name, and also tell me who won the world cup in 1978?") > Entering new AgentExecutor chain... { "action": "Final Answer",
https://python.langchain.com/en/latest/modules/agents/agents/examples/chat_conversation_agent.html
f5d03f0fc3f7-2
> Entering new AgentExecutor chain... { "action": "Final Answer", "action_input": "The last letter in your name is 'b'. Argentina won the World Cup in 1978." } > Finished chain. "The last letter in your name is 'b'. Argentina won the World Cup in 1978." agent_chain.run(input="whats the weather like in pomfret?") > Entering new AgentExecutor chain... { "action": "Current Search", "action_input": "weather in pomfret" } Observation: Cloudy with showers. Low around 55F. Winds S at 5 to 10 mph. Chance of rain 60%. Humidity76%. Thought:{ "action": "Final Answer", "action_input": "Cloudy with showers. Low around 55F. Winds S at 5 to 10 mph. Chance of rain 60%. Humidity76%." } > Finished chain. 'Cloudy with showers. Low around 55F. Winds S at 5 to 10 mph. Chance of rain 60%. Humidity76%.' previous Custom Agent with Tool Retrieval next Conversation Agent By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 07, 2023.
https://python.langchain.com/en/latest/modules/agents/agents/examples/chat_conversation_agent.html
9c99db091c06-0
.ipynb .pdf ReAct ReAct# This notebook showcases using an agent to implement the ReAct logic. from langchain import OpenAI, Wikipedia from langchain.agents import initialize_agent, Tool from langchain.agents import AgentType from langchain.agents.react.base import DocstoreExplorer docstore=DocstoreExplorer(Wikipedia()) tools = [ Tool( name="Search", func=docstore.search, description="useful for when you need to ask with search" ), Tool( name="Lookup", func=docstore.lookup, description="useful for when you need to ask with lookup" ) ] llm = OpenAI(temperature=0, model_name="text-davinci-002") react = initialize_agent(tools, llm, agent=AgentType.REACT_DOCSTORE, verbose=True) question = "Author David Chanoff has collaborated with a U.S. Navy admiral who served as the ambassador to the United Kingdom under which President?" react.run(question) > Entering new AgentExecutor chain... Thought: I need to search David Chanoff and find the U.S. Navy admiral he collaborated with. Then I need to find which President the admiral served under. Action: Search[David Chanoff]
https://python.langchain.com/en/latest/modules/agents/agents/examples/react.html
9c99db091c06-1
Action: Search[David Chanoff] Observation: David Chanoff is a noted author of non-fiction work. His work has typically involved collaborations with the principal protagonist of the work concerned. His collaborators have included; Augustus A. White, Joycelyn Elders, Đoàn Văn Toại, William J. Crowe, Ariel Sharon, Kenneth Good and Felix Zandman. He has also written about a wide range of subjects including literary history, education and foreign for The Washington Post, The New Republic and The New York Times Magazine. He has published more than twelve books. Thought: The U.S. Navy admiral David Chanoff collaborated with is William J. Crowe. I need to find which President he served under. Action: Search[William J. Crowe] Observation: William James Crowe Jr. (January 2, 1925 – October 18, 2007) was a United States Navy admiral and diplomat who served as the 11th chairman of the Joint Chiefs of Staff under Presidents Ronald Reagan and George H. W. Bush, and as the ambassador to the United Kingdom and Chair of the Intelligence Oversight Board under President Bill Clinton. Thought: William J. Crowe served as the ambassador to the United Kingdom under President Bill Clinton, so the answer is Bill Clinton. Action: Finish[Bill Clinton] > Finished chain. 'Bill Clinton' previous MRKL Chat next Self Ask With Search By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 07, 2023.
https://python.langchain.com/en/latest/modules/agents/agents/examples/react.html
ba2c7726bf06-0
.ipynb .pdf MRKL Chat MRKL Chat# This notebook showcases using an agent to replicate the MRKL chain using an agent optimized for chat models. This uses the example Chinook database. To set it up follow the instructions on https://database.guide/2-sample-databases-sqlite/, placing the .db file in a notebooks folder at the root of this repository. from langchain import OpenAI, LLMMathChain, SerpAPIWrapper, SQLDatabase, SQLDatabaseChain from langchain.agents import initialize_agent, Tool from langchain.agents import AgentType from langchain.chat_models import ChatOpenAI llm = ChatOpenAI(temperature=0) llm1 = OpenAI(temperature=0) search = SerpAPIWrapper() llm_math_chain = LLMMathChain(llm=llm1, verbose=True) db = SQLDatabase.from_uri("sqlite:///../../../../../notebooks/Chinook.db") db_chain = SQLDatabaseChain.from_llm(llm1, db, verbose=True) tools = [ Tool( name = "Search", func=search.run, description="useful for when you need to answer questions about current events. You should ask targeted questions" ), Tool( name="Calculator", func=llm_math_chain.run, description="useful for when you need to answer questions about math" ), Tool( name="FooBar DB", func=db_chain.run, description="useful for when you need to answer questions about FooBar. Input should be in the form of a question containing full context" ) ] mrkl = initialize_agent(tools, llm, agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
https://python.langchain.com/en/latest/modules/agents/agents/examples/mrkl_chat.html
ba2c7726bf06-1
mrkl.run("Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?") > Entering new AgentExecutor chain... Thought: The first question requires a search, while the second question requires a calculator. Action: ``` { "action": "Search", "action_input": "Leo DiCaprio girlfriend" } ``` Observation: Gigi Hadid: 2022 Leo and Gigi were first linked back in September 2022, when a source told Us Weekly that Leo had his “sights set" on her (alarming way to put it, but okay). Thought:For the second question, I need to calculate the age raised to the 0.43 power. I will use the calculator tool. Action: ``` { "action": "Calculator", "action_input": "((2022-1995)^0.43)" } ``` > Entering new LLMMathChain chain... ((2022-1995)^0.43) ```text (2022-1995)**0.43 ``` ...numexpr.evaluate("(2022-1995)**0.43")... Answer: 4.125593352125936 > Finished chain. Observation: Answer: 4.125593352125936 Thought:I now know the final answer. Final Answer: Gigi Hadid is Leo DiCaprio's girlfriend and her current age raised to the 0.43 power is approximately 4.13. > Finished chain. "Gigi Hadid is Leo DiCaprio's girlfriend and her current age raised to the 0.43 power is approximately 4.13."
https://python.langchain.com/en/latest/modules/agents/agents/examples/mrkl_chat.html
ba2c7726bf06-2
mrkl.run("What is the full name of the artist who recently released an album called 'The Storm Before the Calm' and are they in the FooBar database? If so, what albums of theirs are in the FooBar database?") > Entering new AgentExecutor chain... Question: What is the full name of the artist who recently released an album called 'The Storm Before the Calm' and are they in the FooBar database? If so, what albums of theirs are in the FooBar database? Thought: I should use the Search tool to find the answer to the first part of the question and then use the FooBar DB tool to find the answer to the second part. Action: ``` { "action": "Search", "action_input": "Who recently released an album called 'The Storm Before the Calm'" } ``` Observation: Alanis Morissette Thought:Now that I know the artist's name, I can use the FooBar DB tool to find out if they are in the database and what albums of theirs are in it. Action: ``` { "action": "FooBar DB", "action_input": "What albums does Alanis Morissette have in the database?" } ``` > Entering new SQLDatabaseChain chain... What albums does Alanis Morissette have in the database? SQLQuery: /Users/harrisonchase/workplace/langchain/langchain/sql_database.py:191: SAWarning: Dialect sqlite+pysqlite does *not* support Decimal objects natively, and SQLAlchemy must convert from floating point - rounding errors and other issues may occur. Please consider storing Decimal numbers as strings or integers on this platform for lossless storage. sample_rows = connection.execute(command)
https://python.langchain.com/en/latest/modules/agents/agents/examples/mrkl_chat.html
ba2c7726bf06-3
sample_rows = connection.execute(command) SELECT "Title" FROM "Album" WHERE "ArtistId" IN (SELECT "ArtistId" FROM "Artist" WHERE "Name" = 'Alanis Morissette') LIMIT 5; SQLResult: [('Jagged Little Pill',)] Answer: Alanis Morissette has the album Jagged Little Pill in the database. > Finished chain. Observation: Alanis Morissette has the album Jagged Little Pill in the database. Thought:The artist Alanis Morissette is in the FooBar database and has the album Jagged Little Pill in it. Final Answer: Alanis Morissette is in the FooBar database and has the album Jagged Little Pill in it. > Finished chain. 'Alanis Morissette is in the FooBar database and has the album Jagged Little Pill in it.' previous MRKL next ReAct By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 07, 2023.
https://python.langchain.com/en/latest/modules/agents/agents/examples/mrkl_chat.html
f1fd38a31e23-0
.ipynb .pdf Structured Tool Chat Agent Contents Initialize Tools Adding in memory Structured Tool Chat Agent# This notebook walks through using a chat agent capable of using multi-input tools. Older agents are configured to specify an action input as a single string, but this agent can use the provided tools’ args_schema to populate the action input. This functionality is natively available in the (structured-chat-zero-shot-react-description or AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION). import os os.environ["LANGCHAIN_TRACING"] = "true" # If you want to trace the execution of the program, set to "true" from langchain.agents import AgentType from langchain.chat_models import ChatOpenAI from langchain.agents import initialize_agent Initialize Tools# We will test the agent using a web browser. from langchain.agents.agent_toolkits import PlayWrightBrowserToolkit from langchain.tools.playwright.utils import ( create_async_playwright_browser, create_sync_playwright_browser, # A synchronous browser is available, though it isn't compatible with jupyter. ) # This import is required only for jupyter notebooks, since they have their own eventloop import nest_asyncio nest_asyncio.apply() async_browser = create_async_playwright_browser() browser_toolkit = PlayWrightBrowserToolkit.from_browser(async_browser=async_browser) tools = browser_toolkit.get_tools() llm = ChatOpenAI(temperature=0) # Also works well with Anthropic models agent_chain = initialize_agent(tools, llm, agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=True) response = await agent_chain.arun(input="Hi I'm Erica.") print(response) > Entering new AgentExecutor chain... Action: ``` {
https://python.langchain.com/en/latest/modules/agents/agents/examples/structured_chat.html
f1fd38a31e23-1
print(response) > Entering new AgentExecutor chain... Action: ``` { "action": "Final Answer", "action_input": "Hello Erica, how can I assist you today?" } ``` > Finished chain. Hello Erica, how can I assist you today? response = await agent_chain.arun(input="Don't need help really just chatting.") print(response) > Entering new AgentExecutor chain... > Finished chain. I'm here to chat! How's your day going? response = await agent_chain.arun(input="Browse to blog.langchain.dev and summarize the text, please.") print(response) > Entering new AgentExecutor chain... Action: ``` { "action": "navigate_browser", "action_input": { "url": "https://blog.langchain.dev/" } } ``` Observation: Navigating to https://blog.langchain.dev/ returned status code 200 Thought:I need to extract the text from the webpage to summarize it. Action: ``` { "action": "extract_text", "action_input": {} } ``` Observation: LangChain LangChain Home About GitHub Docs LangChain The official LangChain blog. Auto-Evaluator Opportunities Editor's Note: this is a guest blog post by Lance Martin. TL;DR
https://python.langchain.com/en/latest/modules/agents/agents/examples/structured_chat.html
f1fd38a31e23-2
We recently open-sourced an auto-evaluator tool for grading LLM question-answer chains. We are now releasing an open source, free to use hosted app and API to expand usability. Below we discuss a few opportunities to further improve May 1, 2023 5 min read Callbacks Improvements TL;DR: We're announcing improvements to our callbacks system, which powers logging, tracing, streaming output, and some awesome third-party integrations. This will better support concurrent runs with independent callbacks, tracing of deeply nested trees of LangChain components, and callback handlers scoped to a single request (which is super useful for May 1, 2023 3 min read Unleashing the power of AI Collaboration with Parallelized LLM Agent Actor Trees Editor's note: the following is a guest blog post from Cyrus at Shaman AI. We use guest blog posts to highlight interesting and novel applciations, and this is certainly that. There's been a lot of talk about agents recently, but most have been discussions around a single agent. If multiple Apr 28, 2023 4 min read Gradio & LLM Agents Editor's note: this is a guest blog post from Freddy Boulton, a software engineer at Gradio. We're excited to share this post because it brings a large number of exciting new tools into the ecosystem. Agents are largely defined by the tools they have, so to be able to equip Apr 23, 2023 4 min read RecAlign - The smart content filter for social media feed [Editor's Note] This is a guest post by Tian Jin. We are highlighting this application as we think it is a novel use case. Specifically, we think recommendation systems are incredibly impactful in our everyday lives and there has not been a ton of discourse on how LLMs will impact Apr 22, 2023 3 min read Improving Document Retrieval with Contextual Compression Note: This post assumes some familiarity with LangChain
https://python.langchain.com/en/latest/modules/agents/agents/examples/structured_chat.html
f1fd38a31e23-3
read Improving Document Retrieval with Contextual Compression Note: This post assumes some familiarity with LangChain and is moderately technical.
https://python.langchain.com/en/latest/modules/agents/agents/examples/structured_chat.html
f1fd38a31e23-4
💡 TL;DR: We’ve introduced a new abstraction and a new document Retriever to facilitate the post-processing of retrieved documents. Specifically, the new abstraction makes it easy to take a set of retrieved documents and extract from them Apr 20, 2023 3 min read Autonomous Agents & Agent Simulations Over the past two weeks, there has been a massive increase in using LLMs in an agentic manner. Specifically, projects like AutoGPT, BabyAGI, CAMEL, and Generative Agents have popped up. The LangChain community has now implemented some parts of all of those projects in the LangChain framework. While researching and Apr 18, 2023 7 min read AI-Powered Medical Knowledge: Revolutionizing Care for Rare Conditions [Editor's Note]: This is a guest post by Jack Simon, who recently participated in a hackathon at Williams College. He built a LangChain-powered chatbot focused on appendiceal cancer, aiming to make specialized knowledge more accessible to those in need. If you are interested in building a chatbot for another rare Apr 17, 2023 3 min read Auto-Eval of Question-Answering Tasks By Lance Martin Context LLM ops platforms, such as LangChain, make it easy to assemble LLM components (e.g., models, document retrievers, data loaders) into chains. Question-Answering is one of the most popular applications of these chains. But it is often not always obvious to determine what parameters (e.g. Apr 15, 2023 3 min read Announcing LangChainJS Support for Multiple JS Environments TLDR: We're announcing support for running LangChain.js in browsers, Cloudflare Workers, Vercel/Next.js, Deno, Supabase Edge Functions, alongside existing support for Node.js ESM and CJS. See install/upgrade docs and breaking changes list. Context
https://python.langchain.com/en/latest/modules/agents/agents/examples/structured_chat.html
f1fd38a31e23-5
Context Originally we designed LangChain.js to run in Node.js, which is the Apr 11, 2023 3 min read LangChain x Supabase Supabase is holding an AI Hackathon this week. Here at LangChain we are big fans of both Supabase and hackathons, so we thought this would be a perfect time to highlight the multiple ways you can use LangChain and Supabase together.
https://python.langchain.com/en/latest/modules/agents/agents/examples/structured_chat.html
f1fd38a31e23-6
The reason we like Supabase so much is that Apr 8, 2023 2 min read Announcing our $10M seed round led by Benchmark It was only six months ago that we released the first version of LangChain, but it seems like several years. When we launched, generative AI was starting to go mainstream: stable diffusion had just been released and was captivating people’s imagination and fueling an explosion in developer activity, Jasper Apr 4, 2023 4 min read Custom Agents One of the most common requests we've heard is better functionality and documentation for creating custom agents. This has always been a bit tricky - because in our mind it's actually still very unclear what an "agent" actually is, and therefor what the "right" abstractions for them may be. Recently, Apr 3, 2023 3 min read Retrieval TL;DR: We are adjusting our abstractions to make it easy for other retrieval methods besides the LangChain VectorDB object to be used in LangChain. This is done with the goals of (1) allowing retrievers constructed elsewhere to be used more easily in LangChain, (2) encouraging more experimentation with alternative Mar 23, 2023 4 min read LangChain + Zapier Natural Language Actions (NLA) We are super excited to team up with Zapier and integrate their new Zapier NLA API into LangChain, which you can now use with your agents and chains. With this integration, you have access to the 5k+ apps and 20k+ actions on Zapier's platform through a natural language API interface. Mar 16, 2023 2 min read Evaluation Evaluation of language models, and by extension applications built on top of language models, is hard. With recent model releases (OpenAI, Anthropic, Google) evaluation is becoming a bigger and bigger issue. People are starting to try to tackle this, with OpenAI releasing
https://python.langchain.com/en/latest/modules/agents/agents/examples/structured_chat.html
f1fd38a31e23-7
becoming a bigger and bigger issue. People are starting to try to tackle this, with OpenAI releasing OpenAI/evals - focused on evaluating OpenAI models. Mar 14, 2023 3 min read LLMs and SQL Francisco Ingham and Jon Luo are two of the community members leading the change on the SQL integrations. We’re really excited to write this blog post with them going over all the tips and tricks they’ve learned doing so. We’re even more excited to announce that we’ Mar 13, 2023 8 min read Origin Web Browser [Editor's Note]: This is the second of hopefully many guest posts. We intend to highlight novel applications building on top of LangChain. If you are interested in working with us on such a post, please reach out to [email protected].
https://python.langchain.com/en/latest/modules/agents/agents/examples/structured_chat.html
f1fd38a31e23-8
Authors: Parth Asawa (pgasawa@), Ayushi Batwara (ayushi.batwara@), Jason Mar 8, 2023 4 min read Prompt Selectors One common complaint we've heard is that the default prompt templates do not work equally well for all models. This became especially pronounced this past week when OpenAI released a ChatGPT API. This new API had a completely new interface (which required new abstractions) and as a result many users Mar 8, 2023 2 min read Chat Models Last week OpenAI released a ChatGPT endpoint. It came marketed with several big improvements, most notably being 10x cheaper and a lot faster. But it also came with a completely new API endpoint. We were able to quickly write a wrapper for this endpoint to let users use it like Mar 6, 2023 6 min read Using the ChatGPT API to evaluate the ChatGPT API OpenAI released a new ChatGPT API yesterday. Lots of people were excited to try it. But how does it actually compare to the existing API? It will take some time before there is a definitive answer, but here are some initial thoughts. Because I'm lazy, I also enrolled the help Mar 2, 2023 5 min read Agent Toolkits Today, we're announcing agent toolkits, a new abstraction that allows developers to create agents designed for a particular use-case (for example, interacting with a relational database or interacting with an OpenAPI spec). We hope to continue developing different toolkits that can enable agents to do amazing feats. Toolkits are supported Mar 1, 2023 3 min read TypeScript Support It's finally here... TypeScript support for LangChain.
https://python.langchain.com/en/latest/modules/agents/agents/examples/structured_chat.html
f1fd38a31e23-9
What does this mean? It means that all your favorite prompts, chains, and agents are all recreatable in TypeScript natively. Both the Python version and TypeScript version utilize the same serializable format, meaning that artifacts can seamlessly be shared between languages. As an Feb 17, 2023 2 min read Streaming Support in LangChain We’re excited to announce streaming support in LangChain. There's been a lot of talk about the best UX for LLM applications, and we believe streaming is at its core. We’ve also updated the chat-langchain repo to include streaming and async execution. We hope that this repo can serve Feb 14, 2023 2 min read LangChain + Chroma Today we’re announcing LangChain's integration with Chroma, the first step on the path to the Modern A.I Stack. LangChain - The A.I-native developer toolkit We started LangChain with the intent to build a modular and flexible framework for developing A.I-native applications. Some of the use cases Feb 13, 2023 2 min read Page 1 of 2 Older Posts → LangChain © 2023 Sign up Powered by Ghost Thought: > Finished chain. The LangChain blog has recently released an open-source auto-evaluator tool for grading LLM question-answer chains and is now releasing an open-source, free-to-use hosted app and API to expand usability. The blog also discusses various opportunities to further improve the LangChain platform. response = await agent_chain.arun(input="What's the latest xkcd comic about?") print(response) > Entering new AgentExecutor chain... Thought: I can navigate to the xkcd website and extract the latest comic title and alt text to answer the question. Action: ``` { "action": "navigate_browser", "action_input": { "url": "https://xkcd.com/" }
https://python.langchain.com/en/latest/modules/agents/agents/examples/structured_chat.html
f1fd38a31e23-10
"url": "https://xkcd.com/" } } ``` Observation: Navigating to https://xkcd.com/ returned status code 200 Thought:I can extract the latest comic title and alt text using CSS selectors. Action: ``` { "action": "get_elements", "action_input": { "selector": "#ctitle, #comic img", "attributes": ["alt", "src"] } } ``` Observation: [{"alt": "Tapetum Lucidum", "src": "//imgs.xkcd.com/comics/tapetum_lucidum.png"}] Thought: > Finished chain. The latest xkcd comic is titled "Tapetum Lucidum" and the image can be found at https://xkcd.com/2565/. Adding in memory# Here is how you add in memory to this agent from langchain.prompts import MessagesPlaceholder from langchain.memory import ConversationBufferMemory chat_history = MessagesPlaceholder(variable_name="chat_history") memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True) agent_chain = initialize_agent( tools, llm, agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=True, memory=memory, agent_kwargs = { "memory_prompts": [chat_history], "input_variables": ["input", "agent_scratchpad", "chat_history"] } ) response = await agent_chain.arun(input="Hi I'm Erica.") print(response) > Entering new AgentExecutor chain... Action: ``` { "action": "Final Answer",
https://python.langchain.com/en/latest/modules/agents/agents/examples/structured_chat.html
f1fd38a31e23-11
Action: ``` { "action": "Final Answer", "action_input": "Hi Erica! How can I assist you today?" } ``` > Finished chain. Hi Erica! How can I assist you today? response = await agent_chain.arun(input="whats my name?") print(response) > Entering new AgentExecutor chain... Your name is Erica. > Finished chain. Your name is Erica. previous Self Ask With Search next Toolkits Contents Initialize Tools Adding in memory By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 07, 2023.
https://python.langchain.com/en/latest/modules/agents/agents/examples/structured_chat.html
ea3b0443b23d-0
.rst .pdf LLMs LLMs# Note Conceptual Guide Large Language Models (LLMs) are a core component of LangChain. LangChain is not a provider of LLMs, but rather provides a standard interface through which you can interact with a variety of LLMs. The following sections of documentation are provided: Getting Started: An overview of all the functionality the LangChain LLM class provides. How-To Guides: A collection of how-to guides. These highlight how to accomplish various objectives with our LLM class (streaming, async, etc). Integrations: A collection of examples on how to integrate different LLM providers with LangChain (OpenAI, Hugging Face, etc). Reference: API reference documentation for all LLM classes. previous Getting Started next Getting Started By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 07, 2023.
https://python.langchain.com/en/latest/modules/models/llms.html
551c03712004-0
.rst .pdf Chat Models Chat Models# Note Conceptual Guide Chat models are a variation on language models. While chat models use language models under the hood, the interface they expose is a bit different. Rather than expose a “text in, text out” API, they expose an interface where “chat messages” are the inputs and outputs. Chat model APIs are fairly new, so we are still figuring out the correct abstractions. The following sections of documentation are provided: Getting Started: An overview of all the functionality the LangChain LLM class provides. How-To Guides: A collection of how-to guides. These highlight how to accomplish various objectives with our LLM class (streaming, async, etc). Integrations: A collection of examples on how to integrate different LLM providers with LangChain (OpenAI, Hugging Face, etc). previous LLMs next Getting Started By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 07, 2023.
https://python.langchain.com/en/latest/modules/models/chat.html
19b4e98f5845-0
.ipynb .pdf Getting Started Contents Language Models text -> text interface messages -> message interface Getting Started# One of the core value props of LangChain is that it provides a standard interface to models. This allows you to swap easily between models. At a high level, there are two main types of models: Language Models: good for text generation Text Embedding Models: good for turning text into a numerical representation Language Models# There are two different sub-types of Language Models: LLMs: these wrap APIs which take text in and return text ChatModels: these wrap models which take chat messages in and return a chat message This is a subtle difference, but a value prop of LangChain is that we provide a unified interface accross these. This is nice because although the underlying APIs are actually quite different, you often want to use them interchangeably. To see this, let’s look at OpenAI (a wrapper around OpenAI’s LLM) vs ChatOpenAI (a wrapper around OpenAI’s ChatModel). from langchain.llms import OpenAI from langchain.chat_models import ChatOpenAI llm = OpenAI() chat_model = ChatOpenAI() text -> text interface# llm.predict("say hi!") '\n\nHi there!' chat_model.predict("say hi!") 'Hello there!' messages -> message interface# from langchain.schema import HumanMessage llm.predict_messages([HumanMessage(content="say hi!")]) AIMessage(content='\n\nHello! Nice to meet you!', additional_kwargs={}, example=False) chat_model.predict_messages([HumanMessage(content="say hi!")]) AIMessage(content='Hello! How can I assist you today?', additional_kwargs={}, example=False) previous Models next LLMs Contents Language Models text -> text interface messages -> message interface By Harrison Chase
https://python.langchain.com/en/latest/modules/models/getting_started.html
19b4e98f5845-1
Language Models text -> text interface messages -> message interface By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 07, 2023.
https://python.langchain.com/en/latest/modules/models/getting_started.html
bdfaf0a0b46b-0
.rst .pdf Text Embedding Models Text Embedding Models# Note Conceptual Guide This documentation goes over how to use the Embedding class in LangChain. The Embedding class is a class designed for interfacing with embeddings. There are lots of Embedding providers (OpenAI, Cohere, Hugging Face, etc) - this class is designed to provide a standard interface for all of them. Embeddings create a vector representation of a piece of text. This is useful because it means we can think about text in the vector space, and do things like semantic search where we look for pieces of text that are most similar in the vector space. The base Embedding class in LangChain exposes two methods: embed_documents and embed_query. The largest difference is that these two methods have different interfaces: one works over multiple documents, while the other works over a single document. Besides this, another reason for having these as two separate methods is that some embedding providers have different embedding methods for documents (to be searched over) vs queries (the search query itself). The following integrations exist for text embeddings. Aleph Alpha Amazon Bedrock Azure OpenAI Cohere Elasticsearch Fake Embeddings Google Vertex AI PaLM Hugging Face Hub HuggingFace Instruct Jina Llama-cpp MiniMax ModelScope MosaicML OpenAI SageMaker Endpoint Self Hosted Embeddings Sentence Transformers Tensorflow Hub previous PromptLayer ChatOpenAI next Aleph Alpha By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 07, 2023.
https://python.langchain.com/en/latest/modules/models/text_embedding.html
809da27d9bbe-0
.rst .pdf Integrations Integrations# The examples here are all “how-to” guides for how to integrate with various LLM providers. AI21 Aleph Alpha Anyscale Aviary Azure OpenAI Banana Beam Bedrock CerebriumAI Cohere C Transformers Databricks DeepInfra ForefrontAI Google Cloud Platform Vertex AI PaLM GooseAI GPT4All Hugging Face Hub Hugging Face Pipeline Huggingface TextGen Inference Jsonformer Llama-cpp Manifest Modal MosaicML NLP Cloud OpenAI OpenLM Petals PipelineAI Prediction Guard Control the output structure/ type of LLMs Chaining PromptLayer OpenAI ReLLM Replicate Runhouse SageMaker Endpoint StochasticAI Writer previous How to track token usage next AI21 By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 07, 2023.
https://python.langchain.com/en/latest/modules/models/llms/integrations.html
642fac8e64bc-0
.rst .pdf Generic Functionality Generic Functionality# The examples here all address certain “how-to” guides for working with LLMs. How to use the async API for LLMs How to write a custom LLM wrapper How (and why) to use the fake LLM How (and why) to use the human input LLM How to cache LLM calls How to serialize LLM classes How to stream LLM and Chat Model responses How to track token usage previous Getting Started next How to use the async API for LLMs By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 07, 2023.
https://python.langchain.com/en/latest/modules/models/llms/how_to_guides.html
4360ff36f6a6-0
.ipynb .pdf Getting Started Getting Started# This notebook goes over how to use the LLM class in LangChain. The LLM class is a class designed for interfacing with LLMs. There are lots of LLM providers (OpenAI, Cohere, Hugging Face, etc) - this class is designed to provide a standard interface for all of them. In this part of the documentation, we will focus on generic LLM functionality. For details on working with a specific LLM wrapper, please see the examples in the How-To section. For this notebook, we will work with an OpenAI LLM wrapper, although the functionalities highlighted are generic for all LLM types. from langchain.llms import OpenAI llm = OpenAI(model_name="text-ada-001", n=2, best_of=2) Generate Text: The most basic functionality an LLM has is just the ability to call it, passing in a string and getting back a string. llm("Tell me a joke") '\n\nWhy did the chicken cross the road?\n\nTo get to the other side.' Generate: More broadly, you can call it with a list of inputs, getting back a more complete response than just the text. This complete response includes things like multiple top responses, as well as LLM provider specific information llm_result = llm.generate(["Tell me a joke", "Tell me a poem"]*15) len(llm_result.generations) 30 llm_result.generations[0] [Generation(text='\n\nWhy did the chicken cross the road?\n\nTo get to the other side!'), Generation(text='\n\nWhy did the chicken cross the road?\n\nTo get to the other side.')] llm_result.generations[-1]
https://python.langchain.com/en/latest/modules/models/llms/getting_started.html