Class: OllamaChat::Tools::CVE

Inherits:
Object
  • Object
show all
Includes:
Ollama
Defined in:
lib/ollama_chat/tools/cve.rb

Overview

A tool for fetching CVE (Common Vulnerabilities and Exposures) information.

This tool allows the chat client to retrieve CVE details by ID from a configured API endpoint. It integrates with the Ollama tool calling system to provide security-related information to the language model.

Examples:

Using the CVE tool

# The tool can be invoked with a CVE ID
{ "name": "get_cve", "arguments": { "cve_id": "CVE-2023-12345" } }

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOllamaChat::Tools::CVE

Initializes a new CVE tool instance.



18
19
20
# File 'lib/ollama_chat/tools/cve.rb', line 18

def initialize
  @name = 'get_cve'
end

Instance Attribute Details

#nameString (readonly)

Returns the name of the tool.

Returns:

  • (String)

    the name of the tool (‘get_cve’)



25
26
27
# File 'lib/ollama_chat/tools/cve.rb', line 25

def name
  @name
end

Instance Method Details

#execute(tool_call, **opts) ⇒ Hash, String

Executes the CVE lookup operation.

This method fetches CVE data from the configured API endpoint using the provided CVE ID. It handles the HTTP request, parses the JSON response, and returns the structured data.

Parameters:

  • tool_call (Ollama::Tool::Call)

    the tool call object containing function details

  • opts (Hash)

    additional options

Options Hash (**opts):

  • :config (ComplexConfig::Settings)

    the configuration object

Returns:

  • (Hash, String)

    the parsed CVE data as a hash or an error message

Raises:

  • (StandardError)

    if there’s an issue with the HTTP request or JSON parsing



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ollama_chat/tools/cve.rb', line 65

def execute(tool_call, **opts)
  config = opts[:config]
  cve_id = tool_call.function.arguments.cve_id
  url    = config.tools.get_cve.url % { cve_id: }
  OllamaChat::Utils::Fetcher.get(
    url,
    headers: {
      'Accept' => 'application/json',
    },
    debug: OllamaChat::EnvConfig::OLLAMA::CHAT::DEBUG
  ) do |tmp|
    data = JSON.parse(tmp.read, object_class: JSON::GenericObject)
    return data
  end
rescue StandardError => e
  "Failed to fetch CVE for #{cve_id} #{e.class}: #{e.message}"
end

#to_hashHash

Converts the tool to a hash representation.

This method provides a standardized way to serialize the tool definition for use in tool calling systems.

Returns:

  • (Hash)

    a hash representation of the tool



89
90
91
# File 'lib/ollama_chat/tools/cve.rb', line 89

def to_hash
  tool.to_hash
end

#toolOllama::Tool

Creates and returns a tool definition for getting CVE information.

This method constructs the function signature that describes what the tool does, its parameters, and required fields. The tool expects a CVE-ID parameter to be provided.

Returns:

  • (Ollama::Tool)

    a tool definition for retrieving CVE information



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ollama_chat/tools/cve.rb', line 34

def tool
  Tool.new(
    type: 'function',
    function: Tool::Function.new(
      name:,
      description: 'Get the CVE for id as JSON',
      parameters: Tool::Function::Parameters.new(
        type: 'object',
        properties: {
          cve_id: Tool::Function::Parameters::Property.new(
            type: 'string',
            description: 'The CVE-ID to get'
          ),
        },
        required: %w[cve_id]
      )
    )
  )
end