Class: LlmMemory::Broca
- Inherits:
-
Object
- Object
- LlmMemory::Broca
- Includes:
- Llms::Openai
- Defined in:
- lib/llm_memory/broca.rb
Instance Attribute Summary collapse
-
#messages ⇒ Object
Returns the value of attribute messages.
Instance Method Summary collapse
- #adjust_token_count ⇒ Object
- #generate_prompt(args) ⇒ Object
-
#initialize(prompt:, model: "gpt-3.5-turbo", temperature: 0.7, max_token: 4096) ⇒ Broca
constructor
A new instance of Broca.
- #respond(args) ⇒ Object
- #respond_with_schema(context: {}, schema: {}) ⇒ Object
- #tokenizer ⇒ Object
Methods included from Llms::Openai
Constructor Details
permalink #initialize(prompt:, model: "gpt-3.5-turbo", temperature: 0.7, max_token: 4096) ⇒ Broca
Returns a new instance of Broca.
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/llm_memory/broca.rb', line 9 def initialize( prompt:, model: "gpt-3.5-turbo", temperature: 0.7, max_token: 4096 ) LlmMemory.configure @prompt = prompt @model = model @messages = [] @temperature = temperature @max_token = max_token end |
Instance Attribute Details
permalink #messages ⇒ Object
Returns the value of attribute messages.
7 8 9 |
# File 'lib/llm_memory/broca.rb', line 7 def @messages end |
Instance Method Details
permalink #adjust_token_count ⇒ Object
[View source]
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/llm_memory/broca.rb', line 91 def adjust_token_count count = 0 = [] @messages.reverse_each do || encoded = tokenizer.encode([:content], add_special_tokens: true) token_count = encoded.tokens.length count += token_count if count <= @max_token .push() else break end end @messages = .reverse end |
permalink #generate_prompt(args) ⇒ Object
[View source]
86 87 88 89 |
# File 'lib/llm_memory/broca.rb', line 86 def generate_prompt(args) erb = ERB.new(@prompt) erb.result_with_hash(args) end |
permalink #respond(args) ⇒ Object
[View source]
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/llm_memory/broca.rb', line 23 def respond(args) final_prompt = generate_prompt(args) @messages.push({role: "user", content: final_prompt}) adjust_token_count begin response = client.chat( parameters: { model: @model, messages: @messages, temperature: @temperature } ) LlmMemory.logger.debug(response) response_content = response.dig("choices", 0, "message", "content") @messages.push({role: "system", content: response_content}) unless response_content.nil? response_content rescue => e LlmMemory.logger.info(e.inspect) # @messages = [] nil end end |
permalink #respond_with_schema(context: {}, schema: {}) ⇒ Object
[View source]
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 |
# File 'lib/llm_memory/broca.rb', line 46 def respond_with_schema(context: {}, schema: {}) response_content = respond(context) begin response = client.chat( parameters: { model: "gpt-3.5-turbo-0613", # as of July 3, 2023 messages: [ { role: "user", content: response_content } ], functions: [ { name: "broca", description: "Formating the content with the specified schema", parameters: schema } ] } ) LlmMemory.logger.debug(response) = response.dig("choices", 0, "message") if ["role"] == "assistant" && ["function_call"] function_name = .dig("function_call", "name") args = JSON.parse( .dig("function_call", "arguments"), {symbolize_names: true} ) if function_name == "broca" args end end rescue => e LlmMemory.logger.info(e.inspect) nil end end |
permalink #tokenizer ⇒ Object
[View source]
107 108 109 |
# File 'lib/llm_memory/broca.rb', line 107 def tokenizer @tokenizer ||= Tokenizers.from_pretrained("gpt2") end |