Tutorial 4
from pydantic import BaseModel, Field
from typing import List
from typing import Optional
from langchain.utils.openai_functions import convert_pydantic_to_openai_function
from langchain.prompts import ChatPromptTemplate
from langchain.chat_models import ChatOpenAI
from langchain.output_parsers.openai_functions import JsonOutputFunctionsParser
from langchain.output_parsers.openai_functions import JsonKeyOutputFunctionsParser
from langchain.document_loaders import WebBaseLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.schema.runnable import RunnableLambda{‘name’: ‘Tagging’, ‘description’: ‘Tag the piece of text with particular info.’, ‘parameters’: {‘title’: ‘Tagging’, ‘description’: ‘Tag the piece of text with particular info.’, ‘type’: ‘object’, ‘properties’: {‘sentiment’: {‘title’: ‘Sentiment’, ‘description’: ‘sentiment of text, should be pos, neg, or neutral’, ‘type’: ‘string’}, ‘language’: {‘title’: ‘Language’, ‘description’: ‘language of text (should be ISO 639-1 code)’, ‘type’: ‘string’}}, ‘required’: [‘sentiment’, ‘language’]}}
We force the model to use the tagging functions
JsonOutputFunctionsParser()Extraction is similar to tagging, but used for extracting multiple pieces of information.
{‘name’: ‘Information’, ‘description’: ‘Information to extract.’, ‘parameters’: {‘title’: ‘Information’, ‘description’: ‘Information to extract.’, ‘type’: ‘object’, ‘properties’: {‘people’: {‘title’: ‘People’, ‘description’: ‘List of info about people’, ‘type’: ‘array’, ‘items’: {‘title’: ‘Person’, ‘description’: ‘Information about a person.’, ‘type’: ‘object’, ‘properties’: {‘name’: {‘title’: ‘Name’, ‘description’: “person’s name”, ‘type’: ‘string’}, ‘age’: {‘title’: ‘Age’, ‘description’: “person’s age”, ‘type’: ‘integer’}}, ‘required’: [‘name’]}}}, ‘required’: [‘people’]}}
AIMessage(content=’‘, additional_kwargs={’function_call’: {‘name’: ‘Information’, ‘arguments’: ‘{“people”: [,]}’}})
Model inputs age 0 if age isn’t provided
JsonKeyOutputFunctionsParser()to only extract relevant infoWe can apply tagging and axtracting to a larger body of text.
LLM Powered Autonomous Agents | Lil'Log
Lil'Log
Posts
Archive
Search
Tags
FAQ
emojisearch.app
LLM Powered Autonomous Agents
June 23, 2023 · 31 min · Lilian Weng
Table of Contents
Agent System Overview
Component One: Planning
Task Decomposition
Self-Reflection
Component Two: Memory
Types of Memory
Maximum Inner Product Search (MIPS)
Component Three: Tool Use
Case Studies
Scientific Discovery Agent
Generative Agents Simulation
Proof-of-Concept Examples
Challenges
Citation
References
Building agents with LLM (large language model) as its core controller is a cool concept. Several proof-of-concepts demos, such as AutoGPT, GPT-Engineer and BabyAGI, serve as inspiring examples. The potentiality of LLM extends beyond generating well-written copies, stories, essays and programs; it can be framed as a powerful general problem solver.
Agent System Overview#
Inclass Overview(BaseModel):
"""Overview of a section of text."""
summary: str = Field(description="Provide a concise summary of the content.")
language: str = Field(description="Provide the language that the content is written in.")
keywords: str = Field(description="Provide keywords related to the content.")template = """A article will be passed to you. Extract from it all papers that are mentioned by this article.
Do not extract the name of the article itself. If no papers are mentioned that's fine - you don't need to extract any! Just return an empty list.
Do not make up or guess ANY extra information. Only extract what exactly is in the text."""
prompt = ChatPromptTemplate.from_messages([
("system", template),
("human", "{input}")
])extraction_chain operates over a single element
However, we have a list of elements
Therefore, we call .map()
[{'title': 'AutoGPT', 'author': ''},
{'title': 'GPT-Engineer', 'author': ''},
{'title': 'BabyAGI', 'author': ''},
{'title': 'Chain of thought (CoT; Wei et al. 2022)', 'author': 'Wei et al.'},
{'title': 'Tree of Thoughts (Yao et al. 2023)', 'author': 'Yao et al.'},
{'title': 'LLM+P (Liu et al. 2023)', 'author': 'Liu et al.'},
{'title': 'ReAct (Yao et al. 2023)', 'author': 'Yao et al.'},
{'title': 'Reflexion (Shinn & Labash 2023)', 'author': 'Shinn & Labash'},
{'title': 'Reflexion framework', 'author': 'Shinn & Labash'},
{'title': 'Chain of Hindsight', 'author': 'Liu et al. 2023'},
{'title': 'Algorithm Distillation', 'author': 'Laskin et al. 2023'},
{'title': 'Algorithm Distillation', 'author': 'Laskin et al. 2023'},
{'title': 'ED (expert distillation)', 'author': ''},
{'title': 'RL^2', 'author': 'Duan et al. 2017'},
{'title': 'LSH: Locality-Sensitive Hashing', 'author': ''},
{'title': 'ANNOY: Approximate Nearest Neighbors Oh Yeah', 'author': ''},
{'title': 'HNSW: Hierarchical Navigable Small World', 'author': ''},
{'title': 'FAISS: Facebook AI Similarity Search', 'author': ''},
{'title': 'ScaNN: Scalable Nearest Neighbors', 'author': ''},
{'title': 'MRKL: Modular Reasoning, Knowledge and Language',
'author': 'Karpas et al. 2022'},
{'title': 'TALM: Tool Augmented Language Models',
'author': 'Parisi et al. 2022'},
{'title': 'Toolformer', 'author': 'Schick et al. 2023'},
{'title': 'HuggingGPT', 'author': 'Shen et al. 2023'},
{'title': 'API-Bank', 'author': 'Li et al. 2023'},
{'title': 'ChemCrow', 'author': 'Bran et al. 2023'},
{'title': 'Boiko et al. (2023)', 'author': 'Boiko et al.'},
{'title': 'Generative Agents Simulation', 'author': 'Park, et al. 2023'},
{'title': 'Park et al. 2023', 'author': ''},
{'title': 'Super Mario: How Nintendo Conquered America',
'author': 'Jeff Ryan'},
{'title': 'Model-View-Controller (MVC) Explained', 'author': 'Techopedia'},
{'title': 'Python Game Development: Creating a Snake Game',
'author': 'Real Python'},
{'title': 'Paper A', 'author': 'Author A'},
{'title': 'Paper B', 'author': 'Author B'},
{'title': 'Paper C', 'author': 'Author C'},
{'title': 'Chain of thought prompting elicits reasoning in large language models',
'author': 'Wei et al.'},
{'title': 'Tree of Thoughts: Deliberate Problem Solving with Large Language Models',
'author': 'Yao et al.'},
{'title': 'Chain of Hindsight Aligns Language Models with Feedback',
'author': 'Liu et al.'},
{'title': 'LLM+P: Empowering Large Language Models with Optimal Planning Proficiency',
'author': 'Liu et al.'},
{'title': 'ReAct: Synergizing reasoning and acting in language models',
'author': 'Yao et al.'},
{'title': 'Reflexion: an autonomous agent with dynamic memory and self-reflection',
'author': 'Shinn & Labash'},
{'title': 'In-context Reinforcement Learning with Algorithm Distillation',
'author': 'Laskin et al.'},
{'title': 'MRKL Systems A modular, neuro-symbolic architecture that combines large language models, external knowledge sources and discrete reasoning',
'author': 'Karpas et al.'},
{'title': 'API-Bank: A Benchmark for Tool-Augmented LLMs',
'author': 'Li et al.'},
{'title': 'HuggingGPT: Solving AI Tasks with ChatGPT and its Friends in HuggingFace',
'author': 'Shen et al.'},
{'title': 'ChemCrow: Augmenting large-language models with chemistry tools',
'author': 'Bran et al.'},
{'title': 'Emergent autonomous scientific research capabilities of large language models',
'author': 'Boiko et al.'},
{'title': 'Generative Agents: Interactive Simulacra of Human Behavior',
'author': 'Joon Sung Park, et al.'}]Congratulations! You have completed this tutorial 👍
Next, you may want to go back to the lab’s website
Jan Kirenz