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
51
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 = {}
    self.window = memory
    self.template = template

__call__(text, maxlength=8192, stream=False, session=None, 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
session

session id for stored memory, defaults to None which shares all memory

None
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
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
def __call__(self, text, maxlength=8192, stream=False, session=None, reset=False, **kwargs):
    """
    Runs an agent loop.

    Args:
        text: instructions to run
        maxlength: maximum sequence length
        stream: stream response if True, defaults to False
        session: session id for stored memory, defaults to None which shares all memory
        reset: clears previously stored memory if True, defaults to False
        kwargs: additional keyword arguments

    Returns:
        result
    """

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

    # Create memory, if necessary
    if self.window and (session not in self.memory or reset):
        self.memory[session] = deque(maxlen=self.window)

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

    # Add output to memory, if necessary
    if session in self.memory:
        self.memory[session].append((text, output))

    return output