Class: LlmMemory::Broca

Inherits:
Object
  • Object
show all
Includes:
Llms::Openai
Defined in:
lib/llm_memory/broca.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Llms::Openai

#client

Constructor Details

#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

#messagesObject

Returns the value of attribute messages.



7
8
9
# File 'lib/llm_memory/broca.rb', line 7

def messages
  @messages
end

Instance Method Details

#adjust_token_countObject



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
  new_messages = []
  @messages.reverse_each do |message|
    encoded = tokenizer.encode(message[:content], add_special_tokens: true)
    token_count = encoded.tokens.length
    count += token_count
    if count <= @max_token
      new_messages.push(message)
    else
      break
    end
  end
  @messages = new_messages.reverse
end

#generate_prompt(args) ⇒ Object



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

#respond(args) ⇒ Object



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

#respond_with_schema(context: {}, schema: {}) ⇒ Object



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)
    message = response.dig("choices", 0, "message")
    if message["role"] == "assistant" && message["function_call"]
      function_name = message.dig("function_call", "name")
      args =
        JSON.parse(
          message.dig("function_call", "arguments"),
          {symbolize_names: true}
        )
      if function_name == "broca"
        args
      end
    end
  rescue => e
    LlmMemory.logger.info(e.inspect)
    nil
  end
end

#tokenizerObject



107
108
109
# File 'lib/llm_memory/broca.rb', line 107

def tokenizer
  @tokenizer ||= Tokenizers.from_pretrained("gpt2")
end