|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe DiscourseAi::Completions::Endpoints::SambaNova do |
| 4 | + fab!(:llm_model) { Fabricate(:samba_nova_model) } |
| 5 | + let(:llm) { llm_model.to_llm } |
| 6 | + |
| 7 | + it "can stream completions" do |
| 8 | + body = <<~PARTS |
| 9 | + data: {"id": "4c5e4a44-e847-467d-b9cd-d2f6530678cd", "object": "chat.completion.chunk", "created": 1721336361, "model": "llama3-8b", "system_fingerprint": "fastcoe", "choices": [{"index": 0, "delta": {"content": "I am a bot"}, "logprobs": null, "finish_reason": null}]} |
| 10 | +
|
| 11 | +data: {"id": "4c5e4a44-e847-467d-b9cd-d2f6530678cd", "object": "chat.completion.chunk", "created": 1721336361, "model": "llama3-8b", "system_fingerprint": "fastcoe", "choices": [], "usage": {"is_last_response": true, "total_tokens": 62, "prompt_tokens": 21, "completion_tokens": 41, "time_to_first_token": 0.09152531623840332, "end_time": 1721336361.582011, "start_time": 1721336361.413994, "total_latency": 0.16801691055297852, "total_tokens_per_sec": 369.010475171488, "completion_tokens_per_sec": 244.02305616179046, "completion_tokens_after_first_per_sec": 522.9332759819093, "completion_tokens_after_first_per_sec_first_ten": 1016.0004844667837}} |
| 12 | +
|
| 13 | +data: [DONE] |
| 14 | + PARTS |
| 15 | + |
| 16 | + stub_request(:post, "https://api.sambanova.ai/v1/chat/completions").with( |
| 17 | + body: |
| 18 | + "{\"model\":\"samba-nova\",\"messages\":[{\"role\":\"system\",\"content\":\"You are a helpful bot\"},{\"role\":\"user\",\"content\":\"who are you?\"}],\"stream\":true}", |
| 19 | + headers: { |
| 20 | + "Authorization" => "Bearer ABC", |
| 21 | + "Content-Type" => "application/json", |
| 22 | + }, |
| 23 | + ).to_return(status: 200, body: body, headers: {}) |
| 24 | + |
| 25 | + response = +"" |
| 26 | + llm.generate("who are you?", user: Discourse.system_user) { |partial| response << partial } |
| 27 | + |
| 28 | + expect(response).to eq("I am a bot") |
| 29 | + end |
| 30 | + |
| 31 | + it "can perform regular completions" do |
| 32 | + body = { choices: [message: { content: "I am a bot" }] }.to_json |
| 33 | + |
| 34 | + stub_request(:post, "https://api.sambanova.ai/v1/chat/completions").with( |
| 35 | + body: |
| 36 | + "{\"model\":\"samba-nova\",\"messages\":[{\"role\":\"system\",\"content\":\"You are a helpful bot\"},{\"role\":\"user\",\"content\":\"who are you?\"}]}", |
| 37 | + headers: { |
| 38 | + "Authorization" => "Bearer ABC", |
| 39 | + "Content-Type" => "application/json", |
| 40 | + }, |
| 41 | + ).to_return(status: 200, body: body, headers: {}) |
| 42 | + |
| 43 | + response = llm.generate("who are you?", user: Discourse.system_user) |
| 44 | + |
| 45 | + expect(response).to eq("I am a bot") |
| 46 | + end |
| 47 | +end |
0 commit comments