Package: langroid
Documentation·Examples Repo·Discord·ContributingLangroidis an intuitive, lightweight, extensible and principled
Python framework to easily build LLM-powered applications, from ex-CMU and UW-Madison researchers.
You set up Agents, equip them with optional components (LLM,
vector-store and tools/functions), assign them tasks, and have them
collaboratively solve a problem by exchanging messages.
This Multi-Agent paradigm is inspired by theActor Framework(but you do not need to know anything about this!).Langroidis a fresh take on LLM app-development, where considerable thought has gone
into simplifying the developer experience; it does not useLangchain.:fire: See thisIntro to Langroidblog post from the LanceDB teamWe welcome contributions -- See thecontributionsdocument
for ideas on what to contribute.Are you building LLM Applications, or want help with Langroid for your company,
or want to prioritize Langroid features for your company use-cases?Prasad Chalasaniis available for consulting
(advisory/development): pchalasani at gmail dot com.Sponsorship is also accepted viaGitHub SponsorsQuestions, Feedback, Ideas? Join us onDiscord!Quick glimpse of coding with LangroidThis is just a teaser; there's much more, like function-calling/tools,
Multi-Agent Collaboration, Structured Information Extraction, DocChatAgent
(RAG), SQLChatAgent, non-OpenAI local/remote LLMs, etc. Scroll down or see docs for more.
See the Langroid Quick-StartColabthat builds up to a 2-agent information-extraction example using the OpenAI ChatCompletion API.
See also thisversionthat uses the OpenAI Assistants API instead.:fire: just released!Examplescript showing how you can use Langroid multi-agents and tools
to extract structured information from a document usingonly a local LLM(Mistral-7b-instruct-v0.2).importlangroidaslrimportlangroid.language_modelsaslm# set up LLMllm_cfg=lm.OpenAIGPTConfig(# or OpenAIAssistant to use Assistant API# any model served via an OpenAI-compatible APIchat_model=lm.OpenAIChatModel.GPT4_TURBO,# or, e.g., "ollama/mistral")# use LLM directlymdl=lm.OpenAIGPT(llm_cfg)response=mdl.chat("What is the capital of Ontario?",max_tokens=10)# use LLM in an Agentagent_cfg=lr.ChatAgentConfig(llm=llm_cfg)agent=lr.ChatAgent(agent_cfg)agent.llm_response("What is the capital of China?")response=agent.llm_response("And India?")# maintains conversation state# wrap Agent in a Task to run interactive loop with user (or other agents)task=lr.Task(agent,name="Bot",system_message="You are a helpful assistant")task.run("Hello")# kick off with user saying "Hello"# 2-Agent chat loop: Teacher Agent asks questions to Student Agentteacher_agent=lr.ChatAgent(agent_cfg)teacher_task=lr.Task(teacher_agent,name="Teacher",system_message="""Ask your student concise numbers questions, and give feedback.Start with a question.""")student_agent=lr.ChatAgent(agent_cfg)student_task=lr.Task(student_agent,name="Student",system_message="Concisely answer the teacher's questions.",single_round=True,)teacher_task.add_sub_task(student_task)teacher_task.run():fire: Updates/ReleasesClick to expandFeb 2024:0.1.193:Support local LLMs using Ollama's new OpenAI-Compatible server:
simply specifychat_model="ollama/mistral". Seerelease notes.0.1.183:Added Chainlit support viacallbacks.
Seeexamples.Jan 2024:0.1.175Neo4jChatAgentto chat with a neo4j knowledge-graph.
(Thanks toMohannad!). The agent uses tools to query the Neo4j schema and translate user queries to Cypher queries,
and the tool handler executes these queries, returning them to the LLM to compose
a natural language response (analogous to howSQLChatAgentworks).
See examplescriptusing this Agent to answer questions about Python pkg dependencies.Support for.docfile parsing (in addition to.docx)Specify optionalformatterparaminOpenAIGPTConfigto ensure accurate chat formatting for local LLMs.0.1.157:DocChatAgentConfighas a new param:add_fields_to_content, to specify additional document fields to insert into
the maincontentfield, to help improve retrieval.0.1.156:New Task control signals
PASS_TO, SEND_TO; VectorStore: Compute Pandas expression on documents; LanceRAGTaskCreator creates 3-agent RAG system with Query Planner, Critic and RAG Agent.Dec 2023:0.1.154:(For details see release notes of0.1.149and0.1.154).DocChatAgent: Ingest Pandas dataframes and filtering.LanceDocChatAgentleveragesLanceDBvector-db for efficient vector search
and full-text search and filtering.Improved task and multi-agent control mechanismsLanceRAGTaskCreatorto create a 2-agent system consisting of aLanceFilterAgentthat
decides a filter and rephrase query to send to a RAG agent.0.1.141:API Simplifications to reduce boilerplate:
auto-select an available OpenAI model (preferring gpt-4-turbo), simplifies defaults.
SimplerTaskinitialization with defaultChatAgent.Nov 2023:0.1.126:OpenAIAssistant agent: Caching Support.0.1.117:Support for OpenAI Assistant API tools: Function-calling,
Code-intepreter, and Retriever (RAG), file uploads. These work seamlessly
with Langroid's task-orchestration.
Until docs are ready, it's best to see these usage examples:Tests:test_openai_assistant.pytest_openai_assistant_async.pyExample scripts:The most basic chat appChat with code interpreterChat with retrieval (RAG)2-agent RAG chat0.1.112:OpenAIAssistantis a subclass ofChatAgentthat
leverages the new OpenAI Assistant API. It can be used as a drop-in
replacement forChatAgent, and relies on the Assistant API to
maintain conversation state, and leverages persistent threads and
assistants to reconnect to them if needed. Examples:test_openai_assistant.py,test_openai_assistant_async.py0.1.111:Support latest OpenAI model:GPT4_TURBO(seetest_llm.pyfor example usage)0.1.110:Upgrade from OpenAI v0.x to v1.1.1 (in preparation for
Assistants API and more); (litellmtemporarily disabled due to OpenAI
version conflict).Oct 2023:0.1.107:DocChatAgentre-rankers:rank_with_diversity,rank_to_periphery(lost in middle).0.1.102:DocChatAgentConfig.n_neighbor_chunks > 0allows returning context chunks around match.0.1.101:DocChatAgentusesRelevanceExtractorAgentto have
the LLM extract relevant portions of a chunk using
sentence-numbering, resulting in huge speed up and cost reduction
compared to the naive "sentence-parroting" approach (writing out full
sentences out relevant whole sentences) whichLangChainuses in theirLLMChainExtractor.0.1.100:API update: all of Langroid is accessible with a single import, i.e.import langroid as lr. See thedocumentationfor usage.0.1.99:Convenience batch functions to run tasks, agent methods on a list of inputs concurrently in async mode. See examples intest_batch.py.0.1.95:Added support forMomento Serverless Vector Index0.1.94:Added support forLanceDBvector-store -- allows vector, Full-text, SQL search.0.1.84:AddedLiteLLM, so now Langroid can be used with over 100 LLM providers (remote or local)!
See guidehere.Sep 2023:0.1.78:Async versions of several Task, Agent and LLM methods;
Nested Pydantic classes are now supported for LLM Function-calling, Tools, Structured Output.0.1.76:DocChatAgent: support for loadingdocxfiles (preliminary).0.1.72:Many improvements to DocChatAgent: better embedding model,
hybrid search to improve retrieval, better pdf parsing, re-ranking retrieved results with cross-encoders.Use with local LLama Models:see tutorialhereLangroid Blog/Newsletter Launched!: First post ishere-- Please subscribe to stay updated.0.1.56:Support Azure OpenAI.0.1.55:ImprovedSQLChatAgentthat efficiently retrieves relevant schema info when translating natural language to SQL.Aug 2023:Hierarchical computationexample using Langroid agents and task orchestration.0.1.51:Support for global state, seetest_global_state.py.:whale: Langroid Docker image, available, see instructions below.RecipientToolenables (+ enforces) LLM to
specify an intended recipient when talking to 2 or more agents.
Seethis testfor example usage.Example:Answer questionsusing Google Search + vecdb-retrieval from URL contents.0.1.39:GoogleSearchToolto enable Agents (their LLM) to do Google searches via function-calling/tools.
Seethis chat examplefor how easy it is to add this tool to an agent.Colab notebookto try the quick-start examples:0.1.37:AddedSQLChatAgent-- thanks to our latest contributorRithwik Babu!Multi-agent Example:Autocorrect chatJuly 2023:0.1.30:AddedTableChatAgenttochatwith tabular datasets (dataframes, files, URLs): LLM generates Pandas code,
and code is executed using Langroid's tool/function-call mechanism.Demo:3-agent system for AudienceTargeting.0.1.27: AddedsupportforMomento Serverless Cacheas an alternative to Redis.0.1.24:DocChatAgentnowacceptsPDF files or URLs.:rocket: DemoSuppose you want to extract structured information about the key terms
of a commercial lease document. You can easily do this with Langroid using a two-agent system,
as we show in thelangroid-examplesrepo.
(Seethis scriptfor a version with the same functionality using a local Mistral-7b model.)
The demo showcases just a few of the many features of Langroid, such as:Multi-agent collaboration:LeaseExtractoris in charge of the task, and its LLM (GPT4) generates questions
to be answered by theDocAgent.Retrieval augmented question-answering, withsource-citation:DocAgentLLM (GPT4) uses retrieval from a vector-store to
answer theLeaseExtractor's questions, cites the specific excerpt supporting the answer.Function-calling (also known as tool/plugin): When it has all the information it
needs, theLeaseExtractorLLM presents the information in a structured
format using a Function-call.Here is what it looks like in action
(a pausable mp4 video ishere).:zap: Highlights(For a more up-to-date list see thereleasesection above)Agents as first-class citizens:TheAgentclass encapsulates LLM conversation state,
and optionally a vector-store and tools. Agents are a core abstraction in Langroid;
Agents act asmessage transformers, and by default provide 3respondermethods, one corresponding to each entity: LLM, Agent, User.Tasks:ATaskclass wraps an Agent, and gives the agent instructions (or roles, or goals),
manages iteration over an Agent's responder methods,
and orchestrates multi-agent interactions via hierarchical, recursive
task-delegation. TheTask.run()method has the same
type-signature as an Agent's responder's methods, and this is key to how
a task of an agent can delegate to other sub-tasks: from the point of view of a Task,
sub-tasks are simply additional responders, to be used in a round-robin fashion
after the agent's own responders.Modularity, Reusabilily, Loose coupling:TheAgentandTaskabstractions allow users to design
Agents with specific skills, wrap them in Tasks, and combine tasks in a flexible way.LLM Support: Langroid supports OpenAI LLMs as well as LLMs from hundreds of
providers (local/open or remote/commercial) via proxy libraries and local model servers
such asLiteLLMthat in effect mimic the OpenAI API.Caching of LLM responses:Langroid supportsRedisandMomentoto cache LLM responses.Vector-stores:LanceDB,Qdrant,Chromaare currently supported.
Vector stores allow for Retrieval-Augmented-Generation (RAG).Grounding and source-citation:Access to external documents via vector-stores
allows for grounding and source-citation.Observability, Logging, Lineage:Langroid generates detailed logs of multi-agent interactions and
maintains provenance/lineage of messages, so that you can trace back
the origin of a message.Tools/Plugins/Function-calling: Langroid supports OpenAI's recently
releasedfunction callingfeature. In addition, Langroid has its own native equivalent, which we
calltools(also known as "plugins" in other contexts). Function
calling and tools have the same developer-facing interface, implemented
usingPydantic,
which makes it very easy to define tools/functions and enable agents
to use them. Benefits of using Pydantic are that you never have to write
complex JSON specs for function calling, and when the LLM
hallucinates malformed JSON, the Pydantic error message is sent back to
the LLM so it can fix it!:gear: Installation and SetupInstalllangroidLangroid requires Python 3.11+. We recommend using a virtual environment.
Usepipto installlangroid(from PyPi) to your virtual environment:pipinstalllangroidThe core Langroid package lets you use OpenAI Embeddings models via their API.
If you instead want to use thesentence-transformersembedding models from HuggingFace,
install Langroid like this:pipinstalllangroid[hf-embeddings]If usingzsh(or similar shells), you may need to escape the square brackets, e.g.:pip install langroid\[hf-embeddings\]or use quotes:pip install "langroid[hf-embeddings]"Optional Installs for using SQL Chat with a PostgreSQL DBIf you are usingSQLChatAgent(e.g. the scriptexamples/data-qa/sql-chat/sql_chat.py),
with a postgres db, you will need to:Install PostgreSQL dev libraries for your platform, e.g.sudo apt-get install libpq-devon Ubuntu,brew install postgresqlon Mac, etc.Install langroid with the postgres extra, e.g.pip install langroid[postgres]orpoetry add langroid[postgres]orpoetry install -E postgres.
If this gives you an error, trypip install psycopg2-binaryin your virtualenv.Set up environment variables (API keys, etc)To get started, all you need is an OpenAI API Key.
If you don't have one, seethis OpenAI Page.
Currently only OpenAI models are supported. Others will be added later
(Pull Requests welcome!).In the root of the repo, copy the.env-templatefile to a new file.env:cp.env-template.envThen insert your OpenAI API Key.
Your.envfile should look like this (the organization is optional
but may be required in some scenarios).OPENAI_API_KEY=your-key-here-without-quotesOPENAI_ORGANIZATION=optionally-your-organization-idAlternatively, you can set this as an environment variable in your shell
(you will need to do this every time you open a new shell):exportOPENAI_API_KEY=your-key-here-without-quotesOptional Setup Instructions (click to expand)All of the following environment variable settings are optional, and some are only needed
to use specific features (as noted below).QdrantVector Store API Key, URL. This is only required if you want to use Qdrant cloud.
The default vector store in our RAG agent (DocChatAgent) is LanceDB which uses file storage,
and you do not need to set up any environment variables for that.
AlternativelyChromais also currently supported.
We use the local-storage version of Chroma, so there is no need for an API key.RedisPassword, host, port: This is optional, and only needed to cache LLM API responses
using Redis Cloud. Redisoffersa free 30MB Redis account
which is more than sufficient to try out Langroid and even beyond.
If you don't set up these, Langroid will use a pure-python
Redis in-memory cache via theFakeredislibrary.MomentoServerless Caching of LLM API responses (as an alternative to Redis).
To use Momento instead of Redis:enter your Momento Token in the.envfile, as the value ofMOMENTO_AUTH_TOKEN(see example file below),in the.envfile setCACHE_TYPE=momento(instead ofCACHE_TYPE=rediswhich is the default).GitHubPersonal Access Token (required for apps that need to analyze git
repos; token-based API calls are less rate-limited). See thisGitHub page.Google Custom Search API Credentials:Only needed to enable an Agent to use theGoogleSearchTool.
To use Google Search as an LLM Tool/Plugin/function-call,
you'll need to set upa Google API key,
thensetup a Google Custom Search Engine (CSE) and get the CSE ID.
(Documentation for these can be challenging, we suggest asking GPT4 for a step-by-step guide.)
After obtaining these credentials, store them as values ofGOOGLE_API_KEYandGOOGLE_CSE_IDin your.envfile.
Full documentation on using this (and other such "stateless" tools) is coming soon, but
in the meantime take a peek at thischat example, which
shows how you can easily equip an Agent with aGoogleSearchtool.If you add all of these optional variables, your.envfile should look like this:OPENAI_API_KEY=your-key-here-without-quotesGITHUB_ACCESS_TOKEN=your-personal-access-token-no-quotesCACHE_TYPE=redis# or momentoREDIS_PASSWORD=your-redis-password-no-quotesREDIS_HOST=your-redis-hostname-no-quotesREDIS_PORT=your-redis-port-no-quotesMOMENTO_AUTH_TOKEN=your-momento-token-no-quotes# instead of REDIS* variablesQDRANT_API_KEY=your-keyQDRANT_API_URL=https://your.url.here:6333# note port number must be includedGOOGLE_API_KEY=your-keyGOOGLE_CSE_ID=your-cse-idOptional setup instructions for Microsoft Azure OpenAI(click to expand)When using Azure OpenAI, additional environment variables are required in the.envfile.
This pageMicrosoft Azure OpenAIprovides more information, and you can set each environment variable as follows:AZURE_OPENAI_API_KEY, from the value ofAPI_KEYAZURE_OPENAI_API_BASEfrom the value ofENDPOINT, typically looks likehttps://your.domain.azure.com.ForAZURE_OPENAI_API_VERSION, you can use the default value in.env-template, and latest version can be foundhereAZURE_OPENAI_DEPLOYMENT_NAMEis the name of the deployed model, which is defined by the user during the model setupAZURE_OPENAI_MODEL_NAMEAzure OpenAI allows specific model names when you select the model for your deployment. You need to put precisly the exact model name that was selected. For example, GPT-3.5 (should begpt-35-turbo-16korgpt-35-turbo) or GPT-4 (should begpt-4-32korgpt-4).AZURE_OPENAI_MODEL_VERSIONis required ifAZURE_OPENAI_MODEL_NAME = gpt=4, which will assist Langroid to determine the cost of the model:whale: Docker InstructionsWe provide a containerized version of thelangroid-examplesrepository via thisDocker Image.
All you need to do is set up environment variables in the.envfile.
Please follow these steps to setup the container:# get the .env file template from `langroid` repowget-O.envhttps://raw.githubusercontent.com/langroid/langroid/main/.env-template# Edit the .env file with your favorite editor (here nano), and remove any un-used settings. E.g. there are "dummy" values like "your-redis-port" etc -- if you are not using them, you MUST remove them.nano.env# launch the containerdockerrun-it--rm-v./.env:/langroid/.envlangroid/langroid# Use this command to run any of the scripts in the `examples` directorypythonexamples/<Path/To/Example.py>:tada: Usage ExamplesThese are quick teasers to give a glimpse of what you can do with Langroid
and how your code would look.:warning: The code snippets below are intended to give a flavor of the code
and they arenotcomplete runnable examples! For that we encourage you to
consult thelangroid-examplesrepository.:information_source: The various LLM prompts and instructions in Langroid
have been tested to work well with GPT4.
Switching to GPT3.5-Turbo is easy via a config flag
(e.g.,cfg = OpenAIGPTConfig(chat_model=OpenAIChatModel.GPT3_5_TURBO)),
and may suffice for some applications, but in general you may see inferior results.:book: Also see theGetting Started Guidefor a detailed tutorial.Click to expand any of the code examples below.
All of these can be run in a Colab notebook:Direct interaction with OpenAI LLMimportlangroid.language_modelsaslmmdl=lm.OpenAIGPT()messages=[lm.LLMMessage(content="You are a helpful assistant",role=lm.Role.SYSTEM),lm.LLMMessage(content="What is the capital of Ontario?",role=lm.Role.USER),]response=mdl.chat(messages,max_tokens=200)print(response.message)Interaction with non-OpenAI LLM (local or remote)Local model: if model is served at `http://localhost:8000`:cfg=lm.OpenAIGPTConfig(chat_model="local/localhost:8000",chat_context_length=4096)mdl=lm.OpenAIGPT(cfg)# now interact with it as above, or create an Agent + Task as shown below.If the model issupported byliteLLM,
then no need to launch the proxy server.
Just set thechat_modelparam above tolitellm/[provider]/[model], e.g.litellm/anthropic/claude-instant-1and use the config object as above.
Note that to uselitellmyou need to install langroid with thelitellmextra:poetry install -E litellmorpip install langroid[litellm].
For remote models, you will typically need to set API Keys etc as environment variables.
You can set those based on the LiteLLM docs.
If any required environment variables are missing, Langroid gives a helpful error
message indicating which ones are needed.
Note that to uselangroidwithlitellmyou need to install thelitellmextra, i.e. eitherpip install langroid[litellm]in your virtual env,
or if you are developing within thelangroidrepo,poetry install -E litellm.pipinstalllangroid[litellm]Define an agent, set up a task, and run itimportlangroidaslragent=lr.ChatAgent()# get response from agent's LLM, and put this in an interactive loop...# answer = agent.llm_response("What is the capital of Ontario?")# ... OR instead, set up a task (which has a built-in loop) and run ittask=lr.Task(agent,name="Bot")task.run()# ... a loop seeking response from LLM or User at each turnThree communicating agentsA toy numbers game, where when given a numbern:repeater_task's LLM simply returnsn,even_task's LLM returnsn/2ifnis even, else says "DO-NOT-KNOW"odd_task's LLM returns3*n+1ifnis odd, else says "DO-NOT-KNOW"Each of theseTasks automatically configures a defaultChatAgent.importlangroidaslrfromlangroid.utils.constantsimportNO_ANSWERrepeater_task=lr.Task(name="Repeater",system_message="""Your job is to repeat whatever number you receive.""",llm_delegate=True,# LLM takes charge of tasksingle_round=False,)even_task=lr.Task(name="EvenHandler",system_message=f"""You will be given a number.If it is even, divide by 2 and say the result, nothing else.If it is odd, say{NO_ANSWER}""",single_round=True,# task done after 1 step() with valid response)odd_task=lr.Task(name="OddHandler",system_message=f"""You will be given a number n.If it is odd, return (n*3+1), say nothing else.If it is even, say{NO_ANSWER}""",single_round=True,# task done after 1 step() with valid response)Then add theeven_taskandodd_taskas sub-tasks ofrepeater_task,
and run therepeater_task, kicking it off with a number as input:repeater_task.add_sub_task([even_task,odd_task])repeater_task.run("3")Simple Tool/Function-calling exampleLangroid leverages Pydantic to support OpenAI'sFunction-calling APIas well as its own native tools. The benefits are that you don't have to write
any JSON to specify the schema, and also if the LLM hallucinates a malformed
tool syntax, Langroid sends the Pydantic validation error (suitably sanitized)
to the LLM so it can fix it!Simple example: Say the agent has a secret list of numbers,
and we want the LLM to find the smallest number in the list.
We want to give the LLM aprobetool/function which takes a
single numbernas argument. The tool handler method in the agent
returns how many numbers in its list are at mostn.First define the tool using Langroid'sToolMessageclass:importlangroidaslrclassProbeTool(lr.agent.ToolMessage):request:str="probe"# specifies which agent method handles this toolpurpose:str="""To find how many numbers in my list are less than or equal tothe <number> you specify."""# description used to instruct the LLM on when/how to use the toolnumber:int# required argument to the toolThen define aSpyGameAgentas a subclass ofChatAgent,
with a methodprobethat handles this tool:classSpyGameAgent(lr.ChatAgent):def__init__(self,config:lr.ChatAgentConfig):super().__init__(config)self.numbers=[3,4,8,11,15,25,40,80,90]defprobe(self,msg:ProbeTool)->str:# return how many numbers in self.numbers are less or equal to msg.numberreturnstr(len([nforninself.numbersifn<=msg.number]))We then instantiate the agent and enable it to use and respond to the tool:spy_game_agent=SpyGameAgent(lr.ChatAgentConfig(name="Spy",vecdb=None,use_tools=False,#  don't use Langroid native tooluse_functions_api=True,# use OpenAI function-call API))spy_game_agent.enable_message(ProbeTool)For a full working example see thechat-agent-tool.pyscript in thelangroid-examplesrepo.Tool/Function-calling to extract structured information from textSuppose you want an agent to extract
the key terms of a lease, from a lease document, as a nested JSON structure.
First define the desired structure via Pydantic models:frompydanticimportBaseModelclassLeasePeriod(BaseModel):start_date:strend_date:strclassLeaseFinancials(BaseModel):monthly_rent:strdeposit:strclassLease(BaseModel):period:LeasePeriodfinancials:LeaseFinancialsaddress:strThen define theLeaseMessagetool as a subclass of Langroid'sToolMessage.
Note the tool has a required argumenttermsof typeLease:importlangroidaslrclassLeaseMessage(lr.agent.ToolMessage):request:str="lease_info"purpose:str="""Collect information about a Commercial Lease."""terms:LeaseThen define aLeaseExtractorAgentwith a methodlease_infothat handles this tool,
instantiate the agent, and enable it to use and respond to this tool:classLeaseExtractorAgent(lr.ChatAgent):deflease_info(self,message:LeaseMessage)->str:print(f"""DONE! Successfully extracted Lease Info:{message.terms}""")returnjson.dumps(message.terms.dict())lease_extractor_agent=LeaseExtractorAgent()lease_extractor_agent.enable_message(LeaseMessage)See thechat_multi_extract.pyscript in thelangroid-examplesrepo for a full working example.Chat with documents (file paths, URLs, etc)Langroid provides a specialized agent classDocChatAgentfor this purpose.
It incorporates document sharding, embedding, storage in a vector-DB,
and retrieval-augmented query-answer generation.
Using this class to chat with a collection of documents is easy.
First create aDocChatAgentConfiginstance, with adoc_pathsfield that specifies the documents to chat with.importlangroidaslrfromlangroid.agent.specialimportDocChatAgentConfig,DocChatAgentconfig=DocChatAgentConfig(doc_paths=["https://en.wikipedia.org/wiki/Language_model","https://en.wikipedia.org/wiki/N-gram_language_model","/path/to/my/notes-on-language-models.txt",],vecdb=lr.vector_store.LanceDBConfig(),)Then instantiate theDocChatAgent(this ingests the docs into the vector-store):agent=DocChatAgent(config)Then we can either ask the agent one-off questions,agent.llm_response("What is a language model?")or wrap it in aTaskand run an interactive loop with the user:task=lr.Task(agent)task.run()See full working scripts in thedocqafolder of thelangroid-examplesrepo.:fire: Chat with tabular data (file paths, URLs, dataframes)Using Langroid you can set up aTableChatAgentwith a dataset (file path, URL or dataframe),
and query it. The Agent's LLM generates Pandas code to answer the query,
via function-calling (or tool/plugin), and the Agent's function-handling method
executes the code and returns the answer.Here is how you can do this:importlangroidaslrfromlangroid.agent.specialimportTableChatAgent,TableChatAgentConfigSet up aTableChatAgentfor a data file, URL or dataframe
(Ensure the data table has a header row; the delimiter/separator is auto-detected):dataset="https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv"# or dataset = "/path/to/my/data.csv"# or dataset = pd.read_csv("/path/to/my/data.csv")agent=TableChatAgent(config=TableChatAgentConfig(data=dataset,))Set up a task, and ask one-off questions like this:task=lr.Task(agent,name="DataAssistant",default_human_response="",# to avoid waiting for user input)result=task.run("What is the average alcohol content of wines with a quality rating above 7?",turns=2# return after user question, LLM fun-call/tool response, Agent code-exec result)print(result.content)Or alternatively, set up a task and run it in an interactive loop with the user:task=lr.Task(agent,name="DataAssistant")task.run()For a full working example see thetable_chat.pyscript in thelangroid-examplesrepo.:heart: Thank you to oursupportersIf you like this project, please give it a star ⭐ and 📢 spread the word in your network or social media:Your support will help build Langroid's momentum and community.Langroid Co-FoundersPrasad Chalasani(IIT BTech/CS, CMU PhD/ML; Independent ML Consultant)Somesh Jha(IIT BTech/CS, CMU PhD/CS; Professor of CS, U Wisc at Madison)