luo 0.1.25 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +7 -4
  3. data/exe/luo +2 -3
  4. data/lib/luo/cli/init_app.rb +17 -0
  5. data/lib/luo/cli/init_base.rb +45 -0
  6. data/lib/luo/cli/init_notebook.rb +20 -0
  7. data/lib/luo/cli.rb +33 -49
  8. data/lib/luo/helpers.rb +54 -0
  9. data/lib/luo/{init_project.rb → init_project._rb} +1 -1
  10. data/lib/luo/marqo.rb +1 -1
  11. data/lib/luo/open_ai_agent_runner.rb +15 -4
  12. data/lib/luo/parser_markdown.rb +34 -0
  13. data/lib/luo/project_init.rb +9 -0
  14. data/lib/luo/prompt_template.rb +7 -3
  15. data/lib/luo/prompts.rb +30 -2
  16. data/lib/luo/version.rb +1 -1
  17. data/lib/luo/xinghuo.rb +9 -8
  18. data/lib/luo/xinghuo_agent_runner.rb +7 -3
  19. data/lib/luo.rb +24 -2
  20. data/luo.gemspec +4 -1
  21. data/sig/luo/prompt_template.rbs +1 -1
  22. data/templates/AppGemfile +3 -0
  23. data/templates/NoteBookGemfile +5 -0
  24. data/{lib/luo/projects → templates}/application.rb +5 -1
  25. data/templates/luo.ipynb +292 -0
  26. data/templates/prompts/luo_error.md.erb +8 -0
  27. metadata +70 -46
  28. data/.idea/.gitignore +0 -8
  29. data/.idea/dictionaries/mj.xml +0 -7
  30. data/.idea/inspectionProfiles/Project_Default.xml +0 -9
  31. data/.idea/luo.iml +0 -81
  32. data/.idea/misc.xml +0 -4
  33. data/.idea/modules.xml +0 -8
  34. data/.idea/vcs.xml +0 -6
  35. data/init.rb +0 -16
  36. data/lib/luo/projects/luo.ipynb +0 -167
  37. data/lib/luo/templates/agent_input.md.erb +0 -39
  38. data/lib/luo/templates/agent_system.md.erb +0 -4
  39. data/lib/luo/templates/agent_tool_input.md.erb +0 -10
  40. data/lib/luo/templates/demo.md.erb +0 -1
  41. data/lib/luo/templates/luo_xinghuo_agent_tool_input.md.erb +0 -16
  42. data/lib/luo/templates/xinghuo_agent_input.md.erb +0 -10
  43. data/lib/luo/templates/xinghuo_response_error.md.erb +0 -10
  44. data/luo.ipynb +0 -84
  45. data/templates/luo_agent_input.md.erb +0 -39
  46. data/templates/luo_agent_system.md.erb +0 -4
  47. data/templates/luo_agent_tool_input.md.erb +0 -10
  48. data/templates/luo_commit.md.erb +0 -2
  49. data/templates/luo_xinghuo_agent_input.md.erb +0 -10
  50. data/templates/luo_xinghuo_agent_tool_input.md.erb +0 -16
  51. data/templates/luo_xinghuo_response_error.md.erb +0 -10
  52. data/templates/prompt.demo.erb +0 -1
  53. /data/{lib/luo/projects → templates}/env +0 -0
  54. /data/{lib/luo/projects → templates}/init.rb +0 -0
  55. /data/{lib/luo/projects → templates}/prompts/agent_input.md.erb +0 -0
  56. /data/{lib/luo/projects → templates}/prompts/agent_system.md.erb +0 -0
  57. /data/{lib/luo/projects → templates}/prompts/agent_tool_input.md.erb +0 -0
  58. /data/{lib/luo/templates → templates/prompts}/luo_commit.md.erb +0 -0
  59. /data/{lib/luo/projects → templates}/prompts/xinghuo_agent_input.md.erb +0 -0
  60. /data/{lib/luo/projects → templates}/prompts/xinghuo_response_error.md.erb +0 -0
  61. /data/{lib/luo/projects → templates}/test.yml +0 -0
  62. /data/{lib/luo/projects → templates}/time_agent.rb +0 -0
  63. /data/{lib/luo/projects → templates}/weather_agent.rb +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 12601aff9972b8acace601e1fd79ed219577d8063379f0c2c8677c6af24a731b
4
- data.tar.gz: ae7ccf1bed46c7ff1eeea7328a034b6557674f4fc1c2df5135ead23f4eb5fa70
3
+ metadata.gz: b6fd7dfc689ad963f3f3e8c9f394242ca4aac334eccfe17ac0945397911a55b1
4
+ data.tar.gz: 4b0ddf9adeb237187ab8dc44c152502f1e0831b1b4ad9ea44aef880672de008f
5
5
  SHA512:
6
- metadata.gz: 79bf668ce8fbef426bc81409e159401a9eb301d38dadb9e3b5af21dd913477e47ef873cab930caebdc51e50ef84c2976d1bf43cb8e82db7746ad66df6deaddc0
7
- data.tar.gz: c6d75bba67f02b51c4a99e38a9651687534e947cb0bed38b49534691d5c5a1a23e8a7ee80ba328bd579f21a72c12acec825858c986949cdb0fe95577daa97989
6
+ metadata.gz: 90121e2c75de091278163ef0914cea6bb2c1d32683b3b7dec5b4a487e7a9b4fe1eb3a38f615dfceb4cde71b0727fd3e46679fa6688ddb8ef2f47b8ca278f9886
7
+ data.tar.gz: 75ebe8f70681cc926bfcab440b1b5d25c2c2f20dd180c66022df540f2459eab83989011e2c020208297921ff801c025e335b4036179472865653bbbb56b6a369
data/Gemfile.lock CHANGED
@@ -1,13 +1,15 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- luo (0.1.25)
4
+ luo (0.2.1)
5
5
  dotenv (~> 2.8, >= 2.8.1)
6
- dry-cli (~> 1.0)
7
6
  dry-configurable (~> 1.0, >= 1.0.1)
8
7
  dry-schema (~> 1.13, >= 1.13.1)
9
8
  faraday (~> 2.7, >= 2.7.4)
10
9
  faraday-retry (~> 2.1)
10
+ redcarpet (~> 3.6)
11
+ rouge (~> 4.1, >= 4.1.1)
12
+ thor (~> 1.2, >= 1.2.2)
11
13
  tty-markdown (~> 0.7.2)
12
14
  zeitwerk (~> 2.6, >= 2.6.8)
13
15
 
@@ -17,7 +19,6 @@ GEM
17
19
  concurrent-ruby (1.2.2)
18
20
  diff-lcs (1.5.0)
19
21
  dotenv (2.8.1)
20
- dry-cli (1.0.0)
21
22
  dry-configurable (1.0.1)
22
23
  dry-core (~> 1.0, < 2)
23
24
  zeitwerk (~> 2.6)
@@ -55,8 +56,9 @@ GEM
55
56
  pastel (0.8.0)
56
57
  tty-color (~> 0.5)
57
58
  rake (13.0.6)
59
+ redcarpet (3.6.0)
58
60
  rexml (3.2.5)
59
- rouge (4.1.0)
61
+ rouge (4.1.1)
60
62
  rspec (3.12.0)
61
63
  rspec-core (~> 3.12.0)
62
64
  rspec-expectations (~> 3.12.0)
@@ -76,6 +78,7 @@ GEM
76
78
  unicode-display_width (>= 1.5, < 3.0)
77
79
  unicode_utils (~> 1.4)
78
80
  strings-ansi (0.2.0)
81
+ thor (1.2.2)
79
82
  tty-color (0.6.0)
80
83
  tty-markdown (0.7.2)
81
84
  kramdown (>= 1.16.2, < 3.0)
data/exe/luo CHANGED
@@ -1,7 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- require "bundler/setup"
5
- require "luo"
4
+ require_relative '../lib/luo'
6
5
 
7
- Dry::CLI.new(Luo::CLI::Commands).call
6
+ Luo::CLI::APP.start
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Luo
4
+ module CLI
5
+ class InitApp < InitBase
6
+ def copy_app
7
+ say "Copying App...", :green
8
+ copy_file "application.rb", "app.rb"
9
+ end
10
+
11
+ def copy_gemfile
12
+ say "Copying Gemfile...", :green
13
+ copy_file "AppGemfile", "Gemfile"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Luo
4
+ module CLI
5
+ class InitBase < Thor::Group
6
+ include Thor::Actions
7
+ def create_dirtories
8
+ say "Creating directories...", :green
9
+ empty_directory("prompts")
10
+ empty_directory("agents")
11
+ end
12
+
13
+ def copy_files
14
+ say "Copying OpenAI Prompts...", :green
15
+ copy_file "prompts/agent_input.md.erb", "prompts/agent_input.md.erb"
16
+ copy_file "prompts/agent_system.md.erb", "prompts/agent_system.md.erb"
17
+ copy_file "prompts/agent_tool_input.md.erb", "prompts/agent_tool_input.md.erb"
18
+
19
+ say "Copying Xinghuo Prompts...", :green
20
+ copy_file "prompts/xinghuo_agent_input.md.erb", "prompts/xinghuo_agent_input.md.erb"
21
+ copy_file "prompts/xinghuo_response_error.md.erb", "prompts/xinghuo_response_error.md.erb"
22
+ end
23
+
24
+ def copy_agents
25
+ say "Copying Agents Demo...", :green
26
+ copy_file "time_agent.rb", "agents/time_agent.rb"
27
+ copy_file "weather_agent.rb", "agents/weather_agent.rb"
28
+ end
29
+
30
+ def copy_env
31
+ say "Copying luo.env...", :green
32
+ copy_file "env", "luo.env"
33
+ end
34
+
35
+ def copy_test_file
36
+ say "Copying test file...", :green
37
+ copy_file "test.yml", "test.yml"
38
+ end
39
+
40
+ def self.source_root
41
+ Pathname.new(File.join(__dir__, "../../../templates")).expand_path
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Luo
4
+ module CLI
5
+ class InitNotebook < InitBase
6
+
7
+ desc "Initialize a notebook project"
8
+ def copy_notebook
9
+ say "Copying Notebook...", :green
10
+ copy_file "luo.ipynb", "luo.ipynb"
11
+ end
12
+
13
+ def copy_gemfile
14
+ say "Copying Gemfile...", :green
15
+ copy_file "NotebookGemfile", "Gemfile"
16
+ end
17
+
18
+ end
19
+ end
20
+ end
data/lib/luo/cli.rb CHANGED
@@ -2,70 +2,54 @@
2
2
 
3
3
  module Luo
4
4
  module CLI
5
- module Commands
6
- extend Dry::CLI::Registry
5
+ class APP < Thor
7
6
 
8
- class Version < Dry::CLI::Command
9
- desc "Print version"
7
+ map %w[--version -v] => :__print_version
8
+ map 'c' => :commit
10
9
 
11
- def call(*)
12
- puts Luo::VERSION
13
- end
10
+ desc "--version, -v", "print the version"
11
+ def __print_version
12
+ puts "Luo version #{Luo::VERSION}"
14
13
  end
15
14
 
16
- class Init < Dry::CLI::Command
17
- desc "Init Luo Project"
18
-
19
- def call(*)
20
- Luo::InitProject.run
21
- end
22
- end
23
-
24
- class Commit < Dry::CLI::Command
25
- desc "Commit with Luo"
26
-
27
- argument :message, desc: "Commit message", required: true, type: :string
28
-
29
- def call(message:, **)
30
- messages = Messages.create.system(prompt: Luo::Prompts.luo_commit, context: {commit: message}).to_a
15
+ desc "commit <message>", "git commit with luo"
16
+ def commit(message)
17
+ messages = Messages.create.user(prompt: Luo::Prompts.luo_commit, context: {commit: message}).to_a
18
+ if ENV["LUO_LLM_MODE"] == "openai"
31
19
  response = OpenAI.new.chat(messages)
32
- exec "git commit -m '#{response}'"
20
+ else
21
+ response = Xinghuo.new.chat(messages)
33
22
  end
23
+ exec "git commit -m '#{response}'"
34
24
  end
35
25
 
36
- class Run < Dry::CLI::Command
37
- desc "Run Luo"
38
-
39
- def call(*)
40
- exec "jupyter lab --ip=0.0.0.0 --port=8888 --no-browser --allow-root"
26
+ desc "init", "initialize luo"
27
+ option :type, aliases: "-t", default: "notebook", desc: "type of luo project", enum: %w[notebook app]
28
+ def init
29
+ if options[:type] == "notebook"
30
+ invoke InitNotebook
31
+ elsif options[:type] == "app"
32
+ invoke InitApp
41
33
  end
42
34
  end
43
35
 
44
- class Bundle < Dry::CLI::Command
45
- desc "Bundle with Luo"
46
-
47
- def call(*)
48
- exec "bundle install"
49
- end
50
- end
36
+ def self.exit_on_failure?
37
+ if $! && $!.is_a?(Thor::Error)
51
38
 
52
- class Exec < Dry::CLI::Command
53
- desc "Exec with Luo"
54
- argument :task, type: :string, required: true
39
+ command = "luo " + ARGV.join(' ')
40
+ messages = Messages.create.user(prompt: Luo::Prompts.luo_error, context: {error: $!.message, command: command}).to_a
41
+ if ENV["LUO_LLM_MODE"] == "openai"
42
+ response = OpenAI.new.chat(messages)
43
+ else
44
+ response = Xinghuo.new.chat(messages)
45
+ end
55
46
 
56
- def call(task: ,**)
57
- exec task
47
+ shell = Thor::Shell::Color.new
48
+ shell.say "\n #{response}", :green
58
49
  end
50
+ true
59
51
  end
60
52
 
61
- register "version", Version, aliases: %w[v -v --version]
62
- register "commit", Commit, aliases: ["c"]
63
- register "init", Init, aliases: ["i"]
64
- register "run", Run, aliases: ["r"]
65
- register "bundle", Bundle, aliases: ["b"]
66
- register "exec", Exec, aliases: ["e"]
67
-
68
53
  end
69
-
70
54
  end
71
- end
55
+ end
data/lib/luo/helpers.rb CHANGED
@@ -2,12 +2,58 @@
2
2
 
3
3
  module Luo
4
4
  module Helpers
5
+
6
+ class HTMLwithRouge < Redcarpet::Render::HTML
7
+
8
+ def block_code(code, language)
9
+ lexer = Rouge::Lexer.find(language) || Rouge::Lexers::PlainText
10
+ formatter = Rouge::Formatters::HTML.new
11
+ highlighted_code = formatter.format(lexer.lex(code))
12
+ "<pre class=\"highlight\">#{highlighted_code}</pre>"
13
+ end
14
+ end
15
+
5
16
  extend self
6
17
 
7
18
  def print_md(text)
8
19
  puts TTY::Markdown.parse(text)
9
20
  end
10
21
 
22
+ def display_md(text)
23
+ unless gem_exists?('iruby')
24
+ raise "Please install iruby gem first."
25
+ end
26
+
27
+ if gem_exists?('rouge')
28
+ renderer = HTMLwithRouge.new
29
+ else
30
+ renderer = Redcarpet::Render::HTML.new
31
+ end
32
+
33
+ markdown = Redcarpet::Markdown.new(renderer,
34
+ autolink: true,
35
+ tables: true,
36
+ strikethrough: true,
37
+ highlight: true,
38
+ prettify: true,
39
+ fenced_code_blocks: true)
40
+ body = markdown.render(text)
41
+ css = Rouge::Themes::Github.mode(:light).render(scope: '.highlight')
42
+ html = <<~HTML
43
+ <style>#{css}</style>
44
+ #{body}
45
+ HTML
46
+ # puts html
47
+ IRuby.display(IRuby.html(html))
48
+ end
49
+
50
+ def display_html(text)
51
+ unless gem_exists?('iruby')
52
+ raise "Please install iruby gem first."
53
+ end
54
+ IRuby.display(IRuby.html(text))
55
+ end
56
+
11
57
  def load_test(path, &block)
12
58
  data = YAML.load_file(path)
13
59
  if data.is_a?(Array)
@@ -18,5 +64,13 @@ module Luo
18
64
  yield(data)
19
65
  end
20
66
  end
67
+
68
+ def gem_exists?(gem_name)
69
+ Gem::Specification.find_by_name(gem_name)
70
+ true
71
+ rescue Gem::LoadError
72
+ false
73
+ end
74
+
21
75
  end
22
76
  end
@@ -13,7 +13,7 @@ module Luo
13
13
  unless File.exist?('Gemfile')
14
14
  gemfile = <<-GEMFILE.gsub(/^ */, '')
15
15
  source 'https://rubygems.org'
16
- gem 'luo', '~> #{version}'
16
+ gem 'luo'
17
17
  gem 'iruby'
18
18
  gem 'pry'
19
19
  GEMFILE
data/lib/luo/marqo.rb CHANGED
@@ -5,7 +5,7 @@ module Luo
5
5
  include Configurable
6
6
  setting :host, default: "http://localhost:8882"
7
7
  setting :x_api_key, default: nil
8
- setting :retries, default: ENV.fetch('OPENAI_REQUEST_RETRIES', 3).to_i
8
+ setting :retries, default: 3
9
9
 
10
10
  include HttpClient.init_client(headers: { 'X-API-KEY' => config.x_api_key })
11
11
 
@@ -6,6 +6,11 @@ module Luo
6
6
  include Configurable
7
7
 
8
8
  setting :retires, default: 3
9
+ setting :prompts do
10
+ setting :input, default: Luo::Prompts.agent_input
11
+ setting :system, default: Luo::Prompts.agent_system
12
+ setting :tool_input, default: Luo::Prompts.agent_tool_input
13
+ end
9
14
 
10
15
  on_init do
11
16
  @openai = OpenAI.new
@@ -13,13 +18,19 @@ module Luo
13
18
 
14
19
  on_request do
15
20
  context.messages = Messages.create(history: context.histories)
16
- .system(prompt: Luo::Prompts.agent_system)
17
- .user(prompt: Luo::Prompts.agent_input, context: {agents: self.class.agents, last_user_input: context.user_input})
21
+ .system(prompt: config.prompts.system)
22
+ .user(prompt: config.prompts.input, context: {agents: self.class.agents, last_user_input: context.user_input})
18
23
  context.response = @openai.chat(context.messages)
19
24
  end
20
25
 
26
+ ##
27
+ # TODO: 用markdown解析库来解析response
21
28
  on_result do
22
- actions = JSON.parse(context.response)
29
+ begin
30
+ actions = JSON.parse(context.response)
31
+ rescue JSON::ParserError => e
32
+ actions = JSON.parse ParserMarkdown.new(context.response).code
33
+ end
23
34
  actions = [actions] if actions.is_a?(Hash)
24
35
  actions.each do |action|
25
36
  agent = self.class.agents[action['action']]&.new(
@@ -39,7 +50,7 @@ module Luo
39
50
  after_run do
40
51
  if context.retries < config.retires && context.final_result.nil?
41
52
  context.messages = context.messages.assistant(
42
- prompt: Luo::Prompts.agent_tool_input,
53
+ prompt: config.prompts.tool_input,
43
54
  context: {
44
55
  tools_response: context.agent_results
45
56
  }
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Luo
4
+ class ParserMarkdown
5
+ def initialize(text)
6
+ @renderer = CustomRenderer.new
7
+ @markdown = Redcarpet::Markdown.new(@renderer, fenced_code_blocks: true)
8
+ @text = text
9
+
10
+ parse @text
11
+ end
12
+
13
+ def code
14
+ @renderer.code
15
+ end
16
+
17
+ def language
18
+ @renderer.language
19
+ end
20
+
21
+ def parse(text)
22
+ @markdown.render(text)
23
+ end
24
+
25
+ class CustomRenderer < Redcarpet::Render::HTML
26
+ attr_reader :code, :language
27
+
28
+ def block_code(code, language)
29
+ @code = code
30
+ @language = language
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Luo
4
+ class ProjectInit
5
+ def initialize(app_type: "notebook")
6
+ @app_type = app_type
7
+ end
8
+ end
9
+ end
@@ -2,9 +2,13 @@
2
2
 
3
3
  module Luo
4
4
  class PromptTemplate
5
- def initialize(file_path)
5
+ def initialize(file_path = nil, text: nil)
6
6
  @file_path = file_path
7
- @template = ERB.new(File.read(@file_path), trim_mode: '-')
7
+ if text.nil? && !@file_path.nil?
8
+ @template = ERB.new(File.read(@file_path), trim_mode: '-')
9
+ else
10
+ @template = ERB.new(text, trim_mode: '-')
11
+ end
8
12
  end
9
13
 
10
14
  def render(data = {})
@@ -31,7 +35,7 @@ module Luo
31
35
  end
32
36
 
33
37
  def load_template(file_name)
34
- self.new File.join(File.dirname(__FILE__), 'templates', file_name)
38
+ self.new File.join(__dir__, '../../templates', 'prompts', file_name)
35
39
  end
36
40
  end
37
41
 
data/lib/luo/prompts.rb CHANGED
@@ -4,7 +4,7 @@ module Luo
4
4
  module Prompts
5
5
  extend Dry::Configurable
6
6
 
7
- setting :gem_templates_dir, default: "#{__dir__}/templates"
7
+ setting :gem_templates_dir, default: "#{__dir__}/../../templates/prompts"
8
8
  setting :prompts_dir, default: "#{Dir.pwd}/prompts"
9
9
 
10
10
  extend self
@@ -15,7 +15,8 @@ module Luo
15
15
  end
16
16
 
17
17
  def define_templates_in_dir(dir_path)
18
- Dir.glob("#{dir_path}/*.md.erb") do |file|
18
+ path = Pathname.new(dir_path).expand_path.join("*.md.erb").to_s
19
+ Dir.glob(path) do |file|
19
20
  template_name = File.basename(file, '.md.erb').to_sym
20
21
  define_template(template_name, file)
21
22
  end
@@ -28,6 +29,33 @@ module Luo
28
29
  end
29
30
 
30
31
  define_templates
32
+
33
+ def method_missing(method_name, *args, &block)
34
+ template_file = template_file_path(method_name)
35
+ if template_file
36
+ define_template(method_name, template_file)
37
+ send(method_name)
38
+ else
39
+ super
40
+ end
41
+ end
42
+
43
+ def respond_to_missing?(method_name, include_private = false)
44
+ template_file_path(method_name) || super
45
+ end
46
+
47
+ private
48
+
49
+ def template_file_path(template_name)
50
+ gem_template_file = "#{config.gem_templates_dir}/#{template_name}.md.erb"
51
+ prompts_template_file = "#{config.prompts_dir}/#{template_name}.md.erb"
52
+
53
+ return prompts_template_file if File.exist?(prompts_template_file)
54
+ return gem_template_file if File.exist?(gem_template_file)
55
+
56
+ nil
57
+ end
58
+
31
59
  end
32
60
  end
33
61
 
data/lib/luo/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Luo
4
- VERSION = "0.1.25"
4
+ VERSION = "0.2.1"
5
5
  end
data/lib/luo/xinghuo.rb CHANGED
@@ -4,11 +4,11 @@ module Luo
4
4
  class Xinghuo
5
5
  include Configurable
6
6
 
7
- setting :access_token, default: ENV.fetch('XINGHUO_ACCESS_TOKEN')
8
- setting :retries, default: ENV.fetch('XINGHUO_REQUEST_RETRIES', 3).to_i
9
- setting :host, default: ENV.fetch('XINGHUO_HOST', 'https://integration-api.iflyos.cn/')
10
- setting :history_limit, default: ENV.fetch('XINGHUO_LIMIT_HISTORY', '6').to_i * 2
11
- setting :temperature, default: ENV.fetch('XINGHUO_TEMPERATURE', 0).to_i
7
+ setting :access_token, default: ENV.fetch('LISTENAI_ACCESS_TOKEN')
8
+ setting :retries, default: ENV.fetch('LISTENAI_REQUEST_RETRIES', 3).to_i
9
+ setting :host, default: ENV.fetch('LISTENAI_HOST', 'https://api.listenai.com')
10
+ setting :history_limit, default: ENV.fetch('LISTENAI_LIMIT_HISTORY', '6').to_i * 2
11
+ setting :random_threshold, default: ENV.fetch('LISTENAI_TEMPERATURE', 0).to_i
12
12
  setting :auditing, default: 'default'
13
13
  setting :domain, default: 'general'
14
14
  setting :max_tokens, default: 1024
@@ -22,15 +22,16 @@ module Luo
22
22
  optional(:domain).maybe(:string)
23
23
  optional(:max_tokens).maybe(:integer)
24
24
  optional(:random_threshold).maybe(:float)
25
+ optional(:uid).maybe(:string)
25
26
  end
26
27
 
27
28
  # header uid max length is 32 todo
28
29
 
29
30
  def request_chat(params)
30
- client.post('/external/ls_log/xf_completions', params.to_h)
31
+ client.post('/v1/spark/completions', params.to_h)
31
32
  end
32
33
 
33
- def chat(messages, temperature: nil)
34
+ def chat(messages, random_threshold: nil)
34
35
  if messages.is_a?(Messages)
35
36
  messages = messages.to_a
36
37
  end
@@ -39,7 +40,7 @@ module Luo
39
40
  domain: config.domain,
40
41
  messages: messages,
41
42
  max_tokens: config.max_tokens,
42
- random_threshold: temperature || config.temperature,
43
+ random_threshold: random_threshold || config.random_threshold,
43
44
  uid: config.uid.call
44
45
  )
45
46
  return params.errors unless params.success?
@@ -5,6 +5,10 @@ module Luo
5
5
  include Configurable
6
6
 
7
7
  setting :retires, default: 3
8
+ setting :prompts do
9
+ setting :input, default: Luo::Prompts.xinghuo_agent_input
10
+ setting :response_error, default: Luo::Prompts.xinghuo_response_error
11
+ end
8
12
 
9
13
  on_init do
10
14
  @xinghuo = Xinghuo.new
@@ -12,13 +16,13 @@ module Luo
12
16
 
13
17
  on_request do
14
18
  context.messages = Messages.create(history: context.histories)
15
- .user(prompt: Luo::Prompts.xinghuo_agent_input, context: {agents: self.class.agents, last_user_input: context.user_input})
19
+ .user(prompt: config.prompts.input, context: {agents: self.class.agents, last_user_input: context.user_input})
16
20
  response = @xinghuo.chat(context.messages)
17
21
  if response.split("\n").select { |line| line.size >1 }.size > 1
18
22
  message = Messages.create(history: context.histories)
19
- .user(prompt: Luo::Prompts.xinghuo_agent_input, context: {agents: self.class.agents, last_user_input: context.user_input})
23
+ .user(prompt: config.prompts.input, context: {agents: self.class.agents, last_user_input: context.user_input})
20
24
  .assistant(text: response)
21
- .user(prompt: Luo::Prompts.xinghuo_response_error, context: {agents: self.class.agents, last_user_input: context.user_input})
25
+ .user(prompt: config.prompts.response_error, context: {agents: self.class.agents, last_user_input: context.user_input})
22
26
  context.response = @xinghuo.chat(message)
23
27
  else
24
28
  context.response = response
data/lib/luo.rb CHANGED
@@ -3,18 +3,19 @@
3
3
  # require_relative "luo/version"
4
4
 
5
5
  require 'erb'
6
- require 'dotenv/load'
6
+ require 'dotenv'
7
7
  require 'json'
8
8
  require 'faraday'
9
9
  require 'faraday/retry'
10
10
  require 'dry-configurable'
11
11
  require 'dry-schema'
12
- require 'dry/cli'
13
12
  require 'yaml'
14
13
  require 'tty-markdown'
15
14
  require 'fileutils'
16
15
  require 'dry-initializer'
17
16
  require 'uri'
17
+ require 'redcarpet'
18
+ require 'thor'
18
19
 
19
20
  require "zeitwerk"
20
21
  loader = Zeitwerk::Loader.for_gem
@@ -27,4 +28,25 @@ loader.setup
27
28
  module Luo
28
29
  class Error < StandardError; end
29
30
  # Your code goes here...
31
+
32
+ module_eval do
33
+ Dotenv.load('luo.env', '.env')
34
+ end
35
+
36
+ def self.app_setup(&block)
37
+ TOPLEVEL_BINDING.eval('include Luo')
38
+ block.call(Loader) if block_given?
39
+ Loader.setup
40
+ end
41
+
42
+ def self.notebook_setup(&block)
43
+ TOPLEVEL_BINDING.eval('include Luo')
44
+ block.call(Loader) if block_given?
45
+ Loader.setup
46
+ if Helpers.gem_exists?('pry')
47
+ require 'pry'
48
+ IRuby::Kernel.instance.switch_backend!(:pry)
49
+ end
50
+ end
51
+
30
52
  end
data/luo.gemspec CHANGED
@@ -35,11 +35,14 @@ Gem::Specification.new do |spec|
35
35
  spec.add_dependency 'faraday-retry', '~> 2.1'
36
36
  spec.add_dependency 'dry-schema', '~> 1.13', '>= 1.13.1'
37
37
  spec.add_dependency 'dry-configurable', '~> 1.0', '>= 1.0.1'
38
- spec.add_dependency 'dry-cli', '~> 1.0'
39
38
  spec.add_dependency 'tty-markdown', '~> 0.7.2'
39
+ spec.add_dependency 'redcarpet', '~> 3.6'
40
+ spec.add_dependency 'rouge', '~> 4.1', '>= 4.1.1'
41
+ spec.add_dependency 'thor', '~> 1.2', '>= 1.2.2'
40
42
 
41
43
  spec.add_development_dependency "rspec", '~> 3.12'
42
44
 
45
+
43
46
  # For more information and examples about making a new gem, check out our
44
47
  # guide at: https://bundler.io/guides/creating_gem.html
45
48
  end
@@ -1,6 +1,6 @@
1
1
  module Luo
2
2
  class PromptTemplate
3
- @file_path: string
3
+ @file_path: String?
4
4
  @template: ERB
5
5
 
6
6
  def self.create: -> PromptTemplate