Skip to content

Methods

__init__(template=None, memory=None, **kwargs)

Creates a new Agent.

Parameters:

Name Type Description Default
template

optional prompt jinja template, must include {{ text }} and {{ memory }} placeholders

None
memory

number of prior outputs to keep as "memory", defaults to None for no memory

None
kwargs

arguments to pass to the underlying Agent backend and LLM pipeline instance

{}
Source code in txtai/agent/base.py
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
def __init__(self, template=None, memory=None, **kwargs):
    """
    Creates a new Agent.

    Args:
        template: optional prompt jinja template, must include {{ text }} and {{ memory }} placeholders
        memory: number of prior outputs to keep as "memory", defaults to None for no memory
        kwargs: arguments to pass to the underlying Agent backend and LLM pipeline instance
    """

    # Ensure backwards compatibility
    if "max_iterations" in kwargs:
        kwargs["max_steps"] = kwargs.pop("max_iterations")

    # Custom instructions
    if "instructions" in kwargs:
        kwargs["instructions"] = self.instructions(kwargs)

    # Create agent process runner
    self.process = ProcessFactory.create(kwargs)

    # Tools dictionary
    self.tools = self.process.tools

    # Agent memory
    self.memory = deque(maxlen=memory) if memory else None
    self.template = template

__call__(text, maxlength=8192, stream=False, reset=False, **kwargs)

Runs an agent loop.

Parameters:

Name Type Description Default
text

instructions to run

required
maxlength

maximum sequence length

8192
stream

stream response if True, defaults to False

False
reset

clears previously stored memory if True, defaults to False

False
kwargs

additional keyword arguments

{}

Returns:

Type Description

result

Source code in txtai/agent/base.py
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
def __call__(self, text, maxlength=8192, stream=False, reset=False, **kwargs):
    """
    Runs an agent loop.

    Args:
        text: instructions to run
        maxlength: maximum sequence length
        stream: stream response if True, defaults to False
        reset: clears previously stored memory if True, defaults to False
        kwargs: additional keyword arguments

    Returns:
        result
    """

    # Process parameters
    self.process.model.parameters(maxlength)

    # Clear memory, if reset flag set
    if reset and self.memory:
        self.memory.clear()

    # Run agent loop
    output = self.process.run(self.prompt(text), stream=stream, **kwargs)

    # Add output to memory, if necessary
    if self.memory is not None:
        self.memory.append((text, output))

    return output