dspy 0.3.1 → 0.5.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 +4 -4
- data/README.md +67 -385
- data/lib/dspy/chain_of_thought.rb +123 -86
- data/lib/dspy/evaluate.rb +554 -0
- data/lib/dspy/example.rb +203 -0
- data/lib/dspy/few_shot_example.rb +81 -0
- data/lib/dspy/instrumentation/token_tracker.rb +6 -6
- data/lib/dspy/instrumentation.rb +199 -18
- data/lib/dspy/lm/adapter_factory.rb +6 -8
- data/lib/dspy/lm.rb +79 -35
- data/lib/dspy/mixins/instrumentation_helpers.rb +133 -0
- data/lib/dspy/mixins/struct_builder.rb +133 -0
- data/lib/dspy/mixins/type_coercion.rb +67 -0
- data/lib/dspy/predict.rb +83 -128
- data/lib/dspy/prompt.rb +222 -0
- data/lib/dspy/propose/grounded_proposer.rb +560 -0
- data/lib/dspy/re_act.rb +242 -173
- data/lib/dspy/registry/registry_manager.rb +504 -0
- data/lib/dspy/registry/signature_registry.rb +725 -0
- data/lib/dspy/storage/program_storage.rb +442 -0
- data/lib/dspy/storage/storage_manager.rb +331 -0
- data/lib/dspy/subscribers/langfuse_subscriber.rb +669 -0
- data/lib/dspy/subscribers/logger_subscriber.rb +180 -5
- data/lib/dspy/subscribers/newrelic_subscriber.rb +686 -0
- data/lib/dspy/subscribers/otel_subscriber.rb +538 -0
- data/lib/dspy/teleprompt/data_handler.rb +107 -0
- data/lib/dspy/teleprompt/mipro_v2.rb +790 -0
- data/lib/dspy/teleprompt/simple_optimizer.rb +497 -0
- data/lib/dspy/teleprompt/teleprompter.rb +336 -0
- data/lib/dspy/teleprompt/utils.rb +380 -0
- data/lib/dspy/version.rb +5 -0
- data/lib/dspy.rb +105 -0
- metadata +32 -12
- data/lib/dspy/lm/adapters/ruby_llm_adapter.rb +0 -81
@@ -1,81 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
begin
|
4
|
-
require 'ruby_llm'
|
5
|
-
rescue LoadError
|
6
|
-
# ruby_llm is optional for backward compatibility
|
7
|
-
end
|
8
|
-
|
9
|
-
module DSPy
|
10
|
-
class LM
|
11
|
-
class RubyLLMAdapter < Adapter
|
12
|
-
def initialize(model:, api_key:)
|
13
|
-
super
|
14
|
-
|
15
|
-
unless defined?(RubyLLM)
|
16
|
-
raise ConfigurationError,
|
17
|
-
"ruby_llm gem is required for RubyLLMAdapter. " \
|
18
|
-
"Add 'gem \"ruby_llm\"' to your Gemfile."
|
19
|
-
end
|
20
|
-
|
21
|
-
configure_ruby_llm
|
22
|
-
end
|
23
|
-
|
24
|
-
def chat(messages:, &block)
|
25
|
-
begin
|
26
|
-
chat = RubyLLM.chat(model: model)
|
27
|
-
|
28
|
-
# Add messages to chat
|
29
|
-
messages.each do |msg|
|
30
|
-
chat.add_message(role: msg[:role].to_sym, content: msg[:content])
|
31
|
-
end
|
32
|
-
|
33
|
-
# Get the last user message for ask method
|
34
|
-
last_user_message = messages.reverse.find { |msg| msg[:role] == 'user' }
|
35
|
-
|
36
|
-
if last_user_message
|
37
|
-
# Remove the last user message since ask() will add it
|
38
|
-
chat.messages.pop if chat.messages.last&.content == last_user_message[:content]
|
39
|
-
chat.ask(last_user_message[:content], &block)
|
40
|
-
else
|
41
|
-
raise AdapterError, "No user message found in conversation"
|
42
|
-
end
|
43
|
-
|
44
|
-
content = chat.messages.last&.content || ""
|
45
|
-
|
46
|
-
Response.new(
|
47
|
-
content: content,
|
48
|
-
usage: nil, # ruby_llm doesn't provide usage info
|
49
|
-
metadata: {
|
50
|
-
provider: 'ruby_llm',
|
51
|
-
model: model,
|
52
|
-
message_count: chat.messages.length
|
53
|
-
}
|
54
|
-
)
|
55
|
-
rescue => e
|
56
|
-
raise AdapterError, "RubyLLM adapter error: #{e.message}"
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
private
|
61
|
-
|
62
|
-
def configure_ruby_llm
|
63
|
-
# Determine provider from model for configuration
|
64
|
-
if model.include?('gpt') || model.include?('openai')
|
65
|
-
RubyLLM.configure do |config|
|
66
|
-
config.openai_api_key = api_key
|
67
|
-
end
|
68
|
-
elsif model.include?('claude') || model.include?('anthropic')
|
69
|
-
RubyLLM.configure do |config|
|
70
|
-
config.anthropic_api_key = api_key
|
71
|
-
end
|
72
|
-
else
|
73
|
-
# Default to OpenAI configuration
|
74
|
-
RubyLLM.configure do |config|
|
75
|
-
config.openai_api_key = api_key
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|