This repository has been archived by the owner on Aug 25, 2024. It is now read-only.
forked from LangStream/langstream
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tests] Add ai-chat-completions e2e test (LangStream#430)
- Loading branch information
1 parent
c48ebde
commit 9ce7e36
Showing
10 changed files
with
555 additions
and
239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
470 changes: 296 additions & 174 deletions
470
langstream-e2e-tests/src/test/java/ai/langstream/tests/BaseEndToEndTest.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
langstream-e2e-tests/src/test/java/ai/langstream/tests/ChatCompletionsIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright DataStax, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ai.langstream.tests; | ||
|
||
import ai.langstream.tests.util.ConsumeGatewayMessage; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Tag; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
@Slf4j | ||
@ExtendWith(BaseEndToEndTest.class) | ||
@Tag(BaseEndToEndTest.CATEGORY_NEEDS_CREDENTIALS) | ||
public class ChatCompletionsIT extends BaseEndToEndTest { | ||
|
||
static Map<String, String> appEnv; | ||
|
||
@BeforeAll | ||
public static void checkCredentials() { | ||
appEnv = | ||
getAppEnvMapFromSystem( | ||
List.of( | ||
"OPEN_AI_ACCESS_KEY", | ||
"OPEN_AI_URL", | ||
"OPEN_AI_CHAT_COMPLETIONS_MODEL", | ||
"OPEN_AI_PROVIDER")); | ||
} | ||
|
||
@Test | ||
public void test() throws Exception { | ||
installLangStreamCluster(false); | ||
final String tenant = "ten-" + System.currentTimeMillis(); | ||
setupTenant(tenant); | ||
|
||
final String applicationId = "app"; | ||
|
||
deployLocalApplication(applicationId, "chat-completions", appEnv); | ||
awaitApplicationReady(applicationId, 1); | ||
|
||
final String sessionId = UUID.randomUUID().toString(); | ||
|
||
executeCommandOnClient( | ||
"bin/langstream gateway produce %s produce-input -v 'Who was the first president of the United States?' -p sessionId=%s" | ||
.formatted(applicationId, sessionId) | ||
.split(" ")); | ||
|
||
final ConsumeGatewayMessage message = | ||
consumeOneMessageFromGateway( | ||
applicationId, | ||
"consume-history", | ||
"-p sessionId=%s --position earliest --connect-timeout 30" | ||
.formatted(sessionId) | ||
.split(" ")); | ||
log.info("Output: {}", message); | ||
Assertions.assertTrue(message.getAnswerFromChatCompletionsValue().contains("Washington")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
langstream-e2e-tests/src/test/java/ai/langstream/tests/util/ConsumeGatewayMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright DataStax, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ai.langstream.tests.util; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.util.Map; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import lombok.SneakyThrows; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class ConsumeGatewayMessage { | ||
protected static final ObjectMapper JSON_MAPPER = new ObjectMapper(); | ||
|
||
@SneakyThrows | ||
public static ConsumeGatewayMessage readValue(String line) { | ||
return JSON_MAPPER.readValue(line, ConsumeGatewayMessage.class); | ||
} | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static final class Record { | ||
private Object key; | ||
private Object value; | ||
private Map<String, String> headers; | ||
} | ||
|
||
private Record record; | ||
private String offset; | ||
|
||
@SneakyThrows | ||
public String getAnswerFromChatCompletionsValue() { | ||
final Map<String, Object> chatHistoryModel = | ||
JSON_MAPPER.readValue((String) record.getValue(), Map.class); | ||
final String answer = chatHistoryModel.get("answer").toString(); | ||
return answer; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
langstream-e2e-tests/src/test/resources/apps/chat-completions/configuration.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# | ||
# | ||
# Copyright DataStax, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
configuration: | ||
resources: | ||
- type: "open-ai-configuration" | ||
name: "OpenAI Azure configuration" | ||
configuration: | ||
url: "{{ secrets.open-ai.url }}" | ||
access-key: "{{ secrets.open-ai.access-key }}" | ||
provider: "{{ secrets.open-ai.provider }}" |
39 changes: 39 additions & 0 deletions
39
langstream-e2e-tests/src/test/resources/apps/chat-completions/gateways.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# | ||
# | ||
# Copyright DataStax, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
gateways: | ||
- id: produce-input | ||
type: produce | ||
topic: input-topic | ||
parameters: | ||
- sessionId | ||
produce-options: | ||
headers: | ||
- key: langstream-client-session-id | ||
value-from-parameters: sessionId | ||
|
||
- id: consume-history | ||
type: consume | ||
topic: history-topic | ||
parameters: | ||
- sessionId | ||
consume-options: | ||
filters: | ||
headers: | ||
- key: langstream-client-session-id | ||
value-from-parameters: sessionId | ||
|
42 changes: 42 additions & 0 deletions
42
langstream-e2e-tests/src/test/resources/apps/chat-completions/pipeline.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# | ||
# Copyright DataStax, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
topics: | ||
- name: "input-topic" | ||
creation-mode: create-if-not-exists | ||
- name: "output-topic" | ||
creation-mode: create-if-not-exists | ||
- name: "history-topic" | ||
creation-mode: create-if-not-exists | ||
pipeline: | ||
- name: "convert-to-json" | ||
type: "document-to-json" | ||
input: "input-topic" | ||
configuration: | ||
text-field: "question" | ||
- name: "ai-chat-completions" | ||
type: "ai-chat-completions" | ||
output: "history-topic" | ||
configuration: | ||
model: "{{{secrets.open-ai.chat-completions-model}}}" | ||
completion-field: "value.answer" | ||
log-field: "value.prompt" | ||
stream-to-topic: "output-topic" | ||
stream-response-completion-field: "value" | ||
min-chunks-per-message: 20 | ||
messages: | ||
- role: user | ||
content: "You are an helpful assistant. Below you can fine a question from the user. Please try to help them the best way you can.\n\n{{% value.question}}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters