Skip to content

Commit 0717703

Browse files
Merge pull request #12 from yjean/feat/chat-streaming-callback
feat(chat): handle block to received streamed data
2 parents b74afc1 + 628a2a9 commit 0717703

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,51 @@ If bundler is not being used to manage dependencies, install the gem by executin
2929
## Usage
3030

3131
### Instantiating API client
32+
3233
```ruby
3334
require "cohere"
3435

3536
client = Cohere::Client.new(
3637
ENV['COHERE_API_KEY']
3738
)
3839
```
40+
3941
### Generate
42+
4043
```ruby
4144
client.generate(
4245
prompt: "Once upon a time in a magical land called"
4346
)
4447
```
4548

4649
### Chat
50+
4751
```ruby
4852
client.chat(
4953
message: "Hey! How are you?"
5054
)
5155
```
5256

57+
`chat` supports a streaming option. You can pass a block to the `chat` method and it will yield a new chunk as soon as it is received.
58+
59+
```ruby
60+
client.chat(message: "Hey! How are you?", stream: true) do |chunk, overall_received_bytes|
61+
puts "Received #{overall_received_bytes} bytes: #{chunk.force_encoding(Encoding::UTF_8)}"
62+
end
63+
```
64+
65+
`force_encoding` is preferred to avoid JSON parsing issue when Cohere returns emoticon.
66+
5367
### Embed
68+
5469
```ruby
5570
client.embed(
5671
texts: ["hello!"]
5772
)
5873
```
5974

6075
### Classify
76+
6177
```ruby
6278
examples = [
6379
{ text: "Dermatologists don't like her!", label: "Spam" },
@@ -84,27 +100,31 @@ client.classify(
84100
```
85101

86102
### Tokenize
103+
87104
```ruby
88105
client.tokenize(
89106
text: "hello world!"
90107
)
91108
```
92109

93110
### Detokenize
111+
94112
```ruby
95113
client.detokenize(
96114
tokens: [33555, 1114 , 34]
97115
)
98116
```
99117

100118
### Detect language
119+
101120
```ruby
102121
client.detect_language(
103122
texts: ["Здравствуй, Мир"]
104123
)
105124
```
106125

107126
### Summarize
127+
108128
```ruby
109129
client.summarize(
110130
text: "..."

lib/cohere/client.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@ def chat(
2929
k: nil,
3030
p: nil,
3131
frequency_penalty: nil,
32-
presence_penalty: nil
32+
presence_penalty: nil,
33+
&block
3334
)
3435
response = connection.post("chat") do |req|
3536
req.body = {message: message}
3637
req.body[:model] = model if model
37-
req.body[:stream] = stream if stream
38+
if stream || block
39+
req.body[:stream] = true
40+
req.options.on_data = block if block
41+
end
3842
req.body[:preamble_override] = preamble_override if preamble_override
3943
req.body[:chat_history] = chat_history if chat_history
4044
req.body[:conversation_id] = conversation_id if conversation_id

0 commit comments

Comments
 (0)