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.
@@ -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