forked from langchain4j/langchain4j
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the implementation for Qwen series models using the new Dash…
…Scope SDK APIs. (langchain4j#155) The design of the Dashscope SDK is evolving towards OpenAI, offering new fields and specifications. Utilize these latest features to refactor the implementation of the Qwen models.
- Loading branch information
1 parent
3bffc97
commit f2bb6f9
Showing
9 changed files
with
89 additions
and
78 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
55 changes: 55 additions & 0 deletions
55
langchain4j-dashscope/src/main/java/dev/langchain4j/model/dashscope/QwenHelper.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,55 @@ | ||
package dev.langchain4j.model.dashscope; | ||
|
||
import com.alibaba.dashscope.aigc.generation.GenerationOutput; | ||
import com.alibaba.dashscope.aigc.generation.GenerationOutput.Choice; | ||
import com.alibaba.dashscope.aigc.generation.GenerationResult; | ||
import com.alibaba.dashscope.common.Message; | ||
import com.alibaba.dashscope.common.Role; | ||
import dev.langchain4j.data.message.AiMessage; | ||
import dev.langchain4j.data.message.ChatMessage; | ||
import dev.langchain4j.data.message.SystemMessage; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
public class QwenHelper { | ||
public static List<Message> toQwenMessages(List<ChatMessage> messages) { | ||
return messages.stream() | ||
.map(QwenHelper::toQwenMessage) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public static Message toQwenMessage(ChatMessage message) { | ||
return Message.builder() | ||
.role(roleFrom(message)) | ||
.content(message.text()) | ||
.build(); | ||
} | ||
|
||
public static String roleFrom(ChatMessage message) { | ||
if (message instanceof AiMessage) { | ||
return Role.ASSISTANT.getValue(); | ||
} else if (message instanceof SystemMessage) { | ||
return Role.SYSTEM.getValue(); | ||
} else { | ||
return Role.USER.getValue(); | ||
} | ||
} | ||
|
||
public static String answerFrom(GenerationResult result) { | ||
return Optional.of(result) | ||
.map(GenerationResult::getOutput) | ||
.map(GenerationOutput::getChoices) | ||
.filter(choices -> !choices.isEmpty()) | ||
.map(choices -> choices.get(0)) | ||
.map(Choice::getMessage) | ||
.map(Message::getContent) | ||
// Compatible with some older models. | ||
.orElseGet(() -> Optional.of(result) | ||
.map(GenerationResult::getOutput) | ||
.map(GenerationOutput::getText) | ||
.orElse("Oops, something wrong...[request id: " + result.getRequestId() + "]")); | ||
} | ||
} |
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
44 changes: 0 additions & 44 deletions
44
langchain4j-dashscope/src/main/java/dev/langchain4j/model/dashscope/QwenParamHelper.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
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