1616 -f: use OpenAI functions api instead of tools
1717 -m <model_name>: run with a specific LLM
1818 (defaults to GPT4-Turbo if blank)
19+ -c <crawler_name>: specify a crawler to use for web search. Options are:
20+ "trafilatura" (default), "firecrawl"
1921
2022See here for guide to using local LLMs with Langroid:
2123https://langroid.github.io/langroid/tutorials/local-llm-setup/
2224"""
2325
26+ import typer
2427import re
25- from typing import List , Any
28+ from typing import List , Any , Optional
2629
2730from rich import print
2831from rich .prompt import Prompt
4144from langroid .utils .constants import NO_ANSWER
4245from langroid .utils .configuration import set_global , Settings
4346from fire import Fire
47+ from langroid .parsing .url_loader import (
48+ TrafilaturaConfig ,
49+ FirecrawlConfig ,
50+ ExaCrawlerConfig ,
51+ )
4452
4553
4654class RelevantExtractsTool (ToolMessage ):
@@ -85,6 +93,26 @@ def instructions(cls) -> str:
8593
8694class SearchDocChatAgent (DocChatAgent ):
8795 tried_vecdb : bool = False
96+ crawler : Optional [str ] = None
97+
98+ def __init__ (self , config : DocChatAgentConfig , crawler : Optional [str ] = None ):
99+ super ().__init__ (config )
100+ self .tried_vecdb = False
101+ self .crawler = crawler
102+ self .update_crawler_config (crawler )
103+
104+ def update_crawler_config (self , crawler : Optional [str ]):
105+ """Updates the crawler config based on the crawler argument."""
106+ if crawler == "firecrawl" :
107+ self .config .crawler_config = FirecrawlConfig ()
108+ elif crawler == "trafilatura" or crawler is None :
109+ self .config .crawler_config = TrafilaturaConfig ()
110+ elif crawler == "exa" :
111+ self .config .crawler_config = ExaCrawlerConfig ()
112+ else :
113+ raise ValueError (
114+ f"Unsupported crawler { crawler } . Options are: 'trafilatura', 'firecrawl'"
115+ )
88116
89117 def llm_response (
90118 self ,
@@ -127,12 +155,32 @@ def cli():
127155 Fire (main )
128156
129157
158+ app = typer .Typer ()
159+
160+
161+ @app .command ()
130162def main (
131163 debug : bool = False ,
132164 nocache : bool = False ,
133165 model : str = "" ,
134166 fn_api : bool = True ,
167+ crawler : Optional [str ] = typer .Option (
168+ None ,
169+ "--crawler" ,
170+ "-c" ,
171+ help = "Specify a crawler to use (trafilatura, firecrawl)" ,
172+ ),
135173) -> None :
174+ """
175+ Main function to run the chatbot.
176+
177+ Args:
178+ debug (bool): Enable debug mode.
179+ nocache (bool): Disable caching.
180+ model (str): Specify the LLM model to use.
181+ fn_api (bool): Use OpenAI functions API instead of tools.
182+ crawler (str): Specify the crawler to use for web search.
183+ """
136184
137185 set_global (
138186 Settings (
@@ -169,7 +217,7 @@ def main(
169217 # "ollama/llama2"
170218 # "local/localhost:8000/v1"
171219 # "local/localhost:8000"
172- chat_context_length = 2048 , # adjust based on model
220+ chat_context_length = 8000 , # adjust based on model
173221 )
174222
175223 config = DocChatAgentConfig (
@@ -188,7 +236,7 @@ def main(
188236 3. If you are still unable to answer, you can use the `relevant_search_extracts`
189237 tool/function-call to get some text from a web search. Once you receive the
190238 text, you can use it to answer my question.
191- 4 . If you still can't answer, simply say { NO_ANSWER }
239+ 5 . If you still can't answer, simply say { NO_ANSWER }
192240
193241 Remember to always FIRST try `relevant_extracts` to see if there are already
194242 any relevant docs, before trying web-search with `relevant_search_extracts`.
@@ -204,7 +252,7 @@ def main(
204252 """ ,
205253 )
206254
207- agent = SearchDocChatAgent (config )
255+ agent = SearchDocChatAgent (config , crawler = crawler )
208256 agent .enable_message (RelevantExtractsTool )
209257 agent .enable_message (RelevantSearchExtractsTool )
210258 collection_name = Prompt .ask (
@@ -225,8 +273,10 @@ def main(
225273 agent .vecdb .set_collection (collection_name , replace = replace )
226274
227275 task = Task (agent , interactive = False )
228- task .run ("Can you help me answer some questions, possibly using web search?" )
276+ task .run (
277+ "Can you help me answer some questions, possibly using web search and crawling?"
278+ )
229279
230280
231281if __name__ == "__main__" :
232- Fire ( main )
282+ app ( )
0 commit comments