Merge pull request #1 from Eric-Guo/main

Do the first calling correctly.
This commit is contained in:
Eric Guo
2025-02-28 14:32:46 +08:00
committed by GitHub
7 changed files with 150 additions and 127 deletions
+2 -2
View File
@@ -10,9 +10,9 @@ jobs:
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.0.0
ruby-version: 3.1.6
- name: Run the default task
run: |
gem install bundler -v 2.2.3
gem install bundler -v 2.6.5
bundle install
bundle exec rake
+6
View File
@@ -1,3 +1,9 @@
Metrics/MethodLength:
Max: 25
Style/Documentation:
Enabled: false
Style/StringLiterals:
Enabled: true
EnforcedStyle: double_quotes
+2 -2
View File
@@ -9,6 +9,6 @@ gem "rake", "~> 13.0"
gem "minitest", "~> 5.0"
gem "rubocop", "~> 0.80"
gem "rubocop", "~> 1.72"
gem 'webmock'
gem "webmock"
+35 -25
View File
@@ -6,50 +6,60 @@ PATH
GEM
remote: https://rubygems.org/
specs:
addressable (2.8.5)
public_suffix (>= 2.0.2, < 6.0)
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
ast (2.4.2)
crack (0.4.5)
bigdecimal (3.1.9)
crack (1.0.0)
bigdecimal
rexml
hashdiff (1.0.1)
minitest (5.19.0)
parallel (1.23.0)
parser (3.2.2.3)
hashdiff (1.1.2)
json (2.10.1)
language_server-protocol (3.17.0.4)
lint_roller (1.1.0)
minitest (5.25.4)
parallel (1.26.3)
parser (3.3.7.1)
ast (~> 2.4.1)
racc
public_suffix (5.0.3)
racc (1.7.1)
public_suffix (6.0.1)
racc (1.8.1)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.8.1)
rexml (3.2.6)
rubocop (0.93.1)
rake (13.2.1)
regexp_parser (2.10.0)
rexml (3.4.1)
rubocop (1.73.1)
json (~> 2.3)
language_server-protocol (~> 3.17.0.2)
lint_roller (~> 1.1.0)
parallel (~> 1.10)
parser (>= 2.7.1.5)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8)
rexml
rubocop-ast (>= 0.6.0)
regexp_parser (>= 2.9.3, < 3.0)
rubocop-ast (>= 1.38.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 2.0)
rubocop-ast (1.29.0)
parser (>= 3.2.1.0)
unicode-display_width (>= 2.4.0, < 4.0)
rubocop-ast (1.38.1)
parser (>= 3.3.1.0)
ruby-progressbar (1.13.0)
unicode-display_width (1.8.0)
webmock (3.18.1)
unicode-display_width (3.1.4)
unicode-emoji (~> 4.0, >= 4.0.4)
unicode-emoji (4.0.4)
webmock (3.25.0)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
PLATFORMS
arm64-darwin-22
arm64-darwin-24
ruby
DEPENDENCIES
dify_client!
minitest (~> 5.0)
rake (~> 13.0)
rubocop (~> 0.80)
rubocop (~> 1.72)
webmock
BUNDLED WITH
2.2.3
2.6.5
+4 -3
View File
@@ -25,13 +25,14 @@ To use the DifyClient gem, follow these steps:
1 Require the gem:
```ruby
require 'dify_client'
require 'dify/client'
```
2 Create a new client instance:
```ruby
api_key = 'YOUR_API_KEY'
client = DifyClient::Client.new(api_key)
client = ChatClient.new(api_key)
```
3 Use the available methods to interact with the Dify.ai API. Here are the methods provided by the DifyClient::Client class:
@@ -101,4 +102,4 @@ Bug reports and pull requests are welcome on GitHub at [https://github.com/langg
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
+78 -73
View File
@@ -1,91 +1,96 @@
# frozen_string_literal: true
require_relative "client/version"
class DifyClient
def initialize(api_key)
@api_key = api_key
@base_url = "https://api.dify.ai/v1"
def initialize(api_key, base_url = "https://api.dify.ai/v1")
@api_key = api_key
@base_url = base_url
end
def message_feedback(message_id, rating, user)
data = {
rating: rating,
user: user
}
_send_request("POST", "/messages/#{message_id}/feedbacks", data)
end
def get_application_parameters(user)
params = { user: user }
_send_request("GET", "/parameters", nil, params)
end
def update_api_key(new_key)
@api_key = new_key
end
private
def _send_request(method, endpoint, data = nil, params = nil, _stream: false)
uri = URI.parse("#{@base_url}#{endpoint}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
headers = {
"Authorization" => "Bearer #{@api_key}",
"Content-Type" => "application/json"
}
if method == "GET"
uri.query = URI.encode_www_form(params) if params
request = Net::HTTP::Get.new(uri.request_uri, headers)
elsif method == "POST"
request = Net::HTTP::Post.new(uri.request_uri, headers)
request.body = data.to_json
end
def _send_request(method, endpoint, data = nil, params = nil, stream = false)
uri = URI.parse("#{@base_url}#{endpoint}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
headers = {
"Authorization" => "Bearer #{@api_key}",
"Content-Type" => "application/json"
}
if method == "GET"
uri.query = URI.encode_www_form(params) if params
request = Net::HTTP::Get.new(uri.request_uri, headers)
elsif method == "POST"
request = Net::HTTP::Post.new(uri.request_uri, headers)
request.body = data.to_json
end
response = http.request(request)
return response
end
def message_feedback(message_id, rating, user)
data = {
rating: rating,
user: user
}
return _send_request("POST", "/messages/#{message_id}/feedbacks", data)
end
def get_application_parameters(user)
params = {user: user}
return _send_request("GET", "/parameters", nil, params)
end
http.request(request)
end
end
class CompletionClient < DifyClient
def create_completion_message(inputs, query, response_mode, user)
data = {
inputs: inputs,
query: query,
response_mode: response_mode,
user: user
}
return _send_request("POST", "/completion-messages", data, nil, response_mode == "streaming")
end
def create_completion_message(inputs, query, response_mode, user)
data = {
inputs: inputs,
query: query,
response_mode: response_mode,
user: user
}
_send_request("POST", "/completion-messages", data, nil, response_mode == "streaming")
end
end
class ChatClient < DifyClient
def create_chat_message(inputs, query, user, response_mode = "blocking", conversation_id = nil)
data = {
inputs: inputs,
query: query,
user: user,
response_mode: response_mode
}
data[:conversation_id] = conversation_id if conversation_id
def create_chat_message(inputs, query, user, response_mode = "blocking", conversation_id = nil)
data = {
inputs: inputs,
query: query,
user: user,
response_mode: response_mode
}
data[:conversation_id] = conversation_id if conversation_id
return _send_request("POST", "/chat-messages", data, nil, response_mode == "streaming")
end
_send_request("POST", "/chat-messages", data, nil, response_mode == "streaming")
end
def get_conversation_messages(user, conversation_id = nil, first_id = nil, limit = nil)
params = {user: user}
params[:conversation_id] = conversation_id if conversation_id
params[:first_id] = first_id if first_id
params[:limit] = limit if limit
def get_conversation_messages(user, conversation_id = nil, first_id = nil, limit = nil)
params = { user: user }
params[:conversation_id] = conversation_id if conversation_id
params[:first_id] = first_id if first_id
params[:limit] = limit if limit
return _send_request("GET", "/messages", nil, params)
end
_send_request("GET", "/messages", nil, params)
end
def get_conversations(user, last_id = nil, limit = nil, pinned = nil)
params = {user: user, last_id: last_id, limit: limit, pinned: pinned}
return _send_request("GET", "/conversations", nil, params)
end
def get_conversations(user, last_id = nil, limit = nil, pinned = nil)
params = { user: user, last_id: last_id, limit: limit, pinned: pinned }
_send_request("GET", "/conversations", nil, params)
end
def rename_conversation(conversation_id, name, user)
data = {name: name, user: user}
return _send_request("POST", "/conversations/#{conversation_id}/name", data)
end
def rename_conversation(conversation_id, name, user)
data = { name: name, user: user }
_send_request("POST", "/conversations/#{conversation_id}/name", data)
end
end
+23 -22
View File
@@ -1,16 +1,18 @@
require 'test_helper'
require 'webmock/minitest'
require 'json'
# frozen_string_literal: true
require "test_helper"
require "webmock/minitest"
require "json"
require "dify/client"
class DifyClientTest < Minitest::Test
def setup
@api_key = 'YOUR_API_KEY'
@client = DifyClient::Client.new(@api_key)
@api_key = "YOUR_API_KEY"
@client = DifyClient.new(@api_key)
end
def test_update_api_key
new_api_key = 'NEW_API_KEY'
new_api_key = "NEW_API_KEY"
@client.update_api_key(new_api_key)
@@ -18,25 +20,24 @@ class DifyClientTest < Minitest::Test
end
def test_get_application_parameters
user = 'USER_ID'
expected_response = {}
user = "USER_ID"
response_body = { "parameters" => [{ "name" => "test", "value" => "test_value" }] }
stub_request(:get, "https://api.dify.ai/v1/parameters").
with(
body: {"user"=>"USER_ID"},
headers: {
'Accept'=>'*/*',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization'=>'Bearer YOUR_API_KEY',
'Content-Type'=>'application/x-www-form-urlencoded',
'Responsetype'=>'json',
'User-Agent'=>'Ruby'
}).
to_return(status: 200, body: expected_response.to_json, headers: {})
stub_request(:get, "https://api.dify.ai/v1/parameters?user=USER_ID")
.with(
headers: {
"Accept" => "*/*",
"Accept-Encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3",
"Authorization" => "Bearer YOUR_API_KEY",
"Content-Type" => "application/json",
"User-Agent" => "Ruby"
}
)
.to_return(status: 200, body: response_body.to_json, headers: {})
response = @client.get_application_parameters(user)
assert_equal expected_response, response
assert_equal 200, response.code.to_i
assert_equal response_body, JSON.parse(response.body)
end
end