|
| 1 | +/* |
| 2 | + * Copyright 2024-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.ai.openai.chat.api; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; |
| 23 | +import reactor.core.publisher.Flux; |
| 24 | + |
| 25 | +import org.springframework.ai.openai.api.OpenAiApi; |
| 26 | +import org.springframework.ai.openai.api.OpenAiApi.ChatCompletion; |
| 27 | +import org.springframework.ai.openai.api.OpenAiApi.ChatCompletionChunk; |
| 28 | +import org.springframework.ai.openai.api.OpenAiApi.ChatCompletionMessage; |
| 29 | +import org.springframework.ai.openai.api.OpenAiApi.ChatCompletionMessage.Role; |
| 30 | +import org.springframework.ai.openai.api.OpenAiApi.ChatCompletionRequest; |
| 31 | +import org.springframework.ai.openai.api.OpenAiApi.Embedding; |
| 32 | +import org.springframework.ai.openai.api.OpenAiApi.EmbeddingList; |
| 33 | +import org.springframework.http.ResponseEntity; |
| 34 | + |
| 35 | +import static org.assertj.core.api.Assertions.assertThat; |
| 36 | + |
| 37 | +/** |
| 38 | + * @author Christian Tzolov |
| 39 | + */ |
| 40 | +@EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+") |
| 41 | +public class OpenAiApiIT { |
| 42 | + |
| 43 | + OpenAiApi openAiApi = new OpenAiApi(System.getenv("OPENAI_API_KEY")); |
| 44 | + |
| 45 | + @Test |
| 46 | + void chatCompletionEntity() { |
| 47 | + ChatCompletionMessage chatCompletionMessage = new ChatCompletionMessage("Hello world", Role.USER); |
| 48 | + ResponseEntity<ChatCompletion> response = openAiApi.chatCompletionEntity( |
| 49 | + new ChatCompletionRequest(List.of(chatCompletionMessage), "gpt-3.5-turbo", 0.8f, false)); |
| 50 | + |
| 51 | + assertThat(response).isNotNull(); |
| 52 | + assertThat(response.getBody()).isNotNull(); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void chatCompletionStream() { |
| 57 | + ChatCompletionMessage chatCompletionMessage = new ChatCompletionMessage("Hello world", Role.USER); |
| 58 | + Flux<ChatCompletionChunk> response = openAiApi.chatCompletionStream( |
| 59 | + new ChatCompletionRequest(List.of(chatCompletionMessage), "gpt-3.5-turbo", 0.8f, true)); |
| 60 | + |
| 61 | + assertThat(response).isNotNull(); |
| 62 | + assertThat(response.collectList().block()).isNotNull(); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void embeddings() { |
| 67 | + ResponseEntity<EmbeddingList<Embedding>> response = openAiApi |
| 68 | + .embeddings(new OpenAiApi.EmbeddingRequest<String>("Hello world")); |
| 69 | + |
| 70 | + assertThat(response).isNotNull(); |
| 71 | + assertThat(response.getBody().data()).hasSize(1); |
| 72 | + assertThat(response.getBody().data().get(0).embedding()).hasSize(1536); |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments