Module: CPEE::LLM::Functions

Includes:
RubyLLM_Requests
Included in:
CreateDataFlow, CreateGeneric, CreateMermaid, CreateText, ValidateDataFlow
Defined in:
lib/cpee/llm/functions.rb

Instance Method Summary collapse

Methods included from RubyLLM_Requests

#adapt_docxml_description, #adapt_mermaid_model, #adapt_xml_model, #connect_llm, #generate_content, #generate_dataflow_content, #generate_endpoint_mermaid_model, #generate_generic_content, #generate_json_content, #generate_mermaid_model, #generate_plain_text, #validate_xml_model

Instance Method Details

#adapt_cpee_model(myllm, doc, user_input, existing_endpoints, endpoints, llms) ⇒ Object

}}}



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/cpee/llm/functions.rb', line 110

def adapt_cpee_model(myllm,doc,user_input,existing_endpoints,endpoints,llms) #{{{
  testset = XML::Smart.string(<<~XML)
    <testset xmlns="http://cpee.org/ns/properties/2.0">
    </testset>
  XML
  root = testset.root
  root.add(existing_endpoints.root)
  dslx = root.add("dslx")
  dslx.add(doc.root)

  #puts testset.to_s

  begin
    llm_response = adapt_xml_model(myllm,user_input,testset,endpoints,llms)
    # raise exceptions if response is empty for some reason
    if llm_response.nil? || llm_response.empty?
      raise LLMError.new("Something went wrong and your content was not generated!", 500)
    else
      llm_response = llm_response.strip
      inside = llm_response.scan(/```(\w+)?\s*\n(.*?)\n```/m)
      llm_response = inside.empty? ? llm_response : inside[0][1]
      #check if response is xml:
      begin
        XML::Smart.string(llm_response)
      rescue Nokogiri::XML::SyntaxError => e
        raise LLMError.new("Something went wrong and llm was not able to generate valid xml model: #{llm_response}", 500)
      end
      return llm_response
    end
  rescue LLMError => e_llm
    raise e_llm
  rescue Exception => e
    raise e
  end
end

#adapt_doccpee_description(myllm, doc, user_input, llms) ⇒ Object

}}}



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/cpee/llm/functions.rb', line 84

def adapt_doccpee_description(myllm,doc,user_input,llms) #{{{
  input_cpee = doc.to_s()
  begin
    llm_response = adapt_docxml_description(myllm,user_input,input_cpee,llms)
    # raise exceptions if response is empty for some reason
    if llm_response.nil? || llm_response.empty?
      raise LLMError.new("Something went wrong and your content was not generated!", 500)
    else
      llm_response = llm_response.strip
      inside = llm_response.scan(/```(\w+)?\s*\n(.*?)\n```/m)
      llm_response = inside.empty? ? llm_response : inside[0][1]
      #check if response is xml:
      begin
        XML::Smart.string(llm_response)
      rescue Nokogiri::XML::SyntaxError => e
        raise LLMError.new("Something went wrong and llm was not able to generate valid xml model: #{llm_response}", 500)
      end
      return llm_response
    end
  rescue LLMError => e_llm
    raise e_llm
  rescue Exception => e
    raise e
  end
end

#adapt_model(myllm, doc, user_input, llms) ⇒ Object

}}}



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cpee/llm/functions.rb', line 66

def adapt_model(myllm,doc,user_input,llms) #{{{
  input_cpee = doc.to_s()
  input_mermaid = cpee_to_mermaid(doc.to_s())
  begin
    llm_response = adapt_mermaid_model(myllm,user_input,input_mermaid,llms)
    # raise exceptions if response is empty for some reason
    if llm_response.nil? || llm_response.empty?
      raise LLMError.new("Something went wrong and your content was not generated!", 500)
    else
      return llm_response
    end
  rescue LLMError => e_llm
    raise e_llm
  rescue Exception => e
    raise e
  end
end

#cpee_to_mermaid(cpee) ⇒ Object

{{{



31
32
33
34
35
36
37
# File 'lib/cpee/llm/functions.rb', line 31

def cpee_to_mermaid(cpee) #{{{
  model = CPEE::Transformation::Source::CPEE.new(cpee)
  trans = CPEE::Transformation::Transformer.new(model)
  traces = trans.build_traces
  tree = trans.build_tree(false)
  trans.generate_model(CPEE::Transformation::Target::Mermaid)
end

#generate_dataflow(myllm, mermaid_model, api_specification, llms) ⇒ Object

}}}



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/cpee/llm/functions.rb', line 184

def generate_dataflow(myllm,mermaid_model,api_specification,llms) #{{{
  begin
    llm_response = generate_dataflow_content(myllm, mermaid_model, api_specification, llms)
    # raise exceptions if response is empty for some reason
    if llm_response.nil? || llm_response.empty?
      raise LLMError.new("Something went wrong and your content was not generated!", 500)
    else
      #check if markdown is there:
      llm_response = llm_response.strip
      inside = llm_response.scan(/```(\w+)?\s*\n(.*?)\n```/m)
      # variable = condition?  if true: if false
      llm_response = inside.empty? ? llm_response : inside[0][1]
      #check if response is json:
      begin
        hash = JSON.parse(llm_response)
      rescue JSON::ParserError => e
        raise LLMError.new("Something went wrong and llm was not able to generate Json data flow: #{llm_response}", 500)
      end
      return llm_response
    end
  rescue LLMError => e_llm
    e_llm.message
    raise e_llm
  rescue Exception => e
    e.message
    raise e
  end
end

#generate_endpoint_model(myllm, user_input, endpoints, llms) ⇒ Object

}}}



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/cpee/llm/functions.rb', line 213

def generate_endpoint_model(myllm,user_input,endpoints,llms) #{{{
  begin
    llm_response = generate_endpoint_mermaid_model(myllm,user_input,endpoints,llms)
    # raise exceptions if response is empty for some reason
    if llm_response.nil? || llm_response.empty?
      raise LLMError.new("Something went wrong and your content was not generated!", 500)
    else
      return llm_response
    end
  rescue LLMError => e_llm
    e_llm.message
    raise e_llm
  rescue Exception => e
    e.message
    raise e
  end
end

#generate_generic(myllm, user_input, system_prompt, format, temperature, llms) ⇒ Object

}}}



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/cpee/llm/functions.rb', line 166

def generate_generic(myllm,user_input,system_prompt,format,temperature,llms) #{{{
  begin
    llm_response = generate_generic_content(myllm, user_input, system_prompt, format, temperature, llms)
    # raise exceptions if response is empty for some reason
    if llm_response.nil? || llm_response.empty?
      raise LLMError.new("Something went wrong and your content was not generated!", 500)
    else
      return llm_response
    end
  rescue LLMError => e_llm
    e_llm.message
    raise e_llm
  rescue Exception => e
    e.message
    raise e
  end
end

#generate_model(myllm, user_input, temperature, llms) ⇒ Object

}}}



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cpee/llm/functions.rb', line 48

def generate_model(myllm,user_input,temperature,llms) #{{{
  begin
    llm_response = generate_mermaid_model(myllm,user_input,temperature,llms)
    # raise exceptions if response is empty for some reason
    if llm_response.nil? || llm_response.empty?
      raise LLMError.new("Something went wrong and your content was not generated!", 500)
    else
      return llm_response
    end
  rescue LLMError => e_llm
    e_llm.message
    raise e_llm
  rescue Exception => e
    e.message
    raise e
  end
end

#generate_text(myllm, doc, llms) ⇒ Object

}}}



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/cpee/llm/functions.rb', line 146

def generate_text(myllm,doc,llms) #{{{
  input_cpee = doc.to_s()
  input_mermaid = cpee_to_mermaid(doc.to_s())
  begin
    llm_response = generate_plain_text(myllm,input_mermaid,llms)
    # raise exceptions if response is empty for some reason
    if llm_response.nil? || llm_response.empty?
      raise LLMError.new("Something went wrong and your content was not generated!", 500)
    else
      return llm_response
    end
  rescue LLMError => e_llm
    e_llm.message
    raise e_llm
  rescue Exception => e
    e.message
    raise e
  end
end

#mermaid_to_cpee(mermaid) ⇒ Object

}}}



38
39
40
41
42
43
44
45
46
# File 'lib/cpee/llm/functions.rb', line 38

def mermaid_to_cpee(mermaid) #{{{
  model = CPEE::Transformation::Source::Mermaid.new(mermaid)

  trans = CPEE::Transformation::Transformer.new(model)
  traces = trans.build_traces

  tree = trans.build_tree(false)
  trans.generate_model(CPEE::Transformation::Target::CPEE)
end

#validate_cpee_model(myllm, cpee_model, llms) ⇒ Object

}}}



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/cpee/llm/functions.rb', line 231

def validate_cpee_model(myllm,cpee_model,llms) #{{{
  begin
    llm_response = validate_xml_model(myllm,cpee_model,llms)
    if llm_response.nil? || llm_response.empty?
      raise LLMError.new("Something went wrong and your content was not generated!", 500)
    elsif llm_response.strip.downcase == "perfect"
      return cpee_model
    else
      llm_response = llm_response.strip
      inside = llm_response.scan(/```(\w+)?\s*\n(.*?)\n```/m)
      llm_response = inside.empty? ? llm_response : inside[0][1]
      #check if response is xml:
      begin
        XML::Smart.string(llm_response)
      rescue Nokogiri::XML::SyntaxError => e
        raise LLMError.new("Something went wrong and llm was not able to generate valid xml model", llm_response)
      end
      return llm_response
    end
  rescue LLMError => e_llm
    e_llm.message
    raise e_llm
  rescue Exception => e
    e.message
    raise e
  end
end