Skip to content

Commit

Permalink
Remove old generate() API
Browse files Browse the repository at this point in the history
  • Loading branch information
dliubarskyi committed Feb 27, 2025
1 parent ee7c8a4 commit 3ea374b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions dbpedia-example/src/main/java/DbPediaSparqlExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private static String extractSubject(AzureOpenAiChatModel model, String question
// Applying the variables to the prompt template
Prompt prompt = extractSubjectPromptTemplate.apply(variables);
// Generating and returning the subject using the Azure OpenAI model
return model.generate(prompt.text());
return model.chat(prompt.text());
}

/**
Expand Down Expand Up @@ -195,6 +195,6 @@ private static String generateAnswer(AzureOpenAiChatModel model, String subject,
// Applying the variables to the prompt template
Prompt prompt = extractAnswerPromptTemplate.apply(variables);
// Generating and returning the answer using the Azure OpenAI model
return model.generate(prompt.text());
return model.chat(prompt.text());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.ToolExecutionResultMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.request.ChatRequest;
import dev.langchain4j.model.github.GitHubModelsChatModel;
import dev.langchain4j.service.tool.DefaultToolExecutor;
import dev.langchain4j.service.tool.ToolExecutor;
Expand Down Expand Up @@ -47,9 +48,13 @@ public static void main(String[] args) {
UserMessage userMessage = userMessage("What will the weather be like in London tomorrow?");
chatMessages.add(userMessage);

ChatRequest request = ChatRequest.builder()
.messages(chatMessages)
.toolSpecifications(toolSpecifications)
.build();

// STEP 2: Model generate function arguments
AiMessage aiMessage = model.generate(chatMessages, toolSpecifications).content();
AiMessage aiMessage = model.chat(request).aiMessage();
List<ToolExecutionRequest> toolExecutionRequests = aiMessage.toolExecutionRequests();
System.out.println("Out of the " + toolSpecifications.size() + " functions declared in WeatherTools, " + toolExecutionRequests.size() + " will be invoked:");
toolExecutionRequests.forEach(toolExecutionRequest -> {
Expand All @@ -70,7 +75,7 @@ public static void main(String[] args) {


// STEP 4: Model generate final response
AiMessage finalResponse = model.generate(chatMessages).content();
AiMessage finalResponse = model.chat(chatMessages).aiMessage();
System.out.println(finalResponse.text()); //According to the payment data, the payment status of transaction T1005 is Pending.
}
}
Expand Down

0 comments on commit 3ea374b

Please sign in to comment.