xfyun-spark 0.1.0 → 0.1.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 99a8d6ec5b1f030402777262e0e03f6ebffcf43bc047affbdff5881597add161
4
- data.tar.gz: 51ed1d8d10046614f919e50b72cbbf773cf04b13cbfb39c4ddb78eb4e225711b
3
+ metadata.gz: 3cac8e5659bd9a13198a6efa899fd07ae8cdb00a4fa0281ce210dd57e39e1100
4
+ data.tar.gz: fe72c126c9a13ea62bf09103bd1e0633a43e291b553a26ad17cd82d1e8cceef6
5
5
  SHA512:
6
- metadata.gz: 3ea278d2d0a8f30d87007cc582dbbe904ffabfb9fa55270b3a0da26853342fe4001986190261f7c09c234d393799cdf654e95013da82f00908a79020921d282e
7
- data.tar.gz: e784fd961bdcb93d8da6f0148726f8dfff4dfabdbc205216bd83f264afd48b59a44b08f331007834ffaefbd57a8b56d5b04ce828eb77100477fb194170d804d5
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
- $ bundle add xfyun-spark
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
- $ gem install xfyun-spark
15
+ `gem install xfyun-spark`
16
16
 
17
17
  ## Usage
18
18
 
19
- xfyun spark ruby sdk
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
- response_body = client.chat([{"role": "user", "content": "你是谁"}])
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`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
93
+ To install this gem onto your local machine, run `bundle exec rake install`.
37
94
 
38
95
  ## Contributing
39
96
 
@@ -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 api_type api_version uri_base request_timeout].freeze
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 chat(question)
16
- request(path: "/chat", parameters: gen_params(question))
41
+ def default_parameter
42
+ {
43
+ chat: {
44
+ domain: @request_domain,
45
+ }
46
+ }
17
47
  end
18
48
 
19
49
  end
@@ -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
- status = response_data.dig('header', 'status')
58
- case status
59
- when 0, 1
60
- content << response_data.dig('payload', 'choices', 'text', 0, 'content')
61
- when 2
62
- # last message
63
- response_data['payload']['choices']['text'][0]['content'] = content
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: #{@uri_base}\n"
61
+ tmp_str = "host: #{@host}\n"
76
62
  tmp_str << "date: #{httpdate}\n"
77
- tmp_str << "GET /#{api_version}#{path} HTTP/1.1"
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: @uri_base,
74
+ host: @host,
89
75
  })
90
76
 
91
- url = "wss://#{@uri_base}/#{@api_version}#{path}?#{query}"
77
+ url = "wss://#{@host}/#{@request_version}#{path}?#{query}"
92
78
  end
93
79
 
94
80
  end
@@ -1,5 +1,5 @@
1
1
  module Xfyun
2
2
  module Spark
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  end
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, :api_type, :api_version, :uri_base, :request_timeout
10
+ attr_accessor :appid, :api_key, :api_secret, :model, :host, :request_timeout
11
11
 
12
- DEFAULT_API_TYPE = "general".freeze
13
- DEFAULT_API_VERSION = "v1.1".freeze
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
- @api_type = DEFAULT_API_TYPE
22
- @api_version = DEFAULT_API_VERSION
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,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xfyun-spark
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - chaucerling