import "github.com/AstraBert/gopheract"
func OpenAILLMStructuredPredict[T any](llm *OpenAILLM, chatHistory any, schemaName, schemaDescription string) (any, error)
Implementation of the structured generation function for an OpenAILLM, given the LLM itself, the chat history and the name and the description of the JSON schema used for structured generation
Struct type representing the action part of a ReAct Agent
The agent can take two type of actions: (1) `_done`, in which case the Action payload will have a non-null `StopReason` field; (2) `tool_call`, in which case the Action payload will have a non-null `ToolCall` field
type Action struct {
ActionType string `json:"type" jsonschema:"enum=_done,enum=tool_call" jsonschema_description:"Type of the action to perform based on the chat history. Use '_done' if you think the conversation should stop, and 'tool_call' if you want to call a tool"`
StopReason *StopReason `json:"stop_reason" jsonschema_description:"Reason why the conversation should stop. Only present when type is '_done'"`
ToolCall *ToolCall `json:"tool_call" jsonschema_description:"Tool to call with its arguments. Only present when type is 'tool_call'"`
}
Helper struct type to represent a message within the chat history
type ChatMessage struct {
Role string `json:"role"`
Content string `json:"content"`
}
func NewChatMessage(role, content string) *ChatMessage
Constructor function for a new chat message
Base LLM interface
type LLM interface {
StructuredChat(any, any) (string, error)
}
Struct type representing the observation part of the ReAct agent
type Observation struct {
Observation string `json:"observation" jsonschema_description:"Observation about the current state of things, based on the chat history"`
}
Implementation of LLM for OpenAI
type OpenAILLM struct {
// The OpenAI model to use
Model openai.ChatModel
// OpenAI API client
Client *openai.Client
}
func NewOpenAILLM(apiKey, model string) *OpenAILLM
Constructor function for a new OpenAILLM (provide an API key and the model identifier)
func (o *OpenAILLM) StructuredChat(chatHistory any, responseFormat any) (string, error)
Produce a structured response, given a response format (struct type) and a chat history.
Since this implementation is for the OpenAILLM, the chat history is validate as a list of OpenAI chat messages
Struct type that implements the ReActAgent interface for OpenAI
type OpenAIReActAgent struct {
Llm *OpenAILLM
ChatHistory []*ChatMessage
SystemPromptTemplate *template.Template
Tools []Tool
}
func NewDefaultOpenAIReactAgent(apiKey, model string, tools []Tool) (*OpenAIReActAgent, error)
Constructor for an OpenAIReactAgent starting based on defaults for the system prompt template and the chat history. Takes, as arguments, an OpenAI API key, an OpenAI model identifier and a list of tool defitions.
func (o *OpenAIReActAgent) Act() (*Action, error)
Method that implements the action part of the ReAct agent process, leveraging the `Action` struct type for structured generation of an action-oriented response based on the previous chat history.
func (o *OpenAIReActAgent) BuildChatHistory() any
Helper method that converts the chat history of the OpenAIReActAgent (slice of ChatMessage) into valid message types for the OpenAI SDK.
func (o *OpenAIReActAgent) BuildSystemPrompt() (*ChatMessage, error)
Helper method that builds the system prompt from the base template provided when defininig the OpenAIReactAgent.
This methods loads the tool name, description and parameters into the system prompt as a clean markdown table, returning the system prompt as a ChatMessage.
func (o *OpenAIReActAgent) Observe() (string, error)
Method that implements the observation part of the ReAct agent process, leveraging the `Observation` struct type for structured generation of an observational response based on the previous chat history.
func (o *OpenAIReActAgent) Run(prompt string, thoughtCallback func(string), actionCallback func(Action), toolEndCallback func(any), observationCallback func(string), stopCallback func(string)) error
Method that implements the Think -> Act -> Observe loop for a ReActAgent.
Apart from the user prompt, this method also needs callback functions to communicate the execution of the loop steps (thoughts, actions, observations, tool call results and stopping) to the external environment.
func (o *OpenAIReActAgent) Think() (string, error)
Method that implements the thinking part of the ReAct agent process, leveraging the `Thought` struct type for structured generation of a thinking response based on the previous chat history.
Base interface for the ReactAgent
type ReActAgent interface {
BuildChatHistory() any
BuildSystemPrompt() (*ChatMessage, error)
Think() (string, error)
Act() (*Action, error)
Observe() (string, error)
Run(string, func(string), func(Action), func(any), func(string), func(string)) error
}
Struct type representing the reason why the agent terminated its loop
type StopReason struct {
Reason string `json:"reason" jsonschema_description:"Reason why the conversation should stop"`
}
Struct type representing the thinking part of the ReAct agent
type Thought struct {
Thought string `json:"thought" jsonschema_description:"Thought about the path forward, based on the chat history"`
}
Base interface that a tool definition should implement
type Tool interface {
GetMetadata() ToolMetadata
Execute(map[string]any) (any, error)
}
Struct type representint a tool call
type ToolCall struct {
Name string `json:"name" jsonschema_description:"Name of the tools to call"`
Args []ToolCallArgs `json:"args" jsonschema_description:"Tool call arguments"`
}
func (t *ToolCall) ArgsToMap() (map[string]any, error)
Helper method to convert the arguments of a ToolCall (a slice of `ToolCallArgs`) to a map
Struct type representing the arguments of a tool call.
Given typing constraints, the `ParameterValue` field is a string meant to represent serialized JSON data
type ToolCallArgs struct {
ParameterValue string `json:"parameter_value" jsonschema_description:"Parameter name and value of the parameter as a JSON string (e.g. '{'age': 40, 'name': 'John Doe'}')"`
}
Struct type representing a tool defintion that implements the `Tool` interface.
The generic type T indicates the struct type representing the parameters of the tool function.
A good practice for `ToolDefition` is to define the Name and the Description field as in detail and as explicitly as possibile.
type ToolDefinition[T any] struct {
Fn func(T) (any, error)
Name string
Description string
}
func (t ToolDefinition[T]) Execute(params map[string]any) (any, error)
Method to execute the tool given the parameters received from the `ToolCall` action field.
Thie method executes the following logic: (1) convers the parameters (passed as a map) to the original struct type for the tool defition (conversion happens based on the `json` tag); (2) calls the tool function with the converted parameters, returning its result.
func (t ToolDefinition[T]) GetMetadata() ToolMetadata
Helper method to get the metadata from the tool definition.
Type struct representing metadata related to a tool defintion
type ToolMetadata struct {
Name string
Description string
ParametersMetadata []ToolParamsMetadata
}
Struct type representing metadata for tool parameters, used when passing the tool defintion to the agent's system prompt.
type ToolParamsMetadata struct {
JsonDef string
Description string
Type string
}
func (tp *ToolParamsMetadata) ToString() string
Helper method to convert the `ToolParamsMetada` into a string
import "github.com/AstraBert/gopheract/cli"
func ContentBlocksToString(blocks []acp.ContentBlock) (string, error)
func GetTools() []gopheract.Tool
func RandomID() string
func RunACP(agent gopheract.OpenAIReActAgent)
func RunPrint(agent gopheract.OpenAIReActAgent, prompt string)
type AgentSession struct {
// contains filtered or unexported fields
}
type BashParams struct {
Command string `json:"command" description:"Main bash command to execute"`
Arguments []string `json:"arguments" description:"Arguments for the bash command"`
}
type CliAgent struct {
// contains filtered or unexported fields
}
func NewCliAgent(agent gopheract.OpenAIReActAgent) *CliAgent
func (a *CliAgent) Authenticate(ctx context.Context, _ acp.AuthenticateRequest) (acp.AuthenticateResponse, error)
func (a *CliAgent) Cancel(ctx context.Context, params acp.CancelNotification) error
func (a *CliAgent) Initialize(ctx context.Context, params acp.InitializeRequest) (acp.InitializeResponse, error)
func (a *CliAgent) LoadSession(ctx context.Context, _ acp.LoadSessionRequest) (acp.LoadSessionResponse, error)
func (a *CliAgent) NewSession(ctx context.Context, params acp.NewSessionRequest) (acp.NewSessionResponse, error)
func (a *CliAgent) Prompt(_ context.Context, params acp.PromptRequest) (acp.PromptResponse, error)
func (a *CliAgent) SetAgentConnection(conn *acp.AgentSideConnection)
Implement acp.AgentConnAware to receive the connection after construction.
func (a *CliAgent) SetSessionMode(ctx context.Context, params acp.SetSessionModeRequest) (acp.SetSessionModeResponse, error)
SetSessionMode implements acp.Agent.
func (a *CliAgent) SetSessionModel(ctx context.Context, params acp.SetSessionModelRequest) (acp.SetSessionModelResponse, error)
SetSessionModel implements acp.AgentExperimental.
type EditParams struct {
FilePath string `json:"file_path" description:"Path to the file to edit"`
OldString string `json:"old_string" description:"String to be replaced"`
NewString string `json:"new_string" description:"String to replace with"`
Count int `json:"count" description:"Number of replacements to make"`
}
type ReadParams struct {
FilePath string `json:"file_path" description:"Path to the file to read"`
}
type WriteParams struct {
FilePath string `json:"file_path" description:"Path to the file to write"`
Content string `json:"content" description:"Content to write to the file"`
}
Generated by gomarkdoc