Class: AppchatFunctionService

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/appchat/templates/services/appchat_function_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chat_id, user_prompt) ⇒ AppchatFunctionService

Returns a new instance of AppchatFunctionService.



4
5
6
7
# File 'lib/generators/appchat/templates/services/appchat_function_service.rb', line 4

def initialize(chat_id, user_prompt)
  @chat = Chat.find(chat_id)
  @user_prompt = user_prompt
end

Instance Attribute Details

#chatObject (readonly)

Returns the value of attribute chat.



2
3
4
# File 'lib/generators/appchat/templates/services/appchat_function_service.rb', line 2

def chat
  @chat
end

#response_jsonObject (readonly)

Returns the value of attribute response_json.



2
3
4
# File 'lib/generators/appchat/templates/services/appchat_function_service.rb', line 2

def response_json
  @response_json
end

#user_promptObject (readonly)

Returns the value of attribute user_prompt.



2
3
4
# File 'lib/generators/appchat/templates/services/appchat_function_service.rb', line 2

def user_prompt
  @user_prompt
end

Instance Method Details

#call_ollamaObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/generators/appchat/templates/services/appchat_function_service.rb', line 14

def call_ollama
  client = Ollama.new(
    credentials: { address: 'http://localhost:11434' },
    options: { server_sent_events: true }
  )
  response = client.generate(
    {
      model: 'llama3.1',
      prompt: prompt,
      context: chat.context,
      "format": "json"
    }
  )
  @response_json = JSON.parse(response.map { |r| r["response"] }.join)
end

#promptObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/generators/appchat/templates/services/appchat_function_service.rb', line 29

def prompt
  <<-PROMPT
    Evaluate the following user prompt in chat context.
    Your goal is to determine whether the user prompt is requesting or requires additional information from any of the available services.
      - If the prompt is a general greeting, casual remark, or something that can be answered without further information, respond with JSON { 'match' => 'false' }.
      - If the prompt asks for specific information, requires a search, or needs a service to generate the correct response, then respond with JSON like this example:
      {
        'match' => 'true',
        'appchat_function' => 'WebSearchService',
        'parameters' => {
          'query' => 'a query based on the prompt and context'
        }
      }

    Here is the user's prompt: #{user_prompt}
    Here are the Available Services: #{ AppchatFunction.all_to_prompt_json }
  PROMPT
end

#runObject



9
10
11
12
# File 'lib/generators/appchat/templates/services/appchat_function_service.rb', line 9

def run
  call_ollama
  response_json
end