### Setup Langchain, OpenAI, ChromaDB, and PyPDF Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/qa-pdf.ipynb Installs necessary libraries for the project. Run this command in your environment. ```bash %setup langchain openai chromadb pypdf ``` -------------------------------- ### Setup Notebook Environment Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/baby-agi.ipynb Installs necessary libraries like openai, langchain, and faiss for the BabyAGI implementation. ```python %setup openai langchain faiss ``` -------------------------------- ### Setup LangChain and OpenAI Packages Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/chat-creative.ipynb Ensures all necessary packages are installed and environment variables are configured for LangChain and OpenAI. ```python # make sure all packages are installed and environment variables are set %setup langchain openai ``` -------------------------------- ### Compile and Install Extension Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/ui-tests/README.md Commands to compile the extension and install necessary dependencies for testing. ```sh jlpm install jlpm build:prod ``` ```sh cd ./ui-tests jlpm install jlpm playwright install cd .. ``` -------------------------------- ### Install LangForge Extension Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/README.md Install the LangForge extension using pip. This is the standard installation method. ```bash pip install langforge ``` -------------------------------- ### Setup LangChain, OpenAI, and ChromaDB Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/qa-txt.ipynb This magic command sets up the necessary LangChain, OpenAI, and ChromaDB environments for the notebook. ```python %setup langchain openai chromadb ``` -------------------------------- ### Install Build Tools Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/RELEASE.md Install the necessary tools for building Python packages: build, twine, and hatch. ```bash pip install build twine hatch ``` -------------------------------- ### Install LangForge Source: https://github.com/mme/langforge/blob/main/README.md Install the LangForge package using pip. ```bash pip install langforge-ai ``` -------------------------------- ### Setup Langchain and OpenAI Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/api-agent.ipynb Initializes the Langchain environment and sets up the OpenAI API key. Ensure your OpenAI API key is set as an environment variable. ```python %setup langchain openai ``` -------------------------------- ### Serve LangForge App Source: https://github.com/mme/langforge/blob/main/README.md Start serving your LangChain application with a REST interface. Specify the notebook file to serve. ```bash langforge serve chat-creative.ipynb ``` -------------------------------- ### Install Langchain Libraries Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/code.ipynb Installs the required libraries for Langchain, DeepLake, OpenAI, and Tiktoken. This is a prerequisite for the subsequent code. ```python %setup langchain deeplake openai tiktoken ``` -------------------------------- ### Development Install LangForge Extension Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/README.md Install the extension in development mode. This requires NodeJS and links the development version with JupyterLab. ```bash pip install -e . jupyter labextension develop . --overwrite jlpm build ``` -------------------------------- ### Create a New LangChain App Source: https://github.com/mme/langforge/blob/main/README.md Use the create command to generate a new LangChain app. This command sets up the environment, installs packages, and configures API keys. ```bash langforge create myapp ``` -------------------------------- ### Run Frontend Tests Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/README.md Execute frontend tests using Jest. Ensure dependencies are installed first. ```bash jlpm jlpm test ``` -------------------------------- ### Initialize OpenAI Embeddings and FAISS Vector Store Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/baby-agi.ipynb Sets up the embedding model using OpenAI and initializes an empty FAISS in-memory vector store. Ensure you have the 'openai' and 'faiss-cpu' libraries installed. ```python from langchain.embeddings import OpenAIEmbeddings from langchain.vectorstores import FAISS from langchain.docstore import InMemoryDocstore # Define your embedding model embeddings_model = OpenAIEmbeddings() # Initialize the vectorstore as empty embedding_size = 1536 index = faiss.IndexFlatL2(embedding_size) vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {}) ``` -------------------------------- ### Initialize Conversational Retrieval Chain Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/qa-txt.ipynb Initializes the conversational retrieval chain with a chat model, vector store retriever, and memory. This setup allows for context-aware conversations about the indexed documents. You can switch to GPT-4 by uncommenting the relevant line. ```python memory = ConversationBufferMemory(memory_key="chat_history", input_key="question") llm = ChatOpenAI(temperature=0) # if you want GPT-4: # llm = ChatOpenAI(temperature=0, model_name="gpt-4") qa = ConversationalRetrievalChain.from_llm(llm, vectorstore.as_retriever(), memory=memory, get_chat_history=lambda inputs: inputs) ``` -------------------------------- ### Define Creative ChatGPT Template Source: https://github.com/mme/langforge/blob/main/README.md Customize the behavior of your LangChain application by defining a template. This example sets up a space adventure scenario for AdventureGPT. ```python template = """This is a conversation between a human and a system called AdventureGPT. AdventureGPT is designed to create immersive and engaging text-based adventure games. AdventureGPT is capable of understanding both simple commands, such as 'look,' and more complex sentences, allowing it to effectively interpret the player's intent. This adventure takes place in space. The player steps into the role of Captain Bravado, a fearless and charismatic leader of the starship 'Infinity Chaser'. Tasked with navigating the uncharted reaches of the cosmos, Captain Bravado and their loyal crew must overcome various challenges, solve intricate puzzles, and make critical decisions that will shape the fate of their mission and the future of interstellar exploration. """ ``` -------------------------------- ### Load and Process Documents for Q&A Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/qa-txt.ipynb Loads text documents, splits them into manageable chunks, generates embeddings using OpenAI, and stores them in a Chroma vector store. Ensure you have the 'langchain', 'openai', and 'chromadb' libraries installed. ```python from langchain.embeddings.openai import OpenAIEmbeddings from langchain.vectorstores import Chroma from langchain.text_splitter import CharacterTextSplitter from langchain.llms import OpenAI from langchain.chat_models import ChatOpenAI from langchain.chains import ConversationalRetrievalChain from langchain.document_loaders import TextLoader from langchain.memory import ConversationBufferMemory import urllib.request # retrieve the state of the union speech urllib.request.urlretrieve("https://raw.githubusercontent.com/hwchase17/chat-your-data/master/state_of_the_union.txt", "state_of_the_union.txt") txts = [ "state_of_the_union.txt" ]; documents = [] for txt in txts: loader = TextLoader(txt) docs = loader.load() documents.extend(docs) text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) documents = text_splitter.split_documents(documents) embeddings = OpenAIEmbeddings() vectorstore = Chroma.from_documents(documents, embeddings) ``` -------------------------------- ### Login to NPM and Publish Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/RELEASE.md Log in to the NPM registry and publish the frontend part of the extension as a public NPM package. ```bash npm login npm publish --access public ``` -------------------------------- ### Configure Conversation Chain for CoachGPT Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/chat-deterministic.ipynb Sets up the system prompt, LLM, and memory for a deterministic conversational agent named CoachGPT. The LLM is configured with a temperature of 0 for consistent output. Optionally, GPT-4 can be used by uncommenting the relevant line. ```python template = """This is a conversation between a human and a system called CoachGPT. CoachGPT is designed to ask the user for their age and name. Once it knows age and name, CoachGPT will give some good life advices. It will refuse to give any advice without knowing the humans age and name. After CoachGPT has given advice, it will ask the user to visit the website "https://example.com/signup" to sign up for free and end the conversation. """ prompt = ChatPromptTemplate.from_messages([ SystemMessagePromptTemplate.from_template(template), MessagesPlaceholder(variable_name="history"), HumanMessagePromptTemplate.from_template("{input}") ]) llm = ChatOpenAI(temperature=0) # if you want GPT-4: # llm = ChatOpenAI(temperature=0, model_name="gpt-4") memory = ConversationBufferMemory(return_messages=True) gpt_adventure = ConversationChain(memory=memory, prompt=prompt, llm=llm) ``` -------------------------------- ### Initialize ReAct Agent with Klarna Toolkit Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/api-agent.ipynb Initializes a ReAct agent using the tools from the Klarna NLAToolkit, the ChatOpenAI language model, and the defined format instructions. This agent is capable of understanding natural language queries and interacting with the Klarna API. ```python mrkl = initialize_agent(klarna_toolkit.get_tools(), llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, agent_kwargs={"format_instructions": openapi_format_instructions}) ``` -------------------------------- ### Initialize Chat Model and NLAToolkit Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/api-agent.ipynb Initializes the ChatOpenAI model with a temperature of 0 for deterministic responses and creates an NLAToolkit using the LLM and the Klarna API documentation URL. ```python llm = ChatOpenAI(temperature=0) klarna_toolkit = NLAToolkit.from_llm_and_url(llm, "https://www.klarna.com/us/shopping/public/openai/v0/api-docs/") ``` -------------------------------- ### Build Extension with Source Maps Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/README.md Build the extension and generate source maps for debugging. Set minimize to False to include source maps for core extensions. ```bash jupyter lab build --minimize=False ``` -------------------------------- ### Launch JupyterLab Source: https://github.com/mme/langforge/blob/main/README.md Navigate to your app directory and launch JupyterLab to access your project and templates. ```bash cd myapp langforge lab ``` -------------------------------- ### Build Python Package Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/RELEASE.md Generate the Python source package (.tar.gz) and binary package (.whl) in the dist/ directory. ```bash python -m build ``` -------------------------------- ### BabyAGI Controller Initialization Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/baby-agi.ipynb Initializes the BabyAGI controller with language model chains and a vector store. Use this method to set up the core components for the autonomous agent. ```python @classmethod def from_llm( cls, llm: BaseLLM, vectorstore: VectorStore, verbose: bool = False, **kwargs ) -> "BabyAGI": """Initialize the BabyAGI Controller.""" task_creation_chain = TaskCreationChain.from_llm( llm, verbose=verbose ) task_prioritization_chain = TaskPrioritizationChain.from_llm( llm, verbose=verbose ) execution_chain = ExecutionChain.from_llm(llm, verbose=verbose) return cls( task_creation_chain=task_creation_chain, task_prioritization_chain=task_prioritization_chain, execution_chain=execution_chain, vectorstore=vectorstore, **kwargs ) ``` -------------------------------- ### Define ReAct Agent Format Instructions Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/api-agent.ipynb Specifies the formatting instructions for the ReAct (Reasoning and Acting) agent, defining the expected structure for questions, thoughts, actions, action inputs, observations, and the final answer. ```python openapi_format_instructions = """Use the following format: Question: the input question you must answer Thought: you should always think about what to do Action: the action to take, should be one of [{tool_names}] Action Input: what to instruct the AI Action representative. Observation: The Agent's response ... (this Thought/Action/Action Input/Observation can repeat N times) Thought: I now know the final answer. User can't see any of my observations, API responses, links, or tools. Final Answer: the final answer to the original input question with the right amount of detail When responding with your Final Answer, remember that the person you are responding to CANNOT see any of your Thought/Action/Action Input/Observations, so if there is any relevant information there you need to include it explicitly in your response.""" ``` -------------------------------- ### Configure Chat Prompt and LLM Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/chat-creative.ipynb Defines the system prompt for the AI persona (AdventureGPT) and initializes the ChatOpenAI model with a high temperature for creative output. Includes an option to use GPT-4. ```python template = """This is a conversation between a human and a system called AdventureGPT. AdventureGPT is designed to create immersive and engaging text-based adventure games. AdventureGPT is capable of understanding both simple commands, such as 'look,' and more complex sentences, allowing it to effectively interpret the player's intent. """ prompt = ChatPromptTemplate.from_messages([ SystemMessagePromptTemplate.from_template(template), MessagesPlaceholder(variable_name="history"), HumanMessagePromptTemplate.from_template("{input}") ]) llm = ChatOpenAI(temperature=1) # if you want GPT-4: # llm = ChatOpenAI(temperature=1, model_name="gpt-4") memory = ConversationBufferMemory(return_messages=True) gpt_adventure = ConversationChain(memory=memory, prompt=prompt, llm=llm) ``` -------------------------------- ### Initialize ChatOpenAI Language Model Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/baby-agi.ipynb Initializes the ChatOpenAI language model with a temperature of 0 for deterministic output. An alternative for GPT-4 is commented out. ```python from langchain.chat_models import ChatOpenAI llm = ChatOpenAI(temperature=0) # or if you want gpt-4 # llm = ChatOpenAI(temperature=0, model="gpt-4") ``` -------------------------------- ### Interact with Served App via cURL Source: https://github.com/mme/langforge/blob/main/README.md Send HTTP POST requests to your served LangForge app using curl. Include user input and conversation memory for context. ```bash curl -X POST -H "Content-Type: application/json" -d '{"input": "look", "memory": []}' http://localhost:2204/chat/gpt_adventure ``` -------------------------------- ### Import LangChain Components Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/chat-creative.ipynb Imports essential classes for building conversational agents, including prompt templates, chat models, and memory management. ```python from langchain.prompts import ( ChatPromptTemplate, MessagesPlaceholder, SystemMessagePromptTemplate, HumanMessagePromptTemplate ) from langchain.chains import ConversationChain from langchain.chat_models import ChatOpenAI from langchain.memory import ConversationBufferMemory ``` -------------------------------- ### Initialize and Run Baby AGI Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/baby-agi.ipynb Initializes the Baby AGI agent with a language model and vector store, then executes it with a given objective. The `max_iterations` parameter controls the maximum number of task cycles. ```python max_iterations: Optional[int] = 10 baby_agi = BabyAGI.from_llm( llm=llm, vectorstore=vectorstore, max_iterations=max_iterations ) baby_agi({"objective": "Teach a dog to say 'I love you'"}) ``` -------------------------------- ### Load and Split Documents Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/code.ipynb Loads all text files from a cloned repository, splits them into manageable chunks using a CharacterTextSplitter, and prepares them for embedding. ```python root_dir = './the-algorithm' docs = [] for dirpath, dirnames, filenames in os.walk(root_dir): for file in filenames: try: loader = TextLoader(os.path.join(dirpath, file), encoding='utf-8') docs.extend(loader.load_and_split()) except Exception as e: pass text_splitter = CharacterTextSplitter(chunk_size=5000, chunk_overlap=0) texts = text_splitter.split_documents(docs) ``` -------------------------------- ### Import Core Libraries Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/baby-agi.ipynb Imports essential modules from Python's standard library and the Langchain framework for building the AI agent. ```python import os from collections import deque from typing import Dict, List, Optional, Any from langchain import LLMChain, 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.vectorstores import FAISS from langchain.docstore import InMemoryDocstore import faiss from langchain.chat_models import ChatOpenAI ``` -------------------------------- ### Configure Retriever and Filter Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/code.ipynb Sets up a retriever for the vector store with specific search parameters (distance metric, fetch_k, MMR, k) and defines a custom filter function to exclude certain documents. ```python retriever = db.as_retriever() retriever.search_kwargs['distance_metric'] = 'cos' retriever.search_kwargs['fetch_k'] = 100 retriever.search_kwargs['maximal_marginal_relevance'] = True retriever.search_kwargs['k'] = 20 def filter(x): # filter based on source code if 'com.google' in x['text'].data()['value']: return False # filter based on path e.g. extension metadata = x['metadata'].data()['value'] return 'scala' in metadata['source'] or 'py' in metadata['source'] ``` -------------------------------- ### Upload Package to PyPI Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/RELEASE.md Upload the built Python packages from the dist/ directory to the Python Package Index (PyPI). ```bash twine upload dist/* ``` -------------------------------- ### Bump Version with Hatch Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/RELEASE.md Use hatch to bump the version of the package. This command typically creates a Git tag. ```bash hatch version ``` -------------------------------- ### Initialize Conversational Retrieval Chain Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/code.ipynb Initializes a ChatOpenAI model and a ConversationalRetrievalChain using the configured retriever and memory. This chain is used for question answering. ```python memory = ConversationBufferMemory(memory_key="chat_history", input_key="question") model = ChatOpenAI(model='gpt-4') # 'gpt-3.5-turbo', qa = ConversationalRetrievalChain.from_llm(model,retriever=retriever, memory=memory, get_chat_history=lambda inputs: inputs) ``` -------------------------------- ### Load and Process PDF Documents Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/qa-pdf.ipynb Downloads two research papers, loads them using PyPDFLoader, splits them into chunks, and creates a Chroma vector store with OpenAI embeddings. Ensure you have your OpenAI API key configured. ```python from langchain.embeddings.openai import OpenAIEmbeddings from langchain.vectorstores import Chroma from langchain.text_splitter import CharacterTextSplitter from langchain.llms import OpenAI from langchain.chat_models import ChatOpenAI from langchain.chains import ConversationalRetrievalChain from langchain.document_loaders import PyPDFLoader from langchain.memory import ConversationBufferMemory import urllib.request # retrieve the "Attention Is All You Need" paper urllib.request.urlretrieve("https://arxiv.org/pdf/1706.03762", "attention.pdf") # retrieve "Language Models are Few-Shot Learners" urllib.request.urlretrieve("https://arxiv.org/pdf/2005.14165v4", "gpt3.pdf") pdfs = [ "attention.pdf", "gpt3.pdf" ]; documents = [] for pdf in pdfs: loader = PyPDFLoader(pdf) docs = loader.load() documents.extend(docs) text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) documents = text_splitter.split_documents(documents) embeddings = OpenAIEmbeddings() vectorstore = Chroma.from_documents(documents, embeddings) ``` -------------------------------- ### Watch for Changes During Development Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/README.md Run the watch command to automatically rebuild the extension when source files change. This should be run in one terminal, with JupyterLab running in another. ```bash jlpm watch jupyter lab ``` -------------------------------- ### Import Necessary Langchain Components Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/api-agent.ipynb Imports all required classes and functions from Langchain for building API agents, including LLM chains, chat models, prompts, tools, and agent initializers. ```python from typing import List, Optional from langchain.chains import LLMChain from langchain.chat_models import ChatOpenAI from langchain.prompts import PromptTemplate from langchain.requests import Requests from langchain.tools import APIOperation, OpenAPISpec from langchain.agents import AgentType, Tool, initialize_agent from langchain.agents.agent_toolkits import NLAToolkit from langchain.chains.api import open_meteo_docs from langchain.tools import OpenAPISpec ``` -------------------------------- ### Import Necessary Libraries Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/code.ipynb Imports essential classes and functions from Langchain and other libraries for document loading, splitting, embeddings, vector stores, and conversational chains. ```python import os from langchain.embeddings.openai import OpenAIEmbeddings from langchain.vectorstores import DeepLake from langchain.document_loaders import TextLoader from langchain.text_splitter import CharacterTextSplitter from langchain.chat_models import ChatOpenAI from langchain.chains import ConversationalRetrievalChain from langchain.memory import ConversationBufferMemory ``` -------------------------------- ### Import Necessary Langchain Components Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/chat-deterministic.ipynb Imports required classes and functions from Langchain for building conversational agents, including prompts, chains, chat models, and memory. ```python from langchain.prompts import ( ChatPromptTemplate, MessagesPlaceholder, SystemMessagePromptTemplate, HumanMessagePromptTemplate ) from langchain.chains import ConversationChain from langchain.chat_models import ChatOpenAI from langchain.memory import ConversationBufferMemory ``` -------------------------------- ### Run Integration Tests Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/ui-tests/README.md Execute Playwright integration tests for the JupyterLab extension. ```sh cd ./ui-tests jlpm playwright test ``` -------------------------------- ### BabyAGI Controller Functions Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/baby-agi.ipynb Helper functions for the BabyAGI controller to manage tasks. Includes functions for creating new tasks, prioritizing existing tasks, retrieving relevant context, and executing a given task. ```python def get_next_task(task_creation_chain: LLMChain, result: Dict, task_description: str, task_list: List[str], objective: str) -> List[Dict]: """Get the next task.""" incomplete_tasks = ", ".join(task_list) response = task_creation_chain.run(result=result, task_description=task_description, incomplete_tasks=incomplete_tasks, objective=objective) new_tasks = response.split('\n') return [{"task_name": task_name} for task_name in new_tasks if task_name.strip()] def prioritize_tasks(task_prioritization_chain: LLMChain, this_task_id: int, task_list: List[Dict], objective: str) -> List[Dict]: """Prioritize tasks.""" task_names = [t["task_name"] for t in task_list] next_task_id = int(this_task_id) + 1 response = task_prioritization_chain.run(task_names=task_names, next_task_id=next_task_id, objective=objective) new_tasks = response.split('\n') prioritized_task_list = [] for task_string in new_tasks: if not task_string.strip(): continue task_parts = task_string.strip().split(".", 1) if len(task_parts) == 2: task_id = task_parts[0].strip() task_name = task_parts[1].strip() prioritized_task_list.append({"task_id": task_id, "task_name": task_name}) return prioritized_task_list def _get_top_tasks(vectorstore, query: str, k: int) -> List[str]: """Get the top k tasks based on the query.""" results = vectorstore.similarity_search_with_score(query, k=k) if not results: return [] sorted_results, _ = zip(*sorted(results, key=lambda x: x[1], reverse=True)) return [str(item.metadata['task']) for item in sorted_results] def execute_task(vectorstore, execution_chain: LLMChain, objective: str, task: str, k: int = 5) -> str: """Execute a task.""" context = _get_top_tasks(vectorstore, query=objective, k=k) return execution_chain.run(objective=objective, context=context, task=task) ``` -------------------------------- ### Clone GitHub Repository Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/code.ipynb Clones a specified GitHub repository to the local file system. Replace the URL with the repository you wish to analyze. ```python !git clone https://github.com/twitter/the-algorithm # replace any repository of your choice ``` -------------------------------- ### Task Creation Chain Definition Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/baby-agi.ipynb Defines the LLMChain responsible for generating new tasks based on the objective, execution results, and incomplete tasks. ```python class TaskCreationChain(LLMChain): """Chain to generates tasks.""" @classmethod def from_llm(cls, llm: BaseLLM, verbose: bool = True) -> LLMChain: """Get the response parser.""" task_creation_template = ( "You are an task creation AI that uses the result of an execution agent" " to create new tasks with the following objective: {objective}," " The last completed task has the result: {result}." " This result was based on this task description: {task_description}." " These are incomplete tasks: {incomplete_tasks}." " Based on the result, create new tasks to be completed" " by the AI system that do not overlap with incomplete tasks." " Return the tasks as an array." ) prompt = PromptTemplate( template=task_creation_template, input_variables=["result", "task_description", "incomplete_tasks", "objective"], ) return cls(prompt=prompt, llm=llm, verbose=verbose) ``` -------------------------------- ### Uninstall LangForge Extension Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/README.md Remove the LangForge extension using pip. This is the standard uninstallation method. ```bash pip uninstall langforge ``` -------------------------------- ### Create DeepLake Vector Store Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/code.ipynb Generates embeddings for the processed text chunks using OpenAIEmbeddings and stores them in a DeepLake vector database. ```python embeddings = OpenAIEmbeddings() db = DeepLake.from_documents(texts, embeddings) ``` -------------------------------- ### Execution Chain Definition Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/baby-agi.ipynb Defines the LLMChain responsible for executing a given task, considering the objective and previously completed tasks. ```python class ExecutionChain(LLMChain): """Chain to execute tasks.""" @classmethod def from_llm(cls, llm: BaseLLM, verbose: bool = True) -> LLMChain: """Get the response parser.""" execution_template = ( "You are an AI who performs one task based on the following objective: {objective}." " Take into account these previously completed tasks: {context}." " Your task: {task}." " Response:" ) prompt = PromptTemplate( template=execution_template, input_variables=["objective", "context", "task"], ) return cls(prompt=prompt, llm=llm, verbose=verbose) ``` -------------------------------- ### Update Test Snapshots Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/ui-tests/README.md Command to update reference snapshots for Playwright tests. ```sh cd ./ui-tests jlpm playwright test -u ``` -------------------------------- ### Debug Tests with Playwright Inspector Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/ui-tests/README.md Run Playwright tests in debug mode using the PWDEBUG environment variable. ```sh cd ./ui-tests PWDEBUG=1 jlpm playwright test ``` -------------------------------- ### Task Prioritization Chain Definition Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/baby-agi.ipynb Defines the LLMChain responsible for re-prioritizing tasks based on the objective and a given list of task names. ```python class TaskPrioritizationChain(LLMChain): """Chain to prioritize tasks.""" @classmethod def from_llm(cls, llm: BaseLLM, verbose: bool = True) -> LLMChain: """Get the response parser.""" task_prioritization_template = ( "You are an task prioritization AI tasked with cleaning the formatting of and reprioritizing" " the following tasks: {task_names}." " Consider the ultimate objective of your team: {objective}." " Do not remove any tasks. Return the result as a numbered list, like:" " #. First task" " #. Second task" " Start the task list with number {next_task_id}." ) prompt = PromptTemplate( template=task_prioritization_template, input_variables=["task_names", "next_task_id", "objective"], ) return cls(prompt=prompt, llm=llm, verbose=verbose) ``` -------------------------------- ### Generate New Tests with Codegen Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/ui-tests/README.md Use Playwright's code generator to create new integration tests. ```sh cd ./ui-tests jlpm playwright codegen localhost:8888 ``` -------------------------------- ### BabyAGI Class Definition Source: https://github.com/mme/langforge/blob/main/jupyterlab_extension/notebooks/baby-agi.ipynb Defines the BabyAGI controller class, inheriting from Chain and BaseModel. It manages the task list, chains, and execution flow. ```python class BabyAGI(Chain, BaseModel): """Controller model for the BabyAGI agent.""" task_list: deque = Field(default_factory=deque) task_creation_chain: TaskCreationChain = Field(...) task_prioritization_chain: TaskPrioritizationChain = Field(...) execution_chain: ExecutionChain = Field(...) task_id_counter: int = Field(1) vectorstore: VectorStore = Field(init=False) max_iterations: Optional[int] = None class Config: """Configuration for this pydantic object.""" arbitrary_types_allowed = True def add_task(self, task: Dict): self.task_list.append(task) def print_task_list(self): print("\033[95m\033[1m" + "\n*****TASK LIST*****\n" + "\033[0m\033[0m") for t in self.task_list: print(str(t["task_id"]) + ": " + t["task_name"]) def print_next_task(self, task: Dict): print("\033[92m\033[1m" + "\n*****NEXT TASK*****\n" + "\033[0m\033[0m") print(str(task["task_id"]) + ": " + task["task_name"]) def print_task_result(self, result: str): print("\033[93m\033[1m" + "\n*****TASK RESULT*****\n" + "\033[0m\033[0m") print(result) @property def input_keys(self) -> List[str]: return ["objective"] @property def output_keys(self) -> List[str]: return [] def _call(self, inputs: Dict[str, Any]) -> Dict[str, Any]: """Run the agent.""" objective = inputs['objective'] first_task = inputs.get("first_task", "Make a todo list") self.add_task({"task_id": 1, "task_name": first_task}) num_iters = 0 while True: if self.task_list: self.print_task_list() # Step 1: Pull the first task task = self.task_list.popleft() self.print_next_task(task) # Step 2: Execute the task result = execute_task( self.vectorstore, self.execution_chain, objective, task["task_name"] ) this_task_id = int(task["task_id"]) self.print_task_result(result) # Step 3: Store the result in Pinecone result_id = f"result_{task['task_id']}" self.vectorstore.add_texts( texts=[result], metadatas=[{"task": task["task_name"]}], ids=[result_id], ) # Step 4: Create new tasks and reprioritize task list new_tasks = get_next_task( self.task_creation_chain, result, task["task_name"], [t["task_name"] for t in self.task_list], objective ) for new_task in new_tasks: self.task_id_counter += 1 new_task.update({"task_id": self.task_id_counter}) self.add_task(new_task) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.