clag 0.0.2 → 0.0.4

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: 590439d5a481273f60f73e23d193490499a0bc6f9c400fa6239f00e790f31191
4
- data.tar.gz: 2b35f0ef8b99e0185761275a405aa44363380df88a0f53449a3e3e1208159659
3
+ metadata.gz: e1108e8ad1844399f3606f873dd03b65fb4131a130b829ba1332636ffa7bae74
4
+ data.tar.gz: c736a7bf0cd0e6fc3a6000b288c9b411dc53cf30857b61698bc6b28b82bb7a63
5
5
  SHA512:
6
- metadata.gz: 87982f697b62eb353350fea66026841e8b0815bff79fea7407b7d3159ecc85aaefad4fab52a0c0f40dbd5389f5b83fa47b86dd6951de7fbd8ba33de58beae4a4
7
- data.tar.gz: 637a5801e9e52443e7bd694fedbef83f1baa9fd624ad263c9abe00f2874760a4ec3bccb5205bf14e33e1d50df323dc921cc8a52df3541f22849fc7640ece90cf
6
+ metadata.gz: 36ae1725de2b5d549d2f07f1401724ab82ea1fea9ba8622fb05d154f4008d53638b750a43654c7391c07f953e58d4dd9e6352519fead7b8182e5dbd8694078d8
7
+ data.tar.gz: 70d477e186eba030431e2e69d6b9cac4d88741d00a946dee73364e07e2e61f178cf39fd0619da059d3a80ff5b631d83bb2d53674cb8a84922ac163a99b9c784d
data/README.md CHANGED
@@ -30,6 +30,23 @@ command with the help of an LLM!
30
30
  * Select Gemini as your preferred LLM by setting CLAG\_LLM=gemini in your
31
31
  environment
32
32
 
33
+ ### Using Anthropic's Claude 3 Opus
34
+
35
+ * Get an API key from Anthropic at https://www.anthropic.com/
36
+
37
+ * Set your API key as ANTHROPIC\_API\_KEY in your environment
38
+
39
+ * Select Claude 3 Opus as your preferred LLM by setting CLAG\_LLM=claude in
40
+ your environment
41
+
42
+ ### Using Groq on Mixtral
43
+
44
+ * Get an API key from https://console.groq.com/
45
+
46
+ * Set your API key as GROQ\_API\_KEY in your environment
47
+
48
+ * Select Groq as your preferred LLM by setting CLAG\_LLM=groq in your environment
49
+
33
50
  ## Usage
34
51
 
35
52
  Currently support one command: "g".
data/clag.gemspec CHANGED
@@ -23,6 +23,8 @@ Gem::Specification.new do |spec|
23
23
  spec.add_dependency 'httparty', '~> 0.21'
24
24
  spec.add_dependency 'clipboard', '~> 1.3'
25
25
  spec.add_dependency 'activesupport'
26
+ spec.add_dependency "pry"
27
+ spec.add_dependency "nokogiri"
26
28
 
27
29
  spec.add_development_dependency 'rake', '~> 10.0'
28
30
  end
data/lib/clag/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Clag
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -1,5 +1,7 @@
1
1
  require "openai"
2
+ require "pry"
2
3
  require "httparty"
4
+ require "nokogiri"
3
5
 
4
6
  module Sublayer
5
7
  module Capabilities
@@ -16,14 +18,122 @@ module Sublayer
16
18
  end
17
19
 
18
20
  def llm_generate
19
- if ENV["CLAG_LLM"] == "gemini"
21
+ case ENV["CLAG_LLM"]
22
+ when "gemini"
20
23
  generate_with_gemini
24
+ when "claude"
25
+ generate_with_claude
26
+ when "groq"
27
+ generate_with_groq
21
28
  else
22
29
  generate_with_openai
23
30
  end
24
31
  end
25
32
 
26
33
  private
34
+
35
+ def generate_with_groq
36
+ system_prompt = <<-PROMPT
37
+ In this environment you have access to a set of tools you can use to answer the user's question.
38
+
39
+ You may call them like this:
40
+ <function_calls>
41
+ <invoke>
42
+ <tool_name>$TOOL_NAME</tool_name>
43
+ <parameters>
44
+ <command>value</command>
45
+ ...
46
+ </parameters>
47
+ </invoke>
48
+ </function_calls>
49
+
50
+ Here are the tools available:
51
+ <tools>
52
+ #{self.class::OUTPUT_FUNCTION.to_xml}
53
+ </tools>
54
+
55
+ Respond only with valid xml.
56
+ The entire response should be wrapped in a <response> tag.
57
+ Any additional information not inside a tool call should go in a <scratch> tag.
58
+ PROMPT
59
+
60
+ response = HTTParty.post(
61
+ "https://api.groq.com/openai/v1/chat/completions",
62
+ headers: {
63
+ "Authorization": "Bearer #{ENV["GROQ_API_KEY"]}",
64
+ "Content-Type": "application/json"
65
+ },
66
+ body: {
67
+ "messages": [{"role": "user", "content": "#{system_prompt}\n#{prompt}"}],
68
+ "model": "mixtral-8x7b-32768"
69
+ }.to_json
70
+ )
71
+
72
+ text_containing_xml = JSON.parse(response.body).dig("choices", 0, "message", "content")
73
+ xml = text_containing_xml.match(/\<response\>(.*?)\<\/response\>/m).to_s
74
+ response_xml = Nokogiri::XML(xml)
75
+ function_output = response_xml.at_xpath("//response/function_calls/invoke/parameters/command").children.to_s
76
+
77
+ return function_output
78
+ end
79
+
80
+ def generate_with_claude
81
+ system_prompt = <<-PROMPT
82
+ In this environment you have access to a set of tools you can use to answer the user's question.
83
+
84
+ You may call them like this:
85
+ <function_calls>
86
+ <invoke>
87
+ <tool_name>$TOOL_NAME</tool_name>
88
+ <parameters>
89
+ <$PARAMETER_NAME>$PARAMETER_VALUE</$PARAMETER_NAME>
90
+ ...
91
+ </parameters>
92
+ </invoke>
93
+ </function_calls>
94
+
95
+ Here are the tools available:
96
+ <tools>
97
+ #{self.class::OUTPUT_FUNCTION.to_xml}
98
+ </tools>
99
+
100
+ Respond only with valid xml. The entire response should be wrapped in a <response> tag. Any additional information not inside a tool call should go in a <scratch> tag.
101
+ PROMPT
102
+
103
+ response = HTTParty.post(
104
+ "https://api.anthropic.com/v1/messages",
105
+ headers: {
106
+ "x-api-key": ENV["ANTHROPIC_API_KEY"],
107
+ "anthropic-version": "2023-06-01",
108
+ "content-type": "application/json"
109
+ },
110
+ body: {
111
+ model: "claude-3-opus-20240229",
112
+ max_tokens: 1337,
113
+ system: system_prompt,
114
+ messages: [
115
+ { "role": "user", "content": prompt }
116
+ ]
117
+ }.to_json
118
+ )
119
+
120
+ # raise an error if the response is not a 200
121
+ raise "Error generating with Claude, error: #{response.body}" unless response.code == 200
122
+
123
+ text_containing_xml = JSON.parse(response.body).dig("content", 0, "text")
124
+
125
+ # Extract the xml from the respons contained in <response> tags the content of the string looksl ike this:
126
+ xml = text_containing_xml.match(/\<response\>(.*?)\<\/response\>/m).to_s
127
+
128
+ # Parse the xml and extract the response
129
+ response_xml = Nokogiri::XML(xml)
130
+
131
+ # Extract the response from the xml
132
+ function_output = response_xml.at_xpath("//response/function_calls/invoke/parameters/command").children.to_s
133
+
134
+ return function_output
135
+ end
136
+
27
137
  def generate_with_gemini
28
138
  gemini_prompt = adapt_prompt_for_gemini
29
139
 
@@ -14,9 +14,8 @@ module Sublayer
14
14
  raise NotImplementedError
15
15
  end
16
16
 
17
- private
18
-
19
- def list_of_objects_hash
17
+ def to_xml
18
+ raise NotImplementedError
20
19
  end
21
20
  end
22
21
  end
@@ -21,6 +21,20 @@ module Sublayer
21
21
  }
22
22
  }
23
23
  end
24
+
25
+ def to_xml
26
+ <<-XML
27
+ <tool_description>
28
+ <tool_name>#{@name}</tool_name>
29
+ <tool_description>#{@description}</tool_description>
30
+ <parameters>
31
+ <name>#{@name}</name>
32
+ <type>string</type>
33
+ <description>#{@description}</description>
34
+ </parameters>
35
+ </tool_description>
36
+ XML
37
+ end
24
38
  end
25
39
  end
26
40
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Werner
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-21 00:00:00.000000000 Z
11
+ date: 2024-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cli-kit
@@ -94,6 +94,34 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: nokogiri
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
97
125
  - !ruby/object:Gem::Dependency
98
126
  name: rake
99
127
  requirement: !ruby/object:Gem::Requirement