-
Notifications
You must be signed in to change notification settings - Fork 30
/
chat-agent-docs.py
101 lines (79 loc) · 2.81 KB
/
chat-agent-docs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""
Example of a Langroid DocChatAgent equipped with a vector-store and LLM.
This is a specialized agent that can ingest (chunk, embed, store in vector-DB)
a collection of documents, and the LLM uses Retrieval Augmented Generation (RAG)
to answer questions about the documents.
Run as follows:
python3 examples/quick-start/chat-agent-docs.py
For more explanation see
[the Getting Started guide](https://langroid.github.io/langroid/quick-start/chat-agent-docs/).
"""
import typer
from rich import print
import langroid as lr
app = typer.Typer()
lr.utils.logging.setup_colored_logging()
documents = [
lr.mytypes.Document(
content="""
In the year 2050, GPT10 was released.
In 2057, paperclips were seen all over the world.
Global warming was solved in 2060.
In 2061, the world was taken over by paperclips.
In 2045, the Tour de France was still going on.
They were still using bicycles.
There was one more ice age in 2040.
""",
metadata=lr.mytypes.DocMetaData(source="wikipedia-2063"),
),
lr.mytypes.Document(
content="""
We are living in an alternate universe
where Germany has occupied the USA, and the capital of USA is Berlin.
Charlie Chaplin was a great comedian.
In 2050, all Asian merged into Indonesia.
""",
metadata=lr.mytypes.DocMetaData(source="Almanac"),
),
]
def chat() -> None:
print(
"""
[blue]Welcome to the retrieval-augmented chatbot!
Enter x or q to quit
"""
)
config = lr.agent.special.DocChatAgentConfig(
llm=lr.language_models.OpenAIGPTConfig(
chat_model=lr.language_models.OpenAIChatModel.GPT4,
),
vecdb=lr.vector_store.QdrantDBConfig(
collection_name="quick-start-chat-agent-docs",
replace_collection=True,
),
parsing=lr.parsing.parser.ParsingConfig(
separators=["\n\n"],
splitter=lr.parsing.parser.Splitter.SIMPLE,
n_similar_docs=2,
),
)
agent = lr.agent.special.DocChatAgent(config)
agent.ingest_docs(documents)
task = lr.Task(agent)
task.run()
@app.command()
def main(
debug: bool = typer.Option(False, "--debug", "-d", help="debug mode"),
no_stream: bool = typer.Option(False, "--nostream", "-ns", help="no streaming"),
nocache: bool = typer.Option(False, "--nocache", "-nc", help="don't use cache"),
) -> None:
lr.utils.configuration.set_global(
lr.utils.configuration.Settings(
debug=debug,
cache=not nocache,
stream=not no_stream,
)
)
chat()
if __name__ == "__main__":
app()