-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #487 from quarkiverse/websockets
Add integration module with quarkus-websockets-next
- Loading branch information
Showing
24 changed files
with
790 additions
and
20 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
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,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>io.quarkiverse.langchain4j</groupId> | ||
<artifactId>quarkus-langchain4j-core-parent</artifactId> | ||
<version>999-SNAPSHOT</version> | ||
</parent> | ||
|
||
|
||
<artifactId>quarkus-langchain4j-core-runtime-spi</artifactId> | ||
<name>Quarkus LangChain4j - Core - Runtime - SPI</name> | ||
|
||
</project> |
27 changes: 27 additions & 0 deletions
27
core/runtime-spi/src/main/java/io/quarkiverse/langchain4j/spi/DefaultMemoryIdProvider.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,27 @@ | ||
package io.quarkiverse.langchain4j.spi; | ||
|
||
/** | ||
* Quarkus extension can decide whether they can provide a default value for the memory ID object if none was explicitly | ||
* provided. | ||
* <p> | ||
* The idea behind this is that depending on the type of request that is being served, Quarkus can determine a unique | ||
* (per request) object to be used. | ||
*/ | ||
public interface DefaultMemoryIdProvider { | ||
int DEFAULT_PRIORITY = 0; | ||
|
||
/** | ||
* Defines the priority of the providers. | ||
* A lower integer value means that the customizer will be considered before one with a higher priority | ||
*/ | ||
default int priority() { | ||
return DEFAULT_PRIORITY; | ||
} | ||
|
||
/** | ||
* Determines the object to be used as the default memory ID. | ||
* A value of {@code null} means that the provider is not going to give a value and therefore he next | ||
* provider should be tried. | ||
*/ | ||
Object getMemoryId(); | ||
} |
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
30 changes: 30 additions & 0 deletions
30
...ain/java/io/quarkiverse/langchain4j/runtime/RequestScopeStateDefaultMemoryIdProvider.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 io.quarkiverse.langchain4j.runtime; | ||
|
||
import io.quarkiverse.langchain4j.spi.DefaultMemoryIdProvider; | ||
import io.quarkus.arc.Arc; | ||
import io.quarkus.arc.ArcContainer; | ||
import io.quarkus.arc.ManagedContext; | ||
|
||
/** | ||
* This implementation uses the state of the request scope as the default value | ||
*/ | ||
public class RequestScopeStateDefaultMemoryIdProvider implements DefaultMemoryIdProvider { | ||
|
||
@Override | ||
public int priority() { | ||
return DefaultMemoryIdProvider.DEFAULT_PRIORITY + 100; | ||
} | ||
|
||
@Override | ||
public Object getMemoryId() { | ||
ArcContainer container = Arc.container(); | ||
if (container == null) { | ||
return null; | ||
} | ||
ManagedContext requestContext = container.requestContext(); | ||
if (requestContext.isActive()) { | ||
return requestContext.getState(); | ||
} | ||
return null; | ||
} | ||
} |
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
1 change: 1 addition & 0 deletions
1
...c/main/resources/META-INF/services/io.quarkiverse.langchain4j.spi.DefaultMemoryIdProvider
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 @@ | ||
io.quarkiverse.langchain4j.runtime.RequestScopeStateDefaultMemoryIdProvider |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
= Using AI services in WebSockets | ||
|
||
include::./includes/attributes.adoc[] | ||
|
||
Using a chatbot in a WebSockets environment is quite common, which is why the extension provides a few facilities to make such usages as easy as possible. | ||
|
||
* 1. Start by adding the link:https://quarkus.io/guides/websockets-next-tutorial[quarkus-websockets-next] dependency to your `pom.xml` file: | ||
[source,xml] | ||
---- | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-websockets-next</artifactId> | ||
</dependency> | ||
---- | ||
|
||
IMPORTANT: `quarkus-websockets-next` is available as of Quarkus 3.9. | ||
|
||
* 2. Annotated your AiService with `@SessionScoped` | ||
[source,java] | ||
---- | ||
import dev.langchain4j.service.SystemMessage; | ||
import io.quarkiverse.langchain4j.RegisterAiService; | ||
import jakarta.enterprise.context.SessionScoped; | ||
@RegisterAiService | ||
@SessionScoped | ||
public interface SessionScopedChatBot { | ||
@SystemMessage("You are chatbot that helps users with their queries") | ||
String chat(String message); | ||
} | ||
---- | ||
|
||
* 3. Create a WebSocket endpoint | ||
[source,java] | ||
---- | ||
import io.quarkus.websockets.next.OnOpen; | ||
import io.quarkus.websockets.next.OnTextMessage; | ||
import io.quarkus.websockets.next.WebSocket; | ||
@WebSocket(path = "/websocket") | ||
public static class WebSocketChatBot { | ||
private final SessionScopedChatBot bot; | ||
public WebSocketChatBot(SessionScopedChatBot bot) { | ||
this.bot = bot; | ||
} | ||
@OnOpen | ||
public String onOpen() { | ||
return bot.chat("Hello, how can I help you?"); | ||
} | ||
@OnTextMessage | ||
public String onMessage(String message) { | ||
return bot.chat(message); | ||
} | ||
} | ||
---- | ||
|
||
Two things are important to note in the snippets above: | ||
|
||
* There is no `@MemoryId` field being used in the AI service. Quarkus will automatically all the WebSocket connection ID as the memory ID. | ||
This ensures that each WebSocket session has its own chat memory. | ||
* The use of `@SessionScoped` is important as the scope of the AI service is tied to the scope of the WebSocket endpoint. | ||
This allows Quarkus to automatically clear chat memory when the WebSocket connection is closed for whatever reason. |
Oops, something went wrong.