|
| 1 | +package org.llm4j.cohere; |
| 2 | + |
| 3 | +import org.apache.commons.configuration2.Configuration; |
| 4 | +import org.apache.commons.configuration2.EnvironmentConfiguration; |
| 5 | +import org.apache.commons.configuration2.MapConfiguration; |
| 6 | +import org.apache.commons.configuration2.builder.fluent.Configurations; |
| 7 | +import org.apache.commons.configuration2.ex.ConfigurationException; |
| 8 | +import org.junit.jupiter.api.Disabled; |
| 9 | +import org.junit.jupiter.api.DisplayName; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.junit.Ignore; |
| 12 | +import org.llm4j.api.ChatHistory; |
| 13 | +import org.llm4j.api.LanguageModel; |
| 14 | + |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static com.google.common.truth.Truth.assertWithMessage; |
| 21 | + |
| 22 | +@Disabled("requires API key to run") |
| 23 | +public class CohereLanguageModelTest { |
| 24 | + |
| 25 | + @Test |
| 26 | + @DisplayName("Should generate text") |
| 27 | + public void should_process_text_generation_request() throws ConfigurationException { |
| 28 | + |
| 29 | + Configuration config = new Configurations().properties("llm4j.properties"); |
| 30 | + |
| 31 | + LanguageModel llm = new CohereLanguageModel.Builder() |
| 32 | + .getLanguageModel(config); |
| 33 | + |
| 34 | + String answer = llm.process("In what country is El Outed located?"); |
| 35 | + |
| 36 | + assertWithMessage("Answer should contain right answer"). |
| 37 | + that(answer.toLowerCase()).contains("algeria"); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + @DisplayName("Should process chat request") |
| 42 | + public void should_process_chat_request() throws ConfigurationException { |
| 43 | + |
| 44 | + Configuration config = new Configurations().properties("llm4j.properties"); |
| 45 | + |
| 46 | + LanguageModel llm = new CohereLanguageModel.Builder() |
| 47 | + .getLanguageModel(config); |
| 48 | + |
| 49 | + ChatHistory history = new ChatHistory() |
| 50 | + .setContext("Respond to all questions with a rhyming poem.") |
| 51 | + .addExample( |
| 52 | + "What is the capital of Algeria?", |
| 53 | + "If the capital of Algeria is what you seek, Algiers is where you ought to peek.") |
| 54 | + .addMessage("How tall is Makam Echahid?"); |
| 55 | + |
| 56 | + String answer = llm.process(history); |
| 57 | + |
| 58 | + assertWithMessage("Answer should contain right answer"). |
| 59 | + that(answer.toLowerCase()).contains("algeria"); |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + @Test |
| 64 | + @DisplayName("Should embed text") |
| 65 | + public void should_process_text_embed_request() throws ConfigurationException { |
| 66 | + Map<String, String> properties = new HashMap<>(); |
| 67 | + properties.put("cohere.apiKey", "${env:COHERE_API_KEY}"); |
| 68 | + properties.put("cohere.model", "embed-english-light-v2.0"); |
| 69 | + MapConfiguration config = new MapConfiguration(properties); |
| 70 | + |
| 71 | + LanguageModel llm = new CohereLanguageModel.Builder() |
| 72 | + .getLanguageModel(config); |
| 73 | + |
| 74 | + List<Float> embeddings = llm.embed("In what country is El Outed located?"); |
| 75 | + |
| 76 | + assertThat(embeddings).isNotEmpty(); |
| 77 | + } |
| 78 | +} |
0 commit comments