Documentation
¶
Index ¶
- func ToolCallsToJSONString(tools []openai.ChatCompletionMessageToolCall) (string, error)
- type Agent
- func (agent *Agent) ChatCompletion() (string, error)
- func (agent *Agent) ChatCompletionStream(callBack func(self *Agent, content string, err error) error) (string, error)
- func (agent *Agent) ExecuteMCPToolCalls() ([]string, error)
- func (agent *Agent) ExecuteToolCalls(toolsImpl map[string]func(any) (any, error)) ([]string, error)
- func (agent *Agent) GetPrompt(name string, args any) (Prompt, error)
- func (agent *Agent) RAGMemorySearchSimilaritiesWith(embedding openai.EmbeddingNewParamsInputUnion, limit float64) ([]string, error)
- func (agent *Agent) RAGMemorySearchSimilaritiesWithText(text string, limit float64) ([]string, error)
- func (agent *Agent) ReadResource(uri string) (Resource, error)
- func (agent *Agent) ReadResourceByName(name string) (Resource, error)
- func (agent *Agent) ToolCallsToJSON() (string, error)
- func (agent *Agent) ToolsCompletion() ([]openai.ChatCompletionMessageToolCall, error)
- type AgentOption
- func WithDMRClient(ctx context.Context, baseURL string) AgentOption
- func WithEmbeddingParams(embeddingParams openai.EmbeddingNewParams) AgentOption
- func WithMCPClient(command STDIOCommandOption) AgentOption
- func WithMCPPrompts(prompts []string) AgentOption
- func WithMCPResources(resources []string) AgentOption
- func WithMCPTools(tools []string) AgentOption
- func WithParams(params openai.ChatCompletionNewParams) AgentOption
- func WithRAGMemory(chunks []string) AgentOption
- func WithTools(tools []openai.ChatCompletionToolParam) AgentOption
- type Content
- type MemoryVectorStore
- func (mvs *MemoryVectorStore) GetAll() ([]VectorRecord, error)
- func (mvs *MemoryVectorStore) Save(vectorRecord VectorRecord) (VectorRecord, error)
- func (mvs *MemoryVectorStore) SearchSimilarities(embeddingFromQuestion VectorRecord, limit float64) ([]VectorRecord, error)
- func (mvs *MemoryVectorStore) SearchTopNSimilarities(embeddingFromQuestion VectorRecord, limit float64, max int) ([]VectorRecord, error)
- type Message
- type Prompt
- type Resource
- type STDIOCommandOption
- type VectorRecord
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ToolCallsToJSONString ¶
func ToolCallsToJSONString(tools []openai.ChatCompletionMessageToolCall) (string, error)
ToolCallsToJSONString converts a slice of openai.ChatCompletionMessageToolCall to a JSON string. It extracts the tool call ID and function arguments, converting them to a generic interface for JSON marshaling. The resulting JSON string is formatted with indentation for readability. If the tool calls are empty, it returns an empty JSON array. If any error occurs during the conversion, it returns an error. The function is useful for logging or storing tool calls in a structured format. It returns a JSON string representation of the tool calls.
Types ¶
type Agent ¶
type Agent struct {
Params openai.ChatCompletionNewParams
EmbeddingParams openai.EmbeddingNewParams
Tools []openai.ChatCompletionToolParam
ToolCalls []openai.ChatCompletionMessageToolCall
Resources []Resource
Prompts []Prompt
Store MemoryVectorStore
// contains filtered or unexported fields
}
func NewAgent ¶
func NewAgent(options ...AgentOption) (*Agent, error)
NewAgent creates a new Agent instance with the provided options. It applies all the options to the Agent and returns it. If any option sets an error, it returns the error instead of the Agent. The Agent can be configured with various options such as DMR client, parameters, tools, and memory.
func (*Agent) ChatCompletion ¶
ChatCompletion handles the chat completion request using the DMR client. It sends the parameters set in the Agent and returns the response content or an error. It is a synchronous operation that waits for the completion to finish.
func (*Agent) ChatCompletionStream ¶
func (agent *Agent) ChatCompletionStream(callBack func(self *Agent, content string, err error) error) (string, error)
ChatCompletionStream handles the chat completion request using the DMR client in a streaming manner. It takes a callback function that is called for each chunk of content received. The callback function receives the Agent instance, the content of the chunk, and any error that occurred. It returns the accumulated response content and any error that occurred during the streaming process. The callback function should return an error if it wants to stop the streaming process.
func (*Agent) ExecuteMCPToolCalls ¶
ExecuteMCPToolCalls executes the tool calls detected by the Agent using the MCP client. It takes no additional parameters as it uses the Agent's context and MCP client. It returns a slice of responses from the executed tools or an error if any tool call fails. It also appends the tool responses to the Agent's messages for further processing. This function is specifically designed to work with the MCP toolkit, which allows for tool calls to be executed in a remote environment using the MCP protocol. It assumes that the Agent has been initialized with an MCP client and the necessary context. The function iterates over the Agent's ToolCalls, unmarshals the arguments for each tool call, and then calls the tool using the MCP client. The responses are collected and returned. If no tool responses are found, it returns an error. It is important to ensure that the MCP client is properly configured and connected to the MCP server before calling this function, as it relies on the MCP protocol for executing tool calls. It is a synchronous operation that waits for the completion of each tool call.
func (*Agent) ExecuteToolCalls ¶
ExecuteToolCalls executes the tool calls detected by the Agent. It takes a map of tool implementations where the key is the tool name and the value is a function that implements the tool. Each tool function should accept a map of arguments and return a response or an error. The function returns a slice of responses from the executed tools or an error if any tool call fails. It also appends the tool responses to the Agent's messages for further processing
func (*Agent) GetPrompt ¶ added in v0.0.2
GetPrompt retrieves a prompt by its name and arguments from the MCP client. It constructs a Prompt object with the name, description, and messages. The messages are converted from the MCP format to the internal Message format. If the prompt is not found or an error occurs, it returns an error. If the prompt is found, it returns the Prompt object. It requires the MCP server to be running and accessible at the specified address.
func (*Agent) RAGMemorySearchSimilaritiesWith ¶ added in v0.0.2
func (agent *Agent) RAGMemorySearchSimilaritiesWith(embedding openai.EmbeddingNewParamsInputUnion, limit float64) ([]string, error)
RAGMemorySearchSimilaritiesWith searches for similar records in the RAG memory using the provided embedding. It creates an embedding from the input and searches for records with cosine similarity above the specified limit. It returns a slice of strings containing the prompts of the similar records and an error if any occurred. If no similar records are found, it returns an empty slice. It requires the DMR client to be initialized and the embedding parameters to be set in the Agent. The limit parameter specifies the minimum cosine similarity score for a record to be considered similar. It returns an error if the embedding creation fails or if the search operation fails.
func (*Agent) RAGMemorySearchSimilaritiesWithText ¶ added in v0.0.2
func (agent *Agent) RAGMemorySearchSimilaritiesWithText(text string, limit float64) ([]string, error)
RAGMemorySearchSimilaritiesWithText searches for similar records in the RAG memory using the provided text. It creates an embedding from the text and searches for records with cosine similarity above the specified limit. It returns a slice of strings containing the prompts of the similar records and an error if any occurred. If no similar records are found, it returns an empty slice. It requires the DMR client to be initialized and the embedding parameters to be set in the Agent. The limit parameter specifies the minimum cosine similarity score for a record to be considered similar. It returns an error if the embedding creation fails or if the search operation fails.
func (*Agent) ReadResource ¶ added in v0.0.2
ReadResource retrieves a resource by its URI from the MCP client. It constructs a Resource object with the URI, MIME type, and text content. The resource name and description are searched in the agent's resources. If the resource is not found or an error occurs, it returns an error. If the resource is found, it returns the Resource object. It requires the MCP server to be running and accessible at the specified address. The resources are expected to be in the format defined by the MCP server.
func (*Agent) ReadResourceByName ¶ added in v0.0.2
func (*Agent) ToolCallsToJSON ¶ added in v0.0.1
ToolCallsToJSON converts the Agent's tool calls to a JSON string. If there are no tool calls, it returns an empty JSON array. It uses the ToolCallsToJSONString function to convert the tool calls to a JSON string format.
func (*Agent) ToolsCompletion ¶
func (agent *Agent) ToolsCompletion() ([]openai.ChatCompletionMessageToolCall, error)
ToolsCompletion handles the tool calls completion request using the DMR client. It sends the parameters set in the Agent and returns the detected tool calls or an error. It is a synchronous operation that waits for the completion to finish.
type AgentOption ¶
type AgentOption func(*Agent)
func WithDMRClient ¶
func WithDMRClient(ctx context.Context, baseURL string) AgentOption
WithDMRClient initializes the Agent with a DMR client using the provided context and base URL.
func WithEmbeddingParams ¶ added in v0.0.2
func WithEmbeddingParams(embeddingParams openai.EmbeddingNewParams) AgentOption
WithEmbeddingParams sets the parameters for the Agent's embedding requests.
func WithMCPClient ¶
func WithMCPClient(command STDIOCommandOption) AgentOption
WithMCPClient initializes the Agent with an MCP client using the provided command. It runs the command to connect to the MCP server and sets up the client transport. The command should be a valid command that can be executed in the environment where the agent runs. It returns an AgentOption that can be used to configure the agent.
func WithMCPPrompts ¶ added in v0.0.2
func WithMCPPrompts(prompts []string) AgentOption
WithMCPPrompts fetches the prompts from the MCP server and sets them in the agent. It filters the prompts based on the provided names and converts them to a Prompt format. It requires the MCP server to be running and accessible at the specified address. The prompts are expected to be in the format defined by the MCP server. It returns an AgentOption that can be used to configure the agent.
func WithMCPResources ¶ added in v0.0.2
func WithMCPResources(resources []string) AgentOption
WithMCPResources fetches the resources from the MCP server and sets them in the agent. It filters the resources based on the provided names and converts them to a Resource format. It requires the MCP server to be running and accessible at the specified address. The resources are expected to be in the format defined by the MCP server. It returns an AgentOption that can be used to configure the agent.
func WithMCPTools ¶
func WithMCPTools(tools []string) AgentOption
WithMCPTools fetches the tools from the MCP server and sets them in the agent. It filters the tools based on the provided names and converts them to OpenAI format. It requires the MCP server to be running and accessible at the specified address. The tools are expected to be in the format defined by the MCP server. It returns an AgentOption that can be used to configure the agent. The tools are fetched using the MCP client and converted to OpenAI format.
func WithParams ¶
func WithParams(params openai.ChatCompletionNewParams) AgentOption
WithParams sets the parameters for the Agent's chat completion requests.
func WithRAGMemory ¶ added in v0.0.2
func WithRAGMemory(chunks []string) AgentOption
WithRAGMemory initializes the Agent with a RAG memory using the provided chunks. It creates a MemoryVectorStore and saves the embeddings of the chunks into it. The chunks should be pre-processed text data that will be used for retrieval-augmented generation (RAG). It returns an AgentOption that can be used to configure the agent.
func WithTools ¶
func WithTools(tools []openai.ChatCompletionToolParam) AgentOption
WithTools sets the tools for the Agent's chat completion requests. It allows the Agent to use specific tools during the chat completion process.
type MemoryVectorStore ¶ added in v0.0.2
type MemoryVectorStore struct {
Records map[string]VectorRecord
}
func (*MemoryVectorStore) GetAll ¶ added in v0.0.2
func (mvs *MemoryVectorStore) GetAll() ([]VectorRecord, error)
func (*MemoryVectorStore) Save ¶ added in v0.0.2
func (mvs *MemoryVectorStore) Save(vectorRecord VectorRecord) (VectorRecord, error)
Save saves a vector record to the MemoryVectorStore. If the record does not have an ID, it generates a new UUID for it. It returns the saved vector record and an error if any occurred during the save operation. If the record already exists, it will be overwritten.
func (*MemoryVectorStore) SearchSimilarities ¶ added in v0.0.2
func (mvs *MemoryVectorStore) SearchSimilarities(embeddingFromQuestion VectorRecord, limit float64) ([]VectorRecord, error)
SearchSimilarities searches for vector records in the MemoryVectorStore that have a cosine distance similarity greater than or equal to the given limit.
Parameters:
- embeddingFromQuestion: the vector record to compare similarities with.
- limit: the minimum cosine distance similarity threshold.
Returns:
- []llm.VectorRecord: a slice of vector records that have a cosine distance similarity greater than or equal to the limit.
- error: an error if any occurred during the search.
func (*MemoryVectorStore) SearchTopNSimilarities ¶ added in v0.0.2
func (mvs *MemoryVectorStore) SearchTopNSimilarities(embeddingFromQuestion VectorRecord, limit float64, max int) ([]VectorRecord, error)
SearchTopNSimilarities searches for the top N similar vector records based on the given embedding from a question. It returns a slice of vector records and an error if any. The limit parameter specifies the minimum similarity score for a record to be considered similar. The max parameter specifies the maximum number of vector records to return.
type STDIOCommandOption ¶
type STDIOCommandOption []string
func WithDockerMCPToolkit ¶
func WithDockerMCPToolkit() STDIOCommandOption
WithDockerMCPToolkit returns a STDIOCommandOption that runs the MCP toolkit using Docker. It uses the Alpine image with Socat to connect to the MCP server running on host.docker.internal:8811.
func WithSocatMCPToolkit ¶
func WithSocatMCPToolkit() STDIOCommandOption
WithSocatMCPToolkit returns a STDIOCommandOption that runs the MCP toolkit using Socat. It connects to the MCP server running on host.docker.internal:8811.