-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
678690a
commit 36f3e96
Showing
9 changed files
with
128 additions
and
34 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
...re/src/main/java/org/springframework/ai/parser/AbstractConversionServiceOutputParser.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,17 @@ | ||
package org.springframework.ai.parser; | ||
|
||
import org.springframework.core.convert.support.DefaultConversionService; | ||
|
||
public abstract class AbstractConversionServiceOutputParser<T> implements OutputParser<T> { | ||
|
||
private final DefaultConversionService conversionService; | ||
|
||
public AbstractConversionServiceOutputParser(DefaultConversionService conversionService) { | ||
this.conversionService = conversionService; | ||
} | ||
|
||
public DefaultConversionService getConversionService() { | ||
return conversionService; | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
spring-ai-core/src/main/java/org/springframework/ai/parser/ListOutputParser.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,29 @@ | ||
package org.springframework.ai.parser; | ||
|
||
import org.springframework.core.convert.support.DefaultConversionService; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Parse out a List from a formatting request to convert a | ||
*/ | ||
public class ListOutputParser extends AbstractConversionServiceOutputParser<List<String>> { | ||
|
||
public ListOutputParser(DefaultConversionService defaultConversionService) { | ||
super(defaultConversionService); | ||
} | ||
|
||
@Override | ||
public String getFormat() { | ||
return """ | ||
Your response should be a list of comma separated values | ||
eg: `foo, bar, baz` | ||
"""; | ||
} | ||
|
||
@Override | ||
public List<String> parse(String text) { | ||
return getConversionService().convert(text, List.class); | ||
} | ||
|
||
} |
24 changes: 18 additions & 6 deletions
24
spring-ai-core/src/main/java/org/springframework/ai/parser/OutputParser.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 |
---|---|---|
@@ -1,11 +1,23 @@ | ||
package org.springframework.ai.parser; | ||
|
||
import org.springframework.ai.client.Generation; | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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. | ||
*/ | ||
|
||
import java.util.List; | ||
package org.springframework.ai.parser; | ||
|
||
public interface OutputParser<T> { | ||
import org.springframework.ai.prompt.FormatProvider; | ||
|
||
T parse(List<Generation> output); | ||
public interface OutputParser<T> extends Parser<T>, FormatProvider { | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
spring-ai-core/src/main/java/org/springframework/ai/parser/Parser.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,10 @@ | ||
package org.springframework.ai.parser; | ||
|
||
import java.util.Locale; | ||
|
||
@FunctionalInterface | ||
public interface Parser<T> { | ||
|
||
T parse(String text); | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
spring-ai-core/src/main/java/org/springframework/ai/prompt/FormatProvider.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,7 @@ | ||
package org.springframework.ai.prompt; | ||
|
||
public interface FormatProvider { | ||
|
||
String getFormat(); | ||
|
||
} |
27 changes: 0 additions & 27 deletions
27
spring-ai-core/src/main/java/org/springframework/ai/prompt/OutputParser.java
This file was deleted.
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
21 changes: 21 additions & 0 deletions
21
spring-ai-core/src/test/java/org/springframework/ai/prompt/parsers/ListOutputParserTest.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,21 @@ | ||
package org.springframework.ai.prompt.parsers; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.ai.parser.ListOutputParser; | ||
import org.springframework.core.convert.support.DefaultConversionService; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class ListOutputParserTest { | ||
|
||
@Test | ||
void csv() { | ||
String csvAsString = "foo, bar, baz"; | ||
ListOutputParser listOutputParser = new ListOutputParser(new DefaultConversionService()); | ||
List<String> list = listOutputParser.parse(csvAsString); | ||
assertThat(list).containsExactlyElementsOf(List.of("foo", "bar", "baz")); | ||
} | ||
|
||
} |
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