The Context
class in the Delphi framework is designed to manage the state of a
conversation, including messages and functions. This API reference details its
usage and capabilities.
BuiltContext
A representation of a context prepared for use with the OpenAI API.
Property | Type | Description |
---|---|---|
messages |
ChatMessage[] |
Array of chat messages in the context. |
functions |
FunctionMap |
Map of functions in the context. |
ContextOptions
Configuration options for a Context
. Both properties are optional.
Property | Type | Description |
---|---|---|
messages |
ChatMessage[] |
Initial array of chat messages (default: []). |
functions |
Iterable<AgentFunction<any, any>> |
Iterable of functions to add. |
Context
This class stores and manages messages and functions for a conversational agent.
constructor(options: ContextOptions)
Initializes a new instance of Context
.
Parameter | Type | Description |
---|---|---|
options |
ContextOptions |
Configuration options of the context. |
Property | Type | Description | ——– | ————— | |
———————————- | messages |
ChatMessage[] |
Array of | ||
chat messages in the context. | functions |
FunctionMap |
Map of functions | ||
in the context. |
addMessage
Adds a message to the context.
addMessage(message: ChatMessage): void
message: ChatMessage
- The message to add.addFunction
Adds a function to the context and optionally enables it.
addFunction<Input, Output>(fn: AgentFunction<Input, Output>, enable = true): void
fn: AgentFunction<Input, Output>
- The function to add.enable: boolean
- Whether to enable the function (default: true
).Error
if a function with the same name already exists.build
Compiles the context into a BuiltContext
format for use with the agent.
build(): BuiltContext
Returns: BuiltContext
- The built context.
duplicate
Creates a duplicate of the context.
duplicate(): Context
Returns: Context
- A new instance of Context
with duplicated messages and
functions.
const myContext = new Context();
myContext.addMessage({ role: "user", content: "Hello, Delphi!" });
const myFunction = new AgentFunction(/* ... */);
myContext.addFunction(myFunction);
const builtContext = myContext.build();
In this example, myContext
is a Context
instance where a message is added,
followed by a custom function. The build
method is then used to prepare this
context for interaction with the agent.