smart_prompt 0.1.4 → 0.1.6
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 +4 -4
- data/README.cn.md +1 -87
- data/README.md +1 -87
- data/lib/smart_prompt/conversation.rb +2 -0
- data/lib/smart_prompt/engine.rb +18 -2
- data/lib/smart_prompt/llm_adapter.rb +10 -0
- data/lib/smart_prompt/version.rb +1 -1
- data/lib/smart_prompt/worker.rb +1 -0
- data/lib/smart_prompt.rb +11 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 18a10421c03b8707b2ff6d7cf40a5e58eee621b62a177c7ab3d10c2b5e75b767
|
4
|
+
data.tar.gz: 4f43968f0b7cf8e9dab3ce5959f2a837729b335b0a1fcc533852339f130b7e43
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e0fd7f1e1995821f53135e01f92c3d3cdf1296405fec138c588ff23450c9be452d81b8a3dd42fb1e9fdf73748a1cc0119fe09285bac281eb035c2bec4742987c
|
7
|
+
data.tar.gz: 45e702684683ff912f149a2ae2f7eb94f0f4efa183f991923db8858223a1c721e975821972f7183fcb958e902168a65b22d5ea14f84be6cea87a434ced74e447
|
data/README.cn.md
CHANGED
@@ -24,90 +24,4 @@ $ gem install smart_prompt
|
|
24
24
|
|
25
25
|
## 用法
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
### 配置文件
|
30
|
-
|
31
|
-
```
|
32
|
-
adapters:
|
33
|
-
openai: OpenAIAdapter
|
34
|
-
ollama: OllamaAdapter
|
35
|
-
llms:
|
36
|
-
siliconflow:
|
37
|
-
adapter: openai
|
38
|
-
url: https://api.siliconflow.cn/v1/
|
39
|
-
api_key: ENV["APIKey"]
|
40
|
-
default_model: Qwen/Qwen2.5-7B-Instruct
|
41
|
-
llamacpp:
|
42
|
-
adapter: openai
|
43
|
-
url: http://localhost:8080/
|
44
|
-
ollama:
|
45
|
-
adapter: ollama
|
46
|
-
url: http://localhost:11434/
|
47
|
-
default_model: qwen2.5
|
48
|
-
default_llm: siliconflow
|
49
|
-
worker_path: "./workers"
|
50
|
-
template_path: "./templates"
|
51
|
-
```
|
52
|
-
|
53
|
-
### 基本使用
|
54
|
-
|
55
|
-
```
|
56
|
-
require 'smart_prompt'
|
57
|
-
engine = SmartPrompt::Engine.new('./config/llm_config.yml')
|
58
|
-
result = engine.call_worker(:daily_report, {location: "Shanghai"})
|
59
|
-
puts result
|
60
|
-
```
|
61
|
-
|
62
|
-
### workers/daily_report.rb
|
63
|
-
|
64
|
-
```
|
65
|
-
SmartPrompt.define_worker :daily_report do
|
66
|
-
use "ollama"
|
67
|
-
model "gemma2"
|
68
|
-
system "You are a helpful report writer."
|
69
|
-
weather = call_worker(:weather_summary, { location: params[:location], date: "today" })
|
70
|
-
prompt :daily_report, { weather: weather, location: params[:location] }
|
71
|
-
send_msg
|
72
|
-
end
|
73
|
-
```
|
74
|
-
|
75
|
-
### workers/weather_summary.rb
|
76
|
-
|
77
|
-
```
|
78
|
-
SmartPrompt.define_worker :weather_summary do
|
79
|
-
use "ollama"
|
80
|
-
model "gemma2"
|
81
|
-
sys_msg "You are a helpful weather assistant."
|
82
|
-
prompt :weather, { location: params[:location], date: params[:date] }
|
83
|
-
weather_info = send_msg
|
84
|
-
prompt :summarize, { text: weather_info }
|
85
|
-
send_msg
|
86
|
-
end
|
87
|
-
```
|
88
|
-
|
89
|
-
### templates/daily_report.erb
|
90
|
-
|
91
|
-
```
|
92
|
-
Please create a brief daily report for <%= location %> based on the following weather information:
|
93
|
-
|
94
|
-
<%= weather %>
|
95
|
-
|
96
|
-
The report should include:
|
97
|
-
1. A summary of the weather
|
98
|
-
2. Any notable events or conditions
|
99
|
-
3. Recommendations for residents
|
100
|
-
```
|
101
|
-
### templates/weather.erb
|
102
|
-
|
103
|
-
```
|
104
|
-
What's the weather like in <%= location %> <%= date %>? Please provide a brief description including temperature and general conditions.
|
105
|
-
```
|
106
|
-
|
107
|
-
### templates/summarize.erb
|
108
|
-
|
109
|
-
```
|
110
|
-
Please summarize the following text in one sentence:
|
111
|
-
|
112
|
-
<%= text %>
|
113
|
-
```
|
27
|
+
更多示例请看项目 [SmartPromptDemo](https://github.com/zhuangbiaowei/SmartPromptDemo)
|
data/README.md
CHANGED
@@ -26,90 +26,4 @@ $ gem install smart_prompt
|
|
26
26
|
|
27
27
|
## Usage
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
### llm_config.yml
|
32
|
-
|
33
|
-
```
|
34
|
-
adapters:
|
35
|
-
openai: OpenAIAdapter
|
36
|
-
ollama: OllamaAdapter
|
37
|
-
llms:
|
38
|
-
siliconflow:
|
39
|
-
adapter: openai
|
40
|
-
url: https://api.siliconflow.cn/v1/
|
41
|
-
api_key: ENV["APIKey"]
|
42
|
-
default_model: Qwen/Qwen2.5-7B-Instruct
|
43
|
-
llamacpp:
|
44
|
-
adapter: openai
|
45
|
-
url: http://localhost:8080/
|
46
|
-
ollama:
|
47
|
-
adapter: ollama
|
48
|
-
url: http://localhost:11434/
|
49
|
-
default_model: qwen2.5
|
50
|
-
default_llm: siliconflow
|
51
|
-
worker_path: "./workers"
|
52
|
-
template_path: "./templates"
|
53
|
-
```
|
54
|
-
|
55
|
-
### Basic usage
|
56
|
-
|
57
|
-
```
|
58
|
-
require 'smart_prompt'
|
59
|
-
engine = SmartPrompt::Engine.new('./config/llm_config.yml')
|
60
|
-
result = engine.call_worker(:daily_report, {location: "Shanghai"})
|
61
|
-
puts result
|
62
|
-
```
|
63
|
-
|
64
|
-
### workers/daily_report.rb
|
65
|
-
|
66
|
-
```
|
67
|
-
SmartPrompt.define_worker :daily_report do
|
68
|
-
use "ollama"
|
69
|
-
model "gemma2"
|
70
|
-
system "You are a helpful report writer."
|
71
|
-
weather = call_worker(:weather_summary, { location: params[:location], date: "today" })
|
72
|
-
prompt :daily_report, { weather: weather, location: params[:location] }
|
73
|
-
send_msg
|
74
|
-
end
|
75
|
-
```
|
76
|
-
|
77
|
-
### workers/weather_summary.rb
|
78
|
-
|
79
|
-
```
|
80
|
-
SmartPrompt.define_worker :weather_summary do
|
81
|
-
use "ollama"
|
82
|
-
model "gemma2"
|
83
|
-
sys_msg "You are a helpful weather assistant."
|
84
|
-
prompt :weather, { location: params[:location], date: params[:date] }
|
85
|
-
weather_info = send_msg
|
86
|
-
prompt :summarize, { text: weather_info }
|
87
|
-
send_msg
|
88
|
-
end
|
89
|
-
```
|
90
|
-
|
91
|
-
### templates/daily_report.erb
|
92
|
-
|
93
|
-
```
|
94
|
-
Please create a brief daily report for <%= location %> based on the following weather information:
|
95
|
-
|
96
|
-
<%= weather %>
|
97
|
-
|
98
|
-
The report should include:
|
99
|
-
1. A summary of the weather
|
100
|
-
2. Any notable events or conditions
|
101
|
-
3. Recommendations for residents
|
102
|
-
```
|
103
|
-
### templates/weather.erb
|
104
|
-
|
105
|
-
```
|
106
|
-
What's the weather like in <%= location %> <%= date %>? Please provide a brief description including temperature and general conditions.
|
107
|
-
```
|
108
|
-
|
109
|
-
### templates/summarize.erb
|
110
|
-
|
111
|
-
```
|
112
|
-
Please summarize the following text in one sentence:
|
113
|
-
|
114
|
-
<%= text %>
|
115
|
-
```
|
29
|
+
See project [SmartPromptDemo](https://github.com/zhuangbiaowei/SmartPromptDemo) for more examples.
|
@@ -5,6 +5,7 @@ module SmartPrompt
|
|
5
5
|
attr_reader :messages, :last_response, :config_file
|
6
6
|
|
7
7
|
def initialize(engine)
|
8
|
+
SmartPrompt.logger.info "Create Conversation"
|
8
9
|
@messages = []
|
9
10
|
@engine = engine
|
10
11
|
@adapters = engine.adapters
|
@@ -26,6 +27,7 @@ module SmartPrompt
|
|
26
27
|
|
27
28
|
def prompt(template_name, params = {})
|
28
29
|
template_name = template_name.to_s
|
30
|
+
SmartPrompt.logger.info "Use template #{template_name}"
|
29
31
|
raise "Template #{template_name} not found" unless @templates.key?(template_name)
|
30
32
|
content = @templates[template_name].render(params)
|
31
33
|
@messages << { role: 'user', content: content }
|
data/lib/smart_prompt/engine.rb
CHANGED
@@ -7,11 +7,16 @@ module SmartPrompt
|
|
7
7
|
@llms={}
|
8
8
|
@templates={}
|
9
9
|
load_config(config_file)
|
10
|
+
SmartPrompt.logger.info "Started create the SmartPrompt engine."
|
10
11
|
end
|
11
12
|
|
12
13
|
def load_config(config_file)
|
13
14
|
@config_file = config_file
|
14
15
|
@config = YAML.load_file(config_file)
|
16
|
+
if @config['logger_file']
|
17
|
+
SmartPrompt.logger = Logger.new(@config['logger_file'])
|
18
|
+
end
|
19
|
+
SmartPrompt.logger.info "Loading configuration from file: #{config_file}"
|
15
20
|
@config['adapters'].each do |adapter_name, adapter_class|
|
16
21
|
adapter_class = SmartPrompt.const_get(adapter_class)
|
17
22
|
@adapters[adapter_name] = adapter_class
|
@@ -35,14 +40,25 @@ module SmartPrompt
|
|
35
40
|
end
|
36
41
|
|
37
42
|
def call_worker(worker_name, params = {})
|
43
|
+
SmartPrompt.logger.info "Calling worker: #{worker_name} with params: #{params}"
|
38
44
|
worker = get_worker(worker_name)
|
39
|
-
|
45
|
+
|
46
|
+
begin
|
47
|
+
result = worker.execute(params)
|
48
|
+
SmartPrompt.logger.info "Worker #{worker_name} executed successfully"
|
49
|
+
result
|
50
|
+
rescue => e
|
51
|
+
SmartPrompt.logger.error "Error executing worker #{worker_name}: #{e.message}"
|
52
|
+
SmartPrompt.logger.debug e.backtrace.join("\n")
|
53
|
+
raise
|
54
|
+
end
|
40
55
|
end
|
41
56
|
|
42
57
|
private
|
43
58
|
|
44
59
|
def get_worker(worker_name)
|
45
|
-
worker
|
60
|
+
SmartPrompt.logger.info "Creating worker instance for: #{worker_name}"
|
61
|
+
Worker.new(worker_name, self)
|
46
62
|
end
|
47
63
|
end
|
48
64
|
end
|
@@ -7,10 +7,12 @@ require 'ollama-ai'
|
|
7
7
|
module SmartPrompt
|
8
8
|
class LLMAdapter
|
9
9
|
def initialize(config)
|
10
|
+
SmartPrompt.logger.info "Start create the SmartPrompt LLMAdapter."
|
10
11
|
@config = config
|
11
12
|
end
|
12
13
|
|
13
14
|
def send_request(messages)
|
15
|
+
SmartPrompt.logger.error "LLMAdapter: Subclasses must implement send_request"
|
14
16
|
raise NotImplementedError, "Subclasses must implement send_request"
|
15
17
|
end
|
16
18
|
end
|
@@ -30,11 +32,13 @@ module SmartPrompt
|
|
30
32
|
end
|
31
33
|
|
32
34
|
def send_request(messages, model=nil)
|
35
|
+
SmartPrompt.logger.info "OpenAIAdapter: Sending request to OpenAI"
|
33
36
|
if model
|
34
37
|
model_name = model
|
35
38
|
else
|
36
39
|
model_name = @config['model']
|
37
40
|
end
|
41
|
+
SmartPrompt.logger.info "OpenAIAdapter: Using model #{model_name}"
|
38
42
|
response = @client.chat(
|
39
43
|
parameters: {
|
40
44
|
model: model_name,
|
@@ -42,6 +46,7 @@ module SmartPrompt
|
|
42
46
|
temperature: @config['temperature'] || 0.7
|
43
47
|
}
|
44
48
|
)
|
49
|
+
SmartPrompt.logger.info "OpenAIAdapter: Received response from OpenAI"
|
45
50
|
response.dig("choices", 0, "message", "content")
|
46
51
|
end
|
47
52
|
end
|
@@ -54,12 +59,14 @@ module SmartPrompt
|
|
54
59
|
)
|
55
60
|
end
|
56
61
|
def send_request(messages, model=nil)
|
62
|
+
SmartPrompt.logger.info "LlamacppAdapter: Sending request to Llamacpp"
|
57
63
|
response = @client.chat(
|
58
64
|
parameters: {
|
59
65
|
messages: messages,
|
60
66
|
temperature: @config['temperature'] || 0.7
|
61
67
|
}
|
62
68
|
)
|
69
|
+
SmartPrompt.logger.info "LlamacppAdapter: Received response from Llamacpp"
|
63
70
|
response.dig("choices", 0, "message", "content")
|
64
71
|
end
|
65
72
|
end
|
@@ -71,11 +78,13 @@ module SmartPrompt
|
|
71
78
|
end
|
72
79
|
|
73
80
|
def send_request(messages, model=nil)
|
81
|
+
SmartPrompt.logger.info "OllamaAdapter: Sending request to Ollama"
|
74
82
|
if model
|
75
83
|
model_name = model
|
76
84
|
else
|
77
85
|
model_name = @config['model']
|
78
86
|
end
|
87
|
+
SmartPrompt.logger.info "OllamaAdapter: Using model #{model_name}"
|
79
88
|
response = @client.generate(
|
80
89
|
{
|
81
90
|
model: model_name,
|
@@ -83,6 +92,7 @@ module SmartPrompt
|
|
83
92
|
stream: false
|
84
93
|
}
|
85
94
|
)
|
95
|
+
SmartPrompt.logger.info "OllamaAdapter: Received response from Ollama"
|
86
96
|
return response[0]["response"]
|
87
97
|
end
|
88
98
|
end
|
data/lib/smart_prompt/version.rb
CHANGED
data/lib/smart_prompt/worker.rb
CHANGED
data/lib/smart_prompt.rb
CHANGED
@@ -7,6 +7,7 @@ require File.expand_path('../smart_prompt/worker', __FILE__)
|
|
7
7
|
|
8
8
|
module SmartPrompt
|
9
9
|
class Error < StandardError; end
|
10
|
+
attr_writer :logger
|
10
11
|
|
11
12
|
def self.define_worker(name, &block)
|
12
13
|
Worker.define(name, &block)
|
@@ -16,4 +17,14 @@ module SmartPrompt
|
|
16
17
|
worker = Worker.new(name, config_file)
|
17
18
|
worker.execute(params)
|
18
19
|
end
|
20
|
+
|
21
|
+
def self.logger=(logger)
|
22
|
+
@logger = logger
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.logger
|
26
|
+
@logger ||= Logger.new($stdout).tap do |log|
|
27
|
+
log.progname = self.name
|
28
|
+
end
|
29
|
+
end
|
19
30
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smart_prompt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- zhuang biaowei
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-10-
|
11
|
+
date: 2024-10-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: yaml
|