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_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

}}}



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/cpee/llm/functions.rb', line 93

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_model(myllm, doc, user_input, llms) ⇒ Object

}}}



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cpee/llm/functions.rb', line 75

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

{{{



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cpee/llm/functions.rb', line 28

def cpee_to_mermaid(cpee) #{{{
  srv = Riddl::Client.new('http://localhost:9295/mermaid/cpee')
  status, res = srv.post [
    Riddl::Parameter::Complex.new("description","text/xml",cpee),
    Riddl::Parameter::Simple.new("type","description")
  ]
  if status >= 200 && status < 300
    res
  else
    raise 'error when converting cpee to mermaid'
  end
  return res[0].value().read()
end

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

}}}



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/cpee/llm/functions.rb', line 167

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
        pp "here"
        hash = JSON.parse(llm_response)
      rescue JSON::ParserError => e
        pp "there"
        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

}}}



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/cpee/llm/functions.rb', line 198

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

}}}



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

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

{{{



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cpee/llm/functions.rb', line 57

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

}}}



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cpee/llm/functions.rb', line 129

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

}}}



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cpee/llm/functions.rb', line 42

def mermaid_to_cpee(mermaid) #{{{
  srv = Riddl::Client.new('http://localhost:9295/cpee/mermaid')
  status, res = srv.post [
    Riddl::Parameter::Complex.new("description","text/plain",mermaid),
    Riddl::Parameter::Simple.new("type","description")
  ]
  if status >= 200 && status < 300
    res
  else
    raise 'error when converting mermaid to cpee'
  end
  return res[0].value().read()
end

#validate_cpee_model(myllm, cpee_model, llms) ⇒ Object

}}}



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/cpee/llm/functions.rb', line 216

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