Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.agentscope.core.rag.integration.dify.model;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
Expand All @@ -25,6 +26,25 @@
* <p>This class maps the JSON response from Dify's retrieve endpoint:
* <pre>{@code
* {
* "query": "..." ,
* "records": [
* {
* "segment": {
* "id": "...",
* "document_id": "...",
* "content": "...",
* "position": 1,
* "document": { "id": "...", "name": "..." }
* },
* "score": 0.95
* }
* ]
* }
*
* <p>Some Dify deployments return {@code query} as an object with a {@code content} field.
* The model keeps that form compatible as well.
* <pre>{@code
* {
* "query": { "content": "..." },
* "records": [
* {
Expand Down Expand Up @@ -70,6 +90,13 @@ public void setRecords(List<Record> records) {
public static class Query {
private String content;

@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
public static Query fromString(String content) {
Query query = new Query();
query.setContent(content);
return query;
}

public String getContent() {
return content;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,31 @@ private MockResponse createSuccessResponse() {
.setBody(responseBody);
}

private MockResponse createSuccessResponseWithQuery(String queryJson) {
String responseBody =
String.format(
"""
{
"query": %s,
"records": [
{
"segment": {
"id": "seg-1",
"content": "Test content",
"document_id": "doc-1"
},
"score": 0.95
}
]
}
""",
queryJson);
return new MockResponse()
.setResponseCode(200)
.setHeader("Content-Type", "application/json")
.setBody(responseBody);
}

private MockResponse createEmptyResponse() {
return new MockResponse()
.setResponseCode(200)
Expand Down Expand Up @@ -408,6 +433,34 @@ void testParseSuccessfulResponse() throws Exception {
assertEquals(0.95, response.getRecords().get(0).getScore(), 0.001);
}

@Test
void testParseStringQueryResponse() throws Exception {
mockWebServer.enqueue(createSuccessResponseWithQuery("\"ping\""));

DifyRAGClient client = new DifyRAGClient(createConfig());
DifyResponse response = client.retrieve("test query", 5).block();

assertNotNull(response);
assertNotNull(response.getQuery());
assertEquals("ping", response.getQuery().getContent());
assertNotNull(response.getRecords());
assertEquals(1, response.getRecords().size());
}

@Test
void testParseObjectQueryResponse() throws Exception {
mockWebServer.enqueue(createSuccessResponseWithQuery("{\"content\":\"ping\"}"));

DifyRAGClient client = new DifyRAGClient(createConfig());
DifyResponse response = client.retrieve("test query", 5).block();

assertNotNull(response);
assertNotNull(response.getQuery());
assertEquals("ping", response.getQuery().getContent());
assertNotNull(response.getRecords());
assertEquals(1, response.getRecords().size());
}

@Test
void testHandleEmptyResponse() throws Exception {
mockWebServer.enqueue(createEmptyResponse());
Expand Down
Loading