Class: Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



7
8
9
10
# File 'lib/client.rb', line 7

def initialize(config)
  @config = config
  @openapi_client = OpenAI::Client.new(access_token: config["openapi_key"])
end

Instance Attribute Details

#config ⇒ Object (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/client.rb', line 5

def config
  @config
end

#openapi_client ⇒ Object (readonly)

Returns the value of attribute openapi_client.



4
5
6
# File 'lib/client.rb', line 4

def openapi_client
  @openapi_client
end

Instance Method Details

#final_prompt(prompt) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/client.rb', line 124

def final_prompt(prompt)
  full_prompt = <<~PROMPT
    This is the output of the command you provided to the user in the previous step.

    #{prompt}

    Your NEXT response should be a list of commands that will be automatically executed to fulfill the user's goal.
    - The commands may make changes to the user's system.
    - The commands may install new software using package managers like Homebrew
    - The commands MUST all start with a valid command that you would run in the terminal
    - The commands MUST NOT contain any placeholders in angle brackets like <this>.
    - The response MUST NOT contain any plain language instructions, or backticks indicating where the commands begin or end.
    - THe response MUST NOT start or end with backticks.
    - The response MUST NOT end with a newline character.
    Therefore your NEXT response MUST contain ONLY a list of commands and nothing else.

    VALID example response. These commands are examples of commands which CAN be included in your FINAL response:

      ls
      mkdir new_directory
      brew install git
      git commit -m "This is a great commit message"

    If you cannot keep to this restriction, simply return the string "$$cannot_compute$$" and the user will be asked to provide a new prompt.
  PROMPT

  @messages << { role: "user", content: full_prompt }

  response = openapi_client.chat(
    parameters: {
      model: "gpt-4-turbo-preview",
      messages: @messages,
      temperature: 0.6,
    }
  )
  content = response.dig("choices", 0, "message", "content")

  @messages << { role: "assistant", content: content }

  content
end

#first_prompt(prompt) ⇒ 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/client.rb', line 12

def first_prompt(prompt)
  system_prompt = <<~PROMPT
    You are a command-line application being executed inside of a directory in a macOS environment, on the user's terminal command line.

    You are executed by running `gpterminal` in the terminal, and you are provided with a prompt to respond to with the -p flag.

    Users can add a preset prompt by running `gpterminal -s <name>,<prompt>`.

    The eventual output to the user would be a list of commands that they can run in their terminal to accomplish a task.

    You have the ability to run any command that this system can run, and you can read the output of those commands.

    The user is trying to accomplish a task using the terminal, but they are not sure how to do it.
  PROMPT

  if @config["send_path"]
    system_prompt += <<~PROMPT
      The user's PATH environment variable is:
      #{ENV["PATH"]}
    PROMPT
  end

  full_prompt = <<~PROMPT
    Your FIRST response should be a list of commands that will be automatically executed to gather more information about the user's system.
    - The commands MUST NOT make any changes to the user's system.
    - The commands MUST NOT make any changes to any files on the user's system.
    - The commands MUST NOT write to any files using the > or >> operators.
    - The commands MUST NOT use the touch command.
    - The commands MUST NOT use echo or any other command to write into files using the > or >> operators.
    - The commands MUST NOT send any data to any external servers.
    - The commands MUST NOT contain any placeholders in angle brackets like <this>.
    - The commands MUST NOT contain any plain language instructions, or backticks indicating where the commands begin or end.
    - The commands MAY gather information about the user's system, such as the version of a software package, or the contents of a file.
    - The commands CAN pipe their output into other commands.
    - The commands SHOULD tend to gather more verbose information INSTEAD OF more concise information.
    This will help you to provide a more accurate response to the user's goal.
    Therefore your FIRST response MUST contain ONLY a list of commands and nothing else.

    VALID example response. These commands are examples of commands which CAN be included in your FIRST response:

      for file in *; do cat "$file"; done
      which ls
      which git
      which brew
      git diff
      git status

    INVALID example response. These commands are examples of commands which MUST NOT be included in your FIRST response:

      touch file.txt
      git add .
      git push

    If you cannot create a VALID response, simply return the string "$$cannot_compute$$" and the user will be asked to provide a new prompt.
    If you do not need to gather more information, simply return the string "$$no_gathering_needed$$" and the next step will be executed.
    You probably will need to gather information.
    If you need to gather information directly from the user, you will be able to do so in the next step.

    The user's goal prompt is:
    "#{prompt}"
    Commands to execute to gather more information about the user's system before providing the response which will accomplish the user's goal:
  PROMPT

  @messages = [
    { role: "system", content: system_prompt },
    { role: "user", content: full_prompt }
  ]

  response = openapi_client.chat(
    parameters: {
      model: "gpt-4-turbo-preview",
      messages: @messages,
      temperature: 0.6,
    }
  )
  content = response.dig("choices", 0, "message", "content")

  @messages << { role: "assistant", content: content }

  content
end

#offer_information_prompt(prompt) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/client.rb', line 94

def offer_information_prompt(prompt)
  full_prompt = <<~PROMPT
    This is the output of the command you provided to the user in the previous step.

    #{prompt}

    Before you provide the user with the next command, you have the opportunity to ask the user to provide more information so you can better tailor your response to their needs.

    If you would like to ask the user for more information, please provide a prompt that asks the user for the information you need.
    - Your prompt MUST ONLY contain one question. You will be able to ask another question in the next step.
    If you have all the information you need, simply return the string "$$no_more_information_needed$$" and the next step will be executed.
  PROMPT

  @messages << { role: "user", content: full_prompt }

  response = openapi_client.chat(
    parameters: {
      model: "gpt-4-turbo-preview",
      messages: @messages,
      temperature: 0.6,
    }
  )

  content = response.dig("choices", 0, "message", "content")

  @messages << { role: "assistant", content: content }

  content
end