|
4 | 4 |
|
5 | 5 | RSpec.describe Cohere::Client do |
6 | 6 | let(:instance) { described_class.new(api_key: "123") } |
| 7 | + |
| 8 | + describe "#generate" do |
| 9 | + let(:generate_result) { JSON.parse(File.read("spec/fixtures/generate_result.json")) } |
| 10 | + let(:response) { OpenStruct.new(body: generate_result) } |
| 11 | + |
| 12 | + before do |
| 13 | + allow_any_instance_of(Faraday::Connection).to receive(:post) |
| 14 | + .with("generate") |
| 15 | + .and_return(response) |
| 16 | + end |
| 17 | + |
| 18 | + it "returns a response" do |
| 19 | + expect(instance.generate( |
| 20 | + prompt: "Once upon a time in a magical land called" |
| 21 | + ).dig('generations').first.dig('text')).to eq(" The Past there was a Game called Warhammer Fantasy Battle.") |
| 22 | + end |
| 23 | + end |
| 24 | + |
| 25 | + describe "#embed" do |
| 26 | + let(:embed_result) { JSON.parse(File.read("spec/fixtures/embed_result.json")) } |
| 27 | + let(:response) { OpenStruct.new(body: embed_result) } |
| 28 | + |
| 29 | + before do |
| 30 | + allow_any_instance_of(Faraday::Connection).to receive(:post) |
| 31 | + .with("embed") |
| 32 | + .and_return(response) |
| 33 | + end |
| 34 | + |
| 35 | + it "returns a response" do |
| 36 | + expect(instance.embed( |
| 37 | + texts: ["hello!"] |
| 38 | + ).dig('embeddings')).to eq([[ 1.2177734, 0.67529297, 2.0742188 ]]) |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + describe "#classify" do |
| 43 | + let(:classify_result) { JSON.parse(File.read("spec/fixtures/classify_result.json")) } |
| 44 | + let(:response) { OpenStruct.new(body: classify_result) } |
| 45 | + |
| 46 | + let(:inputs) {[ |
| 47 | + { text: "Dermatologists don't like her!", label: "Spam" }, |
| 48 | + { text: "Hello, open to this?", label: "Spam" } |
| 49 | + ]} |
| 50 | + |
| 51 | + let(:examples) {[ |
| 52 | + "Confirm your email address", |
| 53 | + "hey i need u to send some $" |
| 54 | + ]} |
| 55 | + |
| 56 | + before do |
| 57 | + allow_any_instance_of(Faraday::Connection).to receive(:post) |
| 58 | + .with("classify") |
| 59 | + .and_return(response) |
| 60 | + end |
| 61 | + |
| 62 | + it "returns a response" do |
| 63 | + res = instance.classify( |
| 64 | + inputs: inputs, |
| 65 | + examples: examples |
| 66 | + ).dig('classifications') |
| 67 | + |
| 68 | + expect(res.first.dig('prediction')).to eq("Not spam") |
| 69 | + expect(res.last.dig('prediction')).to eq("Spam") |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + describe "#tokenize" do |
| 74 | + let(:tokenize_result) { JSON.parse(File.read("spec/fixtures/tokenize_result.json")) } |
| 75 | + let(:response) { OpenStruct.new(body: tokenize_result) } |
| 76 | + |
| 77 | + before do |
| 78 | + allow_any_instance_of(Faraday::Connection).to receive(:post) |
| 79 | + .with("tokenize") |
| 80 | + .and_return(response) |
| 81 | + end |
| 82 | + |
| 83 | + it "returns a response" do |
| 84 | + expect(instance.tokenize( |
| 85 | + text: "Hello, world!" |
| 86 | + ).dig('tokens')).to eq([33555, 1114 , 34]) |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + describe "#detokenize" do |
| 91 | + let(:detokenize_result) { JSON.parse(File.read("spec/fixtures/detokenize_result.json")) } |
| 92 | + let(:response) { OpenStruct.new(body: detokenize_result) } |
| 93 | + |
| 94 | + before do |
| 95 | + allow_any_instance_of(Faraday::Connection).to receive(:post) |
| 96 | + .with("detokenize") |
| 97 | + .and_return(response) |
| 98 | + end |
| 99 | + |
| 100 | + it "returns a response" do |
| 101 | + expect(instance.detokenize( |
| 102 | + tokens: [33555, 1114 , 34] |
| 103 | + ).dig('text')).to eq("hello world!") |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + describe "#detect_language" do |
| 108 | + let(:detect_language_result) { JSON.parse(File.read("spec/fixtures/detect_language_result.json")) } |
| 109 | + let(:response) { OpenStruct.new(body: detect_language_result) } |
| 110 | + |
| 111 | + before do |
| 112 | + allow_any_instance_of(Faraday::Connection).to receive(:post) |
| 113 | + .with("detect-language") |
| 114 | + .and_return(response) |
| 115 | + end |
| 116 | + |
| 117 | + it "returns a response" do |
| 118 | + expect(instance.detect_language( |
| 119 | + texts: ["Здравствуй, Мир"] |
| 120 | + ).dig('results').first.dig('language_code')).to eq("ru") |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 124 | + describe "#summarize" do |
| 125 | + let(:summarize_result) { JSON.parse(File.read("spec/fixtures/summarize_result.json")) } |
| 126 | + let(:response) { OpenStruct.new(body: summarize_result) } |
| 127 | + |
| 128 | + before do |
| 129 | + allow_any_instance_of(Faraday::Connection).to receive(:post) |
| 130 | + .with("summarize") |
| 131 | + .and_return(response) |
| 132 | + end |
| 133 | + |
| 134 | + it "returns a response" do |
| 135 | + expect(instance.summarize( |
| 136 | + text: "Ice cream is a sweetened frozen food typically eaten as a snack or dessert. " + |
| 137 | + "It may be made from milk or cream and is flavoured with a sweetener, " + |
| 138 | + "either sugar or an alternative, and a spice, such as cocoa or vanilla, " + |
| 139 | + "or with fruit such as strawberries or peaches. " + |
| 140 | + "It can also be made by whisking a flavored cream base and liquid nitrogen together. " + |
| 141 | + "Food coloring is sometimes added, in addition to stabilizers. " + |
| 142 | + "The mixture is cooled below the freezing point of water and stirred to incorporate air spaces " + |
| 143 | + "and to prevent detectable ice crystals from forming. The result is a smooth, " + |
| 144 | + "semi-solid foam that is solid at very low temperatures (below 2 °C or 35 °F). " + |
| 145 | + "It becomes more malleable as its temperature increases.\n\n" + |
| 146 | + "The meaning of the name \"ice cream\" varies from one country to another. " + |
| 147 | + "In some countries, such as the United States, \"ice cream\" applies only to a specific variety, " + |
| 148 | + "and most governments regulate the commercial use of the various terms according to the " + |
| 149 | + "relative quantities of the main ingredients, notably the amount of cream. " + |
| 150 | + "Products that do not meet the criteria to be called ice cream are sometimes labelled " + |
| 151 | + "\"frozen dairy dessert\" instead. In other countries, such as Italy and Argentina, " + |
| 152 | + "one word is used fo\r all variants. Analogues made from dairy alternatives, " + |
| 153 | + "such as goat's or sheep's milk, or milk substitutes " + |
| 154 | + "(e.g., soy, cashew, coconut, almond milk or tofu), are available for those who are " + |
| 155 | + "lactose intolerant, allergic to dairy protein or vegan." |
| 156 | + ).dig('summary')).to eq("Ice cream is a frozen dessert made from dairy products or non-dairy substitutes. It is flavoured with a sweetener and a spice or with fruit. It is smooth and semi-solid at low temperatures.") |
| 157 | + end |
| 158 | + end |
7 | 159 | end |
0 commit comments