luo 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/.idea/inspectionProfiles/Project_Default.xml +9 -0
  3. data/.idea/luo.iml +26 -1
  4. data/.rspec +1 -0
  5. data/Gemfile.lock +62 -1
  6. data/README.md +3 -0
  7. data/README.zh.md +9 -0
  8. data/exe/luo +7 -0
  9. data/lib/luo/agent.rb +45 -0
  10. data/lib/luo/agent_runner_base.rb +89 -0
  11. data/lib/luo/agent_runner_context.rb +33 -0
  12. data/lib/luo/aiui.rb +41 -0
  13. data/lib/luo/cli.rb +33 -0
  14. data/lib/luo/configurable.rb +14 -0
  15. data/lib/luo/http_client.rb +21 -0
  16. data/lib/luo/memory_history.rb +55 -0
  17. data/lib/luo/messages.rb +50 -0
  18. data/lib/luo/open_ai.rb +48 -0
  19. data/lib/luo/open_ai_agent_runner.rb +55 -0
  20. data/lib/luo/prompt_template.rb +39 -0
  21. data/lib/luo/prompts.rb +32 -0
  22. data/lib/luo/templates/luo_agent_input.md.erb +39 -0
  23. data/lib/luo/templates/luo_agent_system.md.erb +4 -0
  24. data/lib/luo/templates/luo_agent_tool_input.md.erb +10 -0
  25. data/lib/luo/templates/luo_commit.md.erb +2 -0
  26. data/lib/luo/templates/luo_xinghuo_agent_input.md.erb +10 -0
  27. data/lib/luo/templates/luo_xinghuo_agent_tool_input.md.erb +16 -0
  28. data/lib/luo/templates/prompt.demo.erb +1 -0
  29. data/lib/luo/version.rb +1 -1
  30. data/lib/luo/xinghuo.rb +50 -0
  31. data/lib/luo/xinghuo_agent_runner.rb +56 -0
  32. data/lib/luo.rb +18 -1
  33. data/luo.gemspec +9 -0
  34. data/sig/luo/agent.rbs +21 -0
  35. data/sig/luo/agent_runner_base.rbs +25 -0
  36. data/sig/luo/agent_runner_context.rbs +19 -0
  37. data/sig/luo/aiui.rbs +9 -0
  38. data/sig/luo/configurable.rbs +5 -0
  39. data/sig/luo/env.rbs +9 -0
  40. data/sig/luo/http_client.rbs +9 -0
  41. data/sig/luo/memory_history.rbs +18 -0
  42. data/sig/luo/messages.rbs +20 -0
  43. data/sig/luo/open_ai.rbs +11 -0
  44. data/sig/luo/prompt_template.rbs +18 -0
  45. data/sig/luo/prompts.rbs +15 -0
  46. data/sig/luo/xinghuo.rbs +9 -0
  47. metadata +186 -4
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Luo
4
+ class PromptTemplate
5
+ def initialize(file_path)
6
+ @file_path = file_path
7
+ @template = ERB.new(File.read(@file_path), trim_mode: '-')
8
+ end
9
+
10
+ def render(data = {})
11
+ context = data.is_a?(Hash) ? data : data.each_slice(2).to_h
12
+ @template.result(context_binding(context))
13
+ end
14
+
15
+ def result()
16
+ @template.result(binding)
17
+ end
18
+
19
+ private
20
+ def context_binding(context)
21
+ context.each do |key, value|
22
+ instance_variable_set("@#{key}", value)
23
+ self.class.send(:attr_reader, key.to_sym)
24
+ end
25
+ binding
26
+ end
27
+
28
+ class << self
29
+ def create(file_path)
30
+ self.new(file_path)
31
+ end
32
+
33
+ def load_template(file_name)
34
+ self.new File.join(File.dirname(__FILE__), 'templates', file_name)
35
+ end
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Luo
4
+ module Prompts
5
+ TEMPLATES_DIR = "#{__dir__}/templates"
6
+ PWD = ENV['LUO_ENV'] == 'test' ? "#{Dir.pwd}/spec/luo" : Dir.pwd
7
+ RUNTIME_TEMPLATES_DIR = "#{PWD}/templates"
8
+
9
+ extend self
10
+
11
+ def define_templates
12
+ define_templates_in_dir(TEMPLATES_DIR)
13
+ define_templates_in_dir(RUNTIME_TEMPLATES_DIR)
14
+ end
15
+
16
+ def define_templates_in_dir(dir_path)
17
+ Dir.glob("#{dir_path}/*.md.erb") do |file|
18
+ template_name = File.basename(file, '.md.erb').to_sym
19
+ define_template(template_name, file)
20
+ end
21
+ end
22
+
23
+ def define_template(template_name, file_path)
24
+ define_method(template_name) do
25
+ PromptTemplate.create(file_path)
26
+ end
27
+ end
28
+
29
+ define_templates
30
+ end
31
+ end
32
+
@@ -0,0 +1,39 @@
1
+ TOOLS
2
+ ------
3
+ Assistant can ask the user to use tools to look up information that may be helpful in answering the users original question. The tools the human can use are:
4
+
5
+ <% agents.values.each do |agent| -%>
6
+ > <%= agent.agent_name %>: <%= agent.agent_desc %>
7
+ <% end -%>
8
+
9
+ RESPONSE FORMAT INSTRUCTIONS
10
+ ----------------------------
11
+
12
+ When responding to me please, please output a response in one of two formats:
13
+
14
+ **Option 1:**
15
+ Use this if you want humans to use the tools.
16
+ Markdown code snippet formatted in the following schema:
17
+
18
+ ```json
19
+ [{
20
+ "action": string \ The action to take. Must be one of <%= agents.values.map do |a| a.agent_name end.join(', ') %>
21
+ "action_input": string \ The input to the action
22
+ }]
23
+ ```
24
+
25
+ **Option #2:**
26
+ Use this if you want to respond directly to the human. Markdown code snippet formatted in the following schema:
27
+
28
+ ```json
29
+ {
30
+ "action": "Final Answer",
31
+ "action_input": string \ You should put what you want to return to use here <%= Luo::AgentRunnerBase.language_info %>
32
+ }
33
+ ```
34
+
35
+ USER'S INPUT
36
+ --------------------
37
+ Here is the user's input (Remember to respond to a single or multiple actions with a json blob of markdown code snippets, and NOTHING else):
38
+
39
+ <%= last_user_input %>
@@ -0,0 +1,4 @@
1
+ Assistant is a large language model trained by OpenAI.
2
+ Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
3
+ Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.
4
+ Overall, Assistant is a powerful system that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.
@@ -0,0 +1,10 @@
1
+ TOOL RESPONSE:
2
+ ---------------------
3
+ <% tools_response.each do |r| -%>
4
+ > <%= r[:agent_name] %>: <%= r[:agent_response] %>
5
+ <% end -%>
6
+
7
+ USER'S INPUT
8
+ --------------------
9
+
10
+ Okay, so what is the response to my last comment? If using information obtained from the tools you must mention it explicitly without mentioning the tool names - I have forgotten all TOOL RESPONSES! Remember to respond to a single or multiple actions with a json blob of markdown code snippets, and NOTHING else.
@@ -0,0 +1,2 @@
1
+ 你是一个git专家,我给你中文的commit,你提供合适的英文commit,然后我再提交到github上。
2
+ 我的提交说明是:<%= commit %>
@@ -0,0 +1,10 @@
1
+ 在回答的我的问题的时候,你可以使用以下工具:
2
+ <% agents.values.each do |agent| -%>
3
+ - <%= agent.agent_name %>:<%= agent.agent_desc %>
4
+ <% end -%>
5
+ 请用以下格式回答问题
6
+ ```
7
+ 调用工具:你要选择的工具是什么?只能在<%= agents.values.map do |a| a.agent_name end.join(',') %>中选择一个
8
+ 最终回答:你要回答的内容是什么?
9
+ ```
10
+ 我的问题:<%= last_user_input %>
@@ -0,0 +1,16 @@
1
+ 工具返回结果:
2
+ <% @tools_response.each do |r| -%>
3
+ > <%= r[:name] %>:<%= r[:response] %>
4
+ <% end -%>
5
+
6
+ 刚才的问题是:<%= @last_user_input %>
7
+ 结合工具的输出和你的思考。
8
+
9
+ 当你知道答案,用以下格式回答:
10
+ 最终答案: 返回给用户的内容放在这里
11
+
12
+ 当你需要使用工具,用以下格式回答:
13
+ 工具调用: 如果工具 <%= @agents.values.map do |a| a.agent_name end.join(', ') %> 中有适合的那么输入合适的工具名,没有则输入无。
14
+ 工具输入: 工具要求的输入, 如果不用输入或者工具要求不输入直接输入无。
15
+
16
+ 你只能选择其中一种格式,那么你的答案是什么呢?
@@ -0,0 +1 @@
1
+ <%= text %>
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.0"
4
+ VERSION = "0.1.1"
5
5
  end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Luo
4
+ class Xinghuo
5
+ include Configurable
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
12
+ setting :auditing, default: 'default'
13
+ setting :domain, default: 'general'
14
+ setting :max_tokens, default: 1024
15
+ setting :uid, default: -> { SecureRandom.hex(16) }
16
+
17
+ include HttpClient.init_client
18
+
19
+ PARAMS = Dry::Schema.Params do
20
+ required(:auditing).filled(:string)
21
+ required(:messages).filled(:array)
22
+ optional(:domain).maybe(:string)
23
+ optional(:max_tokens).maybe(:integer)
24
+ optional(:random_threshold).maybe(:float)
25
+ end
26
+
27
+ # header uid max length is 32 todo
28
+
29
+ def request_chat(params)
30
+ client.post('/external/ls_log/xf_completions', params.to_h)
31
+ end
32
+
33
+ def chat(messages)
34
+ if messages.is_a?(Messages)
35
+ messages = messages.to_a
36
+ end
37
+ params = PARAMS.call(
38
+ auditing: config.auditing,
39
+ domain: config.domain,
40
+ messages: messages,
41
+ max_tokens: config.max_tokens,
42
+ random_threshold: config.temperature,
43
+ uid: config.uid.call
44
+ )
45
+ return params.errors unless params.success?
46
+ request_chat(params).body.dig('choices', 0, 'message', 'content')
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Luo
4
+ class XinghuoAgentRunner < AgentRunnerBase
5
+ include Configurable
6
+
7
+ setting :retires, default: 3
8
+
9
+ on_init do
10
+ @xinghuo = Xinghuo.new
11
+ end
12
+
13
+ on_request do
14
+ context.messages = Messages.create(history: context.histories)
15
+ .user(prompt: Luo::Prompts.luo_xinghuo_agent_input, context: {agents: self.class.agents, last_user_input: context.user_input})
16
+ context.response = @xinghuo.chat(context.messages)
17
+ end
18
+
19
+ on_result do
20
+ agent_name = context.response.match(/调用工具:(.*)/)&.captures&.first&.strip&.gsub(/[[:punct:]]/, '')
21
+ agent = self.class.agents[agent_name]&.new(
22
+ context: context,
23
+ action_input: context.user_input,
24
+ client: @xinghuo
25
+ )
26
+ answer = context.response.match(/最终回答:(.*)/)&.captures&.first&.strip
27
+
28
+ if agent_name.nil? && !answer.nil? && agent.nil?
29
+ context.final_result = answer
30
+ else
31
+ add_agent(agent)
32
+ end
33
+ end
34
+
35
+ after_run do
36
+ if context.final_result.nil?
37
+ answer = context.response.match(/最终回答:(.*)/)&.captures&.first&.strip
38
+ context.final_result = answer if answer
39
+ end
40
+
41
+ context.histories.user(context.user_input)
42
+ context.histories.assistant(context.final_result)
43
+ end
44
+ end
45
+
46
+ class XinghuoFinalAgent < Agent
47
+ agent_name '智能问答'
48
+ agent_desc '你可以问我任何问题,我都会尽力回答你'
49
+
50
+ on_call_with_final_result do
51
+ messages = Messages.create(history: context.histories).user(text: context.user_input)
52
+ client.chat(messages)
53
+ end
54
+
55
+ end
56
+ end
data/lib/luo.rb CHANGED
@@ -1,6 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "luo/version"
3
+ # require_relative "luo/version"
4
+
5
+ require 'erb'
6
+ require 'dotenv/load'
7
+ require 'json'
8
+ require 'faraday'
9
+ require 'faraday/retry'
10
+ require 'dry-configurable'
11
+ require 'dry-schema'
12
+ require 'dry/cli'
13
+
14
+ require "zeitwerk"
15
+ loader = Zeitwerk::Loader.for_gem
16
+ loader.inflector.inflect("open_ai" => "OpenAI")
17
+ loader.inflector.inflect("aiui" => "AIUI")
18
+ loader.inflector.inflect("cli" => "CLI")
19
+ loader.inflector.inflect("open_ai_agent_runner" => "OpenAIAgentRunner")
20
+ loader.setup
4
21
 
5
22
  module Luo
6
23
  class Error < StandardError; end
data/luo.gemspec CHANGED
@@ -29,8 +29,17 @@ Gem::Specification.new do |spec|
29
29
  spec.require_paths = ["lib"]
30
30
 
31
31
  # Uncomment to register a new dependency of your gem
32
+ spec.add_dependency 'zeitwerk', '~> 2.6', '>= 2.6.8'
33
+ spec.add_dependency 'dotenv', '~> 2.8', '>= 2.8.1'
34
+ spec.add_dependency 'faraday', '~> 2.7', '>= 2.7.4'
35
+ spec.add_dependency 'faraday-retry', '~> 2.1'
36
+ spec.add_dependency 'dry-schema', '~> 1.13', '>= 1.13.1'
37
+ spec.add_dependency 'dry-configurable', '~> 1.0', '>= 1.0.1'
38
+ spec.add_dependency 'dry-cli', '~> 1.0'
32
39
  # spec.add_dependency "example-gem", "~> 1.0"
33
40
 
41
+ spec.add_development_dependency "rspec", '~> 3.12'
42
+
34
43
  # For more information and examples about making a new gem, check out our
35
44
  # guide at: https://bundler.io/guides/creating_gem.html
36
45
  end
data/sig/luo/agent.rbs ADDED
@@ -0,0 +1,21 @@
1
+ module Luo
2
+ class Agent
3
+ @action_input: untyped
4
+ @context: AgentRunnerContext
5
+
6
+ attr_reader self.agent_desc: String
7
+ attr_reader self.agent_name: String
8
+
9
+ attr_reader action_input: untyped
10
+ attr_reader context: AgentRunnerContext
11
+
12
+ def initialize: (AgentRunnerContext) -> untyped
13
+
14
+ def self.create_parameter_method: -> untyped
15
+
16
+ def self.on_call: -> untyped
17
+
18
+ attr_reader input: String
19
+ attr_reader row_input: String?
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ module Luo
2
+ class AgentRunnerBase
3
+ self.@agents: Hash[String, Agent]
4
+
5
+ @context: AgentRunnerContext
6
+
7
+ def self.agents: -> Hash[String, Agent]
8
+
9
+ def self.register: (Agent) -> untyped
10
+
11
+ def add_agent: (Agent)-> untyped
12
+
13
+ def call: -> untyped
14
+
15
+ def context: -> AgentRunnerContext
16
+
17
+ def on_request: -> untyped
18
+
19
+ def on_result: -> untyped
20
+
21
+ def on_run: -> untyped
22
+
23
+ def reset_context: -> untyped
24
+ end
25
+ end
@@ -0,0 +1,19 @@
1
+ module Luo
2
+ class AgentRunnerContext
3
+ @histories: MemoryHistory
4
+
5
+ @running_agents: Array[Agent]
6
+
7
+ attr_accessor action_input: String
8
+ attr_accessor agent_results: Array[Hash[Symbol, string]]
9
+ attr_accessor final_result: String
10
+ attr_accessor messages: Messages
11
+ attr_accessor response: untyped
12
+ attr_accessor retries: Integer
13
+ attr_accessor user_input: String
14
+
15
+ def have_running_agents: -> Set[Agent]
16
+
17
+ def histories: -> MemoryHistory
18
+ end
19
+ end
data/sig/luo/aiui.rbs ADDED
@@ -0,0 +1,9 @@
1
+ module Luo
2
+ class AIUI
3
+ PARAMS: untyped
4
+
5
+ def chat: -> untyped
6
+
7
+ def request_aiui: -> untyped
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module Luo
2
+ module Configurable
3
+ def config: -> untyped
4
+ end
5
+ end
data/sig/luo/env.rbs ADDED
@@ -0,0 +1,9 @@
1
+ module Luo
2
+ class Env
3
+ ENV_KEY: Array[String]
4
+
5
+ def self.get: -> String?
6
+
7
+ def self.set: -> untyped
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Luo
2
+ module HttpClient
3
+ def self.http_client_configurable: -> untyped
4
+
5
+ def self.init_client: -> untyped
6
+
7
+ def http_client_configurable: -> untyped
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ module Luo
2
+ class MemoryHistory
3
+ @max_size: Integer
4
+ @queue: Array[Hash[String, String]]
5
+
6
+ def dequeue: -> untyped
7
+
8
+ def enqueue: -> untyped
9
+
10
+ alias push enqueue
11
+
12
+ def size: -> Integer
13
+
14
+ def to_a: -> Array[Hash[String, String]]
15
+
16
+ def to_json: -> String
17
+ end
18
+ end
@@ -0,0 +1,20 @@
1
+ module Luo
2
+ class Messages
3
+ @history: Array[Hash[Symbol, String]]
4
+ @messages: Array[Hash[Symbol, String]]
5
+ @system: Hash[Symbol, String]
6
+
7
+ def self.create: -> Messages
8
+
9
+ def self.create_role_message_method: -> untyped
10
+
11
+ def self.create_role_message_methods: -> untyped
12
+
13
+ def assistant: -> Messages
14
+ def system: -> Messages
15
+
16
+ def to_a: -> Array[Hash[Symbol, String]]
17
+
18
+ def user: -> Messages
19
+ end
20
+ end
@@ -0,0 +1,11 @@
1
+ module Luo
2
+ class OpenAI
3
+ ACCESS_TOKEN: String
4
+ HOST: String
5
+ PARAMS: untyped
6
+
7
+ def chat: -> untyped
8
+
9
+ def chat_completions: -> untyped
10
+ end
11
+ end
@@ -0,0 +1,18 @@
1
+ module Luo
2
+ class PromptTemplate
3
+ @file_path: string
4
+ @template: ERB
5
+
6
+ def self.create: -> PromptTemplate
7
+
8
+ def self.load_template: -> PromptTemplate
9
+
10
+ def render: -> string
11
+
12
+ def result: -> String
13
+
14
+ private
15
+
16
+ def context_binding: -> untyped
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ module Luo
2
+ module Prompts
3
+ PWD: String
4
+ RUNTIME_TEMPLATES_DIR: String
5
+ TEMPLATES_DIR: String
6
+
7
+ def agent_input: -> PromptTemplate
8
+
9
+ def define_template: -> untyped
10
+
11
+ def define_templates: -> untyped
12
+
13
+ def define_templates_in_dir: -> untyped
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ module Luo
2
+ class Xinghuo
3
+ PARAMS: untyped
4
+
5
+ def chat: -> untyped
6
+
7
+ def request_chat: -> untyped
8
+ end
9
+ end