Skip to content

Chat

Interactive chat interface for a RAG-powered language model.

chat(project_name, llm_provider, llm, llm_temperature, llm_top_p, llm_top_k, embeddings_provider, embedding_model, chunk_size, chunk_overlap, search_type, k_docs)

Start an interactive chat session using a RAG pipeline.

This function initializes the RAG components with the given parameters and launches a loop that allows users to send queries to the model. It supports simple commands for help (/?), clearing history (/clear), and exiting (/bye).

Parameters:

Name Type Description Default
project_name str

The name of the LangChain project.

required
llm_provider str

The LLM provider (e.g., "google", "ollama", "hf").

required
llm str

The LLM model identifier.

required
llm_temperature float

Sampling temperature for the LLM.

required
llm_top_p float

Top-p nucleus sampling parameter.

required
llm_top_k int

Top-k sampling parameter.

required
embeddings_provider str

The provider for the embeddings model.

required
embedding_model str

The embeddings model identifier.

required
chunk_size int

Size of each document chunk for retrieval.

required
chunk_overlap int

Number of overlapping tokens between chunks.

required
search_type str

The type of retrieval search to use.

required
k_docs int

Number of top documents to retrieve for each query.

required
Source code in ragbot\chat.py
 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
def chat(
    project_name: str,
    llm_provider: str,
    llm: str,
    llm_temperature: float,
    llm_top_p: float,
    llm_top_k: int,
    embeddings_provider: str,
    embedding_model: str,
    chunk_size: int,
    chunk_overlap: int,
    search_type: str,
    k_docs: int,
):
    """Start an interactive chat session using a RAG pipeline.

    This function initializes the RAG components with the given parameters and launches
    a loop that allows users to send queries to the model. It supports simple commands
    for help (`/?`), clearing history (`/clear`), and exiting (`/bye`).

    Args:
        project_name: The name of the LangChain project.
        llm_provider: The LLM provider (e.g., "google", "ollama", "hf").
        llm: The LLM model identifier.
        llm_temperature: Sampling temperature for the LLM.
        llm_top_p: Top-p nucleus sampling parameter.
        llm_top_k: Top-k sampling parameter.
        embeddings_provider: The provider for the embeddings model.
        embedding_model: The embeddings model identifier.
        chunk_size: Size of each document chunk for retrieval.
        chunk_overlap: Number of overlapping tokens between chunks.
        search_type: The type of retrieval search to use.
        k_docs: Number of top documents to retrieve for each query.
    """
    qa = setup(
        project_name=project_name,
        llm_provider=llm_provider,
        llm=llm,
        llm_temperature=llm_temperature,
        llm_top_p=llm_top_p,
        llm_top_k=llm_top_k,
        embeddings_provider=embeddings_provider,
        embedding_model=embedding_model,
        chunk_size=chunk_size,
        chunk_overlap=chunk_overlap,
        search_type=search_type,
        k_docs=k_docs,
    )

    history = []
    while True:
        query = input(">>> ")
        if query == "/?":
            print("Type /? to show this help")
            print("Type /bye to exit.")
            print("Type /clear to clear the chat history.")
            continue
        if query == "/clear":
            history = []
            continue
        if query.lower() == "/bye":
            print("Session closed!")
            exit(0)
        response = qa.invoke({"history": history, "input": query})
        history.extend([("human", query), ("ai", response["answer"])])
        display_markdown(response["answer"])