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 TrafilaturaConfig , FirecrawlConfig
4448
4549
4650class RelevantExtractsTool (ToolMessage ):
@@ -85,6 +89,24 @@ def instructions(cls) -> str:
8589
8690class SearchDocChatAgent (DocChatAgent ):
8791 tried_vecdb : bool = False
92+ crawler : Optional [str ] = None
93+
94+ def __init__ (self , config : DocChatAgentConfig , crawler : Optional [str ] = None ):
95+ super ().__init__ (config )
96+ self .tried_vecdb = False
97+ self .crawler = crawler
98+ self .update_crawler_config (crawler )
99+
100+ def update_crawler_config (self , crawler : Optional [str ]):
101+ """Updates the crawler config based on the crawler argument."""
102+ if crawler == "firecrawl" :
103+ self .config .crawler_config = FirecrawlConfig ()
104+ elif crawler == "trafilatura" or crawler is None :
105+ self .config .crawler_config = TrafilaturaConfig ()
106+ else :
107+ raise ValueError (
108+ f"Unsupported crawler { crawler } . Options are: 'trafilatura', 'firecrawl'"
109+ )
88110
89111 def llm_response (
90112 self ,
@@ -127,12 +149,32 @@ def cli():
127149 Fire (main )
128150
129151
152+ app = typer .Typer ()
153+
154+
155+ @app .command ()
130156def main (
131157 debug : bool = False ,
132158 nocache : bool = False ,
133159 model : str = "" ,
134160 fn_api : bool = True ,
161+ crawler : Optional [str ] = typer .Option (
162+ None ,
163+ "--crawler" ,
164+ "-c" ,
165+ help = "Specify a crawler to use (trafilatura, firecrawl)" ,
166+ ),
135167) -> None :
168+ """
169+ Main function to run the chatbot.
170+
171+ Args:
172+ debug (bool): Enable debug mode.
173+ nocache (bool): Disable caching.
174+ model (str): Specify the LLM model to use.
175+ fn_api (bool): Use OpenAI functions API instead of tools.
176+ crawler (str): Specify the crawler to use for web search.
177+ """
136178
137179 set_global (
138180 Settings (
@@ -169,7 +211,7 @@ def main(
169211 # "ollama/llama2"
170212 # "local/localhost:8000/v1"
171213 # "local/localhost:8000"
172- chat_context_length = 2048 , # adjust based on model
214+ chat_context_length = 8000 , # adjust based on model
173215 )
174216
175217 config = DocChatAgentConfig (
@@ -188,7 +230,7 @@ def main(
188230 3. If you are still unable to answer, you can use the `relevant_search_extracts`
189231 tool/function-call to get some text from a web search. Once you receive the
190232 text, you can use it to answer my question.
191- 4 . If you still can't answer, simply say { NO_ANSWER }
233+ 5 . If you still can't answer, simply say { NO_ANSWER }
192234
193235 Remember to always FIRST try `relevant_extracts` to see if there are already
194236 any relevant docs, before trying web-search with `relevant_search_extracts`.
@@ -204,7 +246,7 @@ def main(
204246 """ ,
205247 )
206248
207- agent = SearchDocChatAgent (config )
249+ agent = SearchDocChatAgent (config , crawler = crawler )
208250 agent .enable_message (RelevantExtractsTool )
209251 agent .enable_message (RelevantSearchExtractsTool )
210252 collection_name = Prompt .ask (
@@ -225,8 +267,10 @@ def main(
225267 agent .vecdb .set_collection (collection_name , replace = replace )
226268
227269 task = Task (agent , interactive = False )
228- task .run ("Can you help me answer some questions, possibly using web search?" )
270+ task .run (
271+ "Can you help me answer some questions, possibly using web search and crawling?"
272+ )
229273
230274
231275if __name__ == "__main__" :
232- Fire ( main )
276+ app ( )
0 commit comments