Skip to content

Commit

Permalink
doc(stream): add usage for chat streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
yjean committed Mar 27, 2024
1 parent a7c5462 commit 8f08821
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,51 @@ If bundler is not being used to manage dependencies, install the gem by executin
## Usage

### Instantiating API client

```ruby
require "cohere"

client = Cohere::Client.new(
ENV['COHERE_API_KEY']
)
```

### Generate

```ruby
client.generate(
prompt: "Once upon a time in a magical land called"
)
```

### Chat

```ruby
client.chat(
message: "Hey! How are you?"
)
```

`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.

```ruby
client.chat(message: "Hey! How are you?", stream: true) do |chunk, overall_received_bytes|
puts "Received #{overall_received_bytes} bytes: #{chunk.force_encoding(Encoding::UTF_8)}"
end
```

`force_encoding` is preferred to avoid JSON parsing issue when Cohere returns emoticon.

### Embed

```ruby
client.embed(
texts: ["hello!"]
)
```

### Classify

```ruby
examples = [
{ text: "Dermatologists don't like her!", label: "Spam" },
Expand All @@ -84,27 +100,31 @@ client.classify(
```

### Tokenize

```ruby
client.tokenize(
text: "hello world!"
)
```

### Detokenize

```ruby
client.detokenize(
tokens: [33555, 1114 , 34]
)
```

### Detect language

```ruby
client.detect_language(
texts: ["Здравствуй, Мир"]
)
```

### Summarize

```ruby
client.summarize(
text: "..."
Expand Down

0 comments on commit 8f08821

Please sign in to comment.