-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Properly support Smallrye Fault Tolerance
- Loading branch information
Showing
9 changed files
with
222 additions
and
36 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
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
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
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
41 changes: 41 additions & 0 deletions
41
...n/java/io/quarkiverse/langchain4j/runtime/aiservice/QuarkusAiServiceContextQualifier.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,41 @@ | ||
package io.quarkiverse.langchain4j.runtime.aiservice; | ||
|
||
import static java.lang.annotation.ElementType.PARAMETER; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Inherited; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import jakarta.enterprise.util.AnnotationLiteral; | ||
import jakarta.inject.Qualifier; | ||
|
||
@Qualifier | ||
@Inherited | ||
@Target({ PARAMETER }) | ||
@Retention(RUNTIME) | ||
public @interface QuarkusAiServiceContextQualifier { | ||
|
||
/** | ||
* The name of class | ||
*/ | ||
String value(); | ||
|
||
class Literal extends AnnotationLiteral<QuarkusAiServiceContextQualifier> implements QuarkusAiServiceContextQualifier { | ||
|
||
public static Literal of(String value) { | ||
return new Literal(value); | ||
} | ||
|
||
private final String value; | ||
|
||
public Literal(String value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public String value() { | ||
return value; | ||
} | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
...penai/src/main/java/org/acme/example/openai/aiservices/AssistantResourceWithFallback.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,39 @@ | ||
package org.acme.example.openai.aiservices; | ||
|
||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
|
||
import org.eclipse.microprofile.faulttolerance.Fallback; | ||
|
||
import dev.langchain4j.service.SystemMessage; | ||
import io.quarkiverse.langchain4j.RegisterAiService; | ||
|
||
@Path("assistant-with-fallback") | ||
public class AssistantResourceWithFallback { | ||
|
||
private final Assistant assistant; | ||
|
||
public AssistantResourceWithFallback(Assistant assistant) { | ||
this.assistant = assistant; | ||
} | ||
|
||
@GET | ||
public String get() { | ||
return assistant.chat("test"); | ||
} | ||
|
||
@RegisterAiService | ||
interface Assistant { | ||
|
||
@SystemMessage(""" | ||
Help me: {something} | ||
""") | ||
@Fallback(fallbackMethod = "fallback") | ||
String chat(String message); | ||
|
||
static String fallback(String message) { | ||
return "This is a fallback message"; | ||
} | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
...i/src/test/java/org/acme/example/openai/aiservices/AssistantResourceWithFallbackTest.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,30 @@ | ||
package org.acme.example.openai.aiservices; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
|
||
import java.net.URL; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.common.http.TestHTTPEndpoint; | ||
import io.quarkus.test.common.http.TestHTTPResource; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
@QuarkusTest | ||
class AssistantResourceWithFallbackTest { | ||
|
||
@TestHTTPEndpoint(AssistantResourceWithFallback.class) | ||
@TestHTTPResource | ||
URL url; | ||
|
||
@Test | ||
public void fallback() { | ||
given() | ||
.baseUri(url.toString()) | ||
.get() | ||
.then() | ||
.statusCode(200) | ||
.body(equalTo("This is a fallback message")); | ||
} | ||
} |
Oops, something went wrong.