Skip to content

Commit

Permalink
feat: support 2 models
Browse files Browse the repository at this point in the history
  • Loading branch information
Angular2Guy committed Jul 13, 2024
1 parent 1a27421 commit 3ccf750
Showing 1 changed file with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,23 @@ public class CodeGenerationService {
{contextClasses}
{testExample}
""";
private final String ollamaPrompt1 = """
You are an assistant to generate a spring test class for the source class.
1. Analyse the source class
2. Analyse the context classes for the classes used by the source class
3. Analyse the class in test example to base the code of the generated test class on it.
4. Generate a test class for the source class, use the context classes as sources for it and base the code of the test class on the test example.
Generate the complete source code of the test class implementing the tests.
{testExample}
Use these context classes as extension for the source class:
{contextClasses}
Generate the complete source code of the test class implementing the tests.
Generate tests for this source class:
{classToTest}
""";
@Value("${spring.ai.ollama.chat.options.num-ctx:0}")
private Long contextWindowSize;
Expand All @@ -61,23 +78,24 @@ public String generateTest(String url, Optional<String> testUrlOpt) {
var githubSource = this.createTestSources(url, true);
var githubTestSource = testUrlOpt.map(testUrl -> this.createTestSources(testUrl, false))
.orElse(new GithubSource(null, null, List.of(), List.of()));
String contextClasses = githubSource.dependencies().stream().filter(x -> this.contextWindowSize >= 16 * 1024)
String contextClasses = githubSource.dependencies().stream()
.filter(x -> this.contextWindowSize >= 16 * 1024)
.map(myGithubSource -> myGithubSource.sourceName() + ":" + System.getProperty("line.separator")
+ myGithubSource.lines().stream()
.collect(Collectors.joining(System.getProperty("line.separator"))))
.collect(Collectors.joining(System.getProperty("line.separator")));
String testExample = Optional
.ofNullable(
githubTestSource.sourceName())
.map(x -> "Use this class as example:" + System.getProperty("line.separator") + githubTestSource
.map(x -> "Use this as test example class:" + System.getProperty("line.separator") + githubTestSource
.lines().stream().collect(Collectors.joining(System.getProperty("line.separator"))))
.orElse("");
String classToTest = githubSource.lines().stream()
.collect(Collectors.joining(System.getProperty("line.separator")));
LOGGER.debug(new PromptTemplate(this.ollamaPrompt,
LOGGER.debug(new PromptTemplate(this.contextWindowSize >= 16 * 1024 ? this.ollamaPrompt1 : this.ollamaPrompt,
Map.of("classToTest", classToTest, "contextClasses", contextClasses, "testExample", testExample)).createMessage().getContent());
LOGGER.info("Generation started with context window: {}", this.contextWindowSize);
var response = chatClient.call(new PromptTemplate(this.ollamaPrompt,
var response = chatClient.call(new PromptTemplate(this.contextWindowSize >= 16 * 1024 ? this.ollamaPrompt1 : this.ollamaPrompt,
Map.of("classToTest", classToTest, "contextClasses", contextClasses, "testExample", testExample)).create());
if((Instant.now().getEpochSecond() - start.getEpochSecond()) >= 300) {
LOGGER.info(response.getResult().getOutput().getContent());
Expand Down

0 comments on commit 3ccf750

Please sign in to comment.