luo 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 294891b56e33b9a8537542538325ad107f1cead832dc6f86e25f087fd9883fc7
4
- data.tar.gz: 861361be7def0711a6525b1f99378f4a4217e876f236ddb1e628f6458efc53e1
3
+ metadata.gz: 185d14b9e236adfe8d72b9fdd12dc037834a2eafb4586567139983983d26f149
4
+ data.tar.gz: 0d5bc502984155f26ea2e134ca9d97f6951dc7ea064bcc76f1feb3cbf63d2b9f
5
5
  SHA512:
6
- metadata.gz: b35623a4f996d28253dac61a920bbb2ff4b6978f882eff9e514be33dd1fdcd46fe7f3d7d19f8f40c9069ed6605a6ce1f37753b84e61ad23c6a597cdac760ea8c
7
- data.tar.gz: e665f6f8a38cfaeb001765e624e3e83071c9745723130eeb49fa087dc8eb3c7e06c05e31f973ecf251eb89e2dc19598bd6f79eb0a0426a2d7987eac78f3d615d
6
+ metadata.gz: d50c9aaa841bb065bb85ea4270987065ef14aa220f8e6968eb4baa443fbf8808c1edecf95b0f28df567baaf52a778adb1298ea66deb9879cf37a8cc372dc54ff
7
+ data.tar.gz: bbc35c28a3d02ff1d55adc30a4a635bae6c9c3b7ddb23422861bf4a6a11943732d1f50a189bacf6ad49f32b1d203899389adb5eb230c280b4c0cc50d77e60f68
data/README.md CHANGED
@@ -1,34 +1,101 @@
1
1
  # Luo
2
-
3
- TODO: Delete this and the text below, and describe your gem
4
-
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/luo`. To experiment with that code, run `bin/console` for an interactive prompt.
6
-
7
- ## Installation
8
-
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
-
11
- Install the gem and add to the application's Gemfile by executing:
12
-
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
-
15
- If bundler is not being used to manage dependencies, install the gem by executing:
16
-
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
-
19
- ## Usage
20
-
21
- TODO: Write usage instructions here
22
-
23
- ## Development
24
-
25
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
-
27
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
-
29
- ## Contributing
30
-
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/luo.
32
-
33
- ENV.fetch('OPENAI_HOST', 'https://api.openai.com')
34
- ENV.fetch('OPENAI_ACCESS_TOKEN')
2
+ 是一款基于大模型的开发框架(面向产品经理),当前支持大模型提供商有: OpenAI、星火大模型。通过DSL能够快速创作并且测试大模型的效果。
3
+
4
+ ## 安装
5
+ ```
6
+ gem install luo
7
+ ```
8
+
9
+ ## 环境变量说明
10
+ ```Bash
11
+ OPENAI_ACCESS_TOKEN= # OpenAI的访问令牌
12
+ OPENAI_TEMPERATURE= # OpenAI的温度
13
+ OPENAI_LIMIT_HISTORY= # OpenAI的历史限制
14
+ AIUI_APP_KEY= # AIUI的AppKey
15
+ AIUI_APP_ID= # AIUI的AppId
16
+ XINGHUO_ACCESS_TOKEN= # 星火大模型的访问令牌
17
+ ```
18
+ 可以写在项目中的.env也可以放到系统环境变量中。
19
+
20
+ ## 使用
21
+
22
+ ### Hello World
23
+ 1. mkdir demo
24
+ 2. cd demo
25
+ 3. luo init
26
+ 4. 修改 .env 的环境变量
27
+ 5. ruby application.rb
28
+
29
+ ### Messages 说明
30
+ ```ruby
31
+ # 创建一个 Messages 实例
32
+ messages = Luo::Messages.create
33
+
34
+ # 添加用户消息,包含文本内容和提示信息
35
+ messages.user(text: "Hello, world!", prompt: TTY::Prompt.new.ask("What's your name?"))
36
+
37
+ # 添加助手消息,来自文件
38
+ messages.assistant(file: 'welcome.md')
39
+
40
+ # 添加系统消息,包含文本内容和上下文信息
41
+ messages.system(text: "User logged in", context: { user_id: 123 })
42
+
43
+ # 将消息转换为数组并打印出来
44
+ puts messages.to_a.inspect
45
+
46
+ # 输出
47
+ [
48
+ {:role=>"system", :content=>"User logged in"},
49
+ {:role=>"user", :content=>"Hello, world!"},
50
+ {:role=>"assistant", :content=>"Welcome to our app!\n"}
51
+ ]
52
+ ```
53
+ 注意:星火大模型的消息类型只支持:user、assistant
54
+
55
+ ## History
56
+ Luo 内置了 MemoryHistory 用于处理用户历史对话记录
57
+ ```ruby
58
+ history = Luo::MemoryHistory.new
59
+ history.user("Hello, world!")
60
+ history.assistant("Welcome to our app!")
61
+ puts history.to_a.inspect
62
+
63
+ # 输出
64
+ [
65
+ {:role=>"user", :content=>"Hello, world!"},
66
+ {:role=>"assistant", :content=>"Welcome to our app!"}
67
+ ]
68
+
69
+ # 可以在 Messages 使用
70
+ messages = Luo::Messages.create(history: history)
71
+ ```
72
+
73
+ ## Helpers
74
+ `Luo::Helpers` 模块是一个包含了一些辅助方法的模块,可以在不同的上下文中重用这些方法。其中,包括了 `print_md` 和 `load_test` 两个方法。
75
+
76
+ `print_md` 方法用于将 Markdown 格式的文本转换为终端友好的格式,并打印出来。这个方法接受一个参数 `text`,表示需要转换和打印的 Markdown 格式的文本。使用 `print_md` 方法的示例代码如下:
77
+
78
+ ```ruby
79
+ text = "# Hello, world!\n\nThis is a **Markdown** text."
80
+ Luo::Helpers.print_md(text)
81
+ ```
82
+
83
+ 在上面的示例中,首先定义了一个 Markdown 格式的文本 `text`,然后使用 `print_md` 方法将其转换为终端友好的格式,并打印出来。
84
+
85
+ `load_test` 方法用于从 YAML 格式的文件中加载测试数据,并对每个测试数据执行指定的块。这个方法接受两个参数:`path` 和块 `block`。其中 `path` 表示 YAML 文件的路径,块 `block` 表示对每个测试数据要执行的操作。使用 `load_test` 方法的示例代码如下:
86
+
87
+ ```ruby
88
+ Luo::Helpers.load_test('tests.yml') do |test_data|
89
+ # 对每个测试数据执行的操作
90
+ puts test_data.inspect
91
+ end
92
+ ```
93
+ 在上面的示例中,我们使用 `load_test` 方法从 `tests.yml` 文件中加载测试数据,并使用块对每个测试数据进行操作。在这个示例中,我们只是简单地使用 `puts` 方法打印出每个测试数据的内容。实际使用中,块 `block` 可以根据需要执行更复杂的操作,例如执行测试用例、生成报告等。
94
+
95
+ ## 补充资源
96
+ 1. 基于embedding的知识库对话机器人:https://github.com/ankane/neighbor#openai-embeddings
97
+ ```ruby
98
+ def fetch_embeddings(input)
99
+ Luo::OpenAI.new.create_embeddings(input)
100
+ end
101
+ ```
data/README.zh.md CHANGED
@@ -20,7 +20,77 @@ XINGHUO_ACCESS_TOKEN= # 星火大模型的访问令牌
20
20
  ## 使用
21
21
 
22
22
  ### Hello World
23
+ 1. mkdir demo
24
+ 2. cd demo
25
+ 3. luo init
26
+ 4. 修改 .env 的环境变量
27
+ 5. ruby application.rb
23
28
 
29
+ ### Messages 说明
30
+ ```ruby
31
+ # 创建一个 Messages 实例
32
+ messages = Luo::Messages.create
33
+
34
+ # 添加用户消息,包含文本内容和提示信息
35
+ messages.user(text: "Hello, world!", prompt: TTY::Prompt.new.ask("What's your name?"))
36
+
37
+ # 添加助手消息,来自文件
38
+ messages.assistant(file: 'welcome.md')
39
+
40
+ # 添加系统消息,包含文本内容和上下文信息
41
+ messages.system(text: "User logged in", context: { user_id: 123 })
42
+
43
+ # 将消息转换为数组并打印出来
44
+ puts messages.to_a.inspect
45
+
46
+ # 输出
47
+ [
48
+ {:role=>"system", :content=>"User logged in"},
49
+ {:role=>"user", :content=>"Hello, world!"},
50
+ {:role=>"assistant", :content=>"Welcome to our app!\n"}
51
+ ]
52
+ ```
53
+ 注意:星火大模型的消息类型只支持:user、assistant
54
+
55
+ ## History
56
+ Luo 内置了 MemoryHistory 用于处理用户历史对话记录
57
+ ```ruby
58
+ history = Luo::MemoryHistory.new
59
+ history.user("Hello, world!")
60
+ history.assistant("Welcome to our app!")
61
+ puts history.to_a.inspect
62
+
63
+ # 输出
64
+ [
65
+ {:role=>"user", :content=>"Hello, world!"},
66
+ {:role=>"assistant", :content=>"Welcome to our app!"}
67
+ ]
68
+
69
+ # 可以在 Messages 使用
70
+ messages = Luo::Messages.create(history: history)
71
+ ```
72
+
73
+ ## Helpers
74
+ `Luo::Helpers` 模块是一个包含了一些辅助方法的模块,可以在不同的上下文中重用这些方法。其中,包括了 `print_md` 和 `load_test` 两个方法。
75
+
76
+ `print_md` 方法用于将 Markdown 格式的文本转换为终端友好的格式,并打印出来。这个方法接受一个参数 `text`,表示需要转换和打印的 Markdown 格式的文本。使用 `print_md` 方法的示例代码如下:
77
+
78
+ ```ruby
79
+ text = "# Hello, world!\n\nThis is a **Markdown** text."
80
+ Luo::Helpers.print_md(text)
81
+ ```
82
+
83
+ 在上面的示例中,首先定义了一个 Markdown 格式的文本 `text`,然后使用 `print_md` 方法将其转换为终端友好的格式,并打印出来。
84
+
85
+ `load_test` 方法用于从 YAML 格式的文件中加载测试数据,并对每个测试数据执行指定的块。这个方法接受两个参数:`path` 和块 `block`。其中 `path` 表示 YAML 文件的路径,块 `block` 表示对每个测试数据要执行的操作。使用 `load_test` 方法的示例代码如下:
86
+
87
+ ```ruby
88
+ Luo::Helpers.load_test('tests.yml') do |test_data|
89
+ # 对每个测试数据执行的操作
90
+ puts test_data.inspect
91
+ end
92
+ ```
93
+ 在上面的示例中,我们使用 `load_test` 方法从 `tests.yml` 文件中加载测试数据,并使用块对每个测试数据进行操作。在这个示例中,我们只是简单地使用 `puts` 方法打印出每个测试数据的内容。实际使用中,块 `block` 可以根据需要执行更复杂的操作,例如执行测试用例、生成报告等。
24
94
 
25
95
  ## 补充资源
26
96
  1. 基于embedding的知识库对话机器人:https://github.com/ankane/neighbor#openai-embeddings
data/lib/luo/agent.rb CHANGED
@@ -34,6 +34,8 @@ module Luo
34
34
  end
35
35
  end
36
36
 
37
+ alias_method :on_call_with_fallback, :on_call_with_final_result
38
+
37
39
  def self.create_parameter_method(method_name, not_provided = Object.new, &block)
38
40
  define_method(method_name.to_sym) do |content = not_provided|
39
41
  if content === not_provided
data/lib/luo/helpers.rb CHANGED
@@ -9,10 +9,14 @@ module Luo
9
9
  end
10
10
 
11
11
  def load_test(path, &block)
12
- YAML.load_file(path).each do |value|
13
- yield(value)
12
+ data = YAML.load_file(path)
13
+ if data.is_a?(Array)
14
+ data.each do |value|
15
+ yield(value)
16
+ end
17
+ else
18
+ yield(data)
14
19
  end
15
20
  end
16
-
17
21
  end
18
22
  end
@@ -9,10 +9,14 @@ module Luo
9
9
  end
10
10
  def create_bundle_file()
11
11
  puts "create Gemfile"
12
+ version = Luo::VERSION
12
13
  unless File.exist?('Gemfile')
14
+ gemfile = <<-GEMFILE
15
+ source 'https://rubygems.org'
16
+ gem 'luo', '~> #{version}'
17
+ GEMFILE
13
18
  File.open('Gemfile', 'w') do |file|
14
- file.puts("source 'https://rubygems.org'")
15
- file.puts("gem 'luo', '~> 0.1.5'")
19
+ file.write(gemfile)
16
20
  end
17
21
  end
18
22
  end
@@ -2,7 +2,7 @@ class TimeAgent < Agent
2
2
  agent_name '时间查询'
3
3
  agent_desc '查询当前时间'
4
4
 
5
- on_call_with_final_result do
5
+ on_call_with_fallback do
6
6
  Helpers.print_md "** call aiui time **"
7
7
  messages = Luo::Messages.create.user(text: context.user_input)
8
8
  response = Luo::AIUI.new.chat(messages)
@@ -4,7 +4,7 @@ class WeatherAgent < Agent
4
4
  agent_name '天气查询'
5
5
  agent_desc '查询城市的天气情况,穿衣指数,空气质量等'
6
6
 
7
- on_call_with_final_result do
7
+ on_call_with_fallback do
8
8
  Helpers.print_md "** call aiui weather **"
9
9
  messages = Luo::Messages.create.user(text: context.user_input)
10
10
  response = Luo::AIUI.new.chat(messages)
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.5"
4
+ VERSION = "0.1.6"
5
5
  end
@@ -12,6 +12,8 @@ module Luo
12
12
  attr_accessor retries: Integer
13
13
  attr_accessor user_input: String
14
14
 
15
+ def client: -> untyped
16
+
15
17
  def have_running_agents: -> Set[Agent]
16
18
 
17
19
  def histories: -> MemoryHistory
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: luo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - MJ
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-05-09 00:00:00.000000000 Z
11
+ date: 2023-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: zeitwerk