cpee-llm 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3859c1965e2cb0d5bddf13277d2dd60bd821f2bbec81aadbc257efc738fba967
4
+ data.tar.gz: 441ca61ac4fd0992aad64c90da0cc5e698092c5004ec2210fede96b1d2e28519
5
+ SHA512:
6
+ metadata.gz: b56ea1e75b23c1c45d95d9b0c1def1e19bc273b0fd47ae0d10e8c41d01616631f6f2d3b72c2f3b320048dae35f5b41711cfdf7039bd596820a7ed354c6539ab8
7
+ data.tar.gz: 8e348b13cec46552f70ed0ce09f67c1f1cefd9a3d0a68862f0b4737fe0c4f5326ae24d6840bcc0ea2d0935d9f8414858e3d6720aed30cb41b801da99b05f79b8
data/AUTHORS ADDED
@@ -0,0 +1,3 @@
1
+ Nataliia Klievtsova <n.klievtsova@gmail.com>
2
+ Matthias Ehrendorfer <matthias.ehrendorfer@gmail.com>
3
+ Juergen 'eTM' Mangler <juergen.mangler@gmail.com>
data/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
data/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # CPEE-LLM
2
+
3
+ CPEE Conversational Agents
4
+
5
+ * CM-A for creating models
6
+ * EP-A for selecting endpoints
7
+ * DF-A for creating and validating dataflow
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require 'rake'
2
+ require 'rubygems/package_task'
3
+
4
+ spec = eval(File.read('cpee-llm.gemspec'))
5
+ Gem::PackageTask.new(spec) do |pkg|
6
+ pkg.need_zip = true
7
+ pkg.need_tar = true
8
+ FileUtils.mkdir 'pkg' rescue nil
9
+ FileUtils.rm_rf Dir.glob('pkg/*')
10
+ FileUtils.ln_sf "#{pkg.name}.gem", "pkg/#{spec.name}.gem"
11
+ end
12
+
13
+ task :default => [:gem]
14
+
15
+ task :push => :gem do |r|
16
+ `gem push pkg/cpee-llm.gem`
17
+ end
18
+
19
+ task :install => :gem do |r|
20
+ `gem install pkg/cpee-llm.gem`
21
+ end
data/cpee-llm.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "cpee-llm"
3
+ s.version = "1.0.0"
4
+ s.platform = Gem::Platform::RUBY
5
+ s.license = "LGPL-3.0-or-later"
6
+ s.summary = "CPEE Conversational Agents"
7
+
8
+ s.description = "see http://cpee.org"
9
+
10
+ s.files = Dir['{server/*,tools/**/*,lib/**/*}'] + %w(LICENSE Rakefile cpee-llm.gemspec README.md AUTHORS)
11
+ s.require_path = 'lib'
12
+ s.extra_rdoc_files = ['README.md']
13
+ s.bindir = 'tools'
14
+ s.executables = ['cpee-llm']
15
+
16
+ s.required_ruby_version = '>=3.4'
17
+
18
+ s.authors = ['Nataliia Klievtsova', 'Matthias Ehrendorfer', 'Juergen eTM Mangler']
19
+
20
+ s.email = 'n.klievtsova@gmail.com'
21
+ s.homepage = 'http://cpee.org/'
22
+
23
+ s.add_runtime_dependency 'riddl', '~> 1.0'
24
+ s.add_runtime_dependency 'json', '~> 2.10'
25
+ s.add_runtime_dependency 'xml-smart', '~> 0.4'
26
+ s.add_runtime_dependency 'typhoeus', '~> 1.4'
27
+ s.add_runtime_dependency 'ruby_llm', '~> 1.15'
28
+ s.add_runtime_dependency 'rag_embeddings', '~> 0'
29
+ end
@@ -0,0 +1,199 @@
1
+ # This file is part of CPEE-LLM.
2
+ #
3
+ # CPEE-LLM is free software: you can redistribute it and/or modify it under the
4
+ # terms of the GNU Lesser General Public License as published by the Free
5
+ # Software Foundation, either version 3 of the License, or (at your option) any
6
+ # later version.
7
+ #
8
+ # CPEE-LLM is distributed in the hope that it will be useful, but WITHOUT ANY
9
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
10
+ # A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11
+ # details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public License
14
+ # along with CPEE-LLM (file LICENSE in the main directory). If not, see
15
+ # <http://www.gnu.org/licenses/>.
16
+
17
+ require 'typhoeus'
18
+ require 'xml/smart'
19
+ require 'cgi'
20
+ require 'rag_embeddings'
21
+
22
+ # disable Logging, rag_emb uses langchain
23
+ Langchain.logger.level = Logger::UNKNOWN
24
+
25
+ module CPEE
26
+
27
+ module LLM
28
+
29
+ module DataFlow
30
+
31
+ def get_data_from_url(url,method)
32
+ request = Typhoeus::Request.new(
33
+ url,
34
+ method: method,
35
+ )
36
+ request.run
37
+ response = request.response
38
+ if response.code.to_i == 200
39
+ data = response.body
40
+ return data
41
+ else
42
+ raise Exception.new "Request failed with #{response.code}!!!. \n For more details see: \n #{response.body}."
43
+ end
44
+ end
45
+
46
+ def get_input(url)
47
+ data_content = get_data_from_url("https://cpee.org/flow/resources/endpoints/#{url}/schema.rng",:get)
48
+ doc = XML::Smart.string(data_content)
49
+ doc.register_namespace 'd', 'http://relaxng.org/ns/structure/1.0'
50
+ data_info = doc.find("/d:element/*").map{|e| e.dump }.join("\n")
51
+ return data_info
52
+ end
53
+
54
+ def get_demo_endpoints()
55
+ ed = get_data_from_url("https://cpee.org/flow/resources/endpoints/?description",:get)
56
+ doc = XML::Smart.string(ed)
57
+ resources = doc.find("//resource")
58
+ endpoints_description = resources.map do |resource|
59
+ [ CGI.unescape(resource.attributes['endpoint']),
60
+ [ resource.find('string(./endpoint)'),
61
+ resource.find('string(./functionality)'),
62
+ get_input(resource.attributes['endpoint']),
63
+ resource.find('string(./ouput)')
64
+ ]
65
+ ]
66
+ end.to_h
67
+ return endpoints_description
68
+ end
69
+
70
+ def get_idlabel_from_model(doc)
71
+ doc.register_namespace 'd', 'http://cpee.org/ns/description/1.0'
72
+ pairs = doc.find('//d:call').map do |c|
73
+ [c.attributes['id'],
74
+ c.find('string(d:parameters/d:label)')]
75
+ end.to_h
76
+ return pairs
77
+ end
78
+
79
+ EMBED_CACHE = {}
80
+
81
+ def embed_cached(text)
82
+ EMBED_CACHE[text] ||= begin
83
+ emb = RagEmbeddings.embed(text, model: "mxbai-embed-large")
84
+ RagEmbeddings::Embedding.from_array(emb)
85
+ end
86
+ end
87
+
88
+ def text_similarity(string1, string2)
89
+ obj1 = embed_cached(string1)
90
+ obj2 = embed_cached(string2)
91
+ return obj1.cosine_similarity(obj2)
92
+ end
93
+
94
+ def get_matching_endpoints(process_model,endpoints_description)
95
+ ids = get_idlabel_from_model(process_model)
96
+ best_match = {}
97
+ ids.each do |i,l|
98
+ values = endpoints_description.transform_values do |v|
99
+ options = v[0].split(';')
100
+ options.sum { |o| text_similarity(l, o) } / options.size.to_f
101
+ end
102
+ key, value = values.max_by { |_, v| v }
103
+ ed = endpoints_description[key]
104
+ if value > 0.70
105
+ best_match[i] = [{"label":l, "url": key,"description": ed[0],"functionality": ed[1],"input":ed[2], "output":ed[3]}]
106
+ end
107
+ end
108
+ return best_match
109
+ end
110
+
111
+ def integrate_dataflow(cpee_model,dataflow)
112
+ json_e = JSON.parse(dataflow)
113
+ endxml = XML::Smart.string('<endpoints xmlns="http://cpee.org/ns/properties/2.0"/>')
114
+
115
+ # ========================calls=========================
116
+ json_e['tasks'].each do |i, value|
117
+ begin
118
+ if value['url'] == 'script'
119
+ cur_el = cpee_model.find("//d:call[@id='#{i}']").first
120
+ alt = cpee_model.find('string(//d:call/@a:alt_id)')
121
+ info = XML::Smart.string('<manipulate id="" label="" a:alt_id=""></manipulate>')
122
+ info.root.attributes['a:alt_id'] = alt
123
+ info.root.attributes['id'] = i
124
+ info.root.attributes['label'] = value['label']
125
+ info.root.add("code", value['script'])
126
+ cpee_model.find("//d:*[@id='#{i}']").first.replace_by(info.root)
127
+ else
128
+ cur_task = cpee_model.find("//d:call[@id='#{i}']").first
129
+ label = cur_task.find('string(d:parameters/d:label)')
130
+ #short_l = label.gsub(" ", "").downcase
131
+ short_l = label.gsub(/[^a-zA-Z0-9]/, '').downcase
132
+ cur_task.attributes["endpoint"] = short_l
133
+ endxml.root.add(short_l).text = value["url"]
134
+ # set input if exists
135
+ if value['input'].length > 0
136
+ args = cur_task.find('d:parameters/d:arguments').first
137
+ value["input"].each do |i,iv|
138
+ if iv.is_a?(String)
139
+ iv = iv.start_with?("data.") ? "!#{iv}" : iv
140
+ end
141
+ args.add(i, iv)
142
+ end
143
+ end
144
+ end
145
+ rescue StandardError => e
146
+ puts "task and script task are not consistent"
147
+ raise e
148
+ end
149
+
150
+ #set output if exists
151
+ code_fragment = XML::Smart.string(<<~XML)
152
+ <code>
153
+ <prepare/>
154
+ <finalize output="result"/>
155
+ <update output="result"/>
156
+ <rescue output="result"/>
157
+ </code>
158
+ XML
159
+
160
+ if !value['output'].nil?
161
+ result = value['output'].map { |k, v| "#{k} = #{v}" }.join("; ")
162
+ cur_task.add(code_fragment.root)
163
+ finalize = cur_task.find('//d:finalize').first
164
+ finalize.text = result
165
+ end
166
+ end
167
+
168
+ pp json_e
169
+ pp cpee_model
170
+ # ==============================conditions (only alternative)=======================
171
+ json_e['gateway_conditions'].each do |i, value|
172
+ if i.end_with?("s")
173
+ sid = i.chomp("s")
174
+ choose = cpee_model.find("//d:choose[@eid = '#{sid}']").first
175
+ if choose.nil?
176
+ loop = cpee_model.find("//d:loop[@eid = '#{sid}']").first
177
+ if loop.nil?
178
+ #do nothing it is parralel
179
+ else
180
+ cur_cond = CGI.unescapeHTML(loop.attributes['condition'])
181
+ loop.attributes['condition'] = value['branches'][cur_cond]
182
+ end
183
+ else
184
+ cur_gateway = cpee_model.find("//d:choose[@eid = '#{sid}']/d:alternative")
185
+ cur_gateway.each do |branch|
186
+ cur_cond = CGI.unescapeHTML(branch.attributes['condition'])
187
+ branch.attributes['condition'] = value['branches'][cur_cond]
188
+ end
189
+ end
190
+ end
191
+ end
192
+ return cpee_model.serialize, endxml.serialize
193
+ end
194
+
195
+ end
196
+
197
+ end
198
+
199
+ end
@@ -0,0 +1,248 @@
1
+ # This file is part of CPEE-LLM.
2
+ #
3
+ # CPEE-LLM is free software: you can redistribute it and/or modify it under the
4
+ # terms of the GNU Lesser General Public License as published by the Free
5
+ # Software Foundation, either version 3 of the License, or (at your option) any
6
+ # later version.
7
+ #
8
+ # CPEE-LLM is distributed in the hope that it will be useful, but WITHOUT ANY
9
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
10
+ # A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11
+ # details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public License
14
+ # along with CPEE-LLM (file LICENSE in the main directory). If not, see
15
+ # <http://www.gnu.org/licenses/>.
16
+
17
+ require_relative 'rubyllm_requests'
18
+ require 'json'
19
+
20
+ module CPEE
21
+
22
+ module LLM
23
+
24
+ module Functions
25
+
26
+ include RubyLLM_Requests
27
+
28
+ def cpee_to_mermaid(cpee) #{{{
29
+ srv = Riddl::Client.new('http://localhost:9295/mermaid/cpee')
30
+ status, res = srv.post [
31
+ Riddl::Parameter::Complex.new("description","text/xml",cpee),
32
+ Riddl::Parameter::Simple.new("type","description")
33
+ ]
34
+ if status >= 200 && status < 300
35
+ res
36
+ else
37
+ raise 'error when converting cpee to mermaid'
38
+ end
39
+ return res[0].value().read()
40
+ end #}}}
41
+
42
+ def mermaid_to_cpee(mermaid) #{{{
43
+ srv = Riddl::Client.new('http://localhost:9295/cpee/mermaid')
44
+ status, res = srv.post [
45
+ Riddl::Parameter::Complex.new("description","text/plain",mermaid),
46
+ Riddl::Parameter::Simple.new("type","description")
47
+ ]
48
+ if status >= 200 && status < 300
49
+ res
50
+ else
51
+ raise 'error when converting mermaid to cpee'
52
+ end
53
+ return res[0].value().read()
54
+ end #}}}
55
+
56
+
57
+ def generate_model(myllm,user_input,temperature,llms) #{{{
58
+ begin
59
+ llm_response = generate_mermaid_model(myllm,user_input,temperature,llms)
60
+ # raise exceptions if response is empty for some reason
61
+ if llm_response.nil? || llm_response.empty?
62
+ raise LLMError.new("Something went wrong and your content was not generated!", 500)
63
+ else
64
+ return llm_response
65
+ end
66
+ rescue LLMError => e_llm
67
+ e_llm.message
68
+ raise e_llm
69
+ rescue Exception => e
70
+ e.message
71
+ raise e
72
+ end
73
+ end #}}}
74
+
75
+ def adapt_model(myllm,doc,user_input,llms) #{{{
76
+ input_cpee = doc.to_s()
77
+ input_mermaid = cpee_to_mermaid(doc.to_s())
78
+ begin
79
+ llm_response = adapt_mermaid_model(myllm,user_input,input_mermaid,llms)
80
+ # raise exceptions if response is empty for some reason
81
+ if llm_response.nil? || llm_response.empty?
82
+ raise LLMError.new("Something went wrong and your content was not generated!", 500)
83
+ else
84
+ return llm_response
85
+ end
86
+ rescue LLMError => e_llm
87
+ raise e_llm
88
+ rescue Exception => e
89
+ raise e
90
+ end
91
+ end #}}}
92
+
93
+ def adapt_cpee_model(myllm,doc,user_input,existing_endpoints,endpoints,llms) #{{{
94
+ testset = XML::Smart.string(<<~XML)
95
+ <testset xmlns="http://cpee.org/ns/properties/2.0">
96
+ </testset>
97
+ XML
98
+ root = testset.root
99
+ root.add(existing_endpoints.root)
100
+ dslx = root.add("dslx")
101
+ dslx.add(doc.root)
102
+
103
+ #puts testset.to_s
104
+
105
+ begin
106
+ llm_response = adapt_xml_model(myllm,user_input,testset,endpoints,llms)
107
+ # raise exceptions if response is empty for some reason
108
+ if llm_response.nil? || llm_response.empty?
109
+ raise LLMError.new("Something went wrong and your content was not generated!", 500)
110
+ else
111
+ llm_response = llm_response.strip
112
+ inside = llm_response.scan(/```(\w+)?\s*\n(.*?)\n```/m)
113
+ llm_response = inside.empty? ? llm_response : inside[0][1]
114
+ #check if response is xml:
115
+ begin
116
+ XML::Smart.string(llm_response)
117
+ rescue Nokogiri::XML::SyntaxError => e
118
+ raise LLMError.new("Something went wrong and llm was not able to generate valid xml model: #{llm_response}", 500)
119
+ end
120
+ return llm_response
121
+ end
122
+ rescue LLMError => e_llm
123
+ raise e_llm
124
+ rescue Exception => e
125
+ raise e
126
+ end
127
+ end #}}}
128
+
129
+ def generate_text(myllm,doc,llms) #{{{
130
+ input_cpee = doc.to_s()
131
+ input_mermaid = cpee_to_mermaid(doc.to_s())
132
+ begin
133
+ llm_response = generate_plain_text(myllm,input_mermaid,llms)
134
+ # raise exceptions if response is empty for some reason
135
+ if llm_response.nil? || llm_response.empty?
136
+ raise LLMError.new("Something went wrong and your content was not generated!", 500)
137
+ else
138
+ return llm_response
139
+ end
140
+ rescue LLMError => e_llm
141
+ e_llm.message
142
+ raise e_llm
143
+ rescue Exception => e
144
+ e.message
145
+ raise e
146
+ end
147
+ end #}}}
148
+
149
+ def generate_generic(myllm,user_input,system_prompt,format,temperature,llms) #{{{
150
+ begin
151
+ llm_response = generate_generic_content(myllm, user_input, system_prompt, format, temperature, llms)
152
+ # raise exceptions if response is empty for some reason
153
+ if llm_response.nil? || llm_response.empty?
154
+ raise LLMError.new("Something went wrong and your content was not generated!", 500)
155
+ else
156
+ return llm_response
157
+ end
158
+ rescue LLMError => e_llm
159
+ e_llm.message
160
+ raise e_llm
161
+ rescue Exception => e
162
+ e.message
163
+ raise e
164
+ end
165
+ end #}}}
166
+
167
+ def generate_dataflow(myllm,mermaid_model,api_specification,llms) #{{{
168
+ begin
169
+ llm_response = generate_dataflow_content(myllm, mermaid_model, api_specification, llms)
170
+ # raise exceptions if response is empty for some reason
171
+ if llm_response.nil? || llm_response.empty?
172
+ raise LLMError.new("Something went wrong and your content was not generated!", 500)
173
+ else
174
+ #check if markdown is there:
175
+ llm_response = llm_response.strip
176
+ inside = llm_response.scan(/```(\w+)?\s*\n(.*?)\n```/m)
177
+ # variable = condition? if true: if false
178
+ llm_response = inside.empty? ? llm_response : inside[0][1]
179
+ #check if response is json:
180
+ begin
181
+ pp "here"
182
+ hash = JSON.parse(llm_response)
183
+ rescue JSON::ParserError => e
184
+ pp "there"
185
+ raise LLMError.new("Something went wrong and llm was not able to generate Json data flow: #{llm_response}", 500)
186
+ end
187
+ return llm_response
188
+ end
189
+ rescue LLMError => e_llm
190
+ e_llm.message
191
+ raise e_llm
192
+ rescue Exception => e
193
+ e.message
194
+ raise e
195
+ end
196
+ end #}}}
197
+
198
+ def generate_endpoint_model(myllm,user_input,endpoints,llms) #{{{
199
+ begin
200
+ llm_response = generate_endpoint_mermaid_model(myllm,user_input,endpoints,llms)
201
+ # raise exceptions if response is empty for some reason
202
+ if llm_response.nil? || llm_response.empty?
203
+ raise LLMError.new("Something went wrong and your content was not generated!", 500)
204
+ else
205
+ return llm_response
206
+ end
207
+ rescue LLMError => e_llm
208
+ e_llm.message
209
+ raise e_llm
210
+ rescue Exception => e
211
+ e.message
212
+ raise e
213
+ end
214
+ end #}}}
215
+
216
+ def validate_cpee_model(myllm,cpee_model,llms) #{{{
217
+ begin
218
+ llm_response = validate_xml_model(myllm,cpee_model,llms)
219
+ if llm_response.nil? || llm_response.empty?
220
+ raise LLMError.new("Something went wrong and your content was not generated!", 500)
221
+ elsif llm_response.strip.downcase == "perfect"
222
+ return cpee_model
223
+ else
224
+ llm_response = llm_response.strip
225
+ inside = llm_response.scan(/```(\w+)?\s*\n(.*?)\n```/m)
226
+ llm_response = inside.empty? ? llm_response : inside[0][1]
227
+ #check if response is xml:
228
+ begin
229
+ XML::Smart.string(llm_response)
230
+ rescue Nokogiri::XML::SyntaxError => e
231
+ raise LLMError.new("Something went wrong and llm was not able to generate valid xml model", llm_response)
232
+ end
233
+ return llm_response
234
+ end
235
+ rescue LLMError => e_llm
236
+ e_llm.message
237
+ raise e_llm
238
+ rescue Exception => e
239
+ e.message
240
+ raise e
241
+ end
242
+ end #}}}
243
+
244
+ end
245
+
246
+ end
247
+
248
+ end