xfyun-spark 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +14 -0
- data/Gemfile.lock +1 -1
- data/README.md +62 -5
- data/lib/xfyun/spark/client.rb +34 -4
- data/lib/xfyun/spark/request.rb +24 -36
- data/lib/xfyun/spark/version.rb +1 -1
- data/lib/xfyun/spark.rb +5 -7
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2102ae80fc0527c40e5d1b527bce6220811a7f37b3fec31c71f22c088c3f0aa6
|
4
|
+
data.tar.gz: 27de89212f5ae2f16e59ef8a6c504d4595409a0e3b11055d87d516ec613df8a4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe0d3454e91e8f0cf9f16f98aae6c7a72fb7407eabcf72d22557b20f0ea1c297278552b0595829b3e20a478061f6aed603531a4273ad210bea13d8338ab57efd
|
7
|
+
data.tar.gz: 18cd8d389212e1eed574d15ec5ac31044c86e42883c8e2ddd10a37d64ce910ef8b9b5ad43762fa4949a39dbaa337b146a6b330a67521995ab6ccfc65798fb329
|
data/CHANGELOG.md
CHANGED
@@ -5,6 +5,20 @@ 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.2] - 2023-09-20
|
9
|
+
|
10
|
+
### Fixed
|
11
|
+
|
12
|
+
- raise exception when ws onerror
|
13
|
+
- fixed last message content missing
|
14
|
+
|
15
|
+
## [0.1.1] - 2023-09-19
|
16
|
+
|
17
|
+
### Changed
|
18
|
+
|
19
|
+
- return error resonse
|
20
|
+
- update default config and update chat funciton
|
21
|
+
|
8
22
|
## [0.1.0] - 2023-09-18
|
9
23
|
|
10
24
|
### Added
|
data/Gemfile.lock
CHANGED
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,58 +9,46 @@ 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
|
34
15
|
EM.run {
|
35
16
|
url = authorization_url(path)
|
36
|
-
puts url
|
37
|
-
puts parameters
|
17
|
+
# puts url
|
18
|
+
# puts parameters
|
38
19
|
ws = Faye::WebSocket::Client.new(url)
|
39
20
|
|
40
21
|
ws.on(:open) do |event|
|
41
|
-
p [:open, ws.headers]
|
22
|
+
# p [:open, ws.headers]
|
42
23
|
ws.send(parameters.to_json)
|
43
24
|
end
|
44
25
|
|
45
26
|
ws.on(:close) do |event|
|
46
|
-
p [:close, event.code, event.reason]
|
27
|
+
# p [:close, event.code, event.reason]
|
47
28
|
EM.stop
|
48
29
|
end
|
49
30
|
|
50
31
|
ws.on(:error) do |event|
|
51
|
-
p [:error, event.message]
|
32
|
+
# p [:error, event.message]
|
33
|
+
raise Xfyun::Spark::Error.new(event.message)
|
52
34
|
end
|
53
35
|
|
54
36
|
ws.on(:message) do |event|
|
55
|
-
p [:message, event.data]
|
37
|
+
# p [:message, event.data]
|
56
38
|
response_data = JSON.parse(event.data)
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
39
|
+
code = response_data.dig('header', 'code')
|
40
|
+
if code == 0
|
41
|
+
status = response_data.dig('header', 'status')
|
42
|
+
case status
|
43
|
+
when 0, 1
|
44
|
+
content << response_data.dig('payload', 'choices', 'text', 0, 'content')
|
45
|
+
when 2
|
46
|
+
# last message
|
47
|
+
content << response_data.dig('payload', 'choices', 'text', 0, 'content')
|
48
|
+
response_data['payload']['choices']['text'][0]['content'] = content
|
49
|
+
data = response_data
|
50
|
+
end
|
51
|
+
else
|
64
52
|
data = response_data
|
65
53
|
end
|
66
54
|
end
|
@@ -72,9 +60,9 @@ module Xfyun
|
|
72
60
|
def authorization_url(path)
|
73
61
|
httpdate = Time.now.httpdate
|
74
62
|
|
75
|
-
tmp_str = "host: #{@
|
63
|
+
tmp_str = "host: #{@host}\n"
|
76
64
|
tmp_str << "date: #{httpdate}\n"
|
77
|
-
tmp_str << "GET /#{
|
65
|
+
tmp_str << "GET /#{@request_version}#{path} HTTP/1.1"
|
78
66
|
|
79
67
|
tmp_sha = OpenSSL::HMAC.digest('sha256', @api_secret, tmp_str)
|
80
68
|
signature = Base64.strict_encode64(tmp_sha)
|
@@ -85,10 +73,10 @@ module Xfyun
|
|
85
73
|
query = URI.encode_www_form({
|
86
74
|
authorization: authorization,
|
87
75
|
date: httpdate,
|
88
|
-
host: @
|
76
|
+
host: @host,
|
89
77
|
})
|
90
78
|
|
91
|
-
url = "wss://#{@
|
79
|
+
url = "wss://#{@host}/#{@request_version}#{path}?#{query}"
|
92
80
|
end
|
93
81
|
|
94
82
|
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
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xfyun-spark
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- chaucerling
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-09-
|
11
|
+
date: 2023-09-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faye-websocket
|