Class: AppchatFunctionService
- Inherits:
-
Object
- Object
- AppchatFunctionService
- Defined in:
- lib/generators/appchat/templates/services/appchat_function_service.rb
Instance Attribute Summary collapse
-
#chat ⇒ Object
readonly
Returns the value of attribute chat.
-
#response_json ⇒ Object
readonly
Returns the value of attribute response_json.
-
#user_prompt ⇒ Object
readonly
Returns the value of attribute user_prompt.
Instance Method Summary collapse
- #call_ollama ⇒ Object
-
#initialize(chat_id, user_prompt) ⇒ AppchatFunctionService
constructor
A new instance of AppchatFunctionService.
- #prompt ⇒ Object
- #run ⇒ Object
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
#chat ⇒ Object (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_json ⇒ Object (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_prompt ⇒ Object (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_ollama ⇒ Object
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 |
#prompt ⇒ Object
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 |
#run ⇒ Object
9 10 11 12 |
# File 'lib/generators/appchat/templates/services/appchat_function_service.rb', line 9 def run call_ollama response_json end |