Class: Shelp::ShelpCLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/shelp.rb

Instance Method Summary collapse

Instance Method Details

#ask(query) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/shelp.rb', line 12

def ask(query)
  client = OpenAI::Client.new(
    access_token: ENV["OPENAI_ACCESS_TOKEN"],
    log_errors: true, # Disable in production
  )

  # Define the search prompt
  chat_prompt = "    You are a chatbot that assists with Linux/Unix system administration tasks.\n    Provide shell commands to help users with file management, system monitoring, and basic troubleshooting.\n    If a user asks a question unrelated to Linux/Unix system administration, politely respond with \"I'm currently focused on Linux/Unix system administration tasks. Can I help you with file management, system monitoring, or troubleshooting?\"\n    Always format your response in JSON format with the following structure: { \"response\": [\"ANSWER 1\", \"ANSWER 2\", \"ANSWER 3\"] }\n\n    Generate shell commands based on the following query: \#{query}\n  TEXT\n\n  system_prompt = <<~TEXT\n    \"You are a chatbot that assists with Linux/Unix system administration tasks. Provide shell commands to help users with file management, system monitoring, and basic troubleshooting.\"\n  TEXT\n\n  # Call the OpenAI API\n  chat_response = client.chat(\n    parameters: {\n      model: \"gpt-3.5-turbo-0125\",\n      messages: [\n        {\n          role: \"system\",\n          content: system_prompt,\n        },\n        { role: \"user\", content: chat_prompt },\n      ],\n      temperature: 0.7,\n    },\n  )\n\n  # Extract the chat response\n  chat_reply = chat_response.dig(\"choices\", 0, \"message\", \"content\")\n\n  # Parse the JSON response\n  chat_reply_json = JSON.parse(chat_reply)\n\n  # Display the choices to the user\n  choices = chat_reply_json[\"response\"]\n\n  # Copy the selected choice to the clipboard\n  prompt = TTY::Prompt.new\n  choice = prompt.select(\"Select a result:\", choices)\n\n  # Copy the selected choice to the clipboard\n  Clipboard.copy(choice)\n\n  # Display the selected choice to confirm it has been copied to the clipboard\n  puts \"Your choice: \#{choice} has been copied to the clipboard.\"\nend\n"