xfyun-spark 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +62 -5
- data/lib/xfyun/spark/client.rb +34 -4
- data/lib/xfyun/spark/request.rb +16 -30
- data/lib/xfyun/spark/version.rb +1 -1
- data/lib/xfyun/spark.rb +5 -7
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3cac8e5659bd9a13198a6efa899fd07ae8cdb00a4fa0281ce210dd57e39e1100
|
4
|
+
data.tar.gz: fe72c126c9a13ea62bf09103bd1e0633a43e291b553a26ad17cd82d1e8cceef6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de7070e1e4be16c2e44fab3ef2eab7fac21976bcf45b3976647a86d4ae80b217d85fccde809e9b2d1f37edd12f66ea0d49bd81b22e182ce8eb067f3230a2031b
|
7
|
+
data.tar.gz: 0ede79ecff4548733a9c06dc741a5d0da606e09740c2083468d8a1c6bdb9eb1de4126d886b9eabcf95ca787deba95bf734cbb3c8074ad122362a893d9e691f11
|
data/CHANGELOG.md
CHANGED
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
7
|
|
8
|
+
## [0.1.1] - 2023-09-19
|
9
|
+
|
10
|
+
### Changed
|
11
|
+
|
12
|
+
- return error resonse
|
13
|
+
- update default config and update chat funciton
|
14
|
+
|
8
15
|
## [0.1.0] - 2023-09-18
|
9
16
|
|
10
17
|
### Added
|
data/README.md
CHANGED
@@ -8,24 +8,81 @@
|
|
8
8
|
|
9
9
|
Install the gem and add to the application's Gemfile by executing:
|
10
10
|
|
11
|
-
|
11
|
+
`bundle add xfyun-spark`
|
12
12
|
|
13
13
|
If bundler is not being used to manage dependencies, install the gem by executing:
|
14
14
|
|
15
|
-
|
15
|
+
`gem install xfyun-spark`
|
16
16
|
|
17
17
|
## Usage
|
18
18
|
|
19
|
-
|
19
|
+
### Configure
|
20
|
+
|
21
|
+
Configure the gem in initializer file
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
Xfyun::Spark.configure do |config|
|
25
|
+
config.appid = 'appid'
|
26
|
+
config.api_secret = 'api_secret'
|
27
|
+
config.api_key = 'api_key'
|
28
|
+
# Optional configs
|
29
|
+
# config.model = 'V1.5'
|
30
|
+
# config.host = 'your host'
|
31
|
+
# config.request_timeout = 10
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
Create a client
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
client = Xfyun::Spark::Client.new
|
39
|
+
```
|
40
|
+
|
41
|
+
Create a client overriding the default config
|
20
42
|
|
21
43
|
```ruby
|
22
44
|
client = Xfyun::Spark::Client.new({
|
23
45
|
appid: 'appid',
|
24
46
|
api_secret: 'api_secreti',
|
25
47
|
api_key: 'api_key'
|
48
|
+
model: 'V2' # use V2 model
|
26
49
|
})
|
50
|
+
```
|
51
|
+
|
52
|
+
### Chat
|
53
|
+
|
54
|
+
Chat with simple payload, using default header and parameter
|
27
55
|
|
28
|
-
|
56
|
+
```ruby
|
57
|
+
response_body = client.chat(
|
58
|
+
payload: {
|
59
|
+
message: {
|
60
|
+
text: [{"role": "user", "content": "你是谁"}]
|
61
|
+
}
|
62
|
+
}
|
63
|
+
)
|
64
|
+
answer = response_body.dig('payload', 'choices', 'text', 0, 'content')
|
65
|
+
```
|
66
|
+
|
67
|
+
Chat with custom header and parameter
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
response_body = client.chat(
|
71
|
+
parameter: {
|
72
|
+
header: {
|
73
|
+
uid: "12345"
|
74
|
+
},
|
75
|
+
chat: {
|
76
|
+
temperature: 0.5,
|
77
|
+
max_tokens: 1024,
|
78
|
+
}
|
79
|
+
},
|
80
|
+
payload: {
|
81
|
+
message: {
|
82
|
+
text: [{"role": "user", "content": "你是谁"}]
|
83
|
+
}
|
84
|
+
}
|
85
|
+
)
|
29
86
|
answer = response_body.dig('payload', 'choices', 'text', 0, 'content')
|
30
87
|
```
|
31
88
|
|
@@ -33,7 +90,7 @@ answer = response_body.dig('payload', 'choices', 'text', 0, 'content')
|
|
33
90
|
|
34
91
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
35
92
|
|
36
|
-
To install this gem onto your local machine, run `bundle exec rake install`.
|
93
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
37
94
|
|
38
95
|
## Contributing
|
39
96
|
|
data/lib/xfyun/spark/client.rb
CHANGED
@@ -3,17 +3,47 @@ module Xfyun
|
|
3
3
|
class Client
|
4
4
|
include Xfyun::Spark::Request
|
5
5
|
|
6
|
-
CONFIG_KEYS = %i[appid api_key api_secret
|
7
|
-
attr_reader *CONFIG_KEYS
|
6
|
+
CONFIG_KEYS = %i[appid api_key api_secret model host request_timeout].freeze
|
7
|
+
attr_reader *CONFIG_KEYS, :request_version, :request_domain
|
8
8
|
|
9
9
|
def initialize(config = {})
|
10
10
|
CONFIG_KEYS.each do |key|
|
11
11
|
instance_variable_set("@#{key}", config[key] || Xfyun::Spark.configuration.send(key))
|
12
12
|
end
|
13
|
+
|
14
|
+
@request_version = {
|
15
|
+
"V1.5" => "v1.1",
|
16
|
+
"V2" => "v2.1",
|
17
|
+
}.fetch(@model)
|
18
|
+
|
19
|
+
@request_domain = {
|
20
|
+
"V1.5" => "general",
|
21
|
+
"V2" => "generalv2",
|
22
|
+
}.fetch(@model)
|
23
|
+
end
|
24
|
+
|
25
|
+
def chat(header: {}, parameter: {}, payload: {})
|
26
|
+
header = default_header.merge(header)
|
27
|
+
parameter = default_parameter.map do |k, v|
|
28
|
+
[k, v.merge(parameter.fetch(k))]
|
29
|
+
end.to_h
|
30
|
+
request(path: "/chat", parameters: {
|
31
|
+
header: header,
|
32
|
+
parameter: parameter,
|
33
|
+
payload: payload
|
34
|
+
})
|
35
|
+
end
|
36
|
+
|
37
|
+
def default_header
|
38
|
+
{app_id: @appid}
|
13
39
|
end
|
14
40
|
|
15
|
-
def
|
16
|
-
|
41
|
+
def default_parameter
|
42
|
+
{
|
43
|
+
chat: {
|
44
|
+
domain: @request_domain,
|
45
|
+
}
|
46
|
+
}
|
17
47
|
end
|
18
48
|
|
19
49
|
end
|
data/lib/xfyun/spark/request.rb
CHANGED
@@ -9,25 +9,6 @@ module Xfyun
|
|
9
9
|
module Spark
|
10
10
|
module Request
|
11
11
|
|
12
|
-
def gen_params(question)
|
13
|
-
data = {
|
14
|
-
"header": {
|
15
|
-
"app_id": @appid,
|
16
|
-
"uid": "1234"
|
17
|
-
},
|
18
|
-
"parameter": {
|
19
|
-
"chat": {
|
20
|
-
"domain": @api_type,
|
21
|
-
}
|
22
|
-
},
|
23
|
-
"payload": {
|
24
|
-
"message": {
|
25
|
-
"text": question
|
26
|
-
}
|
27
|
-
}
|
28
|
-
}
|
29
|
-
end
|
30
|
-
|
31
12
|
def request(path:, parameters:)
|
32
13
|
content = ""
|
33
14
|
data = nil
|
@@ -54,13 +35,18 @@ module Xfyun
|
|
54
35
|
ws.on(:message) do |event|
|
55
36
|
p [:message, event.data]
|
56
37
|
response_data = JSON.parse(event.data)
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
38
|
+
code = response_data.dig('header', 'code')
|
39
|
+
if code == 0
|
40
|
+
status = response_data.dig('header', 'status')
|
41
|
+
case status
|
42
|
+
when 0, 1
|
43
|
+
content << response_data.dig('payload', 'choices', 'text', 0, 'content')
|
44
|
+
when 2
|
45
|
+
# last message
|
46
|
+
response_data['payload']['choices']['text'][0]['content'] = content
|
47
|
+
data = response_data
|
48
|
+
end
|
49
|
+
else
|
64
50
|
data = response_data
|
65
51
|
end
|
66
52
|
end
|
@@ -72,9 +58,9 @@ module Xfyun
|
|
72
58
|
def authorization_url(path)
|
73
59
|
httpdate = Time.now.httpdate
|
74
60
|
|
75
|
-
tmp_str = "host: #{@
|
61
|
+
tmp_str = "host: #{@host}\n"
|
76
62
|
tmp_str << "date: #{httpdate}\n"
|
77
|
-
tmp_str << "GET /#{
|
63
|
+
tmp_str << "GET /#{@request_version}#{path} HTTP/1.1"
|
78
64
|
|
79
65
|
tmp_sha = OpenSSL::HMAC.digest('sha256', @api_secret, tmp_str)
|
80
66
|
signature = Base64.strict_encode64(tmp_sha)
|
@@ -85,10 +71,10 @@ module Xfyun
|
|
85
71
|
query = URI.encode_www_form({
|
86
72
|
authorization: authorization,
|
87
73
|
date: httpdate,
|
88
|
-
host: @
|
74
|
+
host: @host,
|
89
75
|
})
|
90
76
|
|
91
|
-
url = "wss://#{@
|
77
|
+
url = "wss://#{@host}/#{@request_version}#{path}?#{query}"
|
92
78
|
end
|
93
79
|
|
94
80
|
end
|
data/lib/xfyun/spark/version.rb
CHANGED
data/lib/xfyun/spark.rb
CHANGED
@@ -7,20 +7,18 @@ module Xfyun
|
|
7
7
|
class Error < StandardError; end
|
8
8
|
|
9
9
|
class Configuration
|
10
|
-
attr_accessor :appid, :api_key, :api_secret, :
|
10
|
+
attr_accessor :appid, :api_key, :api_secret, :model, :host, :request_timeout
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
DEFAULT_URI_BASE = "spark-api.xf-yun.com".freeze
|
12
|
+
DEFAULT_MODEL = "V1.5".freeze
|
13
|
+
DEFAULT_HOST = "spark-api.xf-yun.com".freeze
|
15
14
|
DEFAULT_REQUEST_TIMEOUT = 120
|
16
15
|
|
17
16
|
def initialize
|
18
17
|
@appid = nil
|
19
18
|
@api_key = nil
|
20
19
|
@api_secret = nil
|
21
|
-
@
|
22
|
-
@
|
23
|
-
@uri_base = DEFAULT_URI_BASE
|
20
|
+
@model = DEFAULT_MODEL
|
21
|
+
@host = DEFAULT_HOST
|
24
22
|
@request_timeout = DEFAULT_REQUEST_TIMEOUT
|
25
23
|
end
|
26
24
|
end
|