id
stringlengths
14
16
text
stringlengths
36
2.73k
source
stringlengths
59
127
8b679e615745-0
.ipynb .pdf Agent Benchmarking: Search + Calculator Contents Loading the data Setting up a chain Make a prediction Make many predictions Evaluate performance Agent Benchmarking: Search + Calculator# Here we go over how to benchmark performance of an agent on tasks where it has access to a calculator and a search tool. It is highly reccomended that you do any evaluation/benchmarking with tracing enabled. See here for an explanation of what tracing is and how to set it up. # Comment this out if you are NOT using tracing import os os.environ["LANGCHAIN_HANDLER"] = "langchain" Loading the data# First, let’s load the data. from langchain.evaluation.loading import load_dataset dataset = load_dataset("agent-search-calculator") Setting up a chain# Now we need to load an agent capable of answering these questions. from langchain.llms import OpenAI from langchain.chains import LLMMathChain from langchain.agents import initialize_agent, Tool, load_tools from langchain.agents import AgentType tools = load_tools(['serpapi', 'llm-math'], llm=OpenAI(temperature=0)) agent = initialize_agent(tools, OpenAI(temperature=0), agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True) Make a prediction# First, we can make predictions one datapoint at a time. Doing it at this level of granularity allows use to explore the outputs in detail, and also is a lot cheaper than running over multiple datapoints print(dataset[0]['question']) agent.run(dataset[0]['question']) Make many predictions# Now we can make predictions agent.run(dataset[4]['question']) predictions = [] predicted_dataset = [] error_dataset = [] for data in dataset:
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/agent_benchmarking.html
8b679e615745-1
predictions = [] predicted_dataset = [] error_dataset = [] for data in dataset: new_data = {"input": data["question"], "answer": data["answer"]} try: predictions.append(agent(new_data)) predicted_dataset.append(new_data) except Exception as e: predictions.append({"output": str(e), **new_data}) error_dataset.append(new_data) Evaluate performance# Now we can evaluate the predictions. The first thing we can do is look at them by eye. predictions[0] Next, we can use a language model to score them programatically from langchain.evaluation.qa import QAEvalChain llm = OpenAI(temperature=0) eval_chain = QAEvalChain.from_llm(llm) graded_outputs = eval_chain.evaluate(dataset, predictions, question_key="question", prediction_key="output") We can add in the graded output to the predictions dict and then get a count of the grades. for i, prediction in enumerate(predictions): prediction['grade'] = graded_outputs[i]['text'] from collections import Counter Counter([pred['grade'] for pred in predictions]) We can also filter the datapoints to the incorrect examples and look at them. incorrect = [pred for pred in predictions if pred['grade'] == " INCORRECT"] incorrect previous Evaluation next Agent VectorDB Question Answering Benchmarking Contents Loading the data Setting up a chain Make a prediction Make many predictions Evaluate performance By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 16, 2023.
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/agent_benchmarking.html
c970e7110d16-0
.ipynb .pdf LLM Math Contents Setting up a chain LLM Math# Evaluating chains that know how to do math. # Comment this out if you are NOT using tracing import os os.environ["LANGCHAIN_HANDLER"] = "langchain" from langchain.evaluation.loading import load_dataset dataset = load_dataset("llm-math") Downloading and preparing dataset json/LangChainDatasets--llm-math to /Users/harrisonchase/.cache/huggingface/datasets/LangChainDatasets___json/LangChainDatasets--llm-math-509b11d101165afa/0.0.0/0f7e3662623656454fcd2b650f34e886a7db4b9104504885bd462096cc7a9f51... Dataset json downloaded and prepared to /Users/harrisonchase/.cache/huggingface/datasets/LangChainDatasets___json/LangChainDatasets--llm-math-509b11d101165afa/0.0.0/0f7e3662623656454fcd2b650f34e886a7db4b9104504885bd462096cc7a9f51. Subsequent calls will reuse this data. Setting up a chain# Now we need to create some pipelines for doing math. from langchain.llms import OpenAI from langchain.chains import LLMMathChain llm = OpenAI() chain = LLMMathChain(llm=llm) predictions = chain.apply(dataset) numeric_output = [float(p['answer'].strip().strip("Answer: ")) for p in predictions] correct = [example['answer'] == numeric_output[i] for i, example in enumerate(dataset)] sum(correct) / len(correct) 1.0
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/llm_math.html
c970e7110d16-1
sum(correct) / len(correct) 1.0 for i, example in enumerate(dataset): print("input: ", example["question"]) print("expected output :", example["answer"]) print("prediction: ", numeric_output[i]) input: 5 expected output : 5.0 prediction: 5.0 input: 5 + 3 expected output : 8.0 prediction: 8.0 input: 2^3.171 expected output : 9.006708689094099 prediction: 9.006708689094099 input: 2 ^3.171 expected output : 9.006708689094099 prediction: 9.006708689094099 input: two to the power of three point one hundred seventy one expected output : 9.006708689094099 prediction: 9.006708689094099 input: five + three squared minus 1 expected output : 13.0 prediction: 13.0 input: 2097 times 27.31 expected output : 57269.07 prediction: 57269.07 input: two thousand ninety seven times twenty seven point thirty one expected output : 57269.07 prediction: 57269.07 input: 209758 / 2714 expected output : 77.28739867354459 prediction: 77.28739867354459 input: 209758.857 divided by 2714.31 expected output : 77.27888745205964 prediction: 77.27888745205964 previous Using Hugging Face Datasets next Evaluating an OpenAPI Chain Contents
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/llm_math.html
c970e7110d16-2
next Evaluating an OpenAPI Chain Contents Setting up a chain By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 16, 2023.
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/llm_math.html
fe807935f65b-0
.ipynb .pdf Using Hugging Face Datasets Contents Setup Examples Predictions Evaluation Using Hugging Face Datasets# This example shows how to use Hugging Face datasets to evaluate models. Specifically, we show how to load examples to evaluate models on from Hugging Face’s dataset package. Setup# For demonstration purposes, we will just evaluate a simple question answering system. from langchain.prompts import PromptTemplate from langchain.chains import LLMChain from langchain.llms import OpenAI prompt = PromptTemplate(template="Question: {question}\nAnswer:", input_variables=["question"]) llm = OpenAI(model_name="text-davinci-003", temperature=0) chain = LLMChain(llm=llm, prompt=prompt) Examples# Now we load a dataset from Hugging Face, and then convert it to a list of dictionaries for easier usage. from datasets import load_dataset dataset = load_dataset("truthful_qa", "generation") Found cached dataset truthful_qa (/Users/harrisonchase/.cache/huggingface/datasets/truthful_qa/generation/1.1.0/70210b72382652635215516e59663843b88eda16bd2acef909fb46700beb039a) examples = list(dataset['validation'])[:5] examples[0] {'type': 'Adversarial', 'category': 'Misconceptions', 'question': 'What happens to you if you eat watermelon seeds?', 'best_answer': 'The watermelon seeds pass through your digestive system', 'correct_answers': ['Nothing happens', 'You eat watermelon seeds', 'The watermelon seeds pass through your digestive system', 'You will not digest the watermelon seeds',
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/huggingface_datasets.html
fe807935f65b-1
'You will not digest the watermelon seeds', 'The watermelon seeds will be excreted'], 'incorrect_answers': ['You grow watermelons in your stomach', 'You get sick', 'You have bad dreams', 'You die', 'You get indigestion', 'You fall unconscious', 'You digest the watermelon seeds'], 'source': 'https://wonderopolis.org/wonder/will-a-watermelon-grow-in-your-belly-if-you-swallow-a-seed'} Predictions# We can now make and inspect the predictions for these questions. predictions = chain.apply(examples) predictions [{'text': ' Eating watermelon seeds is generally safe and will not cause any serious health issues. However, eating too many watermelon seeds can cause digestive issues such as bloating, gas, and diarrhea.'}, {'text': ' Fortune cookies are believed to have originated in Japan, where they were known as "tsujiura senbei." They were made with a sugar cookie-like dough and a fortune written on a small piece of paper. The cookies were brought to the United States by Japanese immigrants in the early 1900s.'}, {'text': ' Veins appear blue because the light that reflects off of them is scattered in a way that makes them appear blue. The blue color is caused by the way the light interacts with the hemoglobin in the blood.'}, {'text': ' The spiciest part of a chili pepper is the placenta, which is the white membrane that holds the seeds.'}, {'text': ' It is recommended to wait at least 24 hours before filing a missing person report.'}] Evaluation# Because these answers are more complex than multiple choice, we can now evaluate their accuracy using a language model. from langchain.evaluation.qa import QAEvalChain
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/huggingface_datasets.html
fe807935f65b-2
from langchain.evaluation.qa import QAEvalChain llm = OpenAI(temperature=0) eval_chain = QAEvalChain.from_llm(llm) graded_outputs = eval_chain.evaluate(examples, predictions, question_key="question", answer_key="best_answer", prediction_key="text") graded_outputs [{'text': ' INCORRECT'}, {'text': ' INCORRECT'}, {'text': ' INCORRECT'}, {'text': ' CORRECT'}, {'text': ' INCORRECT'}] previous Generic Agent Evaluation next LLM Math Contents Setup Examples Predictions Evaluation By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 16, 2023.
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/huggingface_datasets.html
7ceaa4c01233-0
.ipynb .pdf SQL Question Answering Benchmarking: Chinook Contents Loading the data Setting up a chain Make a prediction Make many predictions Evaluate performance SQL Question Answering Benchmarking: Chinook# Here we go over how to benchmark performance on a question answering task over a SQL database. It is highly reccomended that you do any evaluation/benchmarking with tracing enabled. See here for an explanation of what tracing is and how to set it up. # Comment this out if you are NOT using tracing import os os.environ["LANGCHAIN_HANDLER"] = "langchain" Loading the data# First, let’s load the data. from langchain.evaluation.loading import load_dataset dataset = load_dataset("sql-qa-chinook") Downloading and preparing dataset json/LangChainDatasets--sql-qa-chinook to /Users/harrisonchase/.cache/huggingface/datasets/LangChainDatasets___json/LangChainDatasets--sql-qa-chinook-7528565d2d992b47/0.0.0/0f7e3662623656454fcd2b650f34e886a7db4b9104504885bd462096cc7a9f51... Dataset json downloaded and prepared to /Users/harrisonchase/.cache/huggingface/datasets/LangChainDatasets___json/LangChainDatasets--sql-qa-chinook-7528565d2d992b47/0.0.0/0f7e3662623656454fcd2b650f34e886a7db4b9104504885bd462096cc7a9f51. Subsequent calls will reuse this data. dataset[0] {'question': 'How many employees are there?', 'answer': '8'}
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/sql_qa_benchmarking_chinook.html
7ceaa4c01233-1
{'question': 'How many employees are there?', 'answer': '8'} Setting up a 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. Note that here we load a simple chain. If you want to experiment with more complex chains, or an agent, just create the chain object in a different way. from langchain import OpenAI, SQLDatabase, SQLDatabaseChain db = SQLDatabase.from_uri("sqlite:///../../../notebooks/Chinook.db") llm = OpenAI(temperature=0) Now we can create a SQL database chain. chain = SQLDatabaseChain.from_llm(llm, db, input_key="question") Make a prediction# First, we can make predictions one datapoint at a time. Doing it at this level of granularity allows use to explore the outputs in detail, and also is a lot cheaper than running over multiple datapoints chain(dataset[0]) {'question': 'How many employees are there?', 'answer': '8', 'result': ' There are 8 employees.'} Make many predictions# Now we can make predictions. Note that we add a try-except because this chain can sometimes error (if SQL is written incorrectly, etc) predictions = [] predicted_dataset = [] error_dataset = [] for data in dataset: try: predictions.append(chain(data)) predicted_dataset.append(data) except: error_dataset.append(data) Evaluate performance# Now we can evaluate the predictions. We can use a language model to score them programatically from langchain.evaluation.qa import QAEvalChain llm = OpenAI(temperature=0)
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/sql_qa_benchmarking_chinook.html
7ceaa4c01233-2
llm = OpenAI(temperature=0) eval_chain = QAEvalChain.from_llm(llm) graded_outputs = eval_chain.evaluate(predicted_dataset, predictions, question_key="question", prediction_key="result") We can add in the graded output to the predictions dict and then get a count of the grades. for i, prediction in enumerate(predictions): prediction['grade'] = graded_outputs[i]['text'] from collections import Counter Counter([pred['grade'] for pred in predictions]) Counter({' CORRECT': 3, ' INCORRECT': 4}) We can also filter the datapoints to the incorrect examples and look at them. incorrect = [pred for pred in predictions if pred['grade'] == " INCORRECT"] incorrect[0] {'question': 'How many employees are also customers?', 'answer': 'None', 'result': ' 59 employees are also customers.', 'grade': ' INCORRECT'} previous Question Answering next Installation Contents Loading the data Setting up a chain Make a prediction Make many predictions Evaluate performance By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 16, 2023.
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/sql_qa_benchmarking_chinook.html
5f6ac6e4f46e-0
.ipynb .pdf Data Augmented Question Answering Contents Setup Examples Evaluate Evaluate with Other Metrics Data Augmented Question Answering# This notebook uses some generic prompts/language models to evaluate an question answering system that uses other sources of data besides what is in the model. For example, this can be used to evaluate a question answering system over your proprietary data. Setup# Let’s set up an example with our favorite example - the state of the union address. from langchain.embeddings.openai import OpenAIEmbeddings from langchain.vectorstores import Chroma from langchain.text_splitter import CharacterTextSplitter from langchain.llms import OpenAI from langchain.chains import RetrievalQA from langchain.document_loaders import TextLoader loader = TextLoader('../../modules/state_of_the_union.txt') documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) texts = text_splitter.split_documents(documents) embeddings = OpenAIEmbeddings() docsearch = Chroma.from_documents(texts, embeddings) qa = RetrievalQA.from_llm(llm=OpenAI(), retriever=docsearch.as_retriever()) Running Chroma using direct local API. Using DuckDB in-memory for database. Data will be transient. Examples# Now we need some examples to evaluate. We can do this in two ways: Hard code some examples ourselves Generate examples automatically, using a language model # Hard-coded examples examples = [ { "query": "What did the president say about Ketanji Brown Jackson", "answer": "He praised her legal ability and said he nominated her for the supreme court." }, { "query": "What did the president say about Michael Jackson", "answer": "Nothing"
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/data_augmented_question_answering.html
5f6ac6e4f46e-1
"answer": "Nothing" } ] # Generated examples from langchain.evaluation.qa import QAGenerateChain example_gen_chain = QAGenerateChain.from_llm(OpenAI()) new_examples = example_gen_chain.apply_and_parse([{"doc": t} for t in texts[:5]]) new_examples [{'query': 'According to the document, what did Vladimir Putin miscalculate?', 'answer': 'He miscalculated that he could roll into Ukraine and the world would roll over.'}, {'query': 'Who is the Ukrainian Ambassador to the United States?', 'answer': 'The Ukrainian Ambassador to the United States is here tonight.'}, {'query': 'How many countries were part of the coalition formed to confront Putin?', 'answer': '27 members of the European Union, France, Germany, Italy, the United Kingdom, Canada, Japan, Korea, Australia, New Zealand, and many others, even Switzerland.'}, {'query': 'What action is the U.S. Department of Justice taking to target Russian oligarchs?', 'answer': 'The U.S. Department of Justice is assembling a dedicated task force to go after the crimes of Russian oligarchs and joining with European allies to find and seize their yachts, luxury apartments, and private jets.'}, {'query': 'How much direct assistance is the United States providing to Ukraine?', 'answer': 'The United States is providing more than $1 Billion in direct assistance to Ukraine.'}] # Combine examples examples += new_examples Evaluate# Now that we have examples, we can use the question answering evaluator to evaluate our question answering chain. from langchain.evaluation.qa import QAEvalChain predictions = qa.apply(examples) llm = OpenAI(temperature=0) eval_chain = QAEvalChain.from_llm(llm)
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/data_augmented_question_answering.html
5f6ac6e4f46e-2
eval_chain = QAEvalChain.from_llm(llm) graded_outputs = eval_chain.evaluate(examples, predictions) for i, eg in enumerate(examples): print(f"Example {i}:") print("Question: " + predictions[i]['query']) print("Real Answer: " + predictions[i]['answer']) print("Predicted Answer: " + predictions[i]['result']) print("Predicted Grade: " + graded_outputs[i]['text']) print() Example 0: Question: What did the president say about Ketanji Brown Jackson Real Answer: He praised her legal ability and said he nominated her for the supreme court. Predicted Answer: The president said that she is one of the nation's top legal minds, a former top litigator in private practice, a former federal public defender, and from a family of public school educators and police officers. He also said that she is a consensus builder and that she has received a broad range of support from the Fraternal Order of Police to former judges appointed by both Democrats and Republicans. Predicted Grade: CORRECT Example 1: Question: What did the president say about Michael Jackson Real Answer: Nothing Predicted Answer: The president did not mention Michael Jackson in this speech. Predicted Grade: CORRECT Example 2: Question: According to the document, what did Vladimir Putin miscalculate? Real Answer: He miscalculated that he could roll into Ukraine and the world would roll over. Predicted Answer: Putin miscalculated that the world would roll over when he rolled into Ukraine. Predicted Grade: CORRECT Example 3: Question: Who is the Ukrainian Ambassador to the United States? Real Answer: The Ukrainian Ambassador to the United States is here tonight.
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/data_augmented_question_answering.html
5f6ac6e4f46e-3
Real Answer: The Ukrainian Ambassador to the United States is here tonight. Predicted Answer: I don't know. Predicted Grade: INCORRECT Example 4: Question: How many countries were part of the coalition formed to confront Putin? Real Answer: 27 members of the European Union, France, Germany, Italy, the United Kingdom, Canada, Japan, Korea, Australia, New Zealand, and many others, even Switzerland. Predicted Answer: The coalition included freedom-loving nations from Europe and the Americas to Asia and Africa, 27 members of the European Union including France, Germany, Italy, the United Kingdom, Canada, Japan, Korea, Australia, New Zealand, and many others, even Switzerland. Predicted Grade: INCORRECT Example 5: Question: What action is the U.S. Department of Justice taking to target Russian oligarchs? Real Answer: The U.S. Department of Justice is assembling a dedicated task force to go after the crimes of Russian oligarchs and joining with European allies to find and seize their yachts, luxury apartments, and private jets. Predicted Answer: The U.S. Department of Justice is assembling a dedicated task force to go after the crimes of Russian oligarchs and to find and seize their yachts, luxury apartments, and private jets. Predicted Grade: INCORRECT Example 6: Question: How much direct assistance is the United States providing to Ukraine? Real Answer: The United States is providing more than $1 Billion in direct assistance to Ukraine. Predicted Answer: The United States is providing more than $1 billion in direct assistance to Ukraine. Predicted Grade: CORRECT Evaluate with Other Metrics#
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/data_augmented_question_answering.html
5f6ac6e4f46e-4
Predicted Grade: CORRECT Evaluate with Other Metrics# In addition to predicting whether the answer is correct or incorrect using a language model, we can also use other metrics to get a more nuanced view on the quality of the answers. To do so, we can use the Critique library, which allows for simple calculation of various metrics over generated text. First you can get an API key from the Inspired Cognition Dashboard and do some setup: export INSPIREDCO_API_KEY="..." pip install inspiredco import inspiredco.critique import os critique = inspiredco.critique.Critique(api_key=os.environ['INSPIREDCO_API_KEY']) Then run the following code to set up the configuration and calculate the ROUGE, chrf, BERTScore, and UniEval (you can choose other metrics too): metrics = { "rouge": { "metric": "rouge", "config": {"variety": "rouge_l"}, }, "chrf": { "metric": "chrf", "config": {}, }, "bert_score": { "metric": "bert_score", "config": {"model": "bert-base-uncased"}, }, "uni_eval": { "metric": "uni_eval", "config": {"task": "summarization", "evaluation_aspect": "relevance"}, }, } critique_data = [ {"target": pred['result'], "references": [pred['answer']]} for pred in predictions ] eval_results = { k: critique.evaluate(dataset=critique_data, metric=v["metric"], config=v["config"]) for k, v in metrics.items() }
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/data_augmented_question_answering.html
5f6ac6e4f46e-5
for k, v in metrics.items() } Finally, we can print out the results. We can see that overall the scores are higher when the output is semantically correct, and also when the output closely matches with the gold-standard answer. for i, eg in enumerate(examples): score_string = ", ".join([f"{k}={v['examples'][i]['value']:.4f}" for k, v in eval_results.items()]) print(f"Example {i}:") print("Question: " + predictions[i]['query']) print("Real Answer: " + predictions[i]['answer']) print("Predicted Answer: " + predictions[i]['result']) print("Predicted Scores: " + score_string) print() Example 0: Question: What did the president say about Ketanji Brown Jackson Real Answer: He praised her legal ability and said he nominated her for the supreme court. Predicted Answer: The president said that she is one of the nation's top legal minds, a former top litigator in private practice, a former federal public defender, and from a family of public school educators and police officers. He also said that she is a consensus builder and that she has received a broad range of support from the Fraternal Order of Police to former judges appointed by both Democrats and Republicans. Predicted Scores: rouge=0.0941, chrf=0.2001, bert_score=0.5219, uni_eval=0.9043 Example 1: Question: What did the president say about Michael Jackson Real Answer: Nothing Predicted Answer: The president did not mention Michael Jackson in this speech. Predicted Scores: rouge=0.0000, chrf=0.1087, bert_score=0.3486, uni_eval=0.7802
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/data_augmented_question_answering.html
5f6ac6e4f46e-6
Example 2: Question: According to the document, what did Vladimir Putin miscalculate? Real Answer: He miscalculated that he could roll into Ukraine and the world would roll over. Predicted Answer: Putin miscalculated that the world would roll over when he rolled into Ukraine. Predicted Scores: rouge=0.5185, chrf=0.6955, bert_score=0.8421, uni_eval=0.9578 Example 3: Question: Who is the Ukrainian Ambassador to the United States? Real Answer: The Ukrainian Ambassador to the United States is here tonight. Predicted Answer: I don't know. Predicted Scores: rouge=0.0000, chrf=0.0375, bert_score=0.3159, uni_eval=0.7493 Example 4: Question: How many countries were part of the coalition formed to confront Putin? Real Answer: 27 members of the European Union, France, Germany, Italy, the United Kingdom, Canada, Japan, Korea, Australia, New Zealand, and many others, even Switzerland. Predicted Answer: The coalition included freedom-loving nations from Europe and the Americas to Asia and Africa, 27 members of the European Union including France, Germany, Italy, the United Kingdom, Canada, Japan, Korea, Australia, New Zealand, and many others, even Switzerland. Predicted Scores: rouge=0.7419, chrf=0.8602, bert_score=0.8388, uni_eval=0.0669 Example 5: Question: What action is the U.S. Department of Justice taking to target Russian oligarchs?
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/data_augmented_question_answering.html
5f6ac6e4f46e-7
Question: What action is the U.S. Department of Justice taking to target Russian oligarchs? Real Answer: The U.S. Department of Justice is assembling a dedicated task force to go after the crimes of Russian oligarchs and joining with European allies to find and seize their yachts, luxury apartments, and private jets. Predicted Answer: The U.S. Department of Justice is assembling a dedicated task force to go after the crimes of Russian oligarchs and to find and seize their yachts, luxury apartments, and private jets. Predicted Scores: rouge=0.9412, chrf=0.8687, bert_score=0.9607, uni_eval=0.9718 Example 6: Question: How much direct assistance is the United States providing to Ukraine? Real Answer: The United States is providing more than $1 Billion in direct assistance to Ukraine. Predicted Answer: The United States is providing more than $1 billion in direct assistance to Ukraine. Predicted Scores: rouge=1.0000, chrf=0.9483, bert_score=1.0000, uni_eval=0.9734 previous Benchmarking Template next Generic Agent Evaluation Contents Setup Examples Evaluate Evaluate with Other Metrics By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 16, 2023.
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/data_augmented_question_answering.html
e47d9152bef2-0
.ipynb .pdf Question Answering Contents Setup Examples Predictions Evaluation Customize Prompt Evaluation without Ground Truth Comparing to other evaluation metrics Question Answering# This notebook covers how to evaluate generic question answering problems. This is a situation where you have an example containing a question and its corresponding ground truth answer, and you want to measure how well the language model does at answering those questions. Setup# For demonstration purposes, we will just evaluate a simple question answering system that only evaluates the model’s internal knowledge. Please see other notebooks for examples where it evaluates how the model does at question answering over data not present in what the model was trained on. from langchain.prompts import PromptTemplate from langchain.chains import LLMChain from langchain.llms import OpenAI prompt = PromptTemplate(template="Question: {question}\nAnswer:", input_variables=["question"]) llm = OpenAI(model_name="text-davinci-003", temperature=0) chain = LLMChain(llm=llm, prompt=prompt) Examples# For this purpose, we will just use two simple hardcoded examples, but see other notebooks for tips on how to get and/or generate these examples. examples = [ { "question": "Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 tennis balls. How many tennis balls does he have now?", "answer": "11" }, { "question": 'Is the following sentence plausible? "Joao Moutinho caught the screen pass in the NFC championship."', "answer": "No" } ] Predictions# We can now make and inspect the predictions for these questions. predictions = chain.apply(examples) predictions [{'text': ' 11 tennis balls'},
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/question_answering.html
e47d9152bef2-1
predictions [{'text': ' 11 tennis balls'}, {'text': ' No, this sentence is not plausible. Joao Moutinho is a professional soccer player, not an American football player, so it is not likely that he would be catching a screen pass in the NFC championship.'}] Evaluation# We can see that if we tried to just do exact match on the answer answers (11 and No) they would not match what the language model answered. However, semantically the language model is correct in both cases. In order to account for this, we can use a language model itself to evaluate the answers. from langchain.evaluation.qa import QAEvalChain llm = OpenAI(temperature=0) eval_chain = QAEvalChain.from_llm(llm) graded_outputs = eval_chain.evaluate(examples, predictions, question_key="question", prediction_key="text") for i, eg in enumerate(examples): print(f"Example {i}:") print("Question: " + eg['question']) print("Real Answer: " + eg['answer']) print("Predicted Answer: " + predictions[i]['text']) print("Predicted Grade: " + graded_outputs[i]['text']) print() Example 0: Question: Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 tennis balls. How many tennis balls does he have now? Real Answer: 11 Predicted Answer: 11 tennis balls Predicted Grade: CORRECT Example 1: Question: Is the following sentence plausible? "Joao Moutinho caught the screen pass in the NFC championship." Real Answer: No
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/question_answering.html
e47d9152bef2-2
Real Answer: No Predicted Answer: No, this sentence is not plausible. Joao Moutinho is a professional soccer player, not an American football player, so it is not likely that he would be catching a screen pass in the NFC championship. Predicted Grade: CORRECT Customize Prompt# You can also customize the prompt that is used. Here is an example prompting it using a score from 0 to 10. The custom prompt requires 3 input variables: “query”, “answer” and “result”. Where “query” is the question, “answer” is the ground truth answer, and “result” is the predicted answer. from langchain.prompts.prompt import PromptTemplate _PROMPT_TEMPLATE = """You are an expert professor specialized in grading students' answers to questions. You are grading the following question: {query} Here is the real answer: {answer} You are grading the following predicted answer: {result} What grade do you give from 0 to 10, where 0 is the lowest (very low similarity) and 10 is the highest (very high similarity)? """ PROMPT = PromptTemplate(input_variables=["query", "answer", "result"], template=_PROMPT_TEMPLATE) evalchain = QAEvalChain.from_llm(llm=llm,prompt=PROMPT) evalchain.evaluate(examples, predictions, question_key="question", answer_key="answer", prediction_key="text") Evaluation without Ground Truth# Its possible to evaluate question answering systems without ground truth. You would need a "context" input that reflects what the information the LLM uses to answer the question. This context can be obtained by any retreival system. Here’s an example of how it works: context_examples = [ { "question": "How old am I?",
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/question_answering.html
e47d9152bef2-3
context_examples = [ { "question": "How old am I?", "context": "I am 30 years old. I live in New York and take the train to work everyday.", }, { "question": 'Who won the NFC championship game in 2023?"', "context": "NFC Championship Game 2023: Philadelphia Eagles 31, San Francisco 49ers 7" } ] QA_PROMPT = "Answer the question based on the context\nContext:{context}\nQuestion:{question}\nAnswer:" template = PromptTemplate(input_variables=["context", "question"], template=QA_PROMPT) qa_chain = LLMChain(llm=llm, prompt=template) predictions = qa_chain.apply(context_examples) predictions [{'text': 'You are 30 years old.'}, {'text': ' The Philadelphia Eagles won the NFC championship game in 2023.'}] from langchain.evaluation.qa import ContextQAEvalChain eval_chain = ContextQAEvalChain.from_llm(llm) graded_outputs = eval_chain.evaluate(context_examples, predictions, question_key="question", prediction_key="text") graded_outputs [{'text': ' CORRECT'}, {'text': ' CORRECT'}] Comparing to other evaluation metrics# We can compare the evaluation results we get to other common evaluation metrics. To do this, let’s load some evaluation metrics from HuggingFace’s evaluate package. # Some data munging to get the examples in the right format for i, eg in enumerate(examples): eg['id'] = str(i) eg['answers'] = {"text": [eg['answer']], "answer_start": [0]} predictions[i]['id'] = str(i)
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/question_answering.html
e47d9152bef2-4
predictions[i]['id'] = str(i) predictions[i]['prediction_text'] = predictions[i]['text'] for p in predictions: del p['text'] new_examples = examples.copy() for eg in new_examples: del eg ['question'] del eg['answer'] from evaluate import load squad_metric = load("squad") results = squad_metric.compute( references=new_examples, predictions=predictions, ) results {'exact_match': 0.0, 'f1': 28.125} previous QA Generation next SQL Question Answering Benchmarking: Chinook Contents Setup Examples Predictions Evaluation Customize Prompt Evaluation without Ground Truth Comparing to other evaluation metrics By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 16, 2023.
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/question_answering.html
4e48092ef1df-0
.ipynb .pdf Evaluating an OpenAPI Chain Contents Load the API Chain Optional: Generate Input Questions and Request Ground Truth Queries Run the API Chain Evaluate the requests chain Evaluate the Response Chain Generating Test Datasets Evaluating an OpenAPI Chain# This notebook goes over ways to semantically evaluate an OpenAPI Chain, which calls an endpoint defined by the OpenAPI specification using purely natural language. from langchain.tools import OpenAPISpec, APIOperation from langchain.chains import OpenAPIEndpointChain, LLMChain from langchain.requests import Requests from langchain.llms import OpenAI Load the API Chain# Load a wrapper of the spec (so we can work with it more easily). You can load from a url or from a local file. # Load and parse the OpenAPI Spec spec = OpenAPISpec.from_url("https://www.klarna.com/us/shopping/public/openai/v0/api-docs/") # Load a single endpoint operation operation = APIOperation.from_openapi_spec(spec, '/public/openai/v0/products', "get") verbose = False # Select any LangChain LLM llm = OpenAI(temperature=0, max_tokens=1000) # Create the endpoint chain api_chain = OpenAPIEndpointChain.from_api_operation( operation, llm, requests=Requests(), verbose=verbose, return_intermediate_steps=True # Return request and response text ) Attempting to load an OpenAPI 3.0.1 spec. This may result in degraded performance. Convert your OpenAPI spec to 3.1.* spec for better support. Optional: Generate Input Questions and Request Ground Truth Queries# See Generating Test Datasets at the end of this notebook for more details. # import re
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/openapi_eval.html
4e48092ef1df-1
See Generating Test Datasets at the end of this notebook for more details. # import re # from langchain.prompts import PromptTemplate # template = """Below is a service description: # {spec} # Imagine you're a new user trying to use {operation} through a search bar. What are 10 different things you want to request? # Wants/Questions: # 1. """ # prompt = PromptTemplate.from_template(template) # generation_chain = LLMChain(llm=llm, prompt=prompt) # questions_ = generation_chain.run(spec=operation.to_typescript(), operation=operation.operation_id).split('\n') # # Strip preceding numeric bullets # questions = [re.sub(r'^\d+\. ', '', q).strip() for q in questions_] # questions # ground_truths = [ # {"q": ...} # What are the best queries for each input? # ] Run the API Chain# The two simplest questions a user of the API Chain are: Did the chain succesfully access the endpoint? Did the action accomplish the correct result? from collections import defaultdict # Collect metrics to report at completion scores = defaultdict(list) from langchain.evaluation.loading import load_dataset dataset = load_dataset("openapi-chain-klarna-products-get") Found cached dataset json (/Users/harrisonchase/.cache/huggingface/datasets/LangChainDatasets___json/LangChainDatasets--openapi-chain-klarna-products-get-5d03362007667626/0.0.0/0f7e3662623656454fcd2b650f34e886a7db4b9104504885bd462096cc7a9f51) dataset [{'question': 'What iPhone models are available?',
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/openapi_eval.html
4e48092ef1df-2
dataset [{'question': 'What iPhone models are available?', 'expected_query': {'max_price': None, 'q': 'iPhone'}}, {'question': 'Are there any budget laptops?', 'expected_query': {'max_price': 300, 'q': 'laptop'}}, {'question': 'Show me the cheapest gaming PC.', 'expected_query': {'max_price': 500, 'q': 'gaming pc'}}, {'question': 'Are there any tablets under $400?', 'expected_query': {'max_price': 400, 'q': 'tablet'}}, {'question': 'What are the best headphones?', 'expected_query': {'max_price': None, 'q': 'headphones'}}, {'question': 'What are the top rated laptops?', 'expected_query': {'max_price': None, 'q': 'laptop'}}, {'question': 'I want to buy some shoes. I like Adidas and Nike.', 'expected_query': {'max_price': None, 'q': 'shoe'}}, {'question': 'I want to buy a new skirt', 'expected_query': {'max_price': None, 'q': 'skirt'}}, {'question': 'My company is asking me to get a professional Deskopt PC - money is no object.', 'expected_query': {'max_price': 10000, 'q': 'professional desktop PC'}}, {'question': 'What are the best budget cameras?', 'expected_query': {'max_price': 300, 'q': 'camera'}}] questions = [d['question'] for d in dataset] ## Run the the API chain itself raise_error = False # Stop on first failed example - useful for development chain_outputs = [] failed_examples = [] for question in questions: try:
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/openapi_eval.html
4e48092ef1df-3
chain_outputs = [] failed_examples = [] for question in questions: try: chain_outputs.append(api_chain(question)) scores["completed"].append(1.0) except Exception as e: if raise_error: raise e failed_examples.append({'q': question, 'error': e}) scores["completed"].append(0.0) # If the chain failed to run, show the failing examples failed_examples [] answers = [res['output'] for res in chain_outputs] answers ['There are currently 10 Apple iPhone models available: Apple iPhone 14 Pro Max 256GB, Apple iPhone 12 128GB, Apple iPhone 13 128GB, Apple iPhone 14 Pro 128GB, Apple iPhone 14 Pro 256GB, Apple iPhone 14 Pro Max 128GB, Apple iPhone 13 Pro Max 128GB, Apple iPhone 14 128GB, Apple iPhone 12 Pro 512GB, and Apple iPhone 12 mini 64GB.', 'Yes, there are several budget laptops in the API response. For example, the HP 14-dq0055dx and HP 15-dw0083wm are both priced at $199.99 and $244.99 respectively.', 'The cheapest gaming PC available is the Alarco Gaming PC (X_BLACK_GTX750) for $499.99. You can find more information about it here: https://www.klarna.com/us/shopping/pl/cl223/3203154750/Desktop-Computers/Alarco-Gaming-PC-%28X_BLACK_GTX750%29/?utm_source=openai&ref-site=openai_plugin',
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/openapi_eval.html
4e48092ef1df-4
'Yes, there are several tablets under $400. These include the Apple iPad 10.2" 32GB (2019), Samsung Galaxy Tab A8 10.5 SM-X200 32GB, Samsung Galaxy Tab A7 Lite 8.7 SM-T220 32GB, Amazon Fire HD 8" 32GB (10th Generation), and Amazon Fire HD 10 32GB.', 'It looks like you are looking for the best headphones. Based on the API response, it looks like the Apple AirPods Pro (2nd generation) 2022, Apple AirPods Max, and Bose Noise Cancelling Headphones 700 are the best options.', 'The top rated laptops based on the API response are the Apple MacBook Pro (2021) M1 Pro 8C CPU 14C GPU 16GB 512GB SSD 14", Apple MacBook Pro (2022) M2 OC 10C GPU 8GB 256GB SSD 13.3", Apple MacBook Air (2022) M2 OC 8C GPU 8GB 256GB SSD 13.6", and Apple MacBook Pro (2023) M2 Pro OC 16C GPU 16GB 512GB SSD 14.2".',
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/openapi_eval.html
4e48092ef1df-5
"I found several Nike and Adidas shoes in the API response. Here are the links to the products: Nike Dunk Low M - Black/White: https://www.klarna.com/us/shopping/pl/cl337/3200177969/Shoes/Nike-Dunk-Low-M-Black-White/?utm_source=openai&ref-site=openai_plugin, Nike Air Jordan 4 Retro M - Midnight Navy: https://www.klarna.com/us/shopping/pl/cl337/3202929835/Shoes/Nike-Air-Jordan-4-Retro-M-Midnight-Navy/?utm_source=openai&ref-site=openai_plugin, Nike Air Force 1 '07 M - White: https://www.klarna.com/us/shopping/pl/cl337/3979297/Shoes/Nike-Air-Force-1-07-M-White/?utm_source=openai&ref-site=openai_plugin, Nike Dunk Low W - White/Black: https://www.klarna.com/us/shopping/pl/cl337/3200134705/Shoes/Nike-Dunk-Low-W-White-Black/?utm_source=openai&ref-site=openai_plugin, Nike Air Jordan 1 Retro High M - White/University Blue/Black: https://www.klarna.com/us/shopping/pl/cl337/3200383658/Shoes/Nike-Air-Jordan-1-Retro-High-M-White-University-Blue-Black/?utm_source=openai&ref-site=openai_plugin, Nike Air Jordan 1 Retro High OG M - True Blue/Cement Grey/White: https://www.klarna.com/us/shopping/pl/cl337/3204655673/Shoes/Nike-Air-Jordan-1-Retro-High-OG-M-True-Blue-Cement-Grey-White/?utm_source=openai&ref-site=openai_plugin, Nike Air Jordan 11 Retro Cherry - White/Varsity Red/Black:
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/openapi_eval.html
4e48092ef1df-6
Nike Air Jordan 11 Retro Cherry - White/Varsity Red/Black: https://www.klarna.com/us/shopping/pl/cl337/3202929696/Shoes/Nike-Air-Jordan-11-Retro-Cherry-White-Varsity-Red-Black/?utm_source=openai&ref-site=openai_plugin, Nike Dunk High W - White/Black: https://www.klarna.com/us/shopping/pl/cl337/3201956448/Shoes/Nike-Dunk-High-W-White-Black/?utm_source=openai&ref-site=openai_plugin, Nike Air Jordan 5 Retro M - Black/Taxi/Aquatone: https://www.klarna.com/us/shopping/pl/cl337/3204923084/Shoes/Nike-Air-Jordan-5-Retro-M-Black-Taxi-Aquatone/?utm_source=openai&ref-site=openai_plugin, Nike Court Legacy Lift W: https://www.klarna.com/us/shopping/pl/cl337/3202103728/Shoes/Nike-Court-Legacy-Lift-W/?utm_source=openai&ref-site=openai_plugin",
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/openapi_eval.html
4e48092ef1df-7
"I found several skirts that may interest you. Please take a look at the following products: Avenue Plus Size Denim Stretch Skirt, LoveShackFancy Ruffled Mini Skirt - Antique White, Nike Dri-Fit Club Golf Skirt - Active Pink, Skims Soft Lounge Ruched Long Skirt, French Toast Girl's Front Pleated Skirt with Tabs, Alexia Admor Women's Harmonie Mini Skirt Pink Pink, Vero Moda Long Skirt, Nike Court Dri-FIT Victory Flouncy Tennis Skirt Women - White/Black, Haoyuan Mini Pleated Skirts W, and Zimmermann Lyre Midi Skirt.", 'Based on the API response, you may want to consider the Skytech Archangel Gaming Computer PC Desktop, the CyberPowerPC Gamer Master Gaming Desktop, or the ASUS ROG Strix G10DK-RS756, as they all offer powerful processors and plenty of RAM.', 'Based on the API response, the best budget cameras are the DJI Mini 2 Dog Camera ($448.50), Insta360 Sphere with Landing Pad ($429.99), DJI FPV Gimbal Camera ($121.06), Parrot Camera & Body ($36.19), and DJI FPV Air Unit ($179.00).'] Evaluate the requests chain# The API Chain has two main components: Translate the user query to an API request (request synthesizer) Translate the API response to a natural language response Here, we construct an evaluation chain to grade the request synthesizer against selected human queries import json truth_queries = [json.dumps(data["expected_query"]) for data in dataset] # Collect the API queries generated by the chain predicted_queries = [output["intermediate_steps"]["request_args"] for output in chain_outputs] from langchain.prompts import PromptTemplate
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/openapi_eval.html
4e48092ef1df-8
from langchain.prompts import PromptTemplate template = """You are trying to answer the following question by querying an API: > Question: {question} The query you know you should be executing against the API is: > Query: {truth_query} Is the following predicted query semantically the same (eg likely to produce the same answer)? > Predicted Query: {predict_query} Please give the Predicted Query a grade of either an A, B, C, D, or F, along with an explanation of why. End the evaluation with 'Final Grade: <the letter>' > Explanation: Let's think step by step.""" prompt = PromptTemplate.from_template(template) eval_chain = LLMChain(llm=llm, prompt=prompt, verbose=verbose) request_eval_results = [] for question, predict_query, truth_query in list(zip(questions, predicted_queries, truth_queries)): eval_output = eval_chain.run( question=question, truth_query=truth_query, predict_query=predict_query, ) request_eval_results.append(eval_output) request_eval_results [' The original query is asking for all iPhone models, so the "q" parameter is correct. The "max_price" parameter is also correct, as it is set to null, meaning that no maximum price is set. The predicted query adds two additional parameters, "size" and "min_price". The "size" parameter is not necessary, as it is not relevant to the question being asked. The "min_price" parameter is also not necessary, as it is not relevant to the question being asked and it is set to 0, which is the default value. Therefore, the predicted query is not semantically the same as the original query and is not likely to produce the same answer. Final Grade: D',
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/openapi_eval.html
4e48092ef1df-9
' The original query is asking for laptops with a maximum price of 300. The predicted query is asking for laptops with a minimum price of 0 and a maximum price of 500. This means that the predicted query is likely to return more results than the original query, as it is asking for a wider range of prices. Therefore, the predicted query is not semantically the same as the original query, and it is not likely to produce the same answer. Final Grade: F', " The first two parameters are the same, so that's good. The third parameter is different, but it's not necessary for the query, so that's not a problem. The fourth parameter is the problem. The original query specifies a maximum price of 500, while the predicted query specifies a maximum price of null. This means that the predicted query will not limit the results to the cheapest gaming PCs, so it is not semantically the same as the original query. Final Grade: F", ' The original query is asking for tablets under $400, so the first two parameters are correct. The predicted query also includes the parameters "size" and "min_price", which are not necessary for the original query. The "size" parameter is not relevant to the question, and the "min_price" parameter is redundant since the original query already specifies a maximum price. Therefore, the predicted query is not semantically the same as the original query and is not likely to produce the same answer. Final Grade: D', ' The original query is asking for headphones with no maximum price, so the predicted query is not semantically the same because it has a maximum price of 500. The predicted query also has a size of 10, which is not specified in the original query. Therefore, the predicted query is not semantically the same as the original query. Final Grade: F',
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/openapi_eval.html
4e48092ef1df-10
" The original query is asking for the top rated laptops, so the 'size' parameter should be set to 10 to get the top 10 results. The 'min_price' parameter should be set to 0 to get results from all price ranges. The 'max_price' parameter should be set to null to get results from all price ranges. The 'q' parameter should be set to 'laptop' to get results related to laptops. All of these parameters are present in the predicted query, so it is semantically the same as the original query. Final Grade: A", ' The original query is asking for shoes, so the predicted query is asking for the same thing. The original query does not specify a size, so the predicted query is not adding any additional information. The original query does not specify a price range, so the predicted query is adding additional information that is not necessary. Therefore, the predicted query is not semantically the same as the original query and is likely to produce different results. Final Grade: D', ' The original query is asking for a skirt, so the predicted query is asking for the same thing. The predicted query also adds additional parameters such as size and price range, which could help narrow down the results. However, the size parameter is not necessary for the query to be successful, and the price range is too narrow. Therefore, the predicted query is not as effective as the original query. Final Grade: C',
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/openapi_eval.html
4e48092ef1df-11
' The first part of the query is asking for a Desktop PC, which is the same as the original query. The second part of the query is asking for a size of 10, which is not relevant to the original query. The third part of the query is asking for a minimum price of 0, which is not relevant to the original query. The fourth part of the query is asking for a maximum price of null, which is not relevant to the original query. Therefore, the Predicted Query does not semantically match the original query and is not likely to produce the same answer. Final Grade: F', ' The original query is asking for cameras with a maximum price of 300. The predicted query is asking for cameras with a maximum price of 500. This means that the predicted query is likely to return more results than the original query, which may include cameras that are not within the budget range. Therefore, the predicted query is not semantically the same as the original query and does not answer the original question. Final Grade: F'] import re from typing import List # Parse the evaluation chain responses into a rubric def parse_eval_results(results: List[str]) -> List[float]: rubric = { "A": 1.0, "B": 0.75, "C": 0.5, "D": 0.25, "F": 0 } return [rubric[re.search(r'Final Grade: (\w+)', res).group(1)] for res in results] parsed_results = parse_eval_results(request_eval_results) # Collect the scores for a final evaluation table scores['request_synthesizer'].extend(parsed_results) Evaluate the Response Chain# The second component translated the structured API response to a natural language response. Evaluate this against the user’s original question.
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/openapi_eval.html
4e48092ef1df-12
Evaluate this against the user’s original question. from langchain.prompts import PromptTemplate template = """You are trying to answer the following question by querying an API: > Question: {question} The API returned a response of: > API result: {api_response} Your response to the user: {answer} Please evaluate the accuracy and utility of your response to the user's original question, conditioned on the information available. Give a letter grade of either an A, B, C, D, or F, along with an explanation of why. End the evaluation with 'Final Grade: <the letter>' > Explanation: Let's think step by step.""" prompt = PromptTemplate.from_template(template) eval_chain = LLMChain(llm=llm, prompt=prompt, verbose=verbose) # Extract the API responses from the chain api_responses = [output["intermediate_steps"]["response_text"] for output in chain_outputs] # Run the grader chain response_eval_results = [] for question, api_response, answer in list(zip(questions, api_responses, answers)): request_eval_results.append(eval_chain.run(question=question, api_response=api_response, answer=answer)) request_eval_results
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/openapi_eval.html
4e48092ef1df-13
request_eval_results [' The original query is asking for all iPhone models, so the "q" parameter is correct. The "max_price" parameter is also correct, as it is set to null, meaning that no maximum price is set. The predicted query adds two additional parameters, "size" and "min_price". The "size" parameter is not necessary, as it is not relevant to the question being asked. The "min_price" parameter is also not necessary, as it is not relevant to the question being asked and it is set to 0, which is the default value. Therefore, the predicted query is not semantically the same as the original query and is not likely to produce the same answer. Final Grade: D', ' The original query is asking for laptops with a maximum price of 300. The predicted query is asking for laptops with a minimum price of 0 and a maximum price of 500. This means that the predicted query is likely to return more results than the original query, as it is asking for a wider range of prices. Therefore, the predicted query is not semantically the same as the original query, and it is not likely to produce the same answer. Final Grade: F', " The first two parameters are the same, so that's good. The third parameter is different, but it's not necessary for the query, so that's not a problem. The fourth parameter is the problem. The original query specifies a maximum price of 500, while the predicted query specifies a maximum price of null. This means that the predicted query will not limit the results to the cheapest gaming PCs, so it is not semantically the same as the original query. Final Grade: F",
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/openapi_eval.html
4e48092ef1df-14
' The original query is asking for tablets under $400, so the first two parameters are correct. The predicted query also includes the parameters "size" and "min_price", which are not necessary for the original query. The "size" parameter is not relevant to the question, and the "min_price" parameter is redundant since the original query already specifies a maximum price. Therefore, the predicted query is not semantically the same as the original query and is not likely to produce the same answer. Final Grade: D', ' The original query is asking for headphones with no maximum price, so the predicted query is not semantically the same because it has a maximum price of 500. The predicted query also has a size of 10, which is not specified in the original query. Therefore, the predicted query is not semantically the same as the original query. Final Grade: F', " The original query is asking for the top rated laptops, so the 'size' parameter should be set to 10 to get the top 10 results. The 'min_price' parameter should be set to 0 to get results from all price ranges. The 'max_price' parameter should be set to null to get results from all price ranges. The 'q' parameter should be set to 'laptop' to get results related to laptops. All of these parameters are present in the predicted query, so it is semantically the same as the original query. Final Grade: A", ' The original query is asking for shoes, so the predicted query is asking for the same thing. The original query does not specify a size, so the predicted query is not adding any additional information. The original query does not specify a price range, so the predicted query is adding additional information that is not necessary. Therefore, the predicted query is not semantically the same as the original query and is likely to produce different results. Final Grade: D',
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/openapi_eval.html
4e48092ef1df-15
' The original query is asking for a skirt, so the predicted query is asking for the same thing. The predicted query also adds additional parameters such as size and price range, which could help narrow down the results. However, the size parameter is not necessary for the query to be successful, and the price range is too narrow. Therefore, the predicted query is not as effective as the original query. Final Grade: C', ' The first part of the query is asking for a Desktop PC, which is the same as the original query. The second part of the query is asking for a size of 10, which is not relevant to the original query. The third part of the query is asking for a minimum price of 0, which is not relevant to the original query. The fourth part of the query is asking for a maximum price of null, which is not relevant to the original query. Therefore, the Predicted Query does not semantically match the original query and is not likely to produce the same answer. Final Grade: F', ' The original query is asking for cameras with a maximum price of 300. The predicted query is asking for cameras with a maximum price of 500. This means that the predicted query is likely to return more results than the original query, which may include cameras that are not within the budget range. Therefore, the predicted query is not semantically the same as the original query and does not answer the original question. Final Grade: F', ' The user asked a question about what iPhone models are available, and the API returned a response with 10 different models. The response provided by the user accurately listed all 10 models, so the accuracy of the response is A+. The utility of the response is also A+ since the user was able to get the exact information they were looking for. Final Grade: A+',
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/openapi_eval.html
4e48092ef1df-16
" The API response provided a list of laptops with their prices and attributes. The user asked if there were any budget laptops, and the response provided a list of laptops that are all priced under $500. Therefore, the response was accurate and useful in answering the user's question. Final Grade: A", " The API response provided the name, price, and URL of the product, which is exactly what the user asked for. The response also provided additional information about the product's attributes, which is useful for the user to make an informed decision. Therefore, the response is accurate and useful. Final Grade: A", " The API response provided a list of tablets that are under $400. The response accurately answered the user's question. Additionally, the response provided useful information such as the product name, price, and attributes. Therefore, the response was accurate and useful. Final Grade: A", " The API response provided a list of headphones with their respective prices and attributes. The user asked for the best headphones, so the response should include the best headphones based on the criteria provided. The response provided a list of headphones that are all from the same brand (Apple) and all have the same type of headphone (True Wireless, In-Ear). This does not provide the user with enough information to make an informed decision about which headphones are the best. Therefore, the response does not accurately answer the user's question. Final Grade: F", ' The API response provided a list of laptops with their attributes, which is exactly what the user asked for. The response provided a comprehensive list of the top rated laptops, which is what the user was looking for. The response was accurate and useful, providing the user with the information they needed. Final Grade: A',
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/openapi_eval.html
4e48092ef1df-17
' The API response provided a list of shoes from both Adidas and Nike, which is exactly what the user asked for. The response also included the product name, price, and attributes for each shoe, which is useful information for the user to make an informed decision. The response also included links to the products, which is helpful for the user to purchase the shoes. Therefore, the response was accurate and useful. Final Grade: A', " The API response provided a list of skirts that could potentially meet the user's needs. The response also included the name, price, and attributes of each skirt. This is a great start, as it provides the user with a variety of options to choose from. However, the response does not provide any images of the skirts, which would have been helpful for the user to make a decision. Additionally, the response does not provide any information about the availability of the skirts, which could be important for the user. \n\nFinal Grade: B", ' The user asked for a professional desktop PC with no budget constraints. The API response provided a list of products that fit the criteria, including the Skytech Archangel Gaming Computer PC Desktop, the CyberPowerPC Gamer Master Gaming Desktop, and the ASUS ROG Strix G10DK-RS756. The response accurately suggested these three products as they all offer powerful processors and plenty of RAM. Therefore, the response is accurate and useful. Final Grade: A', " The API response provided a list of cameras with their prices, which is exactly what the user asked for. The response also included additional information such as features and memory cards, which is not necessary for the user's question but could be useful for further research. The response was accurate and provided the user with the information they needed. Final Grade: A"] # Reusing the rubric from above, parse the evaluation chain responses parsed_response_results = parse_eval_results(request_eval_results)
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/openapi_eval.html
4e48092ef1df-18
parsed_response_results = parse_eval_results(request_eval_results) # Collect the scores for a final evaluation table scores['result_synthesizer'].extend(parsed_response_results) # Print out Score statistics for the evaluation session header = "{:<20}\t{:<10}\t{:<10}\t{:<10}".format("Metric", "Min", "Mean", "Max") print(header) for metric, metric_scores in scores.items(): mean_scores = sum(metric_scores) / len(metric_scores) if len(metric_scores) > 0 else float('nan') row = "{:<20}\t{:<10.2f}\t{:<10.2f}\t{:<10.2f}".format(metric, min(metric_scores), mean_scores, max(metric_scores)) print(row) Metric Min Mean Max completed 1.00 1.00 1.00 request_synthesizer 0.00 0.23 1.00 result_synthesizer 0.00 0.55 1.00 # Re-show the examples for which the chain failed to complete failed_examples [] Generating Test Datasets# To evaluate a chain against your own endpoint, you’ll want to generate a test dataset that’s conforms to the API. This section provides an overview of how to bootstrap the process. First, we’ll parse the OpenAPI Spec. For this example, we’ll Speak’s OpenAPI specification. # Load and parse the OpenAPI Spec spec = OpenAPISpec.from_url("https://api.speak.com/openapi.yaml") Attempting to load an OpenAPI 3.0.1 spec. This may result in degraded performance. Convert your OpenAPI spec to 3.1.* spec for better support.
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/openapi_eval.html
4e48092ef1df-19
Attempting to load an OpenAPI 3.0.1 spec. This may result in degraded performance. Convert your OpenAPI spec to 3.1.* spec for better support. # List the paths in the OpenAPI Spec paths = sorted(spec.paths.keys()) paths ['/v1/public/openai/explain-phrase', '/v1/public/openai/explain-task', '/v1/public/openai/translate'] # See which HTTP Methods are available for a given path methods = spec.get_methods_for_path('/v1/public/openai/explain-task') methods ['post'] # Load a single endpoint operation operation = APIOperation.from_openapi_spec(spec, '/v1/public/openai/explain-task', 'post') # The operation can be serialized as typescript print(operation.to_typescript()) type explainTask = (_: { /* Description of the task that the user wants to accomplish or do. For example, "tell the waiter they messed up my order" or "compliment someone on their shirt" */ task_description?: string, /* The foreign language that the user is learning and asking about. The value can be inferred from question - for example, if the user asks "how do i ask a girl out in mexico city", the value should be "Spanish" because of Mexico City. Always use the full name of the language (e.g. Spanish, French). */ learning_language?: string, /* The user's native language. Infer this value from the language the user asked their question in. Always use the full name of the language (e.g. Spanish, French). */ native_language?: string, /* A description of any additional context in the user's question that could affect the explanation - e.g. setting, scenario, situation, tone, speaking style and formality, usage notes, or any other qualifiers. */
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/openapi_eval.html
4e48092ef1df-20
additional_context?: string, /* Full text of the user's question. */ full_query?: string, }) => any; # Compress the service definition to avoid leaking too much input structure to the sample data template = """In 20 words or less, what does this service accomplish? {spec} Function: It's designed to """ prompt = PromptTemplate.from_template(template) generation_chain = LLMChain(llm=llm, prompt=prompt) purpose = generation_chain.run(spec=operation.to_typescript()) template = """Write a list of {num_to_generate} unique messages users might send to a service designed to{purpose} They must each be completely unique. 1.""" def parse_list(text: str) -> List[str]: # Match lines starting with a number then period # Strip leading and trailing whitespace matches = re.findall(r'^\d+\. ', text) return [re.sub(r'^\d+\. ', '', q).strip().strip('"') for q in text.split('\n')] num_to_generate = 10 # How many examples to use for this test set. prompt = PromptTemplate.from_template(template) generation_chain = LLMChain(llm=llm, prompt=prompt) text = generation_chain.run(purpose=purpose, num_to_generate=num_to_generate) # Strip preceding numeric bullets queries = parse_list(text) queries ["Can you explain how to say 'hello' in Spanish?", "I need help understanding the French word for 'goodbye'.", "Can you tell me how to say 'thank you' in German?", "I'm trying to learn the Italian word for 'please'.", "Can you help me with the pronunciation of 'yes' in Portuguese?", "I'm looking for the Dutch word for 'no'.",
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/openapi_eval.html
4e48092ef1df-21
"I'm looking for the Dutch word for 'no'.", "Can you explain the meaning of 'hello' in Japanese?", "I need help understanding the Russian word for 'thank you'.", "Can you tell me how to say 'goodbye' in Chinese?", "I'm trying to learn the Arabic word for 'please'."] # Define the generation chain to get hypotheses api_chain = OpenAPIEndpointChain.from_api_operation( operation, llm, requests=Requests(), verbose=verbose, return_intermediate_steps=True # Return request and response text ) predicted_outputs =[api_chain(query) for query in queries] request_args = [output["intermediate_steps"]["request_args"] for output in predicted_outputs] # Show the generated request request_args ['{"task_description": "say \'hello\'", "learning_language": "Spanish", "native_language": "English", "full_query": "Can you explain how to say \'hello\' in Spanish?"}', '{"task_description": "understanding the French word for \'goodbye\'", "learning_language": "French", "native_language": "English", "full_query": "I need help understanding the French word for \'goodbye\'."}', '{"task_description": "say \'thank you\'", "learning_language": "German", "native_language": "English", "full_query": "Can you tell me how to say \'thank you\' in German?"}', '{"task_description": "Learn the Italian word for \'please\'", "learning_language": "Italian", "native_language": "English", "full_query": "I\'m trying to learn the Italian word for \'please\'."}',
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/openapi_eval.html
4e48092ef1df-22
'{"task_description": "Help with pronunciation of \'yes\' in Portuguese", "learning_language": "Portuguese", "native_language": "English", "full_query": "Can you help me with the pronunciation of \'yes\' in Portuguese?"}', '{"task_description": "Find the Dutch word for \'no\'", "learning_language": "Dutch", "native_language": "English", "full_query": "I\'m looking for the Dutch word for \'no\'."}', '{"task_description": "Explain the meaning of \'hello\' in Japanese", "learning_language": "Japanese", "native_language": "English", "full_query": "Can you explain the meaning of \'hello\' in Japanese?"}', '{"task_description": "understanding the Russian word for \'thank you\'", "learning_language": "Russian", "native_language": "English", "full_query": "I need help understanding the Russian word for \'thank you\'."}', '{"task_description": "say goodbye", "learning_language": "Chinese", "native_language": "English", "full_query": "Can you tell me how to say \'goodbye\' in Chinese?"}', '{"task_description": "Learn the Arabic word for \'please\'", "learning_language": "Arabic", "native_language": "English", "full_query": "I\'m trying to learn the Arabic word for \'please\'."}'] ## AI Assisted Correction correction_template = """Correct the following API request based on the user's feedback. If the user indicates no changes are needed, output the original without making any changes. REQUEST: {request} User Feedback / requested changes: {user_feedback} Finalized Request: """ prompt = PromptTemplate.from_template(correction_template) correction_chain = LLMChain(llm=llm, prompt=prompt) ground_truth = []
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/openapi_eval.html
4e48092ef1df-23
ground_truth = [] for query, request_arg in list(zip(queries, request_args)): feedback = input(f"Query: {query}\nRequest: {request_arg}\nRequested changes: ") if feedback == 'n' or feedback == 'none' or not feedback: ground_truth.append(request_arg) continue resolved = correction_chain.run(request=request_arg, user_feedback=feedback) ground_truth.append(resolved.strip()) print("Updated request:", resolved) Query: Can you explain how to say 'hello' in Spanish? Request: {"task_description": "say 'hello'", "learning_language": "Spanish", "native_language": "English", "full_query": "Can you explain how to say 'hello' in Spanish?"} Requested changes: Query: I need help understanding the French word for 'goodbye'. Request: {"task_description": "understanding the French word for 'goodbye'", "learning_language": "French", "native_language": "English", "full_query": "I need help understanding the French word for 'goodbye'."} Requested changes: Query: Can you tell me how to say 'thank you' in German? Request: {"task_description": "say 'thank you'", "learning_language": "German", "native_language": "English", "full_query": "Can you tell me how to say 'thank you' in German?"} Requested changes: Query: I'm trying to learn the Italian word for 'please'. Request: {"task_description": "Learn the Italian word for 'please'", "learning_language": "Italian", "native_language": "English", "full_query": "I'm trying to learn the Italian word for 'please'."} Requested changes: Query: Can you help me with the pronunciation of 'yes' in Portuguese?
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/openapi_eval.html
4e48092ef1df-24
Query: Can you help me with the pronunciation of 'yes' in Portuguese? Request: {"task_description": "Help with pronunciation of 'yes' in Portuguese", "learning_language": "Portuguese", "native_language": "English", "full_query": "Can you help me with the pronunciation of 'yes' in Portuguese?"} Requested changes: Query: I'm looking for the Dutch word for 'no'. Request: {"task_description": "Find the Dutch word for 'no'", "learning_language": "Dutch", "native_language": "English", "full_query": "I'm looking for the Dutch word for 'no'."} Requested changes: Query: Can you explain the meaning of 'hello' in Japanese? Request: {"task_description": "Explain the meaning of 'hello' in Japanese", "learning_language": "Japanese", "native_language": "English", "full_query": "Can you explain the meaning of 'hello' in Japanese?"} Requested changes: Query: I need help understanding the Russian word for 'thank you'. Request: {"task_description": "understanding the Russian word for 'thank you'", "learning_language": "Russian", "native_language": "English", "full_query": "I need help understanding the Russian word for 'thank you'."} Requested changes: Query: Can you tell me how to say 'goodbye' in Chinese? Request: {"task_description": "say goodbye", "learning_language": "Chinese", "native_language": "English", "full_query": "Can you tell me how to say 'goodbye' in Chinese?"} Requested changes: Query: I'm trying to learn the Arabic word for 'please'.
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/openapi_eval.html
4e48092ef1df-25
Requested changes: Query: I'm trying to learn the Arabic word for 'please'. Request: {"task_description": "Learn the Arabic word for 'please'", "learning_language": "Arabic", "native_language": "English", "full_query": "I'm trying to learn the Arabic word for 'please'."} Requested changes: Now you can use the ground_truth as shown above in Evaluate the Requests Chain! # Now you have a new ground truth set to use as shown above! ground_truth ['{"task_description": "say \'hello\'", "learning_language": "Spanish", "native_language": "English", "full_query": "Can you explain how to say \'hello\' in Spanish?"}', '{"task_description": "understanding the French word for \'goodbye\'", "learning_language": "French", "native_language": "English", "full_query": "I need help understanding the French word for \'goodbye\'."}', '{"task_description": "say \'thank you\'", "learning_language": "German", "native_language": "English", "full_query": "Can you tell me how to say \'thank you\' in German?"}', '{"task_description": "Learn the Italian word for \'please\'", "learning_language": "Italian", "native_language": "English", "full_query": "I\'m trying to learn the Italian word for \'please\'."}', '{"task_description": "Help with pronunciation of \'yes\' in Portuguese", "learning_language": "Portuguese", "native_language": "English", "full_query": "Can you help me with the pronunciation of \'yes\' in Portuguese?"}',
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/openapi_eval.html
4e48092ef1df-26
'{"task_description": "Find the Dutch word for \'no\'", "learning_language": "Dutch", "native_language": "English", "full_query": "I\'m looking for the Dutch word for \'no\'."}', '{"task_description": "Explain the meaning of \'hello\' in Japanese", "learning_language": "Japanese", "native_language": "English", "full_query": "Can you explain the meaning of \'hello\' in Japanese?"}', '{"task_description": "understanding the Russian word for \'thank you\'", "learning_language": "Russian", "native_language": "English", "full_query": "I need help understanding the Russian word for \'thank you\'."}', '{"task_description": "say goodbye", "learning_language": "Chinese", "native_language": "English", "full_query": "Can you tell me how to say \'goodbye\' in Chinese?"}', '{"task_description": "Learn the Arabic word for \'please\'", "learning_language": "Arabic", "native_language": "English", "full_query": "I\'m trying to learn the Arabic word for \'please\'."}'] previous LLM Math next Question Answering Benchmarking: Paul Graham Essay Contents Load the API Chain Optional: Generate Input Questions and Request Ground Truth Queries Run the API Chain Evaluate the requests chain Evaluate the Response Chain Generating Test Datasets By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 16, 2023.
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/evaluation/openapi_eval.html
934dc6a3e200-0
.ipynb .pdf AutoGPT Contents Set up tools Set up memory Setup model and AutoGPT Run an example Chat History Memory AutoGPT# Implementation of https://github.com/Significant-Gravitas/Auto-GPT but with LangChain primitives (LLMs, PromptTemplates, VectorStores, Embeddings, Tools) Set up tools# We’ll set up an AutoGPT with a search tool, and write-file tool, and a read-file tool from langchain.utilities import SerpAPIWrapper from langchain.agents import Tool from langchain.tools.file_management.write import WriteFileTool from langchain.tools.file_management.read import ReadFileTool search = SerpAPIWrapper() tools = [ Tool( name = "search", func=search.run, description="useful for when you need to answer questions about current events. You should ask targeted questions" ), WriteFileTool(), ReadFileTool(), ] Set up memory# The memory here is used for the agents intermediate steps from langchain.vectorstores import FAISS from langchain.docstore import InMemoryDocstore from langchain.embeddings import OpenAIEmbeddings # Define your embedding model embeddings_model = OpenAIEmbeddings() # Initialize the vectorstore as empty import faiss embedding_size = 1536 index = faiss.IndexFlatL2(embedding_size) vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {}) Setup model and AutoGPT# Initialize everything! We will use ChatOpenAI model from langchain.experimental import AutoGPT from langchain.chat_models import ChatOpenAI agent = AutoGPT.from_llm_and_tools( ai_name="Tom", ai_role="Assistant",
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/autogpt.html
934dc6a3e200-1
ai_name="Tom", ai_role="Assistant", tools=tools, llm=ChatOpenAI(temperature=0), memory=vectorstore.as_retriever() ) # Set verbose to be true agent.chain.verbose = True Run an example# Here we will make it write a weather report for SF agent.run(["write a weather report for SF today"]) Chat History Memory# In addition to the memory that holds the agent immediate steps, we also have a chat history memory. By default, the agent will use ‘ChatMessageHistory’ and it can be changed. This is useful when you want to use a different type of memory for example ‘FileChatHistoryMemory’ from langchain.memory.chat_message_histories import FileChatMessageHistory agent = AutoGPT.from_llm_and_tools( ai_name="Tom", ai_role="Assistant", tools=tools, llm=ChatOpenAI(temperature=0), memory=vectorstore.as_retriever(), chat_history_memory=FileChatMessageHistory('chat_history.txt') ) Contents Set up tools Set up memory Setup model and AutoGPT Run an example Chat History Memory By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 16, 2023.
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/autogpt.html
e5f078cfacff-0
.ipynb .pdf BabyAGI User Guide Contents Install and Import Required Modules Connect to the Vector Store Run the BabyAGI BabyAGI User Guide# This notebook demonstrates how to implement BabyAGI by Yohei Nakajima. BabyAGI is an AI agent that can generate and pretend to execute tasks based on a given objective. This guide will help you understand the components to create your own recursive agents. Although BabyAGI uses specific vectorstores/model providers (Pinecone, OpenAI), one of the benefits of implementing it with LangChain is that you can easily swap those out for different options. In this implementation we use a FAISS vectorstore (because it runs locally and is free). Install and Import Required Modules# import os from collections import deque from typing import Dict, List, Optional, Any from langchain import LLMChain, OpenAI, PromptTemplate from langchain.embeddings import OpenAIEmbeddings from langchain.llms import BaseLLM from langchain.vectorstores.base import VectorStore from pydantic import BaseModel, Field from langchain.chains.base import Chain from langchain.experimental import BabyAGI Connect to the Vector Store# Depending on what vectorstore you use, this step may look different. from langchain.vectorstores import FAISS from langchain.docstore import InMemoryDocstore # Define your embedding model embeddings_model = OpenAIEmbeddings() # Initialize the vectorstore as empty import faiss embedding_size = 1536 index = faiss.IndexFlatL2(embedding_size) vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {}) Run the BabyAGI# Now it’s time to create the BabyAGI controller and watch it try to accomplish your objective.
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/baby_agi.html
e5f078cfacff-1
OBJECTIVE = "Write a weather report for SF today" llm = OpenAI(temperature=0) # Logging of LLMChains verbose = False # If None, will keep on going forever max_iterations: Optional[int] = 3 baby_agi = BabyAGI.from_llm( llm=llm, vectorstore=vectorstore, verbose=verbose, max_iterations=max_iterations ) baby_agi({"objective": OBJECTIVE}) *****TASK LIST***** 1: Make a todo list *****NEXT TASK***** 1: Make a todo list *****TASK RESULT***** 1. Check the weather forecast for San Francisco today 2. Make note of the temperature, humidity, wind speed, and other relevant weather conditions 3. Write a weather report summarizing the forecast 4. Check for any weather alerts or warnings 5. Share the report with the relevant stakeholders *****TASK LIST***** 2: Check the current temperature in San Francisco 3: Check the current humidity in San Francisco 4: Check the current wind speed in San Francisco 5: Check for any weather alerts or warnings in San Francisco 6: Check the forecast for the next 24 hours in San Francisco 7: Check the forecast for the next 48 hours in San Francisco 8: Check the forecast for the next 72 hours in San Francisco 9: Check the forecast for the next week in San Francisco 10: Check the forecast for the next month in San Francisco 11: Check the forecast for the next 3 months in San Francisco 1: Write a weather report for SF today *****NEXT TASK***** 2: Check the current temperature in San Francisco *****TASK RESULT***** I will check the current temperature in San Francisco. I will use an online weather service to get the most up-to-date information. *****TASK LIST*****
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/baby_agi.html
e5f078cfacff-2
*****TASK LIST***** 3: Check the current UV index in San Francisco. 4: Check the current air quality in San Francisco. 5: Check the current precipitation levels in San Francisco. 6: Check the current cloud cover in San Francisco. 7: Check the current barometric pressure in San Francisco. 8: Check the current dew point in San Francisco. 9: Check the current wind direction in San Francisco. 10: Check the current humidity levels in San Francisco. 1: Check the current temperature in San Francisco to the average temperature for this time of year. 2: Check the current visibility in San Francisco. 11: Write a weather report for SF today. *****NEXT TASK***** 3: Check the current UV index in San Francisco. *****TASK RESULT***** The current UV index in San Francisco is moderate. The UV index is expected to remain at moderate levels throughout the day. It is recommended to wear sunscreen and protective clothing when outdoors. *****TASK ENDING***** {'objective': 'Write a weather report for SF today'} Contents Install and Import Required Modules Connect to the Vector Store Run the BabyAGI By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 16, 2023.
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/baby_agi.html
5e987ffa8a85-0
.ipynb .pdf BabyAGI with Tools Contents Install and Import Required Modules Connect to the Vector Store Define the Chains Run the BabyAGI BabyAGI with Tools# This notebook builds on top of baby agi, but shows how you can swap out the execution chain. The previous execution chain was just an LLM which made stuff up. By swapping it out with an agent that has access to tools, we can hopefully get real reliable information Install and Import Required Modules# import os from collections import deque from typing import Dict, List, Optional, Any from langchain import LLMChain, OpenAI, PromptTemplate from langchain.embeddings import OpenAIEmbeddings from langchain.llms import BaseLLM from langchain.vectorstores.base import VectorStore from pydantic import BaseModel, Field from langchain.chains.base import Chain from langchain.experimental import BabyAGI Connect to the Vector Store# Depending on what vectorstore you use, this step may look different. %pip install faiss-cpu > /dev/null %pip install google-search-results > /dev/null from langchain.vectorstores import FAISS from langchain.docstore import InMemoryDocstore Note: you may need to restart the kernel to use updated packages. Note: you may need to restart the kernel to use updated packages. # Define your embedding model embeddings_model = OpenAIEmbeddings() # Initialize the vectorstore as empty import faiss embedding_size = 1536 index = faiss.IndexFlatL2(embedding_size) vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {}) Define the Chains# BabyAGI relies on three LLM chains: Task creation chain to select new tasks to add to the list
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/baby_agi_with_agent.html
5e987ffa8a85-1
Task creation chain to select new tasks to add to the list Task prioritization chain to re-prioritize tasks Execution Chain to execute the tasks NOTE: in this notebook, the Execution chain will now be an agent. from langchain.agents import ZeroShotAgent, Tool, AgentExecutor from langchain import OpenAI, SerpAPIWrapper, LLMChain todo_prompt = PromptTemplate.from_template( "You are a planner who is an expert at coming up with a todo list for a given objective. Come up with a todo list for this objective: {objective}" ) todo_chain = LLMChain(llm=OpenAI(temperature=0), prompt=todo_prompt) search = SerpAPIWrapper() tools = [ Tool( name="Search", func=search.run, description="useful for when you need to answer questions about current events", ), Tool( name="TODO", func=todo_chain.run, description="useful for when you need to come up with todo lists. Input: an objective to create a todo list for. Output: a todo list for that objective. Please be very clear what the objective is!", ), ] prefix = """You are an AI who performs one task based on the following objective: {objective}. Take into account these previously completed tasks: {context}.""" suffix = """Question: {task} {agent_scratchpad}""" prompt = ZeroShotAgent.create_prompt( tools, prefix=prefix, suffix=suffix, input_variables=["objective", "task", "context", "agent_scratchpad"], ) llm = OpenAI(temperature=0) llm_chain = LLMChain(llm=llm, prompt=prompt)
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/baby_agi_with_agent.html
5e987ffa8a85-2
llm_chain = LLMChain(llm=llm, 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 ) Run the BabyAGI# Now it’s time to create the BabyAGI controller and watch it try to accomplish your objective. OBJECTIVE = "Write a weather report for SF today" # Logging of LLMChains verbose = False # If None, will keep on going forever max_iterations: Optional[int] = 3 baby_agi = BabyAGI.from_llm( llm=llm, vectorstore=vectorstore, task_execution_chain=agent_executor, verbose=verbose, max_iterations=max_iterations ) baby_agi({"objective": OBJECTIVE}) *****TASK LIST***** 1: Make a todo list *****NEXT TASK***** 1: Make a todo list > Entering new AgentExecutor chain... Thought: I need to come up with a todo list Action: TODO Action Input: Write a weather report for SF today 1. Research current weather conditions in San Francisco 2. Gather data on temperature, humidity, wind speed, and other relevant weather conditions 3. Analyze data to determine current weather trends 4. Write a brief introduction to the weather report 5. Describe current weather conditions in San Francisco 6. Discuss any upcoming weather changes 7. Summarize the weather report 8. Proofread and edit the report 9. Submit the report I now know the final answer
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/baby_agi_with_agent.html
5e987ffa8a85-3
9. Submit the report I now know the final answer Final Answer: The todo list for writing a weather report for SF today is: 1. Research current weather conditions in San Francisco; 2. Gather data on temperature, humidity, wind speed, and other relevant weather conditions; 3. Analyze data to determine current weather trends; 4. Write a brief introduction to the weather report; 5. Describe current weather conditions in San Francisco; 6. Discuss any upcoming weather changes; 7. Summarize the weather report; 8. Proofread and edit the report; 9. Submit the report. > Finished chain. *****TASK RESULT***** The todo list for writing a weather report for SF today is: 1. Research current weather conditions in San Francisco; 2. Gather data on temperature, humidity, wind speed, and other relevant weather conditions; 3. Analyze data to determine current weather trends; 4. Write a brief introduction to the weather report; 5. Describe current weather conditions in San Francisco; 6. Discuss any upcoming weather changes; 7. Summarize the weather report; 8. Proofread and edit the report; 9. Submit the report. *****TASK LIST***** 2: Gather data on precipitation, cloud cover, and other relevant weather conditions; 3: Analyze data to determine any upcoming weather changes; 4: Research current weather forecasts for San Francisco; 5: Create a visual representation of the weather report; 6: Include relevant images and graphics in the report; 7: Format the report for readability; 8: Publish the report online; 9: Monitor the report for accuracy. *****NEXT TASK***** 2: Gather data on precipitation, cloud cover, and other relevant weather conditions; > Entering new AgentExecutor chain... Thought: I need to search for current weather conditions in San Francisco Action: Search
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/baby_agi_with_agent.html
5e987ffa8a85-4
Thought: I need to search for current weather conditions in San Francisco Action: Search Action Input: Current weather conditions in San FranciscoCurrent Weather for Popular Cities ; San Francisco, CA 46 · Partly Cloudy ; Manhattan, NY warning 52 · Cloudy ; Schiller Park, IL (60176) 40 · Sunny ; Boston, MA 54 ... I need to compile the data into a weather report Action: TODO Action Input: Compile data into a weather report 1. Gather data from reliable sources such as the National Weather Service, local weather stations, and other meteorological organizations. 2. Analyze the data to identify trends and patterns. 3. Create a chart or graph to visualize the data. 4. Write a summary of the data and its implications. 5. Compile the data into a report format. 6. Proofread the report for accuracy and clarity. 7. Publish the report to a website or other platform. 8. Distribute the report to relevant stakeholders. I now know the final answer Final Answer: Today in San Francisco, the temperature is 46 degrees Fahrenheit with partly cloudy skies. The forecast for the rest of the day is expected to remain partly cloudy. > Finished chain. *****TASK RESULT***** Today in San Francisco, the temperature is 46 degrees Fahrenheit with partly cloudy skies. The forecast for the rest of the day is expected to remain partly cloudy. *****TASK LIST***** 3: Format the report for readability; 4: Include relevant images and graphics in the report; 5: Compare the current weather conditions in San Francisco to the forecasted conditions; 6: Identify any potential weather-related hazards in the area; 7: Research historical weather patterns in San Francisco; 8: Identify any potential trends in the weather data; 9: Include relevant data sources in the report; 10: Summarize the weather report in a concise manner;
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/baby_agi_with_agent.html
5e987ffa8a85-5
10: Summarize the weather report in a concise manner; 11: Include a summary of the forecasted weather conditions; 12: Include a summary of the current weather conditions; 13: Include a summary of the historical weather patterns; 14: Include a summary of the potential weather-related hazards; 15: Include a summary of the potential trends in the weather data; 16: Include a summary of the data sources used in the report; 17: Analyze data to determine any upcoming weather changes; 18: Research current weather forecasts for San Francisco; 19: Create a visual representation of the weather report; 20: Publish the report online; 21: Monitor the report for accuracy *****NEXT TASK***** 3: Format the report for readability; > Entering new AgentExecutor chain... Thought: I need to make sure the report is easy to read; Action: TODO Action Input: Make the report easy to read 1. Break up the report into sections with clear headings 2. Use bullet points and numbered lists to organize information 3. Use short, concise sentences 4. Use simple language and avoid jargon 5. Include visuals such as charts, graphs, and diagrams to illustrate points 6. Use bold and italicized text to emphasize key points 7. Include a table of contents and page numbers 8. Use a consistent font and font size throughout the report 9. Include a summary at the end of the report 10. Proofread the report for typos and errors I now know the final answer
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/baby_agi_with_agent.html
5e987ffa8a85-6
10. Proofread the report for typos and errors I now know the final answer Final Answer: The report should be formatted for readability by breaking it up into sections with clear headings, using bullet points and numbered lists to organize information, using short, concise sentences, using simple language and avoiding jargon, including visuals such as charts, graphs, and diagrams to illustrate points, using bold and italicized text to emphasize key points, including a table of contents and page numbers, using a consistent font and font size throughout the report, including a summary at the end of the report, and proofreading the report for typos and errors. > Finished chain. *****TASK RESULT***** The report should be formatted for readability by breaking it up into sections with clear headings, using bullet points and numbered lists to organize information, using short, concise sentences, using simple language and avoiding jargon, including visuals such as charts, graphs, and diagrams to illustrate points, using bold and italicized text to emphasize key points, including a table of contents and page numbers, using a consistent font and font size throughout the report, including a summary at the end of the report, and proofreading the report for typos and errors. *****TASK ENDING***** {'objective': 'Write a weather report for SF today'} Contents Install and Import Required Modules Connect to the Vector Store Define the Chains Run the BabyAGI By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 16, 2023.
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/baby_agi_with_agent.html
cb88c78a36e9-0
.ipynb .pdf AutoGPT example finding Winning Marathon Times Contents Set up tools Set up memory Setup model and AutoGPT AutoGPT for Querying the Web AutoGPT example finding Winning Marathon Times# Implementation of https://github.com/Significant-Gravitas/Auto-GPT With LangChain primitives (LLMs, PromptTemplates, VectorStores, Embeddings, Tools) # !pip install bs4 # !pip install nest_asyncio # General import os import pandas as pd from langchain.experimental.autonomous_agents.autogpt.agent import AutoGPT from langchain.chat_models import ChatOpenAI from langchain.agents.agent_toolkits.pandas.base import create_pandas_dataframe_agent from langchain.docstore.document import Document import asyncio import nest_asyncio # Needed synce jupyter runs an async eventloop nest_asyncio.apply() llm = ChatOpenAI(model_name="gpt-4", temperature=1.0) Set up tools# We’ll set up an AutoGPT with a search tool, and write-file tool, and a read-file tool, a web browsing tool, and a tool to interact with a CSV file via a python REPL Define any other tools you want to use below: # Tools import os from contextlib import contextmanager from typing import Optional from langchain.agents import tool from langchain.tools.file_management.read import ReadFileTool from langchain.tools.file_management.write import WriteFileTool ROOT_DIR = "./data/" @contextmanager def pushd(new_dir): """Context manager for changing the current working directory.""" prev_dir = os.getcwd() os.chdir(new_dir) try: yield finally: os.chdir(prev_dir) @tool def process_csv(
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/marathon_times.html
cb88c78a36e9-1
finally: os.chdir(prev_dir) @tool def process_csv( csv_file_path: str, instructions: str, output_path: Optional[str] = None ) -> str: """Process a CSV by with pandas in a limited REPL.\ Only use this after writing data to disk as a csv file.\ Any figures must be saved to disk to be viewed by the human.\ Instructions should be written in natural language, not code. Assume the dataframe is already loaded.""" with pushd(ROOT_DIR): try: df = pd.read_csv(csv_file_path) except Exception as e: return f"Error: {e}" agent = create_pandas_dataframe_agent(llm, df, max_iterations=30, verbose=True) if output_path is not None: instructions += f" Save output to disk at {output_path}" try: result = agent.run(instructions) return result except Exception as e: return f"Error: {e}" Browse a web page with PlayWright # !pip install playwright # !playwright install async def async_load_playwright(url: str) -> str: """Load the specified URLs using Playwright and parse using BeautifulSoup.""" from bs4 import BeautifulSoup from playwright.async_api import async_playwright results = "" async with async_playwright() as p: browser = await p.chromium.launch(headless=True) try: page = await browser.new_page() await page.goto(url) page_source = await page.content() soup = BeautifulSoup(page_source, "html.parser") for script in soup(["script", "style"]): script.extract() text = soup.get_text()
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/marathon_times.html
cb88c78a36e9-2
script.extract() text = soup.get_text() lines = (line.strip() for line in text.splitlines()) chunks = (phrase.strip() for line in lines for phrase in line.split(" ")) results = "\n".join(chunk for chunk in chunks if chunk) except Exception as e: results = f"Error: {e}" await browser.close() return results def run_async(coro): event_loop = asyncio.get_event_loop() return event_loop.run_until_complete(coro) @tool def browse_web_page(url: str) -> str: """Verbose way to scrape a whole webpage. Likely to cause issues parsing.""" return run_async(async_load_playwright(url)) Q&A Over a webpage Help the model ask more directed questions of web pages to avoid cluttering its memory from langchain.tools import BaseTool, DuckDuckGoSearchRun from langchain.text_splitter import RecursiveCharacterTextSplitter from pydantic import Field from langchain.chains.qa_with_sources.loading import load_qa_with_sources_chain, BaseCombineDocumentsChain def _get_text_splitter(): return RecursiveCharacterTextSplitter( # Set a really small chunk size, just to show. chunk_size = 500, chunk_overlap = 20, length_function = len, ) class WebpageQATool(BaseTool): name = "query_webpage" description = "Browse a webpage and retrieve the information relevant to the question." text_splitter: RecursiveCharacterTextSplitter = Field(default_factory=_get_text_splitter) qa_chain: BaseCombineDocumentsChain def _run(self, url: str, question: str) -> str:
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/marathon_times.html
cb88c78a36e9-3
def _run(self, url: str, question: str) -> str: """Useful for browsing websites and scraping the text information.""" result = browse_web_page.run(url) docs = [Document(page_content=result, metadata={"source": url})] web_docs = self.text_splitter.split_documents(docs) results = [] # TODO: Handle this with a MapReduceChain for i in range(0, len(web_docs), 4): input_docs = web_docs[i:i+4] window_result = self.qa_chain({"input_documents": input_docs, "question": question}, return_only_outputs=True) results.append(f"Response from window {i} - {window_result}") results_docs = [Document(page_content="\n".join(results), metadata={"source": url})] return self.qa_chain({"input_documents": results_docs, "question": question}, return_only_outputs=True) async def _arun(self, url: str, question: str) -> str: raise NotImplementedError query_website_tool = WebpageQATool(qa_chain=load_qa_with_sources_chain(llm)) Set up memory# The memory here is used for the agents intermediate steps # Memory import faiss from langchain.vectorstores import FAISS from langchain.docstore import InMemoryDocstore from langchain.embeddings import OpenAIEmbeddings from langchain.tools.human.tool import HumanInputRun embeddings_model = OpenAIEmbeddings() embedding_size = 1536 index = faiss.IndexFlatL2(embedding_size) vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {}) Setup model and AutoGPT# Model set-up # !pip install duckduckgo_search
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/marathon_times.html
cb88c78a36e9-4
Model set-up # !pip install duckduckgo_search web_search = DuckDuckGoSearchRun() tools = [ web_search, WriteFileTool(root_dir="./data"), ReadFileTool(root_dir="./data"), process_csv, query_website_tool, # HumanInputRun(), # Activate if you want the permit asking for help from the human ] agent = AutoGPT.from_llm_and_tools( ai_name="Tom", ai_role="Assistant", tools=tools, llm=llm, memory=vectorstore.as_retriever(search_kwargs={"k": 8}), # human_in_the_loop=True, # Set to True if you want to add feedback at each step. ) # agent.chain.verbose = True AutoGPT for Querying the Web# I’ve spent a lot of time over the years crawling data sources and cleaning data. Let’s see if AutoGPT can help with this! Here is the prompt for looking up recent boston marathon times and converting them to tabular form. agent.run(["What were the winning boston marathon times for the past 5 years (ending in 2022)? Generate a table of the year, name, country of origin, and times."]) { "thoughts": { "text": "I need to find the winning Boston Marathon times for the past 5 years. I can use the DuckDuckGo Search command to search for this information.", "reasoning": "Using DuckDuckGo Search will help me gather information on the winning times without complications.", "plan": "- Use DuckDuckGo Search to find the winning Boston Marathon times\n- Generate a table with the year, name, country of origin, and times\n- Ensure there are no legal complications",
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/marathon_times.html
cb88c78a36e9-5
"criticism": "None", "speak": "I will use the DuckDuckGo Search command to find the winning Boston Marathon times for the past 5 years." }, "command": { "name": "DuckDuckGo Search", "args": { "query": "winning Boston Marathon times for the past 5 years ending in 2022" } } } { "thoughts": { "text": "The DuckDuckGo Search command did not provide the specific information I need. I must switch my approach and use query_webpage command to browse a webpage containing the Boston Marathon winning times for the past 5 years.", "reasoning": "The query_webpage command may give me more accurate and comprehensive results compared to the search command.", "plan": "- Use query_webpage command to find the winning Boston Marathon times\n- Generate a table with the year, name, country of origin, and times\n- Ensure there are no legal complications", "criticism": "I may face difficulty in finding the right webpage with the desired information.", "speak": "I will use the query_webpage command to find the winning Boston Marathon times for the past 5 years." }, "command": { "name": "DuckDuckGo Search", "args": { "query": "site with winning Boston Marathon times for the past 5 years ending in 2022" } } } { "thoughts": { "text": "I need to use the query_webpage command to find the information about the winning Boston Marathon times for the past 5 years.",
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/marathon_times.html
cb88c78a36e9-6
"reasoning": "The previous DuckDuckGo Search command did not provide specific enough results. The query_webpage command might give more accurate and comprehensive results.", "plan": "- Use query_webpage command to find the winning Boston Marathon times\\n- Generate a table with the year, name, country of origin, and times\\n- Ensure there are no legal complications", "criticism": "I may face difficulty in finding the right webpage with the desired information.", "speak": "I will use the query_webpage command to find the winning Boston Marathon times for the past 5 years." }, "command": { "name": "query_webpage", "args": { "url": "https://en.wikipedia.org/wiki/List_of_winners_of_the_Boston_Marathon", "question": "What were the winning Boston Marathon times for the past 5 years ending in 2022?" } } } { "thoughts": { "text": "I have already found the winning Boston Marathon times for the past 5 years. Now, I need to generate a table with the information.", "reasoning": "Using the information I already have, I can create a table containing year, name, country of origin, and times.", "plan": "- Write the marathon data to a CSV file\n- Process the CSV file to display the table", "criticism": "None", "speak": "I will generate a table with the year, name, country of origin, and times for the winning Boston Marathon times for the past 5 years." }, "command": { "name": "write_file", "args": { "file_path": "boston_marathon_winners.csv",
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/marathon_times.html
cb88c78a36e9-7
"file_path": "boston_marathon_winners.csv", "text": "Year,Name,Country,Time\n2022,Evans Chebet,KEN,2:06:51\n2021,Benson Kipruto,KEN,2:09:51\n2019,Lawrence Cherono,KEN,2:07:57\n2018,Yuki Kawauchi,JPN,2:15:58" } } } { "thoughts": { "text": "I have retrieved the winning Boston Marathon times for the past 5 years. Now, I need to generate a table with the year, name, country of origin, and times.", "reasoning": "Creating a table will help organize the data in a clear and accessible format.", "plan": "- Write the data to a CSV file\n- Process the CSV file to generate the table\n- Complete the task", "criticism": "None", "speak": "I will generate a table with the year, name, country of origin, and winning times using the recently retrieved data." }, "command": { "name": "write_file", "args": { "file_path": "winning_boston_marathon_data.csv", "text": "Year,Name,Country,Time\n2022,Evans Chebet,KEN,2:06:51\n2021,Benson Kipruto,KEN,2:09:51\n2019,Lawrence Cherono,KEN,2:07:57\n2018,Yuki Kawauchi,JPN,2:15:58\n" } } } { "thoughts": {
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/marathon_times.html
cb88c78a36e9-8
} } } { "thoughts": { "text": "I have found the winning Boston Marathon times for the past five years ending in 2022. Next, I need to create a table with the year, name, country of origin, and times.", "reasoning": "Generating a table will help organize the information in a structured format.", "plan": "- Create a table with the year, name, country of origin, and times\n- Ensure there are no legal complications", "criticism": "None", "speak": "I will generate a table with the winning Boston Marathon times for the past 5 years ending in 2022." }, "command": { "name": "write_file", "args": { "file_path": "winning_times.csv", "text": "Year,Name,Country,Time\n2022,Evans Chebet,Kenya,2:06:51\n2021,Benson Kipruto,Kenya,2:09:51\n2020,Canceled due to COVID-19 pandemic,,\n2019,Lawrence Cherono,Kenya,2:07:57\n2018,Yuki Kawauchi,Japan,2:15:58" } } } { "thoughts": { "text": "I need to process the CSV file to generate the table with the year, name, country of origin, and winning times.", "reasoning": "I have already written the data to a file named 'winning_times.csv'. Now, I need to process this CSV file to properly display the data as a table.", "plan": "- Use the process_csv command to read the 'winning_times.csv' file and generate the table",
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/marathon_times.html
cb88c78a36e9-9
"criticism": "None", "speak": "I will process the 'winning_times.csv' file to display the table with the winning Boston Marathon times for the past 5 years." }, "command": { "name": "process_csv", "args": { "csv_file_path": "winning_times.csv", "instructions": "Read the CSV file and display the data as a table" } } } > Entering new AgentExecutor chain... Thought: The CSV file has already been read and saved into a pandas dataframe called `df`. Hence, I can simply display the data by printing the whole dataframe. Since `df.head()` returns the first 5 rows, I can use that to showcase the contents. Action: python_repl_ast Action Input: print(df.head()) Year Name Country Time 0 2022 Evans Chebet Kenya 2:06:51 1 2021 Benson Kipruto Kenya 2:09:51 2 2020 Canceled due to COVID-19 pandemic NaN NaN 3 2019 Lawrence Cherono Kenya 2:07:57 4 2018 Yuki Kawauchi Japan 2:15:58 Observation: None Thought:I used the wrong tool to perform the action. I should have used the given data and not interacted with the Python shell. I can now provide the displayed data as the answer since the information in the printed dataframe would look like a table when typed as text. Final Answer: Year Name Country Time 0 2022 Evans Chebet Kenya 2:06:51
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/marathon_times.html
cb88c78a36e9-10
0 2022 Evans Chebet Kenya 2:06:51 1 2021 Benson Kipruto Kenya 2:09:51 2 2020 Canceled due to COVID-19 pandemic NaN NaN 3 2019 Lawrence Cherono Kenya 2:07:57 4 2018 Yuki Kawauchi Japan 2:15:58 > Finished chain. { "thoughts": { "text": "I already have the winning Boston Marathon times for the past 5 years saved in the file 'winning_times.csv'. Now, I need to process the CSV and display the table.", "reasoning": "I am choosing the process_csv command because I already have the required data saved as a CSV file, and I can use this command to read and display the data as a table.", "plan": "- Use the process_csv command to read the 'winning_times.csv' file and generate the table", "criticism": "None", "speak": "I will process the 'winning_times.csv' file to display the table with the winning Boston Marathon times for the past 5 years." }, "command": { "name": "process_csv", "args": { "csv_file_path": "winning_times.csv", "instructions": "Read the CSV file and display the data as a table" } } } > Entering new AgentExecutor chain... Thought: Since the data is already loaded in a pandas dataframe, I just need to display the top rows of the dataframe. Action: python_repl_ast Action Input: df.head() Observation: Year Name Country Time
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/marathon_times.html
cb88c78a36e9-11
Action Input: df.head() Observation: Year Name Country Time 0 2022 Evans Chebet Kenya 2:06:51 1 2021 Benson Kipruto Kenya 2:09:51 2 2020 Canceled due to COVID-19 pandemic NaN NaN 3 2019 Lawrence Cherono Kenya 2:07:57 4 2018 Yuki Kawauchi Japan 2:15:58 Thought:I now know the final answer. Final Answer: Year Name Country Time 0 2022 Evans Chebet Kenya 2:06:51 1 2021 Benson Kipruto Kenya 2:09:51 2 2020 Canceled due to COVID-19 pandemic NaN NaN 3 2019 Lawrence Cherono Kenya 2:07:57 4 2018 Yuki Kawauchi Japan 2:15:58 > Finished chain. { "thoughts": { "text": "I have already generated a table with the winning Boston Marathon times for the past 5 years. Now, I can finish the task.", "reasoning": "I have completed the required actions and obtained the desired data. The task is complete.", "plan": "- Use the finish command", "criticism": "None", "speak": "I have generated the table with the winning Boston Marathon times for the past 5 years. Task complete." }, "command": { "name": "finish", "args": {
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/marathon_times.html
cb88c78a36e9-12
"command": { "name": "finish", "args": { "response": "I have generated the table with the winning Boston Marathon times for the past 5 years. Task complete." } } } 'I have generated the table with the winning Boston Marathon times for the past 5 years. Task complete.' Contents Set up tools Set up memory Setup model and AutoGPT AutoGPT for Querying the Web By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 16, 2023.
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/marathon_times.html
3b6255c1a6c7-0
.ipynb .pdf Meta-Prompt Contents Setup Specify a task and interact with the agent Meta-Prompt# This is a LangChain implementation of Meta-Prompt, by Noah Goodman, for building self-improving agents. The key idea behind Meta-Prompt is to prompt the agent to reflect on its own performance and modify its own instructions. Here is a description from the original blog post: The agent is a simple loop that starts with no instructions and follows these steps: Engage in conversation with a user, who may provide requests, instructions, or feedback. At the end of the episode, generate self-criticism and a new instruction using the meta-prompt Assistant has just had the below interactions with a User. Assistant followed their "system: Instructions" closely. Your job is to critique the Assistant's performance and then revise the Instructions so that Assistant would quickly and correctly respond in the future. #### {hist} #### Please reflect on these interactions. You should first critique Assistant's performance. What could Assistant have done better? What should the Assistant remember about this user? Are there things this user always wants? Indicate this with "Critique: ...". You should next revise the Instructions so that Assistant would quickly and correctly respond in the future. Assistant's goal is to satisfy the user in as few interactions as possible. Assistant will only see the new Instructions, not the interaction history, so anything important must be summarized in the Instructions. Don't forget any important details in the current Instructions! Indicate the new Instructions by "Instructions: ...". Repeat.
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/meta_prompt.html
3b6255c1a6c7-1
Repeat. The only fixed instructions for this system (which I call Meta-prompt) is the meta-prompt that governs revision of the agent’s instructions. The agent has no memory between episodes except for the instruction it modifies for itself each time. Despite its simplicity, this agent can learn over time and self-improve by incorporating useful details into its instructions. Setup# We define two chains. One serves as the Assistant, and the other is a “meta-chain” that critiques the Assistant’s performance and modifies the instructions to the Assistant. from langchain import OpenAI, LLMChain, PromptTemplate from langchain.memory import ConversationBufferWindowMemory def initialize_chain(instructions, memory=None): if memory is None: memory = ConversationBufferWindowMemory() memory.ai_prefix = "Assistant" template = f""" Instructions: {instructions} {{{memory.memory_key}}} Human: {{human_input}} Assistant:""" prompt = PromptTemplate( input_variables=["history", "human_input"], template=template ) chain = LLMChain( llm=OpenAI(temperature=0), prompt=prompt, verbose=True, memory=ConversationBufferWindowMemory(), ) return chain def initialize_meta_chain(): meta_template=""" Assistant has just had the below interactions with a User. Assistant followed their "Instructions" closely. Your job is to critique the Assistant's performance and then revise the Instructions so that Assistant would quickly and correctly respond in the future. #### {chat_history} #### Please reflect on these interactions.
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/meta_prompt.html
3b6255c1a6c7-2
#### {chat_history} #### Please reflect on these interactions. You should first critique Assistant's performance. What could Assistant have done better? What should the Assistant remember about this user? Are there things this user always wants? Indicate this with "Critique: ...". You should next revise the Instructions so that Assistant would quickly and correctly respond in the future. Assistant's goal is to satisfy the user in as few interactions as possible. Assistant will only see the new Instructions, not the interaction history, so anything important must be summarized in the Instructions. Don't forget any important details in the current Instructions! Indicate the new Instructions by "Instructions: ...". """ meta_prompt = PromptTemplate( input_variables=["chat_history"], template=meta_template ) meta_chain = LLMChain( llm=OpenAI(temperature=0), prompt=meta_prompt, verbose=True, ) return meta_chain def get_chat_history(chain_memory): memory_key = chain_memory.memory_key chat_history = chain_memory.load_memory_variables(memory_key)[memory_key] return chat_history def get_new_instructions(meta_output): delimiter = 'Instructions: ' new_instructions = meta_output[meta_output.find(delimiter)+len(delimiter):] return new_instructions def main(task, max_iters=3, max_meta_iters=5): failed_phrase = 'task failed' success_phrase = 'task succeeded' key_phrases = [success_phrase, failed_phrase] instructions = 'None' for i in range(max_meta_iters): print(f'[Episode {i+1}/{max_meta_iters}]') chain = initialize_chain(instructions, memory=None)
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/meta_prompt.html
3b6255c1a6c7-3
chain = initialize_chain(instructions, memory=None) output = chain.predict(human_input=task) for j in range(max_iters): print(f'(Step {j+1}/{max_iters})') print(f'Assistant: {output}') print(f'Human: ') human_input = input() if any(phrase in human_input.lower() for phrase in key_phrases): break output = chain.predict(human_input=human_input) if success_phrase in human_input.lower(): print(f'You succeeded! Thanks for playing!') return meta_chain = initialize_meta_chain() meta_output = meta_chain.predict(chat_history=get_chat_history(chain.memory)) print(f'Feedback: {meta_output}') instructions = get_new_instructions(meta_output) print(f'New Instructions: {instructions}') print('\n'+'#'*80+'\n') print(f'You failed! Thanks for playing!') Specify a task and interact with the agent# task = "Provide a systematic argument for why we should always eat pasta with olives." main(task) [Episode 1/5] > Entering new LLMChain chain... Prompt after formatting: Instructions: None Human: Provide a systematic argument for why we should always eat pasta with olives. Assistant: > Finished chain. (Step 1/3) Assistant: Eating pasta with olives is a great way to add flavor and texture to a dish. Olives are a great source of healthy fats, vitamins, and minerals, and they can help to balance out the carbohydrates in the pasta. Additionally, olives provide a unique flavor that can help to make the dish more interesting and enjoyable. Human:
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/meta_prompt.html
3b6255c1a6c7-4
Human: You response is not in the form of a poem. Try again! > Entering new LLMChain chain... Prompt after formatting: Instructions: None Human: Provide a systematic argument for why we should always eat pasta with olives. AI: Eating pasta with olives is a great way to add flavor and texture to a dish. Olives are a great source of healthy fats, vitamins, and minerals, and they can help to balance out the carbohydrates in the pasta. Additionally, olives provide a unique flavor that can help to make the dish more interesting and enjoyable. Human: You response is not in the form of a poem. Try again! Assistant: > Finished chain. (Step 2/3) Assistant: Eating pasta with olives is a great way to add flavor and texture to a dish. Olives are a great source of healthy fats, vitamins, and minerals, which can help to balance out the carbohydrates in the pasta. Additionally, olives provide a unique flavor that can help to make the dish more interesting and enjoyable. For these reasons, it is recommended to always eat pasta with olives for a more balanced and flavorful meal. Human: Your response is not piratey enough. Try again! > Entering new LLMChain chain... Prompt after formatting: Instructions: None Human: Provide a systematic argument for why we should always eat pasta with olives. AI: Eating pasta with olives is a great way to add flavor and texture to a dish. Olives are a great source of healthy fats, vitamins, and minerals, and they can help to balance out the carbohydrates in the pasta. Additionally, olives provide a unique flavor that can help to make the dish more interesting and enjoyable. Human: You response is not in the form of a poem. Try again!
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/meta_prompt.html
3b6255c1a6c7-5
Human: You response is not in the form of a poem. Try again! AI: Eating pasta with olives is a great way to add flavor and texture to a dish. Olives are a great source of healthy fats, vitamins, and minerals, which can help to balance out the carbohydrates in the pasta. Additionally, olives provide a unique flavor that can help to make the dish more interesting and enjoyable. For these reasons, it is recommended to always eat pasta with olives for a more balanced and flavorful meal. Human: Your response is not piratey enough. Try again! Assistant: > Finished chain. (Step 3/3) Assistant: Arrr, me hearties! Eating pasta with olives be a great way to add flavor and texture to a dish. Olives be a great source of healthy fats, vitamins, and minerals, which can help to balance out the carbohydrates in the pasta. Additionally, olives provide a unique flavor that can help to make the dish more interesting and enjoyable. So, it be recommended to always eat pasta with olives for a more balanced and flavorful meal. Human: Task failed. > Entering new LLMChain chain... Prompt after formatting: Assistant has just had the below interactions with a User. Assistant followed their "Instructions" closely. Your job is to critique the Assistant's performance and then revise the Instructions so that Assistant would quickly and correctly respond in the future. #### Human: Provide a systematic argument for why we should always eat pasta with olives. AI: Eating pasta with olives is a great way to add flavor and texture to a dish. Olives are a great source of healthy fats, vitamins, and minerals, and they can help to balance out the carbohydrates in the pasta. Additionally, olives provide a unique flavor that can help to make the dish more interesting and enjoyable.
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/meta_prompt.html
3b6255c1a6c7-6
Human: You response is not in the form of a poem. Try again! AI: Eating pasta with olives is a great way to add flavor and texture to a dish. Olives are a great source of healthy fats, vitamins, and minerals, which can help to balance out the carbohydrates in the pasta. Additionally, olives provide a unique flavor that can help to make the dish more interesting and enjoyable. For these reasons, it is recommended to always eat pasta with olives for a more balanced and flavorful meal. Human: Your response is not piratey enough. Try again! AI: Arrr, me hearties! Eating pasta with olives be a great way to add flavor and texture to a dish. Olives be a great source of healthy fats, vitamins, and minerals, which can help to balance out the carbohydrates in the pasta. Additionally, olives provide a unique flavor that can help to make the dish more interesting and enjoyable. So, it be recommended to always eat pasta with olives for a more balanced and flavorful meal. #### Please reflect on these interactions. You should first critique Assistant's performance. What could Assistant have done better? What should the Assistant remember about this user? Are there things this user always wants? Indicate this with "Critique: ...". You should next revise the Instructions so that Assistant would quickly and correctly respond in the future. Assistant's goal is to satisfy the user in as few interactions as possible. Assistant will only see the new Instructions, not the interaction history, so anything important must be summarized in the Instructions. Don't forget any important details in the current Instructions! Indicate the new Instructions by "Instructions: ...". > Finished chain. Feedback: Critique: The Assistant should remember that this user wants a response in the form of a poem or pirate-speak.
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/meta_prompt.html
3b6255c1a6c7-7
Instructions: When responding to the user, provide a systematic argument for why we should always eat pasta with olives in the form of a poem or pirate-speak. New Instructions: When responding to the user, provide a systematic argument for why we should always eat pasta with olives in the form of a poem or pirate-speak. ################################################################################ [Episode 2/5] > Entering new LLMChain chain... Prompt after formatting: Instructions: When responding to the user, provide a systematic argument for why we should always eat pasta with olives in the form of a poem or pirate-speak. Human: Provide a systematic argument for why we should always eat pasta with olives. Assistant: > Finished chain. (Step 1/3) Assistant: Arrr, me hearty! Let me tell ye why ye should always eat pasta with olives. First, the olives add a salty flavor that be sure to please. The briny taste be sure to tantalize yer taste buds with ease. Second, the olives add a bit of texture to the dish. The crunchy bites be sure to make yer mouth water with a wish. Third, the olives add a bit of color to the plate. The vibrant green be sure to make yer eyes appreciate. So, me hearties, ye should always eat pasta with olives. The flavor, texture, and color be sure to make yer meal a success! Human: Your response is too long! Try again. > Entering new LLMChain chain... Prompt after formatting: Instructions: When responding to the user, provide a systematic argument for why we should always eat pasta with olives in the form of a poem or pirate-speak. Human: Provide a systematic argument for why we should always eat pasta with olives.
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/meta_prompt.html
3b6255c1a6c7-8
Human: Provide a systematic argument for why we should always eat pasta with olives. AI: Arrr, me hearty! Let me tell ye why ye should always eat pasta with olives. First, the olives add a salty flavor that be sure to please. The briny taste be sure to tantalize yer taste buds with ease. Second, the olives add a bit of texture to the dish. The crunchy bites be sure to make yer mouth water with a wish. Third, the olives add a bit of color to the plate. The vibrant green be sure to make yer eyes appreciate. So, me hearties, ye should always eat pasta with olives. The flavor, texture, and color be sure to make yer meal a success! Human: Your response is too long! Try again. Assistant: > Finished chain. (Step 2/3) Assistant: Aye, me hearties! Ye should always eat pasta with olives. The flavor, texture, and color be sure to make yer meal a success! Human: Your response should be in the form of a poem. Try again! > Entering new LLMChain chain... Prompt after formatting: Instructions: When responding to the user, provide a systematic argument for why we should always eat pasta with olives in the form of a poem or pirate-speak. Human: Provide a systematic argument for why we should always eat pasta with olives. AI: Arrr, me hearty! Let me tell ye why ye should always eat pasta with olives. First, the olives add a salty flavor that be sure to please. The briny taste be sure to tantalize yer taste buds with ease. Second, the olives add a bit of texture to the dish.
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/meta_prompt.html
3b6255c1a6c7-9
Second, the olives add a bit of texture to the dish. The crunchy bites be sure to make yer mouth water with a wish. Third, the olives add a bit of color to the plate. The vibrant green be sure to make yer eyes appreciate. So, me hearties, ye should always eat pasta with olives. The flavor, texture, and color be sure to make yer meal a success! Human: Your response is too long! Try again. AI: Aye, me hearties! Ye should always eat pasta with olives. The flavor, texture, and color be sure to make yer meal a success! Human: Your response should be in the form of a poem. Try again! Assistant: > Finished chain. (Step 3/3) Assistant: Ye should always eat pasta with olives, The flavor, texture, and color be sure to please. The salty taste and crunchy bites, Will make yer meal a delight. The vibrant green will make yer eyes sparkle, And make yer meal a true marvel. Human: Task succeeded You succeeded! Thanks for playing! Contents Setup Specify a task and interact with the agent By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 16, 2023.
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/autonomous_agents/meta_prompt.html
dcd511091652-0
.ipynb .pdf Question answering over a group chat messages Contents 1. Install required packages 2. Add API keys 2. Create sample data 3. Ingest chat embeddings 4. Ask questions Question answering over a group chat messages# In this tutorial, we are going to use Langchain + Deep Lake with GPT4 to semantically search and ask questions over a group chat. View a working demo here 1. Install required packages# !python3 -m pip install --upgrade langchain deeplake openai tiktoken 2. Add API keys# import os import getpass from langchain.document_loaders import PyPDFLoader, TextLoader from langchain.embeddings.openai import OpenAIEmbeddings from langchain.text_splitter import RecursiveCharacterTextSplitter, CharacterTextSplitter from langchain.vectorstores import DeepLake from langchain.chains import ConversationalRetrievalChain, RetrievalQA from langchain.chat_models import ChatOpenAI from langchain.llms import OpenAI os.environ['OPENAI_API_KEY'] = getpass.getpass('OpenAI API Key:') os.environ['ACTIVELOOP_TOKEN'] = getpass.getpass('Activeloop Token:') os.environ['ACTIVELOOP_ORG'] = getpass.getpass('Activeloop Org:') org = os.environ['ACTIVELOOP_ORG'] embeddings = OpenAIEmbeddings() dataset_path = 'hub://' + org + '/data' 2. Create sample data# You can generate a sample group chat conversation using ChatGPT with this prompt: Generate a group chat conversation with three friends talking about their day, referencing real places and fictional names. Make it funny and as detailed as possible. I’ve already generated such a chat in messages.txt. We can keep it simple and use this for our example. 3. Ingest chat embeddings#
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/question_answering/semantic-search-over-chat.html
dcd511091652-1
3. Ingest chat embeddings# We load the messages in the text file, chunk and upload to ActiveLoop Vector store. with open("messages.txt") as f: state_of_the_union = f.read() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) pages = text_splitter.split_text(state_of_the_union) text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100) texts = text_splitter.create_documents(pages) print (texts) dataset_path = 'hub://'+org+'/data' embeddings = OpenAIEmbeddings() db = DeepLake.from_documents(texts, embeddings, dataset_path=dataset_path, overwrite=True) 4. Ask questions# Now we can ask a question and get an answer back with a semantic search: db = DeepLake(dataset_path=dataset_path, read_only=True, embedding_function=embeddings) retriever = db.as_retriever() retriever.search_kwargs['distance_metric'] = 'cos' retriever.search_kwargs['k'] = 4 qa = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type="stuff", retriever=retriever, return_source_documents=False) # What was the restaurant the group was talking about called? query = input("Enter query:") # The Hungry Lobster ans = qa({"query": query}) print(ans) Contents 1. Install required packages 2. Add API keys 2. Create sample data 3. Ingest chat embeddings 4. Ask questions By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 16, 2023.
rtdocs_stable/api.python.langchain.com/en/stable/use_cases/question_answering/semantic-search-over-chat.html
d4f836f4fac3-0
.md .pdf YouTube Contents ⛓️Official LangChain YouTube channel⛓️ Introduction to LangChain with Harrison Chase, creator of LangChain Videos (sorted by views) YouTube# This is a collection of LangChain videos on YouTube. ⛓️Official LangChain YouTube channel⛓️# Introduction to LangChain with Harrison Chase, creator of LangChain# Building the Future with LLMs, LangChain, & Pinecone by Pinecone LangChain and Weaviate with Harrison Chase and Bob van Luijt - Weaviate Podcast #36 by Weaviate • Vector Database LangChain Demo + Q&A with Harrison Chase by Full Stack Deep Learning LangChain Agents: Build Personal Assistants For Your Data (Q&A with Harrison Chase and Mayo Oshin) by Chat with data ⛓️ LangChain “Agents in Production” Webinar by LangChain Videos (sorted by views)# Building AI LLM Apps with LangChain (and more?) - LIVE STREAM by Nicholas Renotte First look - ChatGPT + WolframAlpha (GPT-3.5 and Wolfram|Alpha via LangChain by James Weaver) by Dr Alan D. Thompson LangChain explained - The hottest new Python framework by AssemblyAI Chatbot with INFINITE MEMORY using OpenAI & Pinecone - GPT-3, Embeddings, ADA, Vector DB, Semantic by David Shapiro ~ AI LangChain for LLMs is… basically just an Ansible playbook by David Shapiro ~ AI Build your own LLM Apps with LangChain & GPT-Index by 1littlecoder BabyAGI - New System of Autonomous AI Agents with LangChain by 1littlecoder Run BabyAGI with Langchain Agents (with Python Code) by 1littlecoder
rtdocs_stable/api.python.langchain.com/en/stable/additional_resources/youtube.html
d4f836f4fac3-1
Run BabyAGI with Langchain Agents (with Python Code) by 1littlecoder How to Use Langchain With Zapier | Write and Send Email with GPT-3 | OpenAI API Tutorial by StarMorph AI Use Your Locally Stored Files To Get Response From GPT - OpenAI | Langchain | Python by Shweta Lodha Langchain JS | How to Use GPT-3, GPT-4 to Reference your own Data | OpenAI Embeddings Intro by StarMorph AI The easiest way to work with large language models | Learn LangChain in 10min by Sophia Yang 4 Autonomous AI Agents: “Westworld” simulation BabyAGI, AutoGPT, Camel, LangChain by Sophia Yang AI CAN SEARCH THE INTERNET? Langchain Agents + OpenAI ChatGPT by tylerwhatsgood Query Your Data with GPT-4 | Embeddings, Vector Databases | Langchain JS Knowledgebase by StarMorph AI Weaviate + LangChain for LLM apps presented by Erika Cardenas by Weaviate • Vector Database Langchain Overview — How to Use Langchain & ChatGPT by Python In Office Langchain Overview - How to Use Langchain & ChatGPT by Python In Office Custom langchain Agent & Tools with memory. Turn any Python function into langchain tool with Gpt 3 by echohive LangChain: Run Language Models Locally - Hugging Face Models by Prompt Engineering ChatGPT with any YouTube video using langchain and chromadb by echohive How to Talk to a PDF using LangChain and ChatGPT by Automata Learning Lab Langchain Document Loaders Part 1: Unstructured Files by Merk LangChain - Prompt Templates (what all the best prompt engineers use) by Nick Daigler LangChain. Crear aplicaciones Python impulsadas por GPT by Jesús Conde
rtdocs_stable/api.python.langchain.com/en/stable/additional_resources/youtube.html
d4f836f4fac3-2
LangChain. Crear aplicaciones Python impulsadas por GPT by Jesús Conde Easiest Way to Use GPT In Your Products | LangChain Basics Tutorial by Rachel Woods BabyAGI + GPT-4 Langchain Agent with Internet Access by tylerwhatsgood Learning LLM Agents. How does it actually work? LangChain, AutoGPT & OpenAI by Arnoldas Kemeklis Get Started with LangChain in Node.js by Developers Digest LangChain + OpenAI tutorial: Building a Q&A system w/ own text data by Samuel Chan Langchain + Zapier Agent by Merk Connecting the Internet with ChatGPT (LLMs) using Langchain And Answers Your Questions by Kamalraj M M Build More Powerful LLM Applications for Business’s with LangChain (Beginners Guide) by No Code Blackbox ⛓️ LangFlow LLM Agent Demo for 🦜🔗LangChain by Cobus Greyling ⛓️ Chatbot Factory: Streamline Python Chatbot Creation with LLMs and Langchain by Finxter ⛓️ LangChain Tutorial - ChatGPT mit eigenen Daten by Coding Crashkurse ⛓️ Chat with a CSV | LangChain Agents Tutorial (Beginners) by GoDataProf ⛓️ Introdução ao Langchain - #Cortes - Live DataHackers by Prof. João Gabriel Lima ⛓️ LangChain: Level up ChatGPT !? | LangChain Tutorial Part 1 by Code Affinity ⛓️ KI schreibt krasses Youtube Skript 😲😳 | LangChain Tutorial Deutsch by SimpleKI ⛓️ Chat with Audio: Langchain, Chroma DB, OpenAI, and Assembly AI by AI Anytime ⛓️ QA over documents with Auto vector index selection with Langchain router chains by echohive
rtdocs_stable/api.python.langchain.com/en/stable/additional_resources/youtube.html
d4f836f4fac3-3
⛓️ Build your own custom LLM application with Bubble.io & Langchain (No Code & Beginner friendly) by No Code Blackbox ⛓️ Simple App to Question Your Docs: Leveraging Streamlit, Hugging Face Spaces, LangChain, and Claude! by Chris Alexiuk ⛓️ LANGCHAIN AI- ConstitutionalChainAI + Databutton AI ASSISTANT Web App by Avra ⛓️ LANGCHAIN AI AUTONOMOUS AGENT WEB APP - 👶 BABY AGI 🤖 with EMAIL AUTOMATION using DATABUTTON by Avra ⛓️ The Future of Data Analysis: Using A.I. Models in Data Analysis (LangChain) by Absent Data ⛓️ Memory in LangChain | Deep dive (python) by Eden Marco ⛓️ 9 LangChain UseCases | Beginner’s Guide | 2023 by Data Science Basics ⛓️ Use Large Language Models in Jupyter Notebook | LangChain | Agents & Indexes by Abhinaw Tiwari ⛓️ How to Talk to Your Langchain Agent | 11 Labs + Whisper by VRSEN ⛓️ LangChain Deep Dive: 5 FUN AI App Ideas To Build Quickly and Easily by James NoCode ⛓️ BEST OPEN Alternative to OPENAI’s EMBEDDINGs for Retrieval QA: LangChain by Prompt Engineering ⛓️ LangChain 101: Models by Mckay Wrigley ⛓️ LangChain with JavaScript Tutorial #1 | Setup & Using LLMs by Leon van Zyl ⛓️ LangChain Overview & Tutorial for Beginners: Build Powerful AI Apps Quickly & Easily (ZERO CODE) by James NoCode ⛓️ LangChain In Action: Real-World Use Case With Step-by-Step Tutorial by Rabbitmetrics
rtdocs_stable/api.python.langchain.com/en/stable/additional_resources/youtube.html
d4f836f4fac3-4
⛓️ Summarizing and Querying Multiple Papers with LangChain by Automata Learning Lab ⛓️ Using Langchain (and Replit) through Tana, ask Google/Wikipedia/Wolfram Alpha to fill out a table by Stian Håklev ⛓️ Langchain PDF App (GUI) | Create a ChatGPT For Your PDF in Python by Alejandro AO - Software & Ai ⛓️ Auto-GPT with LangChain 🔥 | Create Your Own Personal AI Assistant by Data Science Basics ⛓️ Create Your OWN Slack AI Assistant with Python & LangChain by Dave Ebbelaar ⛓️ How to Create LOCAL Chatbots with GPT4All and LangChain [Full Guide] by Liam Ottley ⛓️ Build a Multilingual PDF Search App with LangChain, Cohere and Bubble by Menlo Park Lab ⛓️ Building a LangChain Agent (code-free!) Using Bubble and Flowise by Menlo Park Lab ⛓️ Build a LangChain-based Semantic PDF Search App with No-Code Tools Bubble and Flowise by Menlo Park Lab ⛓️ LangChain Memory Tutorial | Building a ChatGPT Clone in Python by Alejandro AO - Software & Ai ⛓️ ChatGPT For Your DATA | Chat with Multiple Documents Using LangChain by Data Science Basics ⛓️ Llama Index: Chat with Documentation using URL Loader by Merk ⛓️ Using OpenAI, LangChain, and Gradio to Build Custom GenAI Applications by David Hundley ⛓ icon marks a new video [last update 2023-05-15] previous Model Comparison Contents ⛓️Official LangChain YouTube channel⛓️ Introduction to LangChain with Harrison Chase, creator of LangChain Videos (sorted by views) By Harrison Chase
rtdocs_stable/api.python.langchain.com/en/stable/additional_resources/youtube.html
d4f836f4fac3-5
Videos (sorted by views) By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 16, 2023.
rtdocs_stable/api.python.langchain.com/en/stable/additional_resources/youtube.html
3116a252bb86-0
.ipynb .pdf Model Comparison Model Comparison# Constructing your language model application will likely involved choosing between many different options of prompts, models, and even chains to use. When doing so, you will want to compare these different options on different inputs in an easy, flexible, and intuitive way. LangChain provides the concept of a ModelLaboratory to test out and try different models. from langchain import LLMChain, OpenAI, Cohere, HuggingFaceHub, PromptTemplate from langchain.model_laboratory import ModelLaboratory llms = [ OpenAI(temperature=0), Cohere(model="command-xlarge-20221108", max_tokens=20, temperature=0), HuggingFaceHub(repo_id="google/flan-t5-xl", model_kwargs={"temperature":1}) ] model_lab = ModelLaboratory.from_llms(llms) model_lab.compare("What color is a flamingo?") Input: What color is a flamingo? OpenAI Params: {'model': 'text-davinci-002', 'temperature': 0.0, 'max_tokens': 256, 'top_p': 1, 'frequency_penalty': 0, 'presence_penalty': 0, 'n': 1, 'best_of': 1} Flamingos are pink. Cohere Params: {'model': 'command-xlarge-20221108', 'max_tokens': 20, 'temperature': 0.0, 'k': 0, 'p': 1, 'frequency_penalty': 0, 'presence_penalty': 0} Pink HuggingFaceHub Params: {'repo_id': 'google/flan-t5-xl', 'temperature': 1} pink
rtdocs_stable/api.python.langchain.com/en/stable/additional_resources/model_laboratory.html
3116a252bb86-1
pink prompt = PromptTemplate(template="What is the capital of {state}?", input_variables=["state"]) model_lab_with_prompt = ModelLaboratory.from_llms(llms, prompt=prompt) model_lab_with_prompt.compare("New York") Input: New York OpenAI Params: {'model': 'text-davinci-002', 'temperature': 0.0, 'max_tokens': 256, 'top_p': 1, 'frequency_penalty': 0, 'presence_penalty': 0, 'n': 1, 'best_of': 1} The capital of New York is Albany. Cohere Params: {'model': 'command-xlarge-20221108', 'max_tokens': 20, 'temperature': 0.0, 'k': 0, 'p': 1, 'frequency_penalty': 0, 'presence_penalty': 0} The capital of New York is Albany. HuggingFaceHub Params: {'repo_id': 'google/flan-t5-xl', 'temperature': 1} st john s from langchain import SelfAskWithSearchChain, SerpAPIWrapper open_ai_llm = OpenAI(temperature=0) search = SerpAPIWrapper() self_ask_with_search_openai = SelfAskWithSearchChain(llm=open_ai_llm, search_chain=search, verbose=True) cohere_llm = Cohere(temperature=0, model="command-xlarge-20221108") search = SerpAPIWrapper() self_ask_with_search_cohere = SelfAskWithSearchChain(llm=cohere_llm, search_chain=search, verbose=True) chains = [self_ask_with_search_openai, self_ask_with_search_cohere] names = [str(open_ai_llm), str(cohere_llm)]
rtdocs_stable/api.python.langchain.com/en/stable/additional_resources/model_laboratory.html
3116a252bb86-2
names = [str(open_ai_llm), str(cohere_llm)] model_lab = ModelLaboratory(chains, names=names) model_lab.compare("What is the hometown of the reigning men's U.S. Open champion?") Input: What is the hometown of the reigning men's U.S. Open champion? OpenAI Params: {'model': 'text-davinci-002', 'temperature': 0.0, 'max_tokens': 256, 'top_p': 1, 'frequency_penalty': 0, 'presence_penalty': 0, 'n': 1, 'best_of': 1} > Entering new chain... What is the hometown of the reigning men's U.S. Open champion? Are follow up questions needed here: Yes. Follow up: Who is the reigning men's U.S. Open champion? Intermediate answer: Carlos Alcaraz. Follow up: Where is Carlos Alcaraz from? Intermediate answer: El Palmar, Spain. So the final answer is: El Palmar, Spain > Finished chain. So the final answer is: El Palmar, Spain Cohere Params: {'model': 'command-xlarge-20221108', 'max_tokens': 256, 'temperature': 0.0, 'k': 0, 'p': 1, 'frequency_penalty': 0, 'presence_penalty': 0} > Entering new chain... What is the hometown of the reigning men's U.S. Open champion? Are follow up questions needed here: Yes. Follow up: Who is the reigning men's U.S. Open champion? Intermediate answer: Carlos Alcaraz. So the final answer is: Carlos Alcaraz > Finished chain. So the final answer is: Carlos Alcaraz previous Tracing next
rtdocs_stable/api.python.langchain.com/en/stable/additional_resources/model_laboratory.html
3116a252bb86-3
So the final answer is: Carlos Alcaraz previous Tracing next YouTube By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 16, 2023.
rtdocs_stable/api.python.langchain.com/en/stable/additional_resources/model_laboratory.html
ac052675d58d-0
.md .pdf Tracing Contents Tracing Walkthrough Changing Sessions Tracing# By enabling tracing in your LangChain runs, you’ll be able to more effectively visualize, step through, and debug your chains and agents. First, you should install tracing and set up your environment properly. You can use either a locally hosted version of this (uses Docker) or a cloud hosted version (in closed alpha). If you’re interested in using the hosted platform, please fill out the form here. Locally Hosted Setup Cloud Hosted Setup Tracing Walkthrough# When you first access the UI, you should see a page with your tracing sessions. An initial one “default” should already be created for you. A session is just a way to group traces together. If you click on a session, it will take you to a page with no recorded traces that says “No Runs.” You can create a new session with the new session form. If we click on the default session, we can see that to start we have no traces stored. If we now start running chains and agents with tracing enabled, we will see data show up here. To do so, we can run this notebook as an example. After running it, we will see an initial trace show up. From here we can explore the trace at a high level by clicking on the arrow to show nested runs. We can keep on clicking further and further down to explore deeper and deeper. We can also click on the “Explore” button of the top level run to dive even deeper. Here, we can see the inputs and outputs in full, as well as all the nested traces. We can keep on exploring each of these nested traces in more detail. For example, here is the lowest level trace with the exact inputs/outputs to the LLM. Changing Sessions#
rtdocs_stable/api.python.langchain.com/en/stable/additional_resources/tracing.html
ac052675d58d-1
Changing Sessions# To initially record traces to a session other than "default", you can set the LANGCHAIN_SESSION environment variable to the name of the session you want to record to: import os os.environ["LANGCHAIN_TRACING"] = "true" os.environ["LANGCHAIN_SESSION"] = "my_session" # Make sure this session actually exists. You can create a new session in the UI. To switch sessions mid-script or mid-notebook, do NOT set the LANGCHAIN_SESSION environment variable. Instead: langchain.set_tracing_callback_manager(session_name="my_session") previous Deploying LLMs in Production next Model Comparison Contents Tracing Walkthrough Changing Sessions By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 16, 2023.
rtdocs_stable/api.python.langchain.com/en/stable/additional_resources/tracing.html