Skip to content

Commit 63331a0

Browse files
committed
added langroid instructions and script
1 parent 15cab4b commit 63331a0

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

langroid-llm-agents/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
### Langroid + Groq
2+
[Langroid](https://github.com/langroid/langroid) is an
3+
open-source Python framework that simplifies building LLM applications,
4+
using a Multi-Agent paradigm.
5+
6+
You can now use Langroid with Groq, by setting the model name to
7+
`groq/<model>`, e.g., `groq/llama3-70b-8192`.
8+
9+
To get started, create a virtual environment using Python 3.11:
10+
11+
12+
```bash
13+
python3 -m venv .venv
14+
. ./.venv/bin/activate
15+
pip install --upgrade langroid
16+
````
17+
Place your `GROQ_API_KEY` in a `.env` file in the root directory of this project,
18+
it should have a line that looks like:
19+
```
20+
GROQ_API_KEY=gsk_...
21+
```
22+
Or you can set explicitly set this key in your environment before
23+
running the scripts.
24+
25+
```bash
26+
Below is a bare-bones code example.
27+
28+
```python
29+
import langroid as lr
30+
import langroid.language_models as lm
31+
32+
llm_config = lm.OpenAIGPTConfig(
33+
chat_model="groq/llama3-70b-8192",
34+
chat_context_length=8192,
35+
)
36+
37+
llm = lm.OpenAIGPT(llm_config)
38+
39+
llm.chat("3+4=?").message
40+
41+
agent_config = lr.ChatAgentConfig(
42+
llm=llm_config,
43+
system_message="""Be nice but concise""",
44+
)
45+
46+
agent = lr.ChatAgent(agent_config)
47+
task = lr.Task(agent, interactive=True)
48+
```
49+
50+
An example script showing a 2-agent assistant is included in this folder.
51+
The langroid repo has numerous example scripts as well, and you can run them
52+
against a groq-hosted LLM by changing `chat_model` to `groq/llama3-70b-8192` (as an
53+
example). Many of the scripts also take a command-line argument `-m` to specify the
54+
model, e.g. `-m groq/llama3-70b-8192`.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
"""
2+
2-Agent system where:
3+
- Assistant takes user's (complex) question, breaks it down into smaller pieces
4+
if needed
5+
- WebSearcher takes Assistant's question, uses the Search tool to search the web
6+
(using DuckDuckGo), and returns a coherent answer to the Assistant.
7+
8+
Once the Assistant thinks it has enough info to answer the user's question, it
9+
says DONE and presents the answer to the user.
10+
11+
Run like this:
12+
13+
python3 search-assistant.py
14+
"""
15+
16+
from dotenv import load_dotenv
17+
from rich import print
18+
from rich.prompt import Prompt
19+
from fire import Fire
20+
import langroid as lr
21+
import langroid.language_models as lm
22+
from langroid.agent.tools.duckduckgo_search_tool import DuckduckgoSearchTool
23+
24+
MODEL = "groq/llama3-70b-8192"
25+
26+
def main() -> None:
27+
print(
28+
"""
29+
[blue]Welcome to the Web Search Assistant chatbot!
30+
I will try to answer your complex questions.
31+
32+
Enter x or q to quit at any point.
33+
"""
34+
)
35+
load_dotenv()
36+
37+
llm_config = lm.OpenAIGPTConfig(
38+
chat_model=MODEL, # or lr.OpenAIChatModel.GPT4_TURBO
39+
chat_context_length=8192,
40+
temperature=0,
41+
max_output_tokens=200,
42+
timeout=45,
43+
)
44+
45+
assistant_config = lr.ChatAgentConfig(
46+
system_message="""
47+
You are a resourceful assistant, able to think step by step to answer
48+
complex questions from the user. You must break down complex questions into
49+
simpler questions that can be answered by a web search. You must ask me
50+
(the user) each question ONE BY ONE, and I will do a web search and send you
51+
a brief answer. Once you have enough information to answer my original
52+
(complex) question, you MUST say DONE and present the answer to me.
53+
""",
54+
llm=llm_config,
55+
vecdb=None,
56+
)
57+
assistant_agent = lr.ChatAgent(assistant_config)
58+
search_tool_handler_method = DuckduckgoSearchTool.default_value("request")
59+
60+
search_agent_config = lr.ChatAgentConfig(
61+
llm=llm_config,
62+
vecdb=None,
63+
system_message=f"""
64+
You are a web-searcher. For any question you get, you must use the
65+
`{search_tool_handler_method}` tool/function-call to get up to 5 results.
66+
I WILL SEND YOU THE RESULTS; DO NOT MAKE UP THE RESULTS!!
67+
Once you receive the results, you must compose a CONCISE answer
68+
based on the search results and say DONE and show the answer to me,
69+
in this format:
70+
DONE [... your CONCISE answer here ...]
71+
IMPORTANT: YOU MUST WAIT FOR ME TO SEND YOU THE
72+
SEARCH RESULTS BEFORE saying you're DONE.
73+
""",
74+
)
75+
search_agent = lr.ChatAgent(search_agent_config)
76+
search_agent.enable_message(DuckduckgoSearchTool)
77+
78+
assistant_task = lr.Task(
79+
assistant_agent,
80+
name="Assistant",
81+
llm_delegate=True,
82+
single_round=False,
83+
interactive=False,
84+
)
85+
search_task = lr.Task(
86+
search_agent,
87+
name="Searcher",
88+
llm_delegate=True,
89+
single_round=False,
90+
interactive=False,
91+
)
92+
assistant_task.add_sub_task(search_task)
93+
question = Prompt.ask("What do you want to know?")
94+
assistant_task.run(question)
95+
96+
97+
if __name__ == "__main__":
98+
Fire(main)

0 commit comments

Comments
 (0)